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

package second;

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

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


    }

}

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

package second;

public class phone {
    String brand;
    String type;
    public phone(String brand,String type) {
        this.brand = brand;
        this.type = type;
         System.out.print("手机品牌:"+brand+"\t手机型号"+type+"\n");
    }
}
package second;

public class Testphone {
    public static void main(String[] args) {
        phone phone1 = new phone("Huawei","honor3C");
        phone phone2 = new phone("Lenovo","A3600D");
        phone phone3 = new phone("Redmi","note");
    }
}

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

package second;

public class book {
    String title;
    int num;
    String editor;
    String press;
    int time;
    int pages;
    double price;
        public book(String name,int number,String editor,String press,int time,int page,double price){
            this.title=name;
            this.num=number;
            this.editor=editor;
            this.press=press;
            this.time= time;
            this.pages=page;
            this.price=price;
        System.out.print("书名:\t"+title+"\t书号:"+num+"\t主编:"+editor+"\t出版社:"+press+"\t出版时间:"+time+"\t页数:"+pages+"\t价格:"+price+"\n");
}
    }
package second;
public class Testbook {
    private static book Testbook1;
    private static book Testbook2;
    public static void main(String[] args) {
        Testbook1 = new book("红楼梦",1,"曹雪芹 / 高鹗 ", "人民出版社", 1957,860,88);    
        Testbook2= new book("解忧杂货店",2,"东野圭吾 ","南海出版公司",2014,291,39.50);
    }
}

 

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

package second;

public class circle {
    final double PI=3.14;
    int r;
    int h;
    public circle(int r, int h) {
        this.r = r;
        this.h = h;
        System.out.println("圆底半径="+r+",高="+h+"圆的底面积="+PI*r*r+"圆的体积:"+PI*r*r*h);
    }
}
package second;

public class Testcircle {
    public static void main(String[] args) {
        circle Testcircle1 = new circle(2, 3);
        circle Testcircle2= new circle(2, 6);
    }
}

 

小结:由于程序练习得比较少,在完成第二次作业的过程中遇到的困难比较多。一开始并没有思路,不过之后一点点的摸索让我有了一些思路,并且加深了对题目的理解,在开始程序中,由于粗心漏掉了一些语句,使我的程序不能成功运行,并且在我查找错误的过程中比较困难。第二次作业花费的时间比较多,不过也得到了一些收获。多多的练习实践才是最有效的方法。