通过设计一个计算不同形状周长的程序,我被分配在 Java 中执行继承中的方法重载,我设计了如下所示的代码,但是当我尝试编译时,出现错误。
import java.io.*;
import java.util.*;
public class Perimeter {
public double getperimeter(int constant,double pi,double radius){
return(constant*pi*radius);
}
public double getperimeter(int sconstant,double length){
return(sconstant*length);
}
public double getperimeter(int rconstant,double rlength,double widith){
return(rconstant*(rlength+widith));
}
public double getperimeter(double base,double height,double hypotenuse){
return(base+height+hypotenuse);
}
public void main (String args []){
final double pi=22/7;
final int constant=2;
double raduius;
final int sconstant=4;
double length;
final int rconstant=2;
double rlength;
double widith;
double base;
double height;
double hypotenuse;
Perimeter g= new Perimeter();
Scanner s=new scanner(System.in);
System.out.println("Enter The Radius");
g.radius=s.nextDouble();
System.out.println("Enter The Square Length");
g.lenght=s.nextInt();
System.out.println("Enter The Rectangle Lenght");
g.rlength=s.nextInt();
System.out.println("Enter The Rectangle widith");
g.widith=s.nextInt();
System.out.println("Enter The Triangle Base");
g.base=s.nextInt();
System.out.println("Enter The Triangle height");
g.height=s.nextInt();
System.out.println("Enter The Triangle hypotenuse");
g.hypotenuse=s.nextInt();
System.out.println("Perimeter = " + g.getperimeter(constant,pi,radius));
System.out.println("Perimeter = " + g.getperimeter(sconstant,length));
System.out.println("Perimeter = " + g.getperimeter(rconstant,rlength,widith));
System.out.println("Perimeter = " + g.getperimeter(base,height,hypotenuse));