7-1 柱形体积
请按类图创建类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
参考答案:
abstract class Shape { // 抽象方法,计算面积 abstract double getArea(); } // Circle 类 class Circle extends Shape { private double r; // 半径 // 构造方法 public Circle(double r) { this.r = r; } // 计算圆的面积 @Override public double getArea() { return Math.PI * r * r; } } // Rectangle 类 class Rectangle extends Shape { private double a; // 长 private double b; // 宽 // 构造方法 public Rectangle(double a, double b) { this.a = a; this.b = b; } // 计算矩形的面积 @Override public double getArea() { return a * b; } } // Pillar 类 class Pillar { private Shape bottom; // 底面 private double height; // 高 // 构造方法 public Pillar(Shape bottom, double height) { this.bottom = bottom; this.height = height; } // 计算柱体的体积 public double getVolume() { return bottom.getArea() * height; } }
7-2 图书和音像租赁
图书和音像店提供出租服务,包括图书和DVD的出租。图书包括书名(String,一个词表示)和价格(double),DVD包括片名(String,一个词表示)。它们都是按天出租,但租金计算方式却不同,图书的日租金为图书价格的1%,DVD的日租金为固定的1元。构造图书和DVD类的继承体系,它们均继承自Media类,且提供方法getDailyRent()返回日租金,构造音像店类MediaShop,提供静态函数double calculateRent(Media[] medias, int days)。
在main函数中构造了Media数组,包含图书和DVD的对象,调用calculateRent方法得到并输出租金,保留小数点两位输入描述:
待租图书和DVD的数量
图书和DVD的详细信息
租借天数
输出描述:
总的租金
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Media[] ms = new Media[n]; for (int i=0; i<n; i++) { String type = sc.next(); if (type.equals("book")) { ms[i] = new Book(sc.next(), sc.nextDouble()); }else { ms[i] = new DVD(sc.next()); } } double rent = MediaShop.calculateRent(ms, sc.nextInt()); System.out.printf("%.2f", rent); } } /* 请在这里填写答案 */
输入样例:
5 book Earth 25.3 book Insights 34 dvd AI dvd Transformer book Sun 45.6 20
输出样例:
60.98
参考答案:
abstract class Media{//根据题意Media时抽象类 abstract double getDailyRent(); } class Book extends Media{//Book继承自Media String name; double price; public Book(String name,double price) { this.name=name; this.price=price; } public double getDailyRent() { return this.price*0.01; } } class DVD extends Media{//DVD继承自Media String name; public DVD(String name) { this.name=name; } public double getDailyRent() { return 1; } } class MediaShop{//MediaShop类 //提供静态方法!!! public static double calculateRent(Media[] medias, int days) { double sum=0; for(Media m:medias) { if(m instanceof DVD) { DVD d=(DVD)m; sum+=d.getDailyRent()*days; }else if(m instanceof Book) { Book b=(Book)m; sum+=b.getDailyRent()*days; } } return sum; } }
7-3 坦克与火箭
已有类Car、Plane。请创建接口Weapon以及类Tank和Fighter。
接口Weapon中定义了无参数和返回值的方法shoot。
类Tank和Fighter分别继承Car和Plane,并且都实现了接口weapon。
请在类Tank和Fighter中分别实现接口weapon中的方法Shoot,Tank类中的shoot方法能够打印“发射大炮”,Fighter类中的shoot方法能够打印“发射火箭”。裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如: class Car{ public void move() { System.out.println("running"); } } class Plane{ public void move() { System.out.println("flying"); } } /* 请在这里填写答案 */ public class Main{ public static void main(String argv[]){ Tank tank = new Tank(); Fighter fighter = new Fighter(); tank.move(); tank.shoot(); fighter.move(); fighter.shoot(); Weapon tank2 = new Tank(); Weapon fighter2= new Fighter(); tank2.shoot(); fighter2.shoot(); } }
输出样例:无输入
输出样例:
在这里给出相应的输出。例如:
running 发射大炮 flying 发射火箭 发射大炮 发射火箭
参考答案:
interface Weapon{ public abstract void shoot(); } class Tank extends Car implements Weapon{ @Override public void shoot(){ System.out.println("发射大炮"); } } class Fighter extends Plane implements Weapon{ @Override public void shoot(){ System.out.println("发射火箭"); } }
7-4 创建一个直角三角形类实现IShape接口
创建一个直角三角形类(regular triangle)RTriangle类,实现下列接口IShape。两条直角边长作为RTriangle类的私有成员,类中包含参数为直角边的构造方法。
interface IShape {// 接口
public abstract double getArea(); // 抽象方法 求面积
public abstract double getPerimeter(); // 抽象方法 求周长
}
###直角三角形类的定义:
直角三角形类的构造函数原型如下:
RTriangle(double a, double b);
其中
a
和b
都是直角三角形的两条直角边。裁判测试程序样例:
import java.util.Scanner; import java.text.DecimalFormat; interface IShape { public abstract double getArea(); public abstract double getPerimeter(); } /*你写的代码将嵌入到这里*/ public class Main { public static void main(String[] args) { DecimalFormat d = new DecimalFormat("#.####"); Scanner input = new Scanner(System.in); double a = input.nextDouble(); double b = input.nextDouble(); IShape r = new RTriangle(a, b); System.out.println(d.format(r.getArea())); System.out.println(d.format(r.getPerimeter())); input.close(); } }
输入样例:
3.1 4.2
输出样例:
6.51 12.5202
参考答案:
class RTriangle implements IShape { private double a; private double b; RTriangle(double a1, double b1){ this.a=a1; this.b=b1; } public double getArea() { return a*b*0.5; } public double getPerimeter() { return a+b+Math.sqrt(a*a+b*b); } }
7-5 面积求和
由于各种图形 求面积的方式不一样,为了使编写的程序能够适应求不同形状的面积和,当新增加一个图形的时候,不需要修改其他类,采用面向接口的编程,其类图如下图所示:
根据以上类图,你需要设计和编写如下类:
######1.IGeometry接口类 ,里面有double getArea()接口方法
2.Rect类实现接口,有属性 a,b,有参构造方法及对接口方法的实现
######3.Circle类实现接口,有属性r,有参构造方法及对接口方法的实现,其中PI=3.14
4.TotalArea类完成对不同形状图形面积求和,有属性IGeometry[] tuxing 数组用来存储 不同形状图形对象,SetTuxing(IGeometry[] t) 给属性tuxing赋值,double computeTotalArea()方法用来计算tuxing数组中存放的图形的面积和,并返回面积和。主类已经给出,请结合主类完成上面类的编写
裁判测试程序主类:
public class Main { public static void main(String[] args) { IGeometry [] tuxing=new IGeometry[29]; //有29个Geometry对象 for(int i=0;i<tuxing.length;i++) { //29个Geometry对象分成两类 if(i%2==0) tuxing[i]=new Rect(16+i,68); else if(i%2==1) tuxing[i]=new Circle(10+i); } TotalArea computer=new TotalArea(); computer.setTuxing(tuxing); System.out.printf("各种图形的面积之和:\n%.2f",computer.computerTotalArea()); } } /* 请在这里填写答案 请结合主类代码,在代码框完成IGeometry接口类,Rect类,Circle类和TotalArea类*/
输入样例:
在这里给出一组输入。例如:
无
输出样例:
在这里给出相应的输出。例如:
各种图形的面积之和: 58778.36
参考答案:
class Circle implements IGeometry{ double r; Circle(double r){ this.r = r; } public double getArea(){ return r*r*3.14; } } class Rect implements IGeometry{ double a,b; Rect(double a,double b){ this.a = a; this.b = b; } public double getArea(){ return a*b; } } class TotalArea { IGeometry[] tuxing; void setTuxing(IGeometry[] t){ tuxing = t; } double computerTotalArea() { double sum = 0; for (int i = 0; i < tuxing.length; i++) { sum = sum + tuxing[i].getArea(); } return sum; } } interface IGeometry { double getArea(); }
7-6 员工的工资
假定要为某个公司编写雇员工资支付程序,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资(都是整数):
(1)经理(Manager)——每月获得一份固定的工资
(2)销售人员(Salesman)——在基本工资的基础上每月还有销售提成
(3)一般工人(Worker)——则按他每月工作的天数计算工资
在Employee中提供方法getSalary(),用于计算每个雇员一个月的工资,并在子类中重写。Post_AppendCode的main方法中已经构造Employee的三个变量,分别指向Manager、Salesman、Worker的对象,调用getSalary方法,输出三个对象的工资。
要求:编码实现经理、销售人员、一般工人三个类。输入描述:
经理的月工资
销售人员的基本工资 销售人员的提成
工人的工作天数 工人每天的工资输出描述:
经理的工资
销售人员的工资
工人的工资裁判测试程序样例:
/*你的代码被嵌在这里*/ public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int managerSalary = scan.nextInt(); int salemanSalary = scan.nextInt(); int salemanRaise = scan.nextInt(); int workerEveryday = scan.nextInt(); int workerDays = scan.nextInt(); Employee e1 = new Manager(managerSalary); Employee e2 = new Salesman(salemanSalary, salemanRaise); Employee e3 = new Worker(workerEveryday, workerDays); System.out.println(e1.getSalary()); System.out.println(e2.getSalary()); System.out.println(e3.getSalary()); scan.close(); } }
输入样例:
在这里给出一组输入。例如:
12000 3000 5000 22 200
输出样例:
在这里给出相应的输出。例如:
12000 8000 4400
参考答案:
import java.util.Scanner; // 基类 Employee abstract class Employee { // 抽象方法,子类需要实现 public abstract int getSalary(); } // 经理类 Manager class Manager extends Employee { private int salary; public Manager(int salary) { this.salary = salary; } @Override public int getSalary() { return salary; } } // 销售人员类 Salesman class Salesman extends Employee { private int basicSalary; private int raise; public Salesman(int basicSalary, int raise) { this.basicSalary = basicSalary; this.raise = raise; } @Override public int getSalary() { return basicSalary + raise; } } // 一般工人类 Worker class Worker extends Employee { private int dailySalary; private int workDays; public Worker(int dailySalary, int workDays) { this.dailySalary = dailySalary; this.workDays = workDays; } @Override public int getSalary() { return dailySalary * workDays; } }
7-7 租车服务
某租车公司提供租车服务,针对不同的车辆类型,日租金的计算方式不同,具体地,对于货车而言,根据载重量load(单位是吨)计算,公式为loadx 1000;对于大型客车而言,根据车内座位数seats计算,公式为seatsx50;对于小型汽车而言,根据车辆等级和折旧年数计算,公式为200*level/sqrt(year),其中sqrt表示平方根。设计合适的类继承结构实现上述功能,构造租车公司类CarRentCompany,提供静态函数rentVehicles,能够给定一组待租车辆,计算日租金总额。
在main函数中,读入多个车辆数据,并计算总的日租金。输入描述:
汽车数量
汽车种类 该类汽车相关属性
其中1表示货车,2表示大型客车,3表示小型汽车输出描述:
总的日租金,保留两位小数
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int c = sc.nextInt(); Vehicle[] vs = new Vehicle[c]; for (int i=0;i<c;i++) { int type = sc.nextInt(); Vehicle v = null; if (type == 1) {//货车 vs[i] = new Truck (sc.nextDouble()); } else if (type == 2) { vs[i] = new Keche(sc.nextInt()); } else if (type == 3) { vs[i] = new Car(sc.nextInt(), sc.nextInt()); } } System.out.printf("%.2f",CarRentCompany.rentVehicles(vs)); } } /* 你的代码被嵌在这里 */
输入样例:
在这里给出一组输入。例如:
3 1 3 2 50 3 5 5
输出样例:
在这里给出相应的输出。例如:
5947.21
参考答案:
abstract class Vehicle{//将车辆类写成抽象类更易实现 abstract double getMoney(); } class Truck extends Vehicle{//货车 double load; public Truck(double load) { this.load=load; } public double getMoney() { return 1000*this.load; } } class Keche extends Vehicle{//卡车 int seat; public Keche(int seat) { this.seat=seat; } public double getMoney() { return 50*this.seat; } } class Car extends Vehicle{//小型车辆 int level; int year; public Car(int level,int year) { this.level=level; this.year=year; } public double getMoney() { return 200*this.level/(Math.sqrt(this.year)); } } class CarRentCompany{ public static double rentVehicles(Vehicle[] vs) {//静态方法 double sum=0; for(int i=0;i<vs.length;i++) { sum+=vs[i].getMoney(); } return sum; } }
7-8 设计门票(抽象类)
某学校举办一次活动,需要凭票参加,每张票都有票号和售价。
门票分为3类:当天票,预订票和学生预订票。
当天票价格50。
预订票,提前时间>10天的,售价30;提前10天以内的,售价40;
学生预订票,提前时间>10天的,售价15;提前10天以内的,售价20。(1)编写抽象类Ticket类,包含以下成员
①属性:
number:私有,int型,表示票号;
②方法:
- Ticket(int number), 构造方法,初始化票号;
- int getPrice(), 返回票价,抽象方法;
- String toString(),返回一个字符串,格式为“Number:票号,Price:票价”。
(2)编写Ticket类的子类WalkupTicket,表示当天票,包含以下成员
①方法:
1)WalkupTicket(int number), 构造方法,初始化票号;
2)int getPrice(), 返回票价50。(3)编写Ticket类的子类AdvanceTicket,表示预订票,包含以下成员
①属性:
- leadTime:私有,int型,表示提前预订的天数;
②方法:- AdvanceTicket(int number,int leadTime), 构造方法,初始化票号和提前天数;
- int getPrice(), 如果提前天数>10天,票价30,否则,票价40。
(4)编写AdvanceTicket类的子类StudentAdvanceTicket,表示学生预订票,包含以下成员
①属性:
height:私有,int型,表示购票学生的身高(单位厘米);
②方法:
1)StudentAdvanceTicket(int number,int leadTime,int height), 构造方法,初始化票号、提前天数和身高;
2)int getPrice(),如果学生身高>120:提前天数>10天,票价20,否则,票价30。
如果学生身高<=120,票价是身高120以上学生的对折。裁判测试程序样例:
public class Main{ public static void main(String args[]) { Scanner in = new Scanner(System.in); Ticket a = new WalkupTicket(in.nextInt()); System.out.println(a.toString()); Ticket b = new AdvanceTicket(in.nextInt(), in.nextInt()); System.out.println(b.toString()); Ticket c = new StudentAdvanceTicket(in.nextInt(), in.nextInt(), in.nextInt()); System.out.println(c.toString()); } } /* 请在这里填写答案 */
按如下框架设计类后提交即可:
abstract class Ticket { …… } class WalkupTicket extends Ticket { …… } class AdvanceTicket extends Ticket { …… } class StudentAdvanceTicket extends AdvanceTicket { …… }
输入样例:
输入票号,创建一个WalkupTicket对象。
输入票号和提前天数,创建一个AdvanceTicket对象。
输入票号、提前天数和身高,创建一个StudentAdvanceTicket对象。1 2 10 3 11 130
输出样例:
输出创建的3个对象的票号和票价(调用toString()方法)。
Number:1,Price:50 Number:2,Price:40 Number:3,Price:20
参考答案:
abstract class Ticket { private int number; public Ticket(int number) { this.number = number; } abstract int getPrice(); public String toString() { return "Number:"+number+",Price:"+getPrice(); } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } } class WalkupTicket extends Ticket { public WalkupTicket(int number) { super(number); } @Override int getPrice() { return 50; } } class AdvanceTicket extends Ticket { private int leadTime; public AdvanceTicket(int number,int leadTime) { super(number); this.leadTime = leadTime; } @Override int getPrice() { if(leadTime>10) return 30; else return 40; } } class StudentAdvanceTicket extends AdvanceTicket { private int height; int lead; public StudentAdvanceTicket(int number,int leadTime,int height) { super(number,leadTime); this.lead=leadTime; this.height = height; } @Override int getPrice() { if(height>120) { if(lead>10) return 20; else return 30; } else { if(lead>10) return 10; else return 15; } } }
7-9 动物体系
基于继承关系编写一个动物体系,具体的动物包含小狗和小猫。每只动物都有名字和颜色,都能够做自我介绍(introduce)。此外,小狗有智商属性(整数),能接飞盘(catchFrisbee(),方法体内输出一行“catch frisbee”即可),小猫有眼睛颜色属性,能抓老鼠(catchMouse(),方法体内输出一行“catch mouse”即可)。各种小动物自我介绍时均介绍自己的姓名和颜色,此外,小狗应介绍自己的智商,小猫应介绍自己的眼睛颜色。小狗介绍时输出”My name is xxx, my color is xxx, my IQ is xxx”, 小猫介绍时输出“My name is xxx, my color is xxx, my eyecolor is xxx”
构造类TestAnimal,提供静态函数introduce(Animal),对参数动物自我介绍。提供静态函数action(Animal),根据参数对象的实际类型进行活动,如果是小狗,则让其接飞盘,如果是小猫,则让其抓老鼠。
Main函数中,根据动物类型构造动物,并调用TestAnimal中的方法进行自我介绍(introduce)和活动(action)输入描述:
动物类型 动物名称 动物颜色 动物其他属性 如
1 猫名称 猫颜色 猫眼睛颜色
2 狗名称 狗颜色 狗的智商输出描述:
自我介绍
活动裁判测试程序样例:
import java.util.Scanner; /*你的代码被嵌在这里 */ public class Main{ public static void main(String args[]) { Scanner s = new Scanner (System.in); int i = s.nextInt(); Animal a = null; if (i==1) { a = new Cat(s.next(), s.next(), s.next()); } else if (i==2) { a = new Dog(s.next(), s.next(), s.nextInt()); } TestAnimal.introduce(a); TestAnimal.action(a); } }
输入样例:
在这里给出一组输入。例如:
1 Mikey white blue
输出样例:
在这里给出相应的输出。例如:
My name is Mikey, my color is white, my eyecolor is blue catch mouse
参考答案:
abstract class Animal { private String name; private String color; Animal(String name,String color) { this.name = name; this.color = color; } public String getName() { return name; } public String getColor() { return color; } abstract public void introduce(); } class Dog extends Animal { private int IQ; Dog(String name,String color,int IQ) { super(name,color); this.IQ = IQ; } public void catchFrisbee() { System.out.println("catch frisbee"); } public void introduce() { System.out.println("My name is "+super.getName()+", my color is "+super.getColor()+", my IQ is "+IQ); } } class Cat extends Animal { private String eyeColor; Cat(String name,String color,String eyeColor) { super(name,color); this.eyeColor = eyeColor; } public void catchMouse() { System.out.println("catch mouse"); } public void introduce() { System.out.println("My name is "+super.getName()+", my color is "+super.getColor()+", my eyecolor is "+eyeColor); } } class TestAnimal { public static void introduce(Animal a) { a.introduce(); } public static void action(Animal a) { if(a instanceof Dog) { Dog dog = (Dog)a; dog.catchFrisbee(); } else { Cat cat = (Cat) a; cat.catchMouse(); } } }
7-10 Animal动物工厂
已知有如下Animal抽象类,请编写其子类Dog类与Cat类,另外再编写一个生产动物的Factory工厂类,具体要求如下。
已有的Animal抽象类定义:
abstract class Animal{ private String name; //名字 private int age; //年龄 public abstract void info(); //返回动物信息 public abstract void speak(); //动物叫 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; } }
需要你编写的Dog子类:
增加violence(凶猛程度)属性(int型),重写info和speak方法
info方法输出Dog的name、age和violence属性,输出格式样例为:该狗的名字是Mike,年龄是2岁,凶猛程度是78度 (注意:输出结果中没有空格,逗号为英文标点符号)
speak方法输出Dog 的叫声,输出格式样例为:旺旺
需要你编写的Cat子类:
增加mousingAbility(捕鼠能力)属性(int型),重写info和speak方法
info方法输出Cat的name、age和mousingAbility属性,输出格式样例为:该猫的名字是Kitty,年龄是4岁,捕鼠能力是88分 (注意:输出结果中没有空格,逗号为英文标点符号)
speak方法输出Cat 的叫声,输出格式样例为:喵喵
需要你编写的Factory工厂类:
Animal getAnimalByType(int type,String name, int age, int ownAttribute):根据传入的子类类型和属性值,返回一个Animal类型的上转型对象,type为1代表狗类,type为2代表猫类
已有的Main类定义:
public class Main { public static void main(String[] args) { Factory factory=new Factory(); Scanner input=new Scanner(System.in); int type=input.nextInt(); String name=input.next(); int age=input.nextInt(); int ownAttribute=input.nextInt(); Animal a=factory.getAnimalByType(type, name, age, ownAttribute); a.info(); a.speak(); System.out.println(a.getName()); System.out.println(a.getAge()); input.close(); } } /*请在这里填写你编写的Dog类、Cat类和Factory类 */
输入样例:
(本题的评分点与输入样例无关)
1 Mike 2 78
输出样例:
该狗的名字是Mike,年龄是2岁,凶猛程度是78度 旺旺 Mike 2
参考答案:
class Factory{ Animal a; public Factory(){} public Animal getAnimalByType(int type,String name, int age, int ownAttribute){ if(type==1){ a = new Dog(name,age,ownAttribute); return a; }else /*if(type==2)*/{ a = new Cat(name,age,ownAttribute); return a; } } } //增加violence(凶猛程度)属性(int型),重写info和speak方法 class Dog extends Animal{ int vi; Dog(String name, int age, int ownAttribute){ super(name,age); this.vi=ownAttribute; } public void info(){ System.out.println("该狗的名字是"+this.getName()+",年龄是"+this.getAge()+"岁,凶猛程度是"+vi+"度"); } //返回动物信息 public void speak(){ System.out.println("旺旺"); } //动物叫 }class Cat extends Animal{ int vi; Cat(String name, int age, int ownAttribute){ super(name,age); this.vi=ownAttribute; }public void info(){ System.out.println("该猫的名字是"+this.getName()+",年龄是"+this.getAge()+"岁,捕鼠能力是"+vi+"分"); } //返回动物信息 public void speak(){ System.out.println("喵喵"); } }
7-11 卡车载重
某大型家电企业拥有一批送货卡车,运送电视机、空调、洗衣机等家电。编程计算每个卡车所装载货物的总重量。
要求如下:
1.创建一个ComputeWeight接口,接口中有方法:double computeWeight();
2.创建三个类Television、AirConditioner和WashMachine。这些类都实现了ComputeWeight接口,能够提供自重。其中TV的自重为16.6kg,AirConditioner的自重为40.0kg,WashMachine的自重为60.0kg。
3.创建一个Truck类,私有成员变量goods是一个ComputeWeight型的数组,包含了该货车上的所有家电,公有方法getTotalWeight()返回goods中所有货物的重量之和。
输入格式:
家电数量
家电种类编号
注意:各个家电的编号为:Television:1 , AirConditioner:2 , WashMachine:3
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如: public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n=input.nextInt(); int type; ComputeWeight [] goods=new ComputeWeight[n]; //n件货物 for(int i=0;i<goods.length ;i++){ type=input.nextInt(); //读入家电类型 if (type==1) goods[i]=new Television(); else if (type==2) goods[i]=new AirConditioner(); else if (type==3) goods[i]=new WashMachine(); } Truck truck =new Truck(goods); System.out.printf("货车装载的货物总量:%8.2f kg",truck.getTotalWeight()); } } /* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
10 1 2 3 3 2 1 3 3 3 2
输出样例:
在这里给出相应的输出。例如:
货车装载的货物总量: 453.20 kg
参考答案:
interface ComputeWeight { 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 { private ComputeWeight[] goods; Truck(ComputeWeight[] goods) { this.goods = goods; } public double getTotalWeight() { double sum=0; for(int i=0;i<goods.length;i++) { sum+=goods[i].computeWeight(); } return sum; } }
7-12 八边形Octagan类(接口)
编写一个名为Octagon的类,表示八边形。假设八边形八条边的边长都相等。它的面积可以使用下面的公式计算:
面积 = (2 + 4 / sqrt(2)) * 边长 * 边长
请实现Octagon类,其实现了Comparable
<Octagon>
和Cloneable接口。1 有一个私有变量double side ,表示八边形的边长。
2 构造函数Octagon(double side),初始化side。
3 为side添加getter和setter方法。
4 double getPerimeter()方法,计算周长。
5 double getArea()方法,计算面积。
6 实现Comparable接口的方法 public int compareTo(Octagon o);
如果当前对象的边长比参数o的边长大,返回1,小则返回-1,相等返回0。
7 实现Cloneable接口的方法 protected Object clone()
编写一个测试程序,根据用户输入的浮点数作为边长,创建一个Octagon对象,然后显示它的面积和周长。使用clone方法创建一个新对象,并使用CompareTo方法比较这两个对象。
此题提交时将会附加裁判测试程序到被提交的Java程序末尾。
程序框架:
程序按以下框架进行设计后提交: class Octagon implements Comparable<Octagon>,Cloneable{ …… @Override public int compareTo(Octagon o){ …… } @Override protected Object clone() { return this; } }
裁判测试程序样例:
public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Octagon a1 = new Octagon(input.nextDouble()); System.out.printf("Area is %.2f\n", a1.getArea()); System.out.println("Perimeter is " + a1.getPerimeter()); Octagon a2 = (Octagon) a1.clone(); System.out.println("Compare the methods " + a1.compareTo(a2)); } } /* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
5
输出样例:
在这里给出相应的输出。例如:
Area is 120.71 Perimeter is 40.0 Compare the methods 0
参考答案:
class Octagon implements Comparable<Octagon>,Cloneable{ // …… private double side; public Octagon(double side) { this.setSide(side); } public void setSide(double side) { this.side=side; } public double getSide() { return this.side; } public double getPerimeter() { return this.getSide()*8; } public double getArea() { return (2 + 4 / Math.sqrt(2)) * this.getSide() * this.getSide(); } @Override public int compareTo(Octagon o){ // …… if(this.getSide()>o.getSide()) return 1; else if(this.getSide()<o.getSide()) return -1; else if(this.getSide()==o.getSide()) return 0; return 0; } @Override public Object clone() { return new Octagon(side); } }