目录
6-8 处理IllegalTriangleException异常
6-20 jmu-Java-05集合-List中指定元素的删除
6-1 可定制排序的矩形
分数 20
全屏浏览题目
切换布局
作者 温彦
单位 山东科技大学
从键盘录入表示矩形个数的数字n,然后录入n个矩形的长和宽,然后对这n个矩形按照面积从大到小排序,并输出排序后的每个矩形的面积。要求:请设计Rectangle类,包含相应的构造函数和成员函数,实现Comparable接口
输入描述:
矩形个数,每个矩形的长和宽
输出描述:
由大到小排序的每个矩形的面积
裁判测试程序样例:
import java.util.Comparator;
import java.util.Arrays;
import java.util.Scanner;
/*你的代码被嵌在这里*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//输入矩形个数
int num_rectangle = scan.nextInt();
Rectangle[] recs = new Rectangle[num_rectangle];
//输入每个矩形的长和宽
for(int i=0;i<num_rectangle;i++){
int length = scan.nextInt();
int width = scan.nextInt();
Rectangle rec = new Rectangle(length,width);
recs[i] = rec;
}
//按照面积由大到小排序
Arrays.sort(recs);
//打印前n-1个矩形的面积
for(int i=0;i<recs.length-1;i++){
System.out.print(recs[i].getArea()+",");
}
//打印最后一个矩形的面积
System.out.print(recs[recs.length-1].getArea());
scan.close();
}
}
输入样例:
在这里给出一组输入。例如:
3 1 2 3 4 2 3
输出样例:
在这里给出相应的输出。例如:
12,6,2
class Rectangle implements Comparable<Rectangle>{
private int length;
private int width;
private int S;
Rectangle(int length,int width){
S=length*width;
}
public int getArea(){
return this.S;
}
public int compareTo(Rectangle o1){
return o1.getArea()-this.S;
}
}
6-2 Shape类
分数 20
全屏浏览题目
切换布局
作者 温彦
单位 山东科技大学
定义一个形状类Shape,提供计算周长getPerimeter()和面积getArea()的函数
定义一个子类正方形类Square继承自Shape类,拥有边长属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
定义一个子类长方形类Rectangle继承自Square类,拥有长、宽属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
定义一个子类圆形类Circle继承自Shape,拥有半径属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
在main函数中,分别构造三个子类的对象,并输出他们的周长、面积.
提示:用System.out.printf("%.2f",d)进行格式化输出
输入描述:
正方形类的边长
长方形类的长宽
圆类的半径
输出描述:
正方形的周长、面积
长方形的周长、面积
裁判测试程序样例:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double length = scan.nextDouble();
Square s = new Square(length);
System.out.printf("%.2f ",s.getPerimeter());
System.out.printf("%.2f\n",s.getArea());
length = scan.nextDouble();
double wide = scan.nextDouble();
Rectangle r = new Rectangle(length,wide);
System.out.printf("%.2f ",r.getPerimeter());
System.out.printf("%.2f\n",r.getArea());
double radius = scan.nextDouble();
Circle c = new Circle(radius);
System.out.printf("%.2f ",c.getPerimeter());
System.out.printf("%.2f\n",c.getArea());
scan.close();
}
}
/* 你的代码被嵌在这里 */
输入样例:
在这里给出一组输入。例如:
1
1 2
2
输出样例:
在这里给出相应的输出。例如:
4.00 1.00
6.00 2.00
12.57 12.57
interface Shape{
public abstract double getPerimeter();
public abstract double getArea();
}
class Square implements Shape{
private double length;
Square(double length){
this.length=length;
}
public double getPerimeter(){
return 4*this.length;
}
public double getArea(){
return length*length;
}
}
class Rectangle implements Shape{
private double length;
private double wide;
Rectangle(double length,double wide){
this.length=length;
this.wide=wide;
}
public double getPerimeter(){
return 2*(length+wide);
}
public double getArea(){
return length*wide;
}
}
class Circle implements Shape{
private double r;
Circle(double r){
this.r=r;
}
public double getPerimeter(){
return Math.PI*r*2;
}
public double getArea(){
return Math.PI*r*r;
}
}
6-3 可比较的几何类(抽象类与接口)
分数 10
全屏浏览题目
切换布局
作者 殷伟凤
单位 浙江传媒学院
通过继承和多态的学习,同学们熟悉了GeometricObject类,现在用抽象类的观点,修改GeometricObject类以实现Comparable接口,且在GeometricObject类中定义一个静态方法:求两个GeometricObject对象中较大者。
此题提交时将会附加下述代码到被提交的Java程序末尾。
裁判测试程序样例:
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Circle circle1 = new Circle(input.nextInt());
Circle circle2 = new Circle(input.nextInt());
Circle circle = (Circle) GeometricObject.max(circle1, circle2);
System.out.println("The max circle's radius is " + circle.getRadius());
System.out.println(circle);
}
}
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
4 10
输出样例:
在这里给出相应的输出。例如:
The max circle's radius is 10.0
Circle radius = 10.0
class GeometricObject {
public static Circle max(Circle c1,Circle c2){
if(c1.getRadius()>=c2.getRadius())
return c1;
return c2;
}
}
class Circle{
private int radius;
Circle(int r){
this.radius=r;
}
public double getRadius(){
return this.radius;
}
public String toString(){
return "Circle radius = "+getRadius();
}
}
6-4 Java类实现-正方形
分数 10
全屏浏览题目
切换布局
作者 zhengjun
单位 浙江传媒学院
构造一个Square类,该类有一个私有double变量side存放边长,可以通过getter/setter方法进行访问。
该类具有getArea和getLength两个方法,能够利用边长计算正方形的面积和周长。
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); while(scanner.hasNextFloat()){ double s=scanner.nextDouble(); Square c = new Square(s); System.out.printf("%.2f %.2f\n",c.getArea(),c.getLength()); c.setSide(c.getSide()*2); System.out.printf("%.2f %.2f\n",c.getArea(),c.getLength()); } } } /* 请在这里填写答案 */
输入样例:
1
2
3
输出样例:
以输入的浮点数作为边长创建正方型对象,输出正方型的面积和周长;将正方形的边长修改为原边长的2倍,输出修改后正方形的面积和周长。
1.00 4.00
4.00 8.00
4.00 8.00
16.00 16.00
9.00 12.00
36.00 24.00
class Square{
private double side;
Square(double s){
this.side=s;
}
public void setSide(double s){
this.side=s;
}
public double getSide(){
return this.side;
}
public double getArea(){
return side*side;
}
public double getLength(){
return 4*side;
}
}
6-5 设计直线类
分数 20
全屏浏览题目
切换布局
作者 wenyan
单位 山东科技大学
两点可以确定一条直线,请设计一个直线类Line,需要通过两个点Point对象来确定。
设计类Point,包含两个坐标值,提供必要的构造函数和其他辅助函数
设计类Line,包含两个点,提供必要的构造函数和其他辅助函数
为Line提供一个getLength方法返回直线的长度
在Main类的main方法中,读入2对Point的坐标,输出2对Point所表示的直线的长度,保留两位小数(可用System.out.printf)
裁判测试程序样例:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); Point p1 = new Point(sc.nextDouble(),sc.nextDouble()); Point p2 = new Point(sc.nextDouble(),sc.nextDouble()); Line l = new Line(p1,p2); System.out.printf("%.2f",l.getLength()); } } /* 请在这里填写答案 */
输入样例:
23.2 22.1 12.2 3.2
输出样例:
21.87
class Line{
private Point a1;
private Point a2;
Line(Point a1,Point a2){
this.a1=a1;
this.a2=a2;
}
public double getLength(){
return Math.sqrt((a1.get1()-a2.get1())*(a1.get1()-a2.get1())+(a1.get2()-a2.get2())*(a1.get2()-a2.get2()));
}
}
class Point{
private double a1;
private double a2;
Point(double a1,double a2){
this.a1=a1;
this.a2=a2;
}
public double get1(){
return this.a1;
}
public double get2(){
return this.a2;
}
}
6-6 学生数统计
分数 20
全屏浏览题目
切换布局
作者 wenyan
单位 山东科技大学
构造类Student,包含姓名,性别,年龄。提供必要的构造函数和其他成员函数。
提供静态函数getMaleCount,getFemaleCount,能够获得所有在main函数中构造的Student对象中男生和女生的数量。
main函数中先读取学生个数n,而后构造n个学生对象,最后分别打印其中男生和女生的人数。(输入的三个字段用空格分开,名字内部没有空格,性别用数字表示,1为男生,0为女生)
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如: import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i=0;i<n;i++){ Student s = new Student(sc.next(),sc.nextInt(), sc.nextInt()); } System.out.println("number of male students:" + Student.getMaleCount() ); System.out.println("number of female students:" + Student.getFemaleCount() ); } } /* 请在这里填写答案 */
输入样例:
5
LiuMing 0 20
FangDa 1 19
DaShan 0 20
ChenHuang 0 21
DeLi 0 20
输出样例:
number of male students:1
number of female students:4
class Student{
private String name;
private int xingbie;
private int age;
private static int mancount=0;
private static int womancount=0;
Student(String name,int xingbie,int age){
this.name=name;
this.xingbie=xingbie;
this.age=age;
if(xingbie==1)
mancount++;
else
womancount++;
}
public static int getMaleCount(){
return mancount;
}
public static int getFemaleCount(){
return womancount;
}
}
6-7 直角三角形
分数 10
全屏浏览题目
切换布局
作者 zheng'jun
单位 浙江传媒学院
编写一个直角三角形类 RightTriangle 。
- 该类有两个私有double变量sideA,sideB,表示直角三角形两条直角边的边长(sideA和sideB不能小于0,如果将其设为负数,则改为0)。可以通过getter/setter方法进行访问。
- 该类有一个无参数的构造方法,将sideA和sideB都初始化为0。
- 该类有一个带两个double型参数的构造方法,用两个参数的值初始化sideA和sideB。
- 该类具有getArea和getPerimeter两个方法,能够利用边长计算直角三角形的面积和周长。
在测试程序中(Main类,代码已提供),先用无参数的构造方法创建一个直角三角形,并输出其面积和周长。
然后从键盘输入两个浮点数,作为直角三角形的两条直角边,创建一个直角三角形对象,并输出其面积和周长。然后将两个直角边都放大到原来的两倍,并输出放大后的面积和周长。
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); RightTriangle rightTriangle1 = new RightTriangle(); System.out.printf("%.2f %.2f\n",rightTriangle1.getArea(),rightTriangle1.getPerimeter()); RightTriangle rightTriangle2 = new RightTriangle(scanner.nextDouble(),scanner.nextDouble()); System.out.printf("%.2f %.2f\n",rightTriangle2.getArea(),rightTriangle2.getPerimeter()); rightTriangle2.setSideA(rightTriangle2.getSideA() * 2); rightTriangle2.setSideB(rightTriangle2.getSideB() * 2); System.out.printf("%.2f %.2f\n",rightTriangle2.getArea(),rightTriangle2.getPerimeter()); } } /* 请在这里填写答案 */
输入样例:
1 2
输出样例:
0.00 0.00
1.00 5.24
4.00 10.47
class RightTriangle{
private double sideA;
private double sideB;
RightTriangle(){
this.sideA=0;
this.sideB=0;
}
RightTriangle(double a,double b){
sideA=a;
sideB=b;
if(a>=0&&b>=0){
}
else if(a<0&&b>=0)
sideA=0;
else if(a>=0&&b<0)
sideB=0;
else if(a<0&&b<0){
sideA=0;
sideB=0;
}
}
public double getArea(){
return sideA*sideB/2.0;
}
public double getPerimeter(){
return sideA+sideB+Math.sqrt(sideA*sideA+sideB*sideB);
}
public void setSideA(double a){
this.sideA=a;
}
public void setSideB(double b){
this.sideB=b;
}
public double getSideA(){
return sideA;
}
public double getSideB(){
return sideB;
}
}
6-8 处理IllegalTriangleException异常
分数 10
全屏浏览题目
切换布局
作者 zhengjun
单位 浙江传媒学院
创建一个Triangle类,包括三角形三边的数据域以及返回周长的getPerimeter()方法。
属性:side1,side2,side3,都是整数。
构造方法:public Triangle(int side1, int side2, int side3) throws IllegalTriangleException
用于初始化side1,side2,side3的值。如果不满足任意两条边的和大于第三条边且三条边的边长都必须大于0的条件,则抛出一个自定义的IllegalTriangleException异常。
方法:public int getPerimeter()
返回三角形的周长。
创建一个IllegalTriangleException类(继承自Exception类)。
构造方法:public IllegalTriangleException(int side1, int side2, int side3)
生成一条异常消息 “IllegalTriangleException: The sum of any two sides must greater than the other side and the sides must greater than zero,side1,side2,side3 can not contruct a triangle”。
裁判测试程序样例:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); while (n > 0) { int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); try { Triangle t1 = new Triangle(a, b, c); System.out.println(t1.getPerimeter()); } catch (IllegalTriangleException ex) { System.out.println(ex); } n--; } } } /* 请在这里填写答案 */
输入格式:
先输入一个整数n,表示有n组数据。
每组包含3个整数,表示三角形三边。
输出格式
若三边不符合要求,抛出IllegalTriangleException异常,并打印异常描述;
若均符合要求,输出三角形周长。
输入样例:
3
1 2 3
3 3 3
4 5 6
输出样例:
IllegalTriangleException: The sum of any two sides must greater than the other side and the sides must greater than zero,1,2,3 can not contruct a triangle
9
15
class Triangle{
private int side1;
private int side2;
private int side3;
Triangle(int side1,int side2,int side3) throws IllegalTriangleException{
this.side1=side1;
this.side2=side2;
this.side3=side3;
if(side1<=0||side2<=0||side3<=0||side1+side2<=side3||side2+side3<=side1||side1+side3<=side2){
throw new IllegalTriangleException(side1,side2,side3);
}
}
public int getPerimeter(){
return side1+side2+side3;
}
}
class IllegalTriangleException extends Exception{
private String s;
public IllegalTriangleException(int side1,int side2,int side3){
s="IllegalTriangleException: The sum of any two sides must greater than the other side and the sides must greater than zero,"+side1+","+side2+","+side3+" can not contruct a triangle";
}
public String toString(){
return s;
}
}
6-9 设计矩形类Rectangle
分数 10
全屏浏览题目
切换布局
作者 BinWang
单位 河北农业大学
设计一个名为Rectangle的类表示矩形。这个类包括: 两个名为width和height的int型数据域,它们分别表示矩形的宽和高。width和height的默认值都为10. 一个无参构造方法。 一个为width和height指定值的矩形构造方法。 一个名为getArea()的方法返回这个矩形的面积。 一个名为getPerimeter()的方法返回这个矩形的周长。。
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int w = input.nextInt(); int h = input.nextInt(); Rectangle myRectangle1 = new Rectangle(w, h); System.out.println(myRectangle1.getArea()); System.out.println(myRectangle1.getPerimeter()); Rectangle myRectangle2 = new Rectangle(); System.out.println(myRectangle2.getArea()); System.out.println(myRectangle2.getPerimeter()); input.close(); } } /* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
3 5
输出样例:
在这里给出相应的输出。例如:
15
16
100
40
class Rectangle{
private int width=10;
private int height=10;
Rectangle(){
}
Rectangle(int width,int height){
this.width=width;
this.height=height;
}
public int getArea(){
return width*height;
}
public int getPerimeter(){
return 2*(width+height);
}
}
6-10 数组求和
分数 10
全屏浏览题目
切换布局
作者 郑珺
单位 浙江传媒学院
编写程序。读入用户输入的10个整数存入数组中,并对数组求和。
要求实现3个数组求和方法。
//求数组a中所有元素的和
static int sum(int[] a){
}
//求数组a中下标从start开始到数组末尾的元素的和
static int sum(int[] a, int start){
}
//求数组a中下标从start开始到end-1的元素的和
static int sum(int[] a, int start, int end){
}
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] a = new int[10]; int start,end; for(int i = 0;i < a.length; i++){ a[i] = input.nextInt(); } System.out.println(sum(a)); start = input.nextInt(); System.out.println(sum(a, start)); start = input.nextInt(); end = input.nextInt(); System.out.println(sum(a, start, end)); } /* 请在这里填写答案 */ }
输入样例:
1 2 3 4 5 6 7 8 9 10
5
5 8
输出样例:
55
40
21
static int sum(int[] a){
int sum1=0;
for(int i=0;i<a.length;i++){
sum1=sum1+a[i];
}
return sum1;
}
static int sum(int[] a,int start){
int sum1=0;
for(int i=start;i<a.length;i++){
sum1=sum1+a[i];
}
return sum1;
}
static int sum(int[] a,int start,int end){
int sum1=0;
for(int i=start;i<end;i++){
sum1=sum1+a[i];
}
return sum1;
}
这里的方法可能会导致非0返回,建议大家使用方法套嵌的方式
6-11 Animal接口
分数 20
全屏浏览题目
切换布局
作者 sy
单位 西南石油大学
已知有如下Animal抽象类和IAbility接口,请编写Animal子类Dog类与Cat类,并分别实现IAbility接口,另外再编写一个模拟器类Simulator调用IAbility接口方法,具体要求如下。
已有的Animal抽象类定义:
abstract class Animal{ private String name; //名字 private int age; //年龄 public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
已有的IAbility接口定义:
interface IAbility{ void showInfo(); //输出动物信息 void cry(); //动物发出叫声 }
需要你编写的Dog子类:
实现IAbility接口
showInfo方法输出Dog的name、age,输出格式样例为:我是一只狗,我的名字是Mike,今年2岁(注意:输出结果中没有空格,逗号为英文标点符号)
cry方法输出Dog 的叫声,输出格式样例为:旺旺
需要你编写的Cat子类:
实现IAbility接口
showInfo方法输出Cat的name、age,输出格式样例为:我是一只猫,我的名字是Anna,今年4岁(注意:输出结果中没有空格,逗号为英文标点符号)
cry方法输出Cat 的叫声,输出格式样例为:喵喵
需要你编写的模拟器类Simulator:
void playSound(IAbility animal):调用实现了IAbility接口类的showInfo和cry方法,并显示传入动物的名字和年龄
已有的Main类定义:
public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); IAbility animal=null; int type=input.nextInt(); String name=input.next(); int age=input.nextInt(); if (type==1) animal=new Dog(name,age); else animal=new Cat(name,age); Simulator sim=new Simulator(); sim.playSound(animal); input.close(); } } /***请在这里填写你编写的Dog类、Cat类和Simulator类** */
输入样例:
(本题的评分点与输入样例无关)
第一个整数代表动物类型,1为狗类,2为猫类
1 Mike 2
输出样例:
我是一只狗,我的名字是Mike,今年2岁
旺旺
Mike
2
class Dog extends Animal implements IAbility{
Dog(String name,int age){
super(name,age);
}
public void showInfo(){
System.out.println("我是一只狗,我的名字是"+getName()+",今年"+getAge()+"岁");
}
public void cry(){
System.out.println("旺旺");
System.out.println(getName());
System.out.println(getAge());
}
}
class Cat extends Animal implements IAbility{
Cat(String name,int age){
super(name,age);
}
public void showInfo(){
System.out.println("我是一只猫,我的名字是"+getName()+",今年"+getAge()+"岁");
}
public void cry(){
System.out.println("喵喵");
System.out.println(getName());
System.out.println(getAge());
}
}
class Simulator{
public void playSound(IAbility animal){
animal.showInfo();
animal.cry();
}
}
6-12 Java中类的设计 继承
分数 10
全屏浏览题目
切换布局
作者 吕行军
单位 河北农业大学
本题要求实现两个类Circle和Cylinder。
函数定义:
设计一个圆类Circle,具有私有属性:圆心坐标x和y及圆的半径r。空参和有参构造方法。除具有设置及获取属性的setXxx()和getXxx()方法外,还具有计算周长的方法perimeter()和计算面积的方法area()。
再设计一个圆柱体类Cylinder,Cylinder继承自Circle,增加了私有属性:高度h,增加了设置和获取h 的方法。通过调用父类Circle的perimter()和area()方法,计算表面积的方法Sarea()和计算体积的方法volume()。定义静态的PrintProperties(Cylinder c)方法,打印其圆心半径r、底面圆心坐标(x,y)、圆柱的高h。
创建Cylinder的类对象,打印其所有属性,计算并显示其面积和体积。
PI使用Math类中的值。
裁判测试程序样例:
import java.util.Scanner; /* 请在这里填写答案 */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double x = sc.nextDouble();//键盘读入x double y = sc.nextDouble();//键盘读入y double r = sc.nextDouble();//键盘读入r double h = sc.nextDouble();//键盘读入h Cylinder c = new Cylinder(x, y, r,h);//创建Cylinder对象c Cylinder.PrintProperties(c);//调用静态方法,打印对象c的所有属性值 System.out.println("表面积:" + c.Sarea());//输出圆柱表面 System.out.println("体积:" + c.Volume());//输出圆柱体积 } }
输入样例:
在这里给出一组输入。例如:
1 2 3 4
输出样例:
在这里给出相应的输出。例如:
半径:3.0
圆心坐标:(1.0, 2.0)
高:4.0
表面积:131.94689145077132
体积:113.09733552923255
class Circle{
private double x;
private double y;
private double r;
Circle(){
}
Circle(double x,double y,double r){
this.x=x;
this.y=y;
this.r=r;
}
public double getx(){
return this.x;
}
public double gety(){
return y;
}
public double getr(){
return r;
}
public double perimeter(){
return Math.PI*r*2;
}
public double area(){
return Math.PI*r*r;
}
}
class Cylinder extends Circle{
private double h;
Cylinder(double x,double y,double r,double h){
super(x,y,r);
this.h=h;
}
public double geth(){
return h;
}
public double Sarea(){
return perimeter()*h+2*area();
}
public double Volume(){
return area()*h;
}
static void PrintProperties(Cylinder c){
System.out.println("半径:"+c.getr());
System.out.println("圆心坐标:("+c.getx()+", "+c.gety()+")");
System.out.println("高:"+c.geth());
}
}
6-13 House类
分数 10
全屏浏览题目
切换布局
作者 郑珺
单位 浙江传媒学院
构造一个House类表示住宅 , 该类实现了Comparable接口。
- 该类有一个私有的String类型成员变量address,两个私有的double型成员变量area和pirce;
- 该类有一个带一个String型参数和两个double型参数的构造方法,用参数的值初始化house对象。
- 为address、area和price添加getter()和setter()方法。注意,住宅的面积和价格必须大于0,如果setArea(double area)方法的参数小于等于0时,抛出一个IllegalArgumentException异常对象,异常对象的成员变量message为"住宅的面积必须大于0";如果setPrice(double price)方法的参数小于等于0时,抛出一个IllegalArgumentException异常对象,异常对象的成员变量message为"住宅的价格必须大于0"。
- 该类有一个公共的方法int compareTo(House house),比较的依据是住宅的单位均价(价格除以面积)。如果当前对象的单位均价大于参数的单位均价,返回1;当前对象的单位均价小于参数的单位均价,返回-1;否则返回0。
- 该类有一个公共方法toString(),根据住宅的数据生成并返回一个字符串(具体要求看输出样例)。
构造一个Main ,执行一个for循环,共循环10次。每次循环从键盘读入数据创建两个House对象,比较并输出其中较大的对象;如果捕捉到异常,则输出异常信息。
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); for(int i=0; i<10; i++) { try{ House house1 = new House(input.next(), input.nextDouble(), input.nextDouble()); House house2 = new House(input.next(), input.nextDouble(), input.nextDouble()); House maxHouse; if (house1.compareTo(house2)>= 0) maxHouse = house1; else maxHouse = house2; System.out.println("The max of " + house1 + " and " + house2 + " is " + maxHouse); } catch (IllegalArgumentException e1) { System.out.println(e1.getMessage()); input.nextLine(); } catch (Exception e2) { System.out.println(e2.getMessage()); input.nextLine(); } } } } /* 请在这里填写答案 */
输入样例:
杭州1号 0 1000000 杭州2号 50 600000
杭州1号 -1 1000000 杭州2号 50 600000
杭州1号 100 0 杭州2号 50 600000
杭州1号 100 -1 杭州2号 50 600000
杭州1号 100 1000000 杭州2号 0 600000
杭州1号 100 1000000 杭州2号 -1 600000
杭州1号 100 1000000 杭州2号 50 0
杭州1号 100 1000000 杭州2号 50 -1
杭州1号 100 1000000 杭州2号 50 600000
杭州1号 100 2000000 杭州2号 50 600000
输出样例:
住宅的面积必须大于0
住宅的面积必须大于0
住宅的价格必须大于0
住宅的价格必须大于0
住宅的面积必须大于0
住宅的面积必须大于0
住宅的价格必须大于0
住宅的价格必须大于0
The max of House{address=杭州1号, area=100.0, price=1000000.0} and House{address=杭州2号, area=50.0, price=600000.0} is House{address=杭州2号, area=50.0, price=600000.0}
The max of House{address=杭州1号, area=100.0, price=2000000.0} and House{address=杭州2号, area=50.0, price
class House implements Comparable<House>{
private String address;
private double area;
private double price;
House(String address,double area,double price){
this.address=address;
this.area=area;
this.price=price;
if(area<=0){
throw new IllegalArgumentException("住宅的面积必须大于0");
}
else if(price<=0){
throw new IllegalArgumentException("住宅的价格必须大于0");
}
}
public double getarea(){
return this.area;
}
public double getprice(){
return this.price;
}
public String getaddress(){
return this.address;
}
public int compareTo(House house){
if(this.price/this.area>house.price/house.area){
return 1;
}
else if(this.price/this.area<house.price/house.area){
return -1;
}
else
return 0;
}
public String toString(){
return "House{address="+getaddress()+", area="+getarea()+", price="+getprice()+"}";
}
}
6-14 Rectangle类
分数 10
全屏浏览题目
切换布局
作者 郑珺
单位 浙江传媒学院
构造一个Rectangle类表示矩形 , 该类实现了Comparable接口。
- 该类有两个私有的double型成员变量width和height,表示矩形的宽度和高度;
- 该类有一个带一个无参的构造方法,把宽带和高度都初始化为0;
- 该类有一个带两个double型参数的构造方法,用参数的值初始化矩形的宽度和高度。
- 为width和height添加getter()和setter()方法。注意,宽度和高度必须大于等于0,如果setWidth(double width)方法的参数小于0时,抛出一个IllegalArgumentException异常对象,异常对象的成员变量message为"矩形的宽度必须大于等于0";setHeight(double height)方法的参数小于0时,抛出一个IllegalArgumentException异常对象,异常对象的成员变量message为"矩形的高度必须大于等于0"。
- getArea()方法,返回矩形的面积;
- getgetPerimeter()方法,返回矩形的周长;
- 该类有一个公共的方法int compareTo(Rectangle rectangle),比较当前对象和参数。如果当前对象的面积大于参数的面积,返回1;当前对象的面积小于参数的面积,返回-1;否则返回0。
- 该类有一个公共方法toString(),根据矩形的宽度和高度生成并返回一个字符串(具体要求看输出样例)。
构造一个Main ,执行一个for循环,共循环6次。每次循环从键盘读入数据创建两个矩形对象,比较并输出其中较大的对象的面积;如果捕捉到异常,则输出异常信息。
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); for(int i=0; i<6; i++) { try{ Rectangle rectangle1 = new Rectangle(input.nextDouble(), input.nextDouble()); Rectangle rectangle2 = new Rectangle(input.nextDouble(), input.nextDouble()); Rectangle maxRectangle; if (rectangle1.compareTo(rectangle2)>= 0) maxRectangle = rectangle1; else maxRectangle = rectangle2; System.out.println("The max area of " + rectangle1 + " and " + rectangle2 + " is " + maxRectangle.getArea()); } catch (IllegalArgumentException e1) { System.out.println(e1.getMessage()); input.nextLine(); } catch (Exception e2) { System.out.println(e2.getMessage()); input.nextLine(); } } } } /* 请在这里填写答案 */
输入样例:
-1 1 1 1
1 -1 1 1
1 1 -1 1
1 1 1 -1
3 4 2 5
3 4 2 7
输出样例:
矩形的宽度必须大于等于0
矩形的高度必须大于等于0
矩形的宽度必须大于等于0
矩形的高度必须大于等于0
The max area of Rectangle(width:3.0,height:4.0) and Rectangle(width:2.0,height:5.0) is 12.0
The max area of Rectangle(width:3.0,height:4.0) and Rectangle(width:2.0,height:7.0) is 14.0
class Rectangle implements Comparable<Rectangle>{
private double width;
private double height;
Rectangle(){
width=0;
height=0;
}
Rectangle(double width,double height){
this.width=width;
this.height=height;
if(width<0){
throw new IllegalArgumentException("矩形的宽度必须大于等于0");
}
else if(height<0){
throw new IllegalArgumentException("矩形的高度必须大于等于0");
}
}
public double getArea(){
return width*height;
}
public int compareTo(Rectangle r){
if(this.getArea()>r.getArea())
return 1;
else if(this.getArea()<r.getArea()){
return -1;
}
else
return 0;
}
public String toString(){
return "Rectangle(width:"+this.width+",height:"+this.height+")";
}
}
6-15 Birds
分数 10
全屏浏览题目
切换布局
作者 翁恺
单位 浙江大学
- Design an abstract class named
Bird
to represent a bird. The class contains anvoid sing()
method to print"This bird sings"
. - Design an interface named
Flyable
to represent anything can fly and contains anvoid fly()
method. - Design a concrete class named
Chicken
as subclass ofBird
and override thesing()
method to print"Chicken sings"
- Design a concrete class named
Superman
as subclass ofFlyable
and override thefly()
method to print"Superman flies"
- Design a concrete class named
Parrot
as subclass ofFlyable
andBird
and override the methodsfly()
andsing()
to print"Parrot flies"
and"Parrot sings"
separately.
裁判测试程序样例:
import java.util.Scanner; class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); int i = in.nextInt(); Flyable[] f = { new Parrot(), new Superman() }; for (Flyable k : f) { k.fly(); } System.out.println(i); Bird[] a = { new Parrot(), new Chicken() }; for ( Bird b : a ) { b.sing(); } in.close(); } } /* 请在这里填写答案 */
输入样例:
123
输出样例:
Parrot flies
Superman flies
123
Parrot sings
Chicken sings
abstract class Bird{
public void sing(){
System.out.println("This bird sings");
}
}
interface Flyable{
public abstract void fly();
}
class Chicken extends Bird{
public void sing(){
System.out.println("Chicken sings");
}
}
class Superman implements Flyable{
public void fly(){
System.out.println("Superman flies");
}
}
class Parrot extends Bird implements Flyable{
public void sing(){
System.out.println("Parrot sings");
}
public void fly(){
System.out.println("Parrot flies");
}
}
6-16 The Trucks
分数 10
全屏浏览题目
切换布局
作者 翁恺
单位 浙江大学
A logistic company owns some cargo trucks for TV, AC, washing machine, etc. Write a program to calculate the total weight of one truck.
-
Define an interface
ComputeWeight
, which has one methoddouble computeWeight()
; -
Create three concrete classes
Television
、AirConditioner
andWashMachine
, that implements interfaceComputeWeight
, and provides its weight. -
The TV weights16.6kg, AirConditioner weights 40.0kg, and WashMachine weights 60.0kg.
-
Define a class
Truck
, with a membergoods
as a collcetion ofComputeWeight
s, represents all the appliants in the truck.Truck
has a public methodgetTotalWeight()
returns the sum of the weight of all the goods.
方法接口定义:
double computeWeight(); double getTotalWeight();
裁判测试程序样例:
import java.util.ArrayList; import java.util.Collection; import java.util.Scanner; enum App { TV(new Television()), AC(new AirConditioner()), WM(new WashMachine()); private ComputeWeight theC; App(ComputeWeight c) { theC = c; } ComputeWeight getGoods() { return theC; } } public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); Collection<ComputeWeight> goods = new ArrayList<>(); for (int i = 0; i < n; i++) { String name = in.next(); goods.add(App.valueOf(name).getGoods()); } Truck truck = new Truck(goods); System.out.printf("Total weight:%8.2fkg\n", truck.getTotalWeight()); in.close(); } } /* Put your code here. */
输入格式:
The first line is a number n indicates the number of goods in the truck.
Then, there will be n lines of abbreviation of the goods. These abbreviations are:
- TV, for television
- WM, for washmachine
- AC, for airconditioner
输出格式:
Total weight:%8.2fkg
输入样例:
4
TV
TV
WM
AC
输出样例:
Total weight: 133.20kg
interface ComputeWeight{
public abstract double computeWeight();
}
class Television implements ComputeWeight{
public double computeWeight(){
return 16.6;
}
}
class AirConditioner implements ComputeWeight{
public double computeWeight(){
return 40.0;
}
}
class WashMachine implements ComputeWeight{
public double computeWeight(){
return 60.0;
}
}
class Truck{
Collection<ComputeWeight> goods;
Truck(Collection goods){
this.goods=goods;
}
public double getTotalWeight(){
double sum=0;
for(ComputeWeight c:goods){
sum=sum+c.computeWeight();
}
return sum;
}
}
6-17 求圆面积自定义异常类
分数 25
全屏浏览题目
切换布局
作者 刘凤良
单位 天津仁爱学院
计算圆的面积,其中PI取3.14,圆半径为负数时应抛出异常,输出相应提示。根据提供的主类信息,编写Circle类和CircleException类,以及在相关方法中抛出异常。。
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { double s = 0; Scanner sc = new Scanner(System.in); double r1, r2; r1 = sc.nextDouble(); r2 = sc.nextDouble(); Circle c1 = new Circle(r1); Circle c2 = new Circle(r2); try { s = c1.area(); System.out.println(s); s = c2.area(); System.out.println(s); } catch (CircleException e) { e.print(); } } } /* 请在这里填写答案 编写 Circle 和 CircleException类*/
输入样例:
3.5 -3.5
输出样例:
38.465
圆半径为-3.5不合理
class Circle{
private double r;
Circle(double r){
this.r=r;
}
public double area() throws CircleException{
if(this.r<0)
throw new CircleException("圆半径为"+this.r+"不合理");
double s=this.r*this.r*3.14;
return s;
}
}
class CircleException extends Exception{
CircleException(String s){
super(s);
}
public void print(){
System.out.println(this.getMessage());
}
}
6-18 柱形体积
分数 10
全屏浏览题目
切换布局
作者 clk
单位 浙江工商大学杭州商学院
请按类图创建类Shape、Circle、Rectangle、Pillar。其中Pillar类(柱类),该类的getVolume()方法可以计算柱体的体积。
柱体的体积=底面积*高。
柱体的底面可能是圆形,矩形或其他形状。
类图:
注意:
- shape类中的getArea()方法是抽象方法。
- 类Circle中,成员变量r表示半径;构造方法Circle()用参数r初始化域r;成员方法getArea()计算并返回圆面积,使用Math.PI。
- 类Rectangle中,成员变量a表示矩形长;成员变量b表示矩形宽;构造方法Rectangle ()用参数a、b分别初始化域a和域b;成员方法getArea()计算并返回矩形面积。
- 类Pillar中,成员变量bottom表示柱形底面;成员变量height表示柱形高;构造方法Pillar()用参数bot和hei分别初始化域bottom和域height;成员方法getVolume()计算并返回柱形体积。
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如: import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner in=new Scanner(System.in); //声明Pillar类对象pil Pillar pil; //声明Shape类对象shp; Shape shp; //键盘输入半径r,创建半径为r的Circle类对象,赋值给对象shp double r=in.nextDouble(); shp=new Circle(r); //键盘输入柱形高h,用底面shp和高h创建Pillar类对象,赋值给对象pil double h=in.nextDouble(); pil=new Pillar(shp,h); //显示柱形体积 System.out.printf("圆形底的柱体体积%.2f\n",pil.getVolume()); //键盘输入矩形长a和宽b,创建Rectangle类对象,赋值给对象shp double a=in.nextDouble(); double b=in.nextDouble(); shp=new Rectangle(a,b); //键盘输入柱形高h,用底面shp和高h创建Pillar类对象,赋值给对象pil h=in.nextDouble(); pil=new Pillar(shp,h); //显示柱形体积 System.out.printf("矩形底的柱体体积%.2f\n",pil.getVolume()); in.close(); } } /* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
1 10
2 5 10
输出样例:
在这里给出相应的输出。例如:
圆形底的柱体体积31.42
矩形底的柱体体积100.00
interface Shape{
public abstract double getArea();
}
class Circle implements Shape{
private double r;
Circle(double r){
this.r=r;
}
public double getArea(){
return r*r*Math.PI;
}
}
class Rectangle implements Shape{
private double a;
private double b;
Rectangle(double a,double b){
this.a=a;
this.b=b;
}
public double getArea(){
return a*b;
}
}
class Pillar implements Shape{
private Shape bottom;
private double height;
Pillar(Shape b,double h){
bottom=b;
height=h;
}
public double getArea(){
return 0;
}
public double getVolume(){
return bottom.getArea()*height;
}
}
6-19 分数类
分数 10
全屏浏览题目
切换布局
作者 Ma
单位 山东科技大学
编写一个分数类Fraction
,该类包含两个int型参数表示分子与分母。
同时,该类包含如下方法:
(1)Fraction plus(Fraction r)
表示将自己的分数和r的分数相加,产生一个新的Fraction
的对象。
(2)void print()
表示将其按照“分子/分母”的形式输出。注意:若存在可以化简的情况需要化简后输出,即2/4应该被化简为1/2再输出,若结果为1/1,则输出1。
注意:暂不考虑分母为0的情况。
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Fraction a = new Fraction(in.nextInt(), in.nextInt()); Fraction b = new Fraction(in.nextInt(),in.nextInt()); a.print(); b.print(); a.plus(b).print(); a.print(); b.print(); in.close(); } } /* 请在这里填写答案 */
输入样例:
2 4 1 3
输出样例:
1/2
1/3
5/6
1/2
1/3
class Fraction{
private int fenzi;
private int fenmu;
Fraction(int f1,int f2){
fenzi=f1;
fenmu=f2;
}
public int getfenzi(){
return this.fenzi;
}
public int getfenmu(){
return this.fenmu;
}
public Fraction plus(Fraction r){
Fraction f;
int zi=this.getfenzi()*r.getfenmu()+this.getfenmu()*r.getfenzi();
int mu=this.getfenmu()*r.getfenmu();
f=new Fraction(zi,mu);
return f;
}
public void print(){
int count=1;
while(count!=0){
count=1;
for(int i=2;i<=9;i++){
if(fenzi%i==0&&fenmu%i==0){
fenzi=fenzi/i;
fenmu=fenmu/i;
count++;
break;
}
}
if(count==1){
break;
}
}
if(fenzi==fenmu){
System.out.println(1);
}
else{
System.out.println(fenzi+"/"+fenmu);
}
}
}
6-20 jmu-Java-05集合-List中指定元素的删除
分数 15
全屏浏览题目
切换布局
作者 郑如滨
单位 集美大学
编写以下两个函数
//以空格(单个或多个)为分隔符,将line中的元素抽取出来,放入一个List
public static List<String> convertStringToList(String line)
//在list中移除掉与str内容相同的元素
public static void remove(List<String> list, String str)
裁判测试程序:
public class Main { /*covnertStringToList函数代码*/ /*remove函数代码*/ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){ List<String> list = convertStringToList(sc.nextLine()); System.out.println(list); String word = sc.nextLine(); remove(list,word); System.out.println(list); } sc.close(); } }
样例说明:底下展示了4组测试数据。
输入样例
1 2 1 2 1 1 1 2
1
11 1 11 1 11
11
2 2 2
1
1 2 3 4 1 3 1
1
输出样例
[1, 2, 1, 2, 1, 1, 1, 2]
[2, 2, 2]
[11, 1, 11, 1, 11]
[1, 1]
[2, 2, 2]
[2, 2, 2]
[1, 2, 3, 4, 1, 3, 1]
[2, 3, 4, 3]
public static List<String> convertStringToList(String line){
String[] s=line.split(" +");
List<String> c=new ArrayList<>();
for(int i=0;i<s.length;i++){
c.add(s[i]);
}
return c;
}
public static void remove(List<String> list,String str){
Iterator<String> it=list.iterator();
while(it.hasNext()){
String s=it.next();
if(str.equals(s)){
it.remove();
}
}
}
6-21 成绩管理系统
分数 30
全屏浏览题目
切换布局
作者 温彦
单位 山东科技大学
构造一个成绩管理系统CourseManagementSystem,该系统包括如下几个方法:void add(int no, int grade)添加该学号的成绩,如果系统已有该学生成绩,则输出"the student already exists";void delete(int no)删除某学号成绩,如果不存在此学生则输出"no such student";int query(int no)查询并返回该学号的成绩;统计成绩void statistics( )统计[0-59]、[60-69]、[70-79]、[80-89]、[90-100]各成绩段的学生个数并打印。请选择合适的容器实现上述功能。(题目假设不会重复添加相同学号的学生成绩)
main函数中读入操作类型及相关参数,并调用statictic函数输出学生成绩统计信息。
输入描述:
操作个数
操作名 操作参数
输出描述:
查询学生的成绩
各成绩段的学生个数
裁判测试程序样例:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CourseManagementSystem cms = new CourseManagementSystem();
int ops = sc.nextInt();
for (int i=0;i<ops;i++) {
String op = sc.next();
if (op.equals("add"))
cms.add(sc.nextInt(), sc.nextInt());
else if (op.equals("delete"))
cms.delete(sc.nextInt());
else if (op.equals("query")) {
int no = sc.nextInt();
int s = cms.query(no);
System.out.println("the score for "+no+" is : "+s);
}
}
cms.statistic();
}
}
/* 你的代码被嵌在这里*/
输入样例:
在这里给出一组输入。例如:
8
add 1 63
add 2 78
add 3 74
delete 3
add 2 20
delete 5
query 1
add 4 90
输出样例:
在这里给出相应的输出。例如:
the student already exists
no such student
the score for 1 is : 63
[0-59] : 0
[60-69] : 1
[70-79] : 1
[80-89] : 0
[90-100] : 1
class CourseManagementSystem{
LinkedHashMap<Integer,Integer> student=new LinkedHashMap<>();
public void add(int no,int grade){
if(student.containsKey(no))
System.out.println("the student already exists");
else
student.put(no,grade);
}
public void delete(int no){
if(!student.containsKey(no)){
System.out.println("no such student");
}
else{
student.remove(no);
}
}
public int query(int no){
return student.get(no);
}
public void statistic(){
int n1=0;
int n2=0;
int n3=0;
int n4=0;
int n5=0;
Set<Integer> s=student.keySet();
for(Integer str:s){
Integer str1=student.get(str);
if(str1>=0&&str1<=59)
n1++;
else if(str1>=60&&str1<=69)
n2++;
else if(str1>=70&&str1<=79)
n3++;
else if(str1>=80&&str1<=89)
n4++;
else
n5++;
}
System.out.println("[0-59] : "+n1);
System.out.println("[60-69] : "+n2);
System.out.println("[70-79] : "+n3);
System.out.println("[80-89] : "+n4);
System.out.println("[90-100] : "+n5);
}
}
6-22 判断一个数列是否已排好序
分数 10
全屏浏览题目
切换布局
作者 殷伟凤
单位 浙江传媒学院
编写如下所示的一个方法,判断一个数列是否已排序,如果已按升序排列则返回true。
public static boolean isSorted(int[] list)
主测试程序输入一组数据,然后输出该数列是否已排序或未排好序。
注意:输入的第一个数为该数列的元素个数。
函数接口定义:
ipublic static boolean isSorted(int[] list)
list为输入的一组数据
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] list = new int[20]; list[0] = input.nextInt(); for (int i=1; i<= list[0]; i++) list[i] = input.nextInt(); if (isSorted(list)) System.out.println("The list is already sorted"); else System.out.println("The list is not sorted"); } /* 请在这里填写答案 */ }
输入样例1:
8 10 1 5 16 61 9 11 1
输出样例1:
The list is not sorted
输入样例2:
10 1 1 3 4 4 5 7 9 11 21
输出样例2:
The list is already sorted
public static boolean isSorted(int[] list){
boolean b=true;
for(int i=1;i<list[0];i++){
if(list[i]>list[i+1])
return false;
}
return true;
}
临近期末考试了,博主希望大家都能考出令自己满意的成绩!