java 一个计算正方形、圆形、长方形和三角形周长的java程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15431985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A java program to calculate the perimeter of a square,circle,rectangle and Triangle
提问by user2173809
I was given an assignment to perform method overloading in inheritence in java by designing a program that calculates the perimeter of different shapes, i designed code as shown below but when i try to compile, there are errors.
通过设计一个计算不同形状周长的程序,我被赋予了在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));
回答by duffymo
This is a "classic" overriding problem that academics love. (Others involve animals or vehicles.)
这是学术界喜爱的“经典”压倒一切的问题。(其他涉及动物或车辆。)
Start with a Shape
interface:
从Shape
接口开始:
public interface Shape {
double getPerimeter();
}
Then have subclasses implement it, each in their own way:
然后让子类以自己的方式实现它:
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; }
}
You'll be able to do things like this:
你将能够做这样的事情:
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
}
Add a new Shape implementation and your original code still just works. It's the essence of polymorphism and dynamic binding.
添加一个新的 Shape 实现,您的原始代码仍然可以正常工作。这是多态和动态绑定的本质。
回答by Xavier DSouza
You are defining two methods with same signature within a single class. This is an error. Your methods :
您正在单个类中定义两个具有相同签名的方法。这是一个错误。你的方法:
double getperimeter(int constant,double pi,double radius);
double getperimeter(int rconstant,double rlength,double widith);
Also your main method must be declared as static
此外,您的主要方法必须声明为静态
回答by alamatula
Hi you should have a the class Perimeter in a different file. I see missing brackets closing the class Perimeter. Also like Xavier mentioned you have two methods with the same signature. You are also reading values from Scanner into Perimeter instance variables and then passing the uninitialized values to the Perimeter methods. So i would separate the Perimeter class and in the main read the values to local variables and pass them to Perimeter methods.
您好,您应该在不同的文件中有一个 Perimeter 类。我看到缺少括号关闭类 Perimeter。也像 Xavier 提到的那样,您有两种具有相同签名的方法。您还将从 Scanner 读取值到 Perimeter 实例变量,然后将未初始化的值传递给 Perimeter 方法。所以我会分开 Perimeter 类,并在主要读取值到局部变量并将它们传递给 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));
回答by Joop Eggen
You are still unfamiliar with java it seems, hence start help:
您似乎仍然不熟悉 java,因此开始帮助:
The main method is static, that is executed first.
You are using overloaded methods getperimeter, where you can easily mix up int and double. Maybe pick a unique name. Java convention is to use funny camel case: 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)); } }
main 方法是静态的,即首先执行。
您正在使用重载方法 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)); } }