1.编写“人”类及其测试类。
1.1 “人”类:
package extend;
public class A_Preson {
private String name;
private char sex;
private int age;
private String id;
public String getName(){
return name;
}
public char getSex(){
return sex;
}
public int getAge(){
return age;
}
public String getId(){
return id;
}
public A_Preson(String name,char sex,int age,String id){
System.out.println("姓名为:"+name+"\t性别为:"+sex+"\t\t年龄为:"+age+"\t身份证号码为:"+id);
}
}
1.2 测试类:
package extend;
public class A_TestPreson {
public static void main(String[] args) {
new A_Preson("张三",'男',18,"430101010101010101");
new A_Preson("李四",'女',18,"123456789009876543");
}
}
2.编写“手机”类及其测试类。
2.1 “手机”类:
package extend;
import java.util.Scanner;
public class B_Phone {
Scanner input = new Scanner(System.in);
private String brand;
private String model;
public String getBrand(){
brand = input.nextLine();
return brand;
}
public void setBrand(String brand){
this.brand = brand;
}
public String getModel(){
model = input.nextLine();
return model;
}
public void setModel(String model){
this.model = model;
}
public B_Phone(){
}
public B_Phone(String brand,String model){
System.out.println("手机的品牌:"+brand+"\t该手机型号为:"+model);
}
}
2.2 测试类:
package extend;
public class B_TestPhone {
public static void main(String[] args) {
B_Phone a = new B_Phone();
System.out.println("手机的品牌:"+a.getBrand()+"\t该手机型号为:"+a.getModel());
B_Phone b = new B_Phone();
System.out.println("手机的品牌:"+b.getBrand()+"\t该手机型号为:"+b.getModel());
B_Phone c = new B_Phone();
System.out.println("手机的品牌:"+c.getBrand()+"\t该手机型号为:"+c.getModel());
}
}
3.编写“书籍”类及其测试类。
3.1 “书籍”类:
package extend;
import java.util.Scanner;
public class C_Book {
Scanner input = new Scanner(System.in);
private String title;
private String number;
private String editor;
private String press;
private String time;
private int pages;
private double price;
public C_Book(String title,String number,String editor,String press,String time,int pages,double price){
this.title = title;
this.number = number;
this.editor = editor;
this.press = press;
this.time = time;
this.pages = pages;
this.price = price;
}
public String getTitle(){
title = input.nextLine();
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getNumber(){
number = input.nextLine();
return number;
}
public void setNumber(String number){
this.number = number;
}
public String getEditor(){
editor = input.nextLine();
return editor;
}
public void setEditor(String editor){
this.editor = editor;
}
public String getPress(){
press = input.nextLine();
return press;
}
public void setPress(String press){
this.press = press;
}
public String getTime(){
time = input.nextLine();
return time;
}
public void setTime(String time){
this.time = time;
}
public int getPages(){
pages = input.nextInt();
return pages;
}
public void setPages(int pages){
this.pages = pages;
}
public double getPrice(){
price = input.nextDouble();
return price;
}
public void setPrice(double price){
this.price = price;
}
public C_Book(){
}
}
3.2 测试类:
package extend;
public class C_TestBook {
public static void main(String[] args) {
// TODO 自动生成的方法存根
C_Book x = new C_Book();
C_Book y = new C_Book();
try{
System.out.println("该书叫《"+x.getTitle()+"》,书号为'"+x.getNumber()+"',主编是"+x.getEditor()+",由"+x.getPress()+"出版,\n出版时间为:"+x.getTime()+" ,总页数为"+x.getPages()+",价格为"+x.getPrice()+"元");
System.out.println("该书叫《"+y.getTitle()+"》,书号为'"+y.getNumber()+"',主编是"+y.getEditor()+",由"+y.getPress()+"出版,\n出版时间为:"+y.getTime()+" ,总页数为"+y.getPages()+",价格为"+y.getPrice()+"元");
}
catch(Exception e){
e.printStackTrace();
System.out.print("此处输入错误!请再次输入");
}
}
}
4.编写“圆柱体”类及其测试类。
4.1 “圆柱体”类:
package extend;
public class D_Cylinder {
private double radius;
private double tall;
final double PI = 3.14;
private double S;
private double V;
public double getRadius(){
return radius;
}
public void setRadius(double radius){
this.radius = radius;
}
public double getTall(){
return tall;
}
public void setTall(double tall){
this.tall = tall;
}
public double Method1(double radius){
S = PI*radius*radius;
return S;
}
public double Method2(double tall){
V = S*tall;
return V;
}
public void Method3(double radius,double tall){
S = Method1(radius);
V = Method2(tall);
System.out.println("圆底半径为:"+radius+"\t圆柱高为:"+tall+"\n圆柱体的表面积为:"+S+"\t圆柱体的体积为:"+V);
}
}
4.2 测试类:
package extend;
import java.util.Scanner;
public class D_TestCylinder {
public static void main(String[] args) {
D_Cylinder x = new D_Cylinder();
x.Method3(5.34,61.25);
@SuppressWarnings("resource")
Scanner a = new Scanner(System.in);
double b = a.nextDouble();
double c = a.nextDouble();
new D_Cylinder().Method1(b);
new D_Cylinder().Method2(c);
new D_Cylinder().Method3(b, c);
}
}
本次作业小结:
首先要感谢老师的教导啦,没老师的指导,我肯定不知道怎么编出这些程序。其次本次作业有些地方没有完美的顾及到引用和get~set的真实意义,不过把错误留在这里吧,这样以后回顾的时候就知道错在哪里。另外在随着题目的完成量,能感觉到程序打出的熟练度越发增加。最后,希望在今后的程序中能越做好,越让自己满意。