第二次java作业
1.编写“人”类及其测试类。
1.1 “人”类:
类名:Person
属性:姓名、性别、年龄、身份证号码
方法:在控制台输出各个信息
1.2 测试类
类名:TestPerson
方法:main
对象:(张三,男,18,430101010101010101)
(李四,女,18,123456789009876543)
public class Person {
String name;
String sex;
int age;
String ID;
public static void main(String[] args) {
Person m=new Person();
Person w=new Person();
System.out.println("姓命:"+m.name+",性别:"+m.sex+",年龄:"+m.age+",身份证:"+m.ID);
System.out.println();
System.out.println("姓名:"+w.name+",性别:"+w.sex+",年龄:"+w.age+",身份证:"+w.ID);
}
}

2.编写“手机”类及其测试类。
2.1 “手机”类:
类名:Phone
属性:手机品牌、手机型号
方法:在控制台输出手机信息
2.2 测试类
类名:TestPhone
方法:main
对象:(华为,荣耀3C)
(联想,A3600D)
(小米,note)
public class Phone {
String name;
String model;
public static void main(String[] args) {
Phone a=new Phone();
System.out.println("品牌:"+a.name+",型号:"+a.model);
}
}

3.编写“书籍”类及其测试类。
3.1 “书籍”类
类名:Book
属性:书名、书号、主编、出版社、出版时间、页数、价格
方法:在控制台输出每本书的信息
3.2 测试类
创建2个对象,并调用方法
public class Book {
String name;
String number;
String chiefeditor;
String publisher;
String publishtime;
int pagenumber;
int price;
public static void main(String[] args) {
Book a=new Book();
System.out.println("书名:"+a.name+",书号:"+a.number+",主编:"+a.chiefeditor+",出版社:"+
a.publisher+",出版时间;"+a.publishtime+",页数:"+a.pagenumber+",价格:"+a.price);
}
}

4.编写“圆柱体”类及其测试类。
4.1 “圆柱体”类
属性:圆底半径、高,
方法1:计算底面积
方法2:计算体积
方法3:打印圆底半径、高、底面积和体积。
4.2 测试类
创建2个对象,并调用方法
import java.util.*;
public class Cylinder {
float r;
float h;
float a=3.14f;
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
Cylinder c1=new Cylinder();
Cylinder c2=new Cylinder();
System.out.println("请输入第一个圆的半径和高:");
c1.r=reader.nextFloat();
c1.h=reader.nextFloat();
System.out.println("请输入第二个圆的半径和高:");
c2.r=reader.nextFloat();
c2.h=reader.nextFloat();
System.out.println("c1底面积:"+(c1.a*c1.r*c1.r)+",c1体积:"+(c1.a*c1.r*c1.r*c1.h));
System.out.println("半径:"+c1.r+",高:"+c1.h+",c1底面积:"+(c1.a*c1.r*c1.r)+",c1体积:"+(c1.a*c1.r*c1.r*c1.h));
System.out.println("c2底面积:"+(c2.a*c2.r*c2.r)+",c2体积:"+(c2.a*c2.r*c2.r*c2.h));
System.out.println("半径:"+c2.r+",高:"+c2.h+",c2底面积:"+(c2.a*c2.r*c2.r)+",c2体积:"+(c2.a*c2.r*c2.r*c2.h));
}
}

感想:Java还是很麻烦的,我还有很多地方不熟息,像是数据从键盘输入。相对于其他三个来说,最后一个程序是我个人认为最麻烦的。
但通过这次程序的练习,我对Java的了解也更深了,也不至于无从下手了。
浙公网安备 33010602011771号