java第4次实训作业

1.编写“电费管理类”及其测试类。

 1 package LHB.inherit;
 2 
 3 public class Electricityfees 
 4 {
 5    private int lastmonth,thismonth;/*上月电表读数,本月电表读数*/
 6    public Electricityfees()
 7    {
 8        
 9    }
10    public Electricityfees(int lastmonth,int thismonth)
11    {
12        this.lastmonth=lastmonth;
13        this.thismonth=thismonth;
14    }
15    public int getLastmonth() 
16     {
17     return lastmonth;
18     }
19 
20    public void setLastmonth(int lastmonth) 
21     {
22     this.lastmonth = lastmonth;
23     }
24 
25   public int getThismonth() 
26    {
27     return thismonth;
28    }
29 
30   public void setThismonth(int thismonth) 
31    {
32     this.thismonth = thismonth;
33    }
34    public void print()
35    {
36        System.out.print("上月电表读数为:"+lastmonth+"  ");
37        System.out.println("这个月电表读数为:"+thismonth+"  ");
38    }
39 }
 1 package LHB.inherit;
 2 
 3 public class TestElectricity 
 4 {
 5 
 6     public static void main(String[] args) 
 7     {    double x;
 8          Electricityfees p=new Electricityfees();
 9          p.setLastmonth(1000);
10          p.setThismonth(1200);
11          x=p.getThismonth()*1.2;
12          System.out.println("本月电费为:"+x);
13          Electricityfees q=new Electricityfees(1200,1450);
14          q.setThismonth(1500);
15          x=q.getThismonth()*1.2;
16          System.out.println("本月电费为:"+x);
17     }
18     
19 }

2. 编写“圆柱体”类及其测试类。

 1 package LHB.inherit;
 2 
 3 public class Cylinder 
 4 {
 5      private int r,high; /*半径,高*/
 6      public Cylinder(int r,int high)
 7      {
 8          this.r=r;
 9          this.high=high;
10      }
11      public double area()
12      {
13          double s;
14          s=3.14*r*r;
15          return s;
16      }
17      public double volume()
18      {
19          double v;
20          v=area()*high;
21          return v;
22      }
23      public void print() 
24      {
25          System.out.println("圆的半径是:"+r+"  "+"高是:"+high);
26          System.out.printf("圆的底面积是:%.2f  体积是:%.2f\n",area(),volume());
27      }
28     public int getR() 
29     {
30         return r;
31     }
32     public void setR(int r) 
33     {
34         this.r = r;
35     }
36     public int getHigh() 
37     {
38         return high;
39     }
40     public void setHigh(int high) 
41     {
42         this.high = high;
43     }
44     public static void main(String[] args) 
45     {
46         
47      
48     }
49 
50 }
 1 package LHB.inherit;
 2 import java.util.*;
 3 public class TestCylinder 
 4 {
 5 
 6     public static void main(String[] args) 
 7     {
 8         int r,high;
 9         Scanner in=new Scanner(System.in);
10         System.out.print("请输入圆柱的半径和高:");
11         r=in.nextInt();
12         high=in.nextInt();
13         Cylinder p=new Cylinder(r,high);
14         Cylinder q=new Cylinder(r,high);
15         p.print();
16         System.out.print("请再次输入圆柱的半径和高:");
17         r=in.nextInt();
18         high=in.nextInt();
19         q.print();
20     }
21 
22 }

3.编写“四则运算类”及其测试类。

 1 package LHB.inherit;
 2 
 3 public class Elementary 
 4 {
 5     private int num1,num2;
 6     private char Char;
 7     public Elementary(int num1,int num2)
 8     {
 9         this.num1=num1;
10         this.num2=num2;
11     }
12     public Elementary(int num1,int num2,char Char)
13     {
14         this.num1=num1;
15         this.num2=num2;
16         this.Char=Char;
17         System.out.print("操作结果是:");
18         switch(Char)
19         {
20         case'+':  System.out.println(add());break;
21         case'-':  System.out.println(minus());break;
22         case'*':  System.out.println(multiply());break;
23         case'/':  System.out.println(divide());break;
24         }
25     }
26     public int add()
27     {    
28         int sum;
29         sum=num1+num2;
30         return sum;
31     }
32     public int minus() 
33     {
34          int minu;
35          minu=num1-num2;
36          return minu;
37     }
38     public int multiply()
39     {
40         int mult;
41         mult=num1*num2;
42         return mult;
43     }
44     public int divide()
45     {
46         int divid;
47         divid=num1/num2;
48         return divid;
49     }
50     public int getNum1() 
51     {
52         return num1;
53     }
54     public void setNum1(int num1) 
55     {
56         this.num1 = num1;
57     }
58     public int getNum2() 
59     {
60         return num2;
61     }
62     public void setNum2(int num2)
63     {
64         this.num2 = num2;
65     }
66     public char getChar() 
67     {
68         return Char;
69     }
70     public void setChar(char c)
71     {
72         Char = c;
73     }
74 }
 1 package LHB.inherit;
 2 import java.util.*;
 3 public class TestElementary 
 4 {
 5    
 6     public static void main(String[] args) 
 7     {    
 8           int num1,num2;
 9           char c;
10           Scanner in=new Scanner(System.in);
11           System.out.print("请输入两个整数和一个操作符:");
12           num1=in.nextInt();
13           num2=in.nextInt();
14           c=in.next().charAt(0);
15           Elementary p=new Elementary(num1,num2,c);
16           System.out.print("请再次输入:");
17           num1=in.nextInt();
18           num2=in.nextInt();
19           c=in.next().charAt(0);
20           Elementary q=new Elementary(num1,num2,c);
21     }
22 
23 }

 

posted @ 2019-04-30 20:14  sky灬刘海波  阅读(235)  评论(0编辑  收藏  举报