Мне нужна помощь в улучшении этого кода ... Я не уверен, что он эффективен, слишком много повторяющегося кода.
Эта программа демонстрирует наследование.где я начинаю тему с точки, затем расширяю ее до круга и, наконец, цилиндра.
это основной класс java-теста
import java.awt.*;
import java.util.*;
public class ShapeTest {
public static void main(String args[]){
Scanner getInput = new Scanner(System.in);
int r=0,option;
double point_x=0,point_y=0;
double circle_x=0, circle_y=0, circle_r=0;
double cylinder_x=0, cylinder_y=0, cylinder_r =0, cylinder_h =0;
while(r!=1) {
System.out.println("Which shape do you want, type in the number:");
System.out.println("1: Point");
System.out.println("2: Circle");
System.out.println("3: Cylinder");
System.out.println("4: Exit");
option = getInput.nextInt();
switch (option) {
case 1:
System.out.println("type in the x co-ordinate");
point_x = getInput.nextDouble();
System.out.println("type in the y co-ordinate");
point_y = getInput.nextDouble();
point point =new point(point_x,point_y);
point.getName();
break;
case 2:
System.out.println("type in the x co-ordinate");
circle_x = getInput.nextDouble();
System.out.println("type in the y co-ordinate");
circle_y = getInput.nextDouble();
System.out.println("type in radius of circle");
circle_r = getInput.nextDouble();
circle circle = new circle(circle_x,circle_y,circle_r);
circle.getName();
circle.getArea(circle_r);
break;
case 3:
System.out.println("type in the x co-ordinate");
cylinder_x = getInput.nextDouble();
System.out.println("type in the y co-ordinate");
cylinder_y = getInput.nextDouble();
System.out.println("type in radius of cylinder");
cylinder_r = getInput.nextDouble();
System.out.println("type in height of cylinder");
cylinder_h = getInput.nextDouble();
cylinder cylinder = new cylinder(cylinder_x,cylinder_y,cylinder_r,cylinder_h);
cylinder.getName();
cylinder.getArea(cylinder_r,cylinder_h);
cylinder.getVolume(cylinder_r,cylinder_h);
break;
case 4:
r=1;
break;
default:
r=1;
break;
}
}
}
}
я думаю, что главное довольно простоЯ понимаю, что чувствую, что программа слишком много повторяется.
класс точек:
public class point{
public point(double x, double y){
centre(x,y);
}
public void centre(double x,double y){
double[] centre = new double[2];
centre[0] = x;
centre[1] = y;
}
public String getName(){
System.out.println("Point");
return "Point";
}
}
класс окружности
public class circle extends point {
public circle(double x, double y, double r){
super(x,y);
radius(r);
}
public void radius(double r){
double radii = r;
}
public String getName(){
System.out.println("Circle");
return "Circle";
}
public double getArea(double r){
double area = 3.14*r*r;
System.out.println("Area is" + area);
return area;
}
}
класс цилиндров
public class cylinder extends circle{
public cylinder(double x, double y, double r, double h){
super(x,y,r);
height(h);
}
public void height(double h){
double depth = h;
}
public double getVolume(double r, double h){
double volume = super.getArea(r)*h;
System.out.println("Volume is"+volume);
return volume;
}
public String getName(){
System.out.println("Cylinder");
return "Cylinder";
}
public double getArea(double r, double h){
double area = 2*3.142*r*h;
System.out.println("Area is" + area);
return area;
}
}
кода много ... есть ли способ сделать этот код более эффективным?
Спасибо за помощь