Java程序设计第二次作业

1.编写“人”类及其测试类。
1.1 “人”类:
 类名:Person
 属性:姓名、性别、年龄、身份证号码
 方法:在控制台输出各个信息
1.2 测试类
 类名:TestPerson
 方法:main
 对象:(张三,男,18,430101010101010101)
(李四,女,18,123456789009876543)

源代码:

“人”类:

```

package JAVAzuoye;

public class person {String name;
char sex;
int age;
String number;
public person(String name,char sex,int age,String number) {
this.name=name;
this.sex=sex;
this.age=age;
this.number=number;
}
}

```

测试类:

```

package JAVAzuoye;

public class testperson {
public static void main(String[] args) {
person s1=new person("张三",'男',18,"430101010101010101");
person s2=new person("李四",'女',18,"123456789009876543");
System.out.println("姓名:"+s1.name+",性别:"+s1.sex+",年龄:"+s1.age+",身份证号码:"+s1.number);
System.out.println("姓名:"+s2.name+",性别:"+s2.sex+",年龄:"+s2.age+",身份证号码:"+s2.number);
}
}

```

 

 


2.编写“手机”类及其测试类。
2.1 “手机”类:
 类名:Phone
 属性:手机品牌、手机型号
 方法:在控制台输出手机信息
2.2 测试类
 类名:TestPhone
 方法:main
 对象:(华为,荣耀3C)
(联想,A3600D)
(小米,note)

“手机”类:

```

package JAVAzuoye;

public class phone {
String brand;
String model;
public phone(String brand,String model) {
this.brand=brand;
this.model=model;
}
}

```

测试类

```

package JAVAzuoye;

public class testphone {
public static void main(String[] args) {
phone s1=new phone("华为","荣耀3C");
phone s2=new phone("联想","A3600D");
phone s3=new phone("小米","note");
System.out.println("品牌:"+s1.brand+",型号:"+s1.model);
System.out.println("品牌:"+s2.brand+",型号:"+s2.model);
System.out.println("品牌:"+s3.brand+",型号:"+s3.model);
}
}

```

 

 


3.编写“书籍”类及其测试类。
3.1 “书籍”类
 类名:Book
 属性:书名、书号、主编、出版社、出版时间、页数、价格
 方法:在控制台输出每本书的信息
3.2 测试类
 创建2个对象,并调用方法

“书籍”类

```

package JAVAzuoye;

public class Book {
String name;
int number;
String editor;
String publish;
String time;
int page;
float price;
public Book(String name,int number,String editor,String publish,String time,int page,float price){
this.name=name;
this.number=number;
this.editor=editor;
this.publish=publish;
this.time=time;
this.page=page;
this.price=price;
System.out.println(name+","+number+","+editor+","+publish+","+time+","+page+","+price);
}
}

````

测试类

````

package JAVAzuoye;

public class tesebook {
public static void main(String[] args) {
Book s1=new Book("挪威的森林",1,"村上春树","上海译文出版社","2007年7月",376,33.0f);
Book s2=new Book("解忧杂货店",2,"东野圭吾","南海出版公司","2012年03月",291,39.5f);
}
}

````

 


4.编写“圆柱体”类及其测试类。
4.1 “圆柱体”类
 属性:圆底半径、高,
 方法1:计算底面积
 方法2:计算体积
 方法3:打印圆底半径、高、底面积和体积。
4.2 测试类
 创建2个对象,并调用方法

“圆柱体”类:

```

package JAVAzuoye;

public class cylinder {
final float PI=3.14f;
float r;
float h;
public cylinder(float r,float h) {
this.r=r;
this.h=h;
System.out.println("圆底半径="+r+",高="+h+",底面积="+PI*r*r+",体积="+PI*r*r*h);
}
}

```

测试类

````

package JAVAzuoye;

public class testcylinder {
public static void main(String[] args) {
cylinder s1=new cylinder(3.2f,3.1f);
cylinder s2=new cylinder(2.5f,3.8f);
}
}

````

 

 

posted on 2019-04-07 22:24  孙泽玺  阅读(237)  评论(0编辑  收藏  举报

导航