java类学习
作者:互联网
package 计算长方形面积和周长; import java.util.Scanner; //长方形类文件 public class Rectangle { private double a,b; //字段:保存长度和宽度 private double Area() //方法:计算面积 { return a*b; } private double Len() //计算周长 { return 2*(a+b); } public void Input() { Scanner sc=new Scanner(System.in); System.out.println("请输入长和宽:"); a=sc.nextDouble(); b=sc.nextDouble(); } public void Output() { System.out.println(Area()+","+Len()); } } package 计算长方形面积和周长; //主类文件 public class RectangleTest { //将主函数main()定义在类中 public static void main(String[] args) { //java需要动态创建对象 Rectangle obj=new Rectangle(); obj.Input();//输入长宽 obj.Output();//显示结果 } } package 鱼塘造价; public class CirclePool { //圆形水池类 public double r; //半径 public double CircleCost() //计算造价 { return 3.14*r*r*10; } } package 鱼塘造价; //长方形养鱼池类 public class RectPool{ //长方形养鱼池类 public double a,b;//长宽 public double RectCost() //计算造价 { return a*b*10; } } //测算养鱼池工程总造价 package 鱼塘造价; import java.util.Scanner; //主类+方法 public class Pool { //主类 public static void main(String[] args) { Scanner sc=new Scanner(System.in); double totalCost=0; //保存总造价的变量 //处理长方形养鱼池 RectPool rObj; //定义引用 rObj=new RectPool(); //创建长方形鱼池对象 System.out.println("请输入长方形鱼池的长和宽:"); rObj.a=sc.nextDouble(); //输入长宽值 rObj.b=sc.nextDouble(); totalCost+=rObj.RectCost();//汇总造价 //处理清水池和污水池 CirclePool cObj1,cObj2; cObj1=new CirclePool(); //创建清水池对象 cObj2=new CirclePool(); //创建污水池对象 System.out.println("请输入清水池和污水池的半径:"); cObj1.r=sc.nextDouble();//输入清水池半径 cObj2.r=sc.nextDouble();//输入污水池半径 totalCost+=cObj1.CircleCost();//汇总造价 totalCost+=cObj1.CircleCost();//汇总造价 //显示总造价 System.out.println(totalCost); } }
标签:java,double,System,学习,造价,sc,new,public 来源: https://www.cnblogs.com/dss-99/p/14016775.html