zoukankan      html  css  js  c++  java
  • Java程序第二次作业

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

    源程序:

    package zuoye;
    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 zuoye;
    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 zuoye;

    public class phone {
    String brand;
    String model;
    public void a(String brand,String model) {
    this.brand=brand;
    this.model=model;
    System.out.println(brand+","+model);
    }
    }

    测试类:

    package zuoye;

    public class testphone {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    phone b=new phone();
    b.a("华为","荣耀3C");
    b.a("联想","A3600D");
    b.a("小米","note");
    }

    }

    截图:

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

    源程序:

    package zuoye;

    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 zuoye;
    public class Testbook {
    private static book a;
    private static book b;
    public static void main(String[] args) {
    a = new book("汤姆逊漂流记",001,"丹尼尔·笛福","xx出版公司","xxxx年xx月",1000,49.5f);
    b = new book("老人与海",002,"海明威","xx出版社","xxxx年xx月",460,78.0f);
    }

    }

    截图:

     

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

    源程序:

    package zuoye;

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

    测试类:

    package zuoye;

    public class Testyuanzhuti {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    yuanzhuti y1=new yuanzhuti(2f,4f);
    yuanzhuti y2=new yuanzhuti(3f,6f);
    }
    }

    截图:

     心得体会:

    本次作业难度不大,题目的类型也很相似,但十分考验细心与耐心,在做前几题时,由于马虎大意,导致程序频繁出错,差点心态爆炸。不过后来也还是慢慢一步一步找错误,终于把作业搞定。

  • 相关阅读:
    面向对象-类
    模块04
    总结
    昨天的新的解决方法
    感冒了~ vs中py和vb实现一个小算法
    vs2015社区版不支持installshield
    网站被黑了
    2018/11/18(python)
    2018/11/14
    2018/11/12(python)
  • 原文地址:https://www.cnblogs.com/1121yjj/p/10666243.html
Copyright © 2011-2022 走看看