第二次java作业

1.编写“人”类及其测试类。属性:姓名、性别、年龄、身份证号码。

 1 package FengZhuang;
 2 import java.util.Scanner;
 3 class Person 
 4 {
 5      String name;
 6      char sex;
 7      int age;
 8      String IdCard;
 9     public void printf()
10     {
11         System.out.print("姓名是:"+name+"   ");
12         System.out.print("性别是:"+sex+"   ");
13         System.out.print("年龄是:"+age+"   ");
14         System.out.println("身份证号码是:"+IdCard);
15     }
16     public Person(String name,char sex,int age,String IdCard)
17     {
18         this.name=name;
19         this.sex=sex;
20         this.age=age;
21         this.IdCard=IdCard;
22         
23     }
24 }
25    public class TestPerson
26    {
27       public static void main(String[] args) 
28         {
29             Scanner in= new Scanner(System.in);
30           String name;
31           char sex;
32           int age;
33           String IdCard; 
34           System.out.println("请输入姓名,性别,年龄,身份证号码:");
35           name=in.next();
36           sex=in.next().charAt(0);
37           age=in.nextInt();
38           IdCard=in.next();
39           Person object=new Person(name,sex,age,IdCard);
40           object.printf();
41         }
42 }

2.编写“手机”类及其测试类。属性:手机品牌、手机型号。

 1 package FengZhuang;
 2 import java.util.Scanner;
 3  class Phone
 4  {
 5      String Phonebrand;  /*手机品牌*/
 6      String Phonemodel;  /*手机型号*/
 7    public void printf()
 8    {
 9       System.out.print("该手机的品牌是:"+Phonebrand+"  ");
10       System.out.println("手机型号是:"+Phonemodel);
11    }
12    
13      public Phone(String Phonebrand,String Phonemodel) 
14      {
15          this.Phonebrand=Phonebrand;
16          this.Phonemodel=Phonemodel;
17      }
18  }
19 public class TestPhone {
20 
21     public static void main(String[] args)
22     {   
23         Scanner in=new Scanner(System.in);
24           String Phonebrand,Phonemodel;
25           System.out.println("请输入手机品牌和型号:");
26           Phonebrand=in.next();
27           Phonemodel=in.next();
28          Phone object= new Phone(Phonebrand,Phonemodel);
29          object.printf();
30     }
31 
32 }

3.编写“书籍”类及其测试类。属性:书名、书号、主编、出版社、出版时间、页数、价格。

 1 package FengZhuang;
 2 import java.util.Scanner;
 3 class Book
 4 {
 5     String BookTitle, BookNumber;/*书名,书号*/
 6     String Compiler,Press;       /*主编,出版社*/
 7     String PublishTime;          /*出版时间*/
 8     int Page,Price;              /*页数,价格*/
 9     void printf()
10     {
11        System.out.print("书名是:"+BookTitle+"  "+"书号是:"+BookNumber+"  ");
12        System.out.print("主编是:"+Compiler+"  "+"出版社是:"+Press+"   ");
13        System.out.print("出版时间是:"+PublishTime+"  ");
14        System.out.println("页数是:"+Page+"  "+"价格是:"+Price); 
15     }
16     public Book(String BookTitle,String BookNumber,String Compiler,String Press,String PublishTime,int Page,int Price)
17     {
18         this.BookTitle=BookTitle;
19         this.BookNumber=BookNumber;
20         this.Compiler=Compiler;
21         this.Press=Press;
22         this.PublishTime=PublishTime;
23         this.Page=Page;
24         this.Price=Price;
25     }   
26 }
27 public class TestBook {
28 
29     public static void main(String[] args) 
30     {
31         Scanner in=new Scanner(System.in);
32         String BookTitle,BookNumber;
33         String Compiler,Press;
34         String PublishTime;
35         int Page,Price;
36         System.out.println("请输入书名、书号、主编、出版社、出版时间、页数、价格");
37         BookTitle=in.next();
38         BookNumber=in.next();
39         Compiler=in.next();
40         Press=in.next();
41         PublishTime=in.next();
42         Page=in.nextInt();
43         Price=in.nextInt();
44         Book test1=new Book(BookTitle,BookNumber,Compiler,Press,PublishTime,Page,Price);
45         Book test2=new Book("C语言从入门到放弃","1976654","谭浩强","清华大学出版社","2010年6月",390,33);
46         test1.printf();
47         test2.printf();
48     }
49 
50 }

4.编写“圆柱体”类及其测试类。属性:圆底半径、高。

方法1:计算底面积

方法2:计算体积

方法3:打印圆底半径、高、底面积和体积

 1 package FengZhuang;
 2 class Cylinder
 3 {
 4     double high,Radius;/*高,半径*/
 5     
 6     public Cylinder(double high,double Radius)
 7     {
 8         this.high=high;
 9         this.Radius=Radius;
10     }
11     public double Bottomarea()  /*底面积方法*/
12     {
13         double s;
14         s=2*3.14*Radius*Radius;
15         return s;
16     }
17     public double  volume()/*体积方法*/
18     {
19           double v;
20           v=Bottomarea()*high;
21           return v;
22     }
23     public void print()
24     {
25         System.out.print("圆柱的半径是:"+Radius+"  "+"高是:"+high+"  ");
26         System.out.println("底面积是:"+Bottomarea()+"  "+"体积是:"+volume());
27     
28     }
29 }
30 public class TestCylinder {
31 
32     public static void main(String[] args) {
33         
34         Cylinder test1=new Cylinder(6,6);
35         Cylinder test2=new Cylinder(9,9);
36         test1.print();
37         test2.print();
38         
39     }
40 
41 }

 

posted @ 2019-04-06 21:41  sky灬刘海波  阅读(253)  评论(2编辑  收藏  举报