Java第二次作业

 1 package first;
 2 
 3 import java.util.Scanner;
 4 class persons 
 5 {
 6     private String name;
 7     private char sex;
 8     private int age;
 9     private String id;
10     public persons(String name,char sex,int age,String id)
11     {
12         this.name=name;
13         this.sex=sex;
14         this.age=age;
15         this.id=id;
16     }
17     public void outdata()
18     {
19         System.out.println(name+" "+sex+" "+age+" "+id);        
20     }    
21 }
22 public class studentdata 
23 {
24     public static void main(String[] args) 
25     {
26         Scanner scanner =new Scanner(System.in);
27         System.out.println("请输入您要录入的学生数目:");
28         int stunumber=scanner.nextInt();
29         persons stu[]=new persons[stunumber];
30         System.out.println("请录入学生信息:");
31         for(int i=0;i<stu.length;i++)
32         {
33             System.out.println("请分别输入第"+(i+1)+"位学生的姓名、性别、年龄和身份证号码");
34             String name=scanner.next();
35             String ch=scanner.next();
36             char sex=ch.charAt(0);
37             int age=scanner.nextInt();
38             String id=scanner.next();
39             stu[i]=new persons(name,sex,age,id);
40             System.out.println();
41         }
42         System.out.println();
43         for(int i=0;i<stu.length;i++)
44         {
45             System.out.println("第"+(i+1)+"位学生的姓名、性别、年龄和身份证号码");
46             stu[i].outdata();
47         }
48         scanner.close();
49     }
50     
51 
52 }

 

 1 class Phone
 2  {
 3      private String brand;
 4      private String model;
 5      public Phone(String brand,String model)
 6      {
 7          this.brand=brand;
 8          this.model=model;
 9      }
10      public void OutputInformation()
11      {
12          System.out.println("品牌:"+brand+"     型号:"+model);
13      }
14  }
15  public class TestPhone
16  {
17      public static void main(String args[])
18      {
19          Phone s[]= {
20                          new Phone("华为","荣耀3c"),
21                          new Phone("联想","A3600D"),
22                          new Phone("小米","note"),
23                     };
24          for(int i=0;i<s.length;i++)
25          {
26              s[i].OutputInformation();
27          }
28      }
29
 1 class Book
 2  {
 3      private String bookname;
 4      private String booknum;
 5      private String bookauthor;
 6      private String publishhouse;
 7      private String booktime;
 8      private int bookpage;
 9      private double bookprice;
10      
11      public Book(
12                  String bookname, String booknum,
13                  String bookauthor, String publishhouse,
14                  String booktime, int bookpage,double bookprice
15                  )
16      {
17          super();
18          this.bookname = bookname;
19          this.booknum = booknum;
20          this.bookauthor = bookauthor;
21          this.publishhouse = publishhouse;
22          this.booktime = booktime;
23          this.bookpage = bookpage;
24          this.bookprice = bookprice;
25     }
26  
27      public void OutputInformation()
28      {
29          System.out.println("书名:"+bookname+"   书号:"+booknum);
30          System.out.println("主编:"+bookauthor+"   出版社:"+publishhouse);
31          System.out.println("出版时间:"+booktime+"  页数:"+bookpage);
32          System.out.println("价格:"+bookprice);
33      }
34  }
35  public class TestBook 
36  {
37      public static void main(String args[])
38      {
39          Book b1=new Book(
40                            "Java从入门到精通","05884701",
41                            "赵洛育","清华大学出版社","2016年10月",
42                            564,69.80);
43          Book b2=new Book(
44                             "数据结构","06832301",
45                             "殷人昆","清华大学出版社","2017年5月",
46                             400,49.50);
47          b1.OutputInformation();
48          System.out.println();
49          b2.OutputInformation();
50      }
51  
52  }

 1 class Circle
 2  {
 3      final static double PI=3.14;
 4      private double radius;
 5      public Circle()
 6      {
 7          radius=0;
 8      }
 9      public Circle(double r)
10      {
11          radius=r;
12      }
13      public double getRadius()
14      {
15          return radius;
16      }
17      public double getPerimeter() //计算圆的周长
18     {
19          return radius*2*PI;
20      }
21      public double getArea() //计算圆的面积
22      {
23          return radius*radius*PI;
24      }
25      
26  }
27 class Cylinder extends Circle
28  {
29      double height;
30      public Cylinder(double r,double h)
31      {
32          super(r);
33          height=h;
34      }
35      public double getHeight() {
36          return height;
37      }
38      public double getCylinderArea() //计算圆柱体表面积
39      {
40          return super.getPerimeter()*height+getArea()*2;
41      }
42     double getVol() //计算圆柱体的体积
43      {
44          return super.getArea()*height;
45      }
46      public void dispVol()
47      {
48          System.out.println("半径为:"+getRadius());
49          System.out.println("高为:"+getHeight());
50          System.out.println("底面积为:"+getArea());
51          System.out.println("表面积为:"+getPerimeter());
52          System.out.println("体积为:"+getCylinderArea());
53      }
54      
55  }
56  public class TestCylinder 
57  {
58      public static void main(String[] args)
59      {
60          Cylinder c1=new Cylinder(10,20);
61          Cylinder c2=new Cylinder(10,40);
62          System.out.println("圆柱体c1:");
63          c1.dispVol();
64          System.out.println();
65          System.out.println("圆柱体c2:");
66          c2.dispVol();
67          
68      }
69      
70  }

 

 }

 

posted on 2019-04-08 16:29  52.Hz  阅读(126)  评论(0编辑  收藏  举报