第二次作业

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");
 
    }

}

结果截图:
1631749-20190608203647635-273165573.png

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());
        
    }

}

结果截图:
1631749-20190608204044029-12649287.png

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("此处输入错误!请再次输入");
        }
    }

}

结果截图:
1631749-20190608205113469-985688182.png

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);
       
    }
}

结果截图:
1631749-20190608210211974-790555292.png

本次作业小结:

首先要感谢老师的教导啦,没老师的指导,我肯定不知道怎么编出这些程序。其次本次作业有些地方没有完美的顾及到引用和get~set的真实意义,不过把错误留在这里吧,这样以后回顾的时候就知道错在哪里。另外在随着题目的完成量,能感觉到程序打出的熟练度越发增加。最后,希望在今后的程序中能越做好,越让自己满意。

转载于:https://www.cnblogs.com/bei852964632/p/10991594.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值