第四次上机作业
package bbb; public class ccc { private int month,lastmonth; public ccc(){ } /*public ccc(int month,int lastmonth){ this.month=month; this.lastmonth=lastmonth; } */ public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getLastmonth() { return lastmonth; } public void setLastmonth(int lastmonth) { this.lastmonth = lastmonth; } public void month(){ System.out.println("今年电表读数为:"+this.month+"\n"+"今年电费为:"+month*1.2); } public void lastmonth(){ System.out.println("去年电表读数为:"+this.lastmonth+"\n"+"去年电费为:"+lastmonth*1.2); } } package bbb; import java.util.Scanner; public class Testccc { public static void main(String[] args) { ccc aa=new ccc(); /*@SuppressWarnings("resource") Scanner sc=new Scanner(System.in); aa.setMonth(sc.nextInt()); aa.setLastmonth(sc.nextInt()); aa.month(); aa.lastmonth();*/ aa.setMonth(1000); aa.setLastmonth(1200); aa.month(); aa.lastmonth(); } }
编写“圆柱体”类及其测试类。
3.1 “圆柱体”类
私有属性:圆底半径、高,
构造方法:带两个参数
方法1:计算底面积
方法2:计算体积
方法3:打印圆底半径、高、底面积和体积。
3.2 测试类
创建2个对象,并调用方法
package bbb; public class circle { private int h, r;; public circle(){ } public int getH(){ return h; } public void setH(int h){ this.h=h; } public int getR(){ return r; } public void setR(int r){ this.r=r; } void S(){ System.out.println("底面积为:"+3.14*r*r); } void V(){ System.out.println("体积为:"+3.14*r*r*h); } } package bbb; public class Testcircle { public static void main(String[] args) { circle aa=new circle(); aa.setH(4); aa.setR(2); aa.S(); aa.V(); } }