KiKi理解了继承可以让代码重用,他现在定义一个基类shape,私有数据为坐标点x,y, 由它派生Rectangle类和Circle类,它们都有成员函数GetArea()求面积。派生类Rectangle类有数据:矩形的长和宽;派生类Circle类有数据:圆的半径。Rectangle类又派生正方形Square类,定义各类并测试。输入三组数据,分别是矩形的长和宽、圆的半径、正方形的边长,输出三组数据,分别是矩形、圆、正方形的面积。圆周率按3.14计算。
输入三行,第一行为矩形的长和宽,第二行为圆的半径,第三行为正方形的边长。
三行,分别是矩形、圆、正方形的面积。
7 8 10 5
56 314 25
import java.text.DecimalFormat; import java.util.Scanner; class shape { private int x; private int y; public shape() {} public shape(int x, int y) { this.x = x; this.y = y; } public shape (int x) { this.x = x; } public int getX() { return this.x; } public int getY() { return this.y; } public double GetArea() { return 0.0; } public void printArea() { double area = this.GetArea(); //格式化不足补零 DecimalFormat decimalFormat = new DecimalFormat("#.##"); String result = decimalFormat.format(area); if (result.endsWith(".00")) {//整数取整 System.out.println((int) area); } else { System.out.println(result); } } } class Rectangle extends shape { public Rectangle(int x, int y) { super(x, y); } public double GetArea() { return super.getX() * super.getY(); } } class Circle extends shape { public Circle (int x) { super(x); } public double GetArea() { return super.getX() * super.getX() * 3.14; } } class Square extends shape { public Square (int x) { super(x); } public double GetArea() { return super.getX() * super.getX(); } } // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 Rectangle s1 = new Rectangle(in.nextInt(), in.nextInt()); s1.printArea(); Circle s2 = new Circle(in.nextInt()); s2.printArea(); Square s3 = new Square(in.nextInt()); s3.printArea(); } }
import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Rectangle rectangle = new Rectangle(); Circle circle = new Circle(); Square square = new Square(); rectangle.setC(sc.nextInt()); rectangle.setK(sc.nextInt()); circle.setR(sc.nextInt()); square.setC(sc.nextInt()); System.out.println(rectangle.getArea()); double AreaCircle = circle.getArea(); int AreaCircle1 = (int)circle.getArea(); DecimalFormat df = new DecimalFormat("###.0"); double temp; if (AreaCircle > AreaCircle1){ temp = Double.parseDouble(df.format(AreaCircle)); if (temp != AreaCircle) { System.out.println(String.format("%.2f", AreaCircle)); }else { System.out.println(AreaCircle); } } else { System.out.println(AreaCircle1); } System.out.println(square.getArea()); } } class Shape{ public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } private int x; private int y; } class Rectangle extends Shape{ private int c; public int getC() { return c; } public void setC(int c) { this.c = c; } public int getK() { return k; } public void setK(int k) { this.k = k; } private int k; public int getArea(){ return c*k; } } class Circle extends Shape{ public int getR() { return r; } public void setR(int r) { this.r = r; } private int r; public double getArea(){ return 3.14*(double) (r*r); } } class Square extends Rectangle{ @Override public int getC() { return c; } @Override public void setC(int c) { this.c = c; } private int c; public int getArea(){ return c*c; } }
//需要对圆的面积结果进行处理,小数点后保留两位小数,小数点若是.00则将00舍去 //输出整数部分 import java.text.DecimalFormat; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int width=sc.nextInt(); int height=sc.nextInt(); int r=sc.nextInt(); int s=sc.nextInt(); Rectangle rectangle=new Rectangle(width,height); System.out.println(rectangle.GetArea()); Circle circle=new Circle(r); Square square = new Square(s); double AreaCircle = circle.GetArea(); int AreaCircle1 = (int)circle.GetArea(); DecimalFormat df = new DecimalFormat("###.0"); double temp; if (AreaCircle > AreaCircle1){ temp = Double.parseDouble(df.format(AreaCircle)); if (temp != AreaCircle) { System.out.println(String.format("%.2f", AreaCircle)); }else { System.out.println(AreaCircle); } } else { System.out.println(AreaCircle1); } System.out.println(square.GetArea()); } static class shape{ private int x; private int y; public shape(int x, int y) { this.x = x; this.y = y; } } static class Rectangle extends shape{ private int width; private int height; public Rectangle(int width, int height) { super(width, height); this.width = width; this.height = height; } public int GetArea(){ int s1=width*height; return s1; } } static class Circle extends shape{ int r; public Circle( int r) { super(r, r); this.r = r; } public double GetArea(){ double s2=3.14*r*r; return s2; } } static class Square extends Rectangle{ int length; public Square(int length){ super(length,length); this.length=length; } public int GetArea(int length){ int Area=length*length; return Area; } } }