• 第四次实训作业


    1.编写“电费管理类”及其测试类。
    第一步 编写“电费管理”类
      1)私有属性:上月电表读数、本月电表读数
      2)构造方法:无参、2个参数
      3)成员方法:getXXX()方法、setXXX()方法
      4)成员方法:显示上月、本月电表读数
    第二步 编写测试类
      1)创建对象一:上月电表读数为1000,本月电表读数为1200。
        要求:调用无参构造方法创建对象;
        调用setXXX()方法初始化对象;
        假设每度电的价格为1.2元,计算并显示本月电费。
      2)创建对象二:上月电表读数1200,本月电表读数为1450。
        要求:调用2个参数的构造方法创建并初始化对象;
        调用setXXX()方法修改本月电表读数为1500(模拟读错了需修改);
        假设每度电的价格为1.2元,计算并显示本月电费。

     1 package JavaPractice;
     2 class ElectricCharge
     3 {
     4     private double lastnum,thisnum;
     5     public ElectricCharge() 
     6     {
     7         
     8     }
     9     public ElectricCharge(double lastnum, double thisnum)
    10     {
    11         this.lastnum = lastnum;
    12         this.thisnum = thisnum;
    13     }
    14     public double getLastnum() 
    15     {
    16         return lastnum;
    17     }
    18     public void setLastnum(double lastnum)
    19     {
    20         this.lastnum = lastnum;
    21     }
    22     public double getThisnum()
    23     {
    24         return thisnum;
    25     }
    26     public void setThisnum(double thisnum)
    27     {
    28         this.thisnum = thisnum;
    29     }
    30     public void PrintMessage()
    31     {
    32         System.out.println("上月用电为:"+lastnum+"    本月用电为:"+thisnum);
    33     }
    34 
    35 }
    36 public class TestEC
    37 {
    38     public static void main(String[] args) 
    39     {
    40         ElectricCharge ec1=new ElectricCharge();
    41         ec1.setLastnum(1000);
    42         ec1.setThisnum(1200);
    43         System.out.println("用户一家的用电情况为:");
    44         ec1.PrintMessage();
    45         System.out.println("本月电费为:"+ec1.getThisnum()*1.2);
    46         System.out.println();
    47         
    48         ElectricCharge ec2=new ElectricCharge(1200,1450);
    49         ec2.setThisnum(1500);
    50         System.out.println("用户二家的用电情况为:");
    51         ec2.PrintMessage();
    52         System.out.println("本月电费为:"+ec2.getThisnum()*1.2);
    53         
    54         
    55     }
    56 }

    2、 编写“圆柱体”类及其测试类。
      2.1 “圆柱体”类
        私有属性:圆底半径、高,
        构造方法:带两个参数
        方法1:计算底面积
        方法2:计算体积
        方法3:打印圆底半径、高、底面积和体积。
      2.2 测试类
         创建2个对象,并调用方法

    1 package JavaPractice;
     2 import java.lang.Math;
     3 import java.util.Scanner;
     4 class Cylinder
     5 {
     6     private double r; //定义半径
     7     private double h; //定义高
     8     public void setR(double r) 
     9     {
    10         this.r = r;
    11     }
    12     public void setH(double h)
    13     {
    14         this.h = h;
    15     }
    16     public Cylinder(double r, double h) 
    17     {
    18         this.r = r;
    19         this.h = h;
    20     }
    21     public double getBasalArea()
    22     {
    23         return r*r*Math.PI;
    24     }
    25     public double getVolume()
    26     {
    27         return getBasalArea()*h;
    28     }
    29     public void printInformation()
    30     {
    31         System.out.println("半径:"+r+"   高:"+h);
    32         System.out.printf("底面积为:%.2f
    ",getBasalArea());
    33         System.out.printf("体积为:%.2f
    
    ",getVolume());
    34                             
    35     }
    36 }
    37 public class TestCylinder
    38 {
    39     public static void main(String[] args)
    40     {
    41         Scanner sc=new Scanner(System.in);
    42         Cylinder S[]=new Cylinder[2];
    43         for(int i=0;i<S.length;i++)
    44         {
    45             System.out.printf("请输入圆柱%c的半径,高
    ",i+'A');
    46             S[i]=new Cylinder(sc.nextDouble(), sc.nextDouble()); 
    47         }
    48         System.out.println();
    49         for(int i=0;i<S.length;i++)
    50         {
    51             System.out.printf("圆柱%c的信息如下:
    ",i+'A');
    52             S[i].printInformation();
    53         }
    54     }
    55 }

  • 相关阅读:
    最优二叉树(简易版本)
    平衡二叉树(AVL树)基础操作
    二叉树的基础操作
    双向链表(C语言)
    循环链表(C语言)
    单向链表(C语言)
    jQuery
    js事件
    JDBC-扩展
    JDBC
  • 原文地址:https://www.cnblogs.com/pamper/p/10996951.html
Copyright © 2020-2023  润新知