一个java程序,用于计算正方形,圆形,矩形和三角形的周长

时间:2013-03-15 12:04:03

标签: java

通过设计计算不同形状周长的程序,我获得了在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));

4 个答案:

答案 0 :(得分:5)

这是学者们喜爱的“经典”压倒一切的问题。 (其他涉及动物或车辆。)

Shape界面开始:

public interface Shape {
    double getPerimeter();
}

然后让子类以自己的方式实现它:

public class Rectangle implements Shape {
    private double height;
    private double width;

    public Rectangle(double w, double h) {
        this.width = w;
        this.height = h;
    }

    public double getPerimeter() { return 2.0*(this.width + this.height); }
}


public class Circle implements Shape {
    private double radius; 

    public Circle(double r) {
        this.radius = r; 
    }

    public double getPerimeter() { return 2.0*Math.PI*this.radius; }
}

你将能够做到这样的事情:

List<Shape> shapes = new ArrayList<Shape>();
shapes.add(new Rectangle(1.0, 2.0));
shapes.add(new Circle(10.0));
for (Shape shape : shapes) {
    System.out.println(shape.getPerimeter()); // every Shape will do it their own way
}

添加一个新的Shape实现,原始代码仍可正常工作。这是多态和动态绑定的本质。

答案 1 :(得分:4)

您在一个类中定义了两个具有相同签名的方法。这是一个错误。你的方法:

double getperimeter(int constant,double pi,double radius);
double getperimeter(int rconstant,double rlength,double widith);

此外,您的main方法必须声明为静态

答案 2 :(得分:0)

您好,您应该在不同的文件中使用Perimeter类。 我看到关闭类Perimeter的缺少括号。 也像Xavier提到的那样,你有两种具有相同签名的方法。 您还要将Scanner中的值读入Perimeter实例变量,然后将未初始化的值传递给Perimeter方法。 所以我将分离Perimeter类,并在main中将值读取到局部变量并将它们传递给Perimeter方法。

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");
 radius=s.nextDouble();
 System.out.println("Enter The Square Length");
 lenght=s.nextInt();
 System.out.println("Enter The Rectangle Lenght");
 rlength=s.nextInt();
 System.out.println("Enter The Rectangle widith");
 widith=s.nextInt();
 System.out.println("Enter The Triangle Base");
 base=s.nextInt();
 System.out.println("Enter The Triangle height");
 height=s.nextInt();
 System.out.println("Enter The Triangle hypotenuse");
 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));

答案 3 :(得分:0)

你似乎还不熟悉java,因此开始帮助:

  • 主要方法是静态的,首先执行。

  • 您正在使用重载方法getperimeter,您可以轻松地将int和double混合使用。也许选择一个独特的名字。 Java惯例是使用有趣的驼峰案例:getPerimeter。

    import java.util.*;
    
    public class Perimeter {
    
    public static void main(String args[]) {
        new Perimeter().execute();
    }
    
    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 getperimeterRLenghtWidith(int rconstant, double rlength, double widith) {
        return (rconstant * (rlength + widith));
    }
    
    public double getperimeter(double base, double height, double hypotenuse) {
        return (base + height + hypotenuse);
    }
    
    private void execute() {
        final double pi = Math.PI; //22 / 7;
        final int constant = 2;
        double radius;
    
        final int sconstant = 4;
        double length;
    
        final int rconstant = 2;
        double rlength;
        double widith;
    
        double base;
        double height;
        double hypotenuse;
    
        Scanner s = new Scanner(System.in);
        System.out.println("Enter The Radius");
        radius = s.nextDouble();
        System.out.println("Enter The Square Length");
        length = s.nextInt();
        System.out.println("Enter The Rectangle Lenght");
        rlength = s.nextInt();
        System.out.println("Ener The Rectangle widith");
        widith = s.nextInt();
        System.out.println("Enter The Triangle Base");
        base = s.nextInt();
        System.out.println("Enter The Triangle height");
        height = s.nextInt();
        System.out.println("Enter The Triangle hypotenuse");
        hypotenuse = s.nextInt();
    
        System.out.println("Perimeter = " + getperimeter(constant, pi, radius));
        System.out.println("Perimeter = " + getperimeter(sconstant, length));
        System.out.println("Perimeter = " + getperimeterRLenghtWidith(rconstant, rlength, widith));
        System.out.println("Perimeter = " + getperimeter(base, height, hypotenuse));
    
    }
    }