Java第四次实践作业

1   

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

package ccc;

public class electricity {)
    private int a;
private int b;
 public electricity() {}
 public electricity(int a,int b) {
 this.a=a;
 this.b=b;
 }
 public int getlast(){
 return a;
 }
 public void setlast(int a){
 if(a<0){
 this.a=0;
 }else{
 this.a=a;
 } }
 public int getnow(){
 return b;
 }
 public void setnow(int b){
 if(b<0){
 this.b=0;
 }else{
 this.b=b;
 }}
 public void print1(){
 System.out.println("本月电费=:"+1.2*b); 
 }
 public void print2(){
 System.out.println("本月电费=:"+1.2*b); 
 }
 public static void main(String[]args){

electricity p1=new electricity(1000,1200); 

p1.print1(); 

electricity p2=new electricity(1200,1450); 

p2.setnow(1500); 

p2.print2(); }}

2

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

package demo;
public class yuanzhu {
 final double PI=3.14; 
   private double r=0f,h=0f;
   double s,v; 
   public Cyl(double r,double h) {
       this.r=r;
       this.h=h;
   }
   public double Cs(double r) {
      
       s=PI*r*r;
       return s;
   }
   public double Cv(double s,double h) {
       s=Cs(r);
       v=s*h;
       return v;
   }
   public void Myprint(){
       System.out.println("圆底半径:"+r+" 高:"+h+" 底面积:"+s+" 体积:"+v);
   }
}
package demo ;
public class Testlyuanzhu {
 public static void main(String [] args) {
     Cyl c1=new Cyl(2,5);
     c1.Cs(2);
     c1.Cv(2, 5);
     c1.Myprint();
     
     Cyl c2=new Cyl(3,9);
     c2.Cs(3);
     c2.Cv(3, 9);
     c2.Myprint();
 }
}

猜你喜欢

转载自www.cnblogs.com/quanzhilong/p/10791455.html