7:
定义一个名为Vehicles(交通工具)的基类,该类中应包含String类型的成员属性brand(商标)和color(颜色),还应包含成员方法run(行驶,在控制台显示“我已经开动了”)和showInfo(显示信息,在控制台显示商标和颜色),并编写构造方法初始化其成员属性。
编写Car(小汽车)类继承于Vehicles类,增加int型成员属性seats(座位),还应增加成员方法showCar(在控制台显示小汽车的信息),并编写构造方法。
编写Truck(卡车)类继承于Vehicles类,增加float型成员属性load(载重),还应增加成员方法showTruck(在控制台显示卡车的信息),并编写构造方法。
在main方法中测试以上各类。
class Vehicles{
String brand;
String color;
Vehicles(String brand,String color){
this.brand=brand;
this.color=color;
}
void showInfo(){
System.out.println("My brand: "+brand);
System.out.println("My color: "+color);
}
void run(){
System.out.println("I am moving on");
System.out.println();
System.out.println();
}
}
class Car extends Vehicles{
int seats;
Car(String brand,String color, int seats){
super(brand,color);
this.seats=seats;
System.out.println("I am a car");
}
void showCar(){
super.showInfo();
System.out.println("i have seats: "+seats);
System.out.println();
System.out.println();
}
}
class Truck extends Vehicles{
float load;
Truck(String brand,String color, float load){
super(brand,color);
this.load=load;
System.out.println("i am a truck");
}
void showTruck(){
super.showInfo();
System.out.println("i can take: "+load);
}
public static void main(String args[]){
Vehicles v = new Vehicles("beng chi","red");
v.showInfo();
v.run();
Car c = new Car("da zhong","Blue",5);
c.showCar();
Truck t = new Truck("wu ling","black",30);
t.showTruck();
}
}
8
定义一个网络用户类,要处理的信息有用户ID、用户密码、email地址。在建立类的实例时,把以上三个信息都作为构造函数的参数输入,其中用户ID和用户密码时必须的,缺省的email地址是用户ID加上字符串"@gameschool.com"
class PersonNet{
String id;
String password;
String address;
PersonNet(String id,String password){
this.id=id;
this.password=password;
this.address=id+"@gameschool.com";
}
public void check(){
if(id.length()<=0)
System.out.println("Please enter your id");
if(password.length()<=0)
System.out.println("Please enter your password");
}
public static void main(String args[]){
PersonNet p1 = new PersonNet("12346","zxcvbn");
p1.check();
System.out.println("id: "+p1.id);
System.out.println("password: "+p1.password);
System.out.println("email address: "+p1.address);
}
}
9. 编写Addition类,该类中应包含一组实现两数相加运算的重载方法。
实现加法运算的方法,应接受两个参数(即加数和被加数),方法将两个参数进行加法运算后,返回相加结果。考虑可能针对不同的数据类型进行计算,重载一组方法,包括整型、长整型、浮点型、双精度浮点型、还有字符串。
在main方法中创建Addition类的实例,分别调用重载方法测试其效果。 应将Addition类打入到包中,以自己名字的拼音为包命名。
public class Addition{
double a;
double b;
int i;
long l;
float f;
String s;
double sum=0;
Addition(double a,double b){
this.a=a;
this.b=b;
sum = a+b;
}
Addition(double a, double b,int i,long l,float f){
this(a,b);
sum = this.sum+i+l+f;
}
public static void main(String args[]){
Addition n = new Addition(10,20,30,40,50);
System.out.println(n.sum);
}
}
11、 创建一个类,为该类定义三个构造函数,分别执行下列操作:
1、传递两个整数值并找出其中较大的一个值
2、传递三个double值并求出其乘积
3、传递两个字符串值并检查其是否相同
4、在main方法中测试构造函数的调用
public class Comp{
int a;
int b;
double d1;
double d2;
double d3;
String s1;
String s2;
Comp(int a,int b){
this.a=a;
this.b=b;
System.out.println("a: "+a);
System.out.println("b: "+b);
if(a>b)
System.out.println("the maximum value is: " +a);
else
System.out.println("The maximum value is: "+b);
}
Comp(double d1,double d2,double d3){
this.d1=d1;
this.d2=d2;
this.d3=d3;
System.out.println("d1: "+d1);
System.out.println("d2: "+d2);
System.out.println("d3: "+d3);
System.out.println("The product of d1 and d2 and d3 is: "+d1*d2*d3);
}
Comp(String s1,String s2){
this.s1 = s1;
this.s2 = s2;
System.out.println("s1: "+s1);
System.out.println("s2: "+s2);
if(s1.compareTo(s2)==0)
System.out.println("s1 equals to s2");
else
System.out.println("s1 doesn't equal to s2");
}
public void newLine(){
System.out.println();
System.out.println();
}
public static void main(String args[]){
Comp c1 = new Comp(10,20);
c1.newLine();
Comp c2 = new Comp(10.00,20.00,30.00);
c2.newLine();
Comp c3 = new Comp("abcd","abcd");
c3.newLine();
Comp c4 = new Comp("abcd","ABCD");
}
}