题目 1:有个接口Camera,包含一个方法void takePicture()。现在有类CellPhone和IWatch如下,Travel类中的snap方法,需要一个参数Carema,现在请改造Cellphone和IWatch类,满足测试样例的输出要求
思路:如果一个类要使用接口,必须重构接口中的方法。接口 interface lei,调用 implemengts lei,不要忘记重构方法。
样例测试:
答案代码:
interface Camera{
void takePicture();
}
class CellPhone implements Camera{
String name;
public CellPhone(String name){
this.name=name;
}
public void takePicture()
{
System.out.println("picture by "+name+" cellphone");
}
}
class IWatch implements Camera{
String name;
public IWatch(String name){
this.name=name;
}
public void takePicture()
{
System.out.println("picture by "+name+" iwatch");
}
}
class Travel{
public void snap(Camera c){
c.takePicture();
}
}
题目 2:观察以下代码,在注释部分添加代码。要求在Student类中,(task1)添加一个有name和ID两个参数的构造方法,对成员变量name和ID进行初始化,(task2)实例化一Student对象,学生姓名:Yaoming,ID:123456,通过两次调用addScore,给总分分别加上90和95,利用getTotalScore()输出名字+总分。注:使用System.out.println()方法输出。
思路:对成员变量进行初始化,只需要public lei(String name){this.name=name}.实例初始化 lei a =new lei("yaoming"),然后调用公用的方法
样例测试:
答案代码:
public class Test {
public static void main(String[] args) {
//task2:write code here
Student student =new Student("Yaoming",123456);
student.addScore(90);
student.addScore(95);
System.out.println(student.getTotalScore());
}
}
class Student{
private String name;
private int ID;
private int total;
//task1:write code here
public Student(String name,int ID)
{
this.name=name;
this.ID=ID;
}
public void addScore(int score){
total+=score;
}
public String getTotalScore(){
return name+":"+String.valueOf(total);
}
}
题目 3:设计一个矩形类Rectangle,包含私有的数据成员宽度(Width)和高度(Height);公有的方法double getArea()返回矩形的面积,公有的方法double getPerimeter()返回矩形的周长。
思路:设置私有数据成员private int ID ,设计公有方法,返回相应的数值
样例测试:
答案代码:
class Rectangle
{
private double Width;
private double Height;
public Rectangle(double Width,double Height)
{
this.Width =Width;
this.Height=Height;
}
public double getArea()
{
return Width*Height;
}
public double getPerimeter()
{
return 2*(Width+Height);
}
}
题目 4:设计一个名为Person的类和它的两个名为Student和Employee的子类。Person类有私有成员变量:姓名、地址、电话号码和邮箱。Student类有私有成员变量:年级,Employee类有私有成员变量:部门和工资。要求各个类都定义带参数的构造方法,对私有数据成员进行初始化。各个类中定义公有的show()方法,输出该类对象的成员变量值。说明:成员变量“工资”为 int 类型
思路:继承父类 class a extends b ,继承父类的变量,super(String name),调用父类的方法,super.show();
样例测试:
答案代码:
class Person
{
private String name;
private String add;
private String tele;
private String ema;
public Person(String name,String add,String tele,String ema)
{
this.name=name;
this.add=add;
this.tele=tele;
this.ema=ema;
}
public void show()
{
System.out.print(name+","+add+","+tele+","+ema);
}
}
class Student extends Person
{
private String nian;
public Student(String name,String add,String tele,String ema,String nian)
{
super(name,add,tele,ema);
this.nian=nian;
}
public void show()
{
super.show();
System.out.println(","+nian);
}
}
class Employee extends Person
{
private String bu;
private int gong;
public Employee(String name,String add,String tele,String ema,String bu,int gong)
{
super(name,add,tele,ema);
this.bu=bu;
this.gong=gong;
}
public void show()
{
super.show();
System.out.println(","+bu+","+gong);
}
}
题目 5:
思路:调用父类的方法;
答案代码:
import java.util.Calendar;
class Manager extends People
{
int y=0;
int getBonus(int x)
{
y =super.getAge();
return (y-1)*x;
}
}
class Employee extends People
{
int y;
int getWorkday()
{
y =super.getAge();
return 365-115-y+1;
}
}
class People{
public String name;
public String birthday;
public void setBirthday(String birthday){
this.birthday=birthday;
}
public int getAge(){
String y=birthday.substring(0,4);
Calendar c=Calendar.getInstance();
return c.get(Calendar.YEAR)-Integer.parseInt(y);
}
}