三角形、台形、六角形の幾何学的形状の辺の数を表示することになっているこのJavaプログラムがあります。java Shapeを実行すると、すべての図形と側面が一度に表示されるようにコードを作成できましたが、ユーザーが希望の図形を入力するように求められるようにインタラクティブにしたいので、戻り値はその側面の数、Javaのどの機能がこれを最もよく行うことができますか?
public abstract class Shape {
public abstract void numberOfSides();
public static void main(String[] args)
{
System.out.println("The Geometrical characteristics of the figures are as follows");
Trapezoid t=new Trapezoid();
Triangle tg=new Triangle();
Hexagon h=new Hexagon();
t.numberOfSides();
tg.numberOfSides();
h.numberOfSides();
}
}
class Trapezoid extends Shape {
public void numberOfSides()
{
System.out.println("Trapezoid~It is geometrical figure with an attribute of 4 sides (Of which two are parallel and with no angles)");
}
}
class Triangle extends Shape {
public void numberOfSides()
{
System.out.println("Triangle~It is geometrical figure with an attribute of 3 sides");
}
}
class Hexagon extends Shape {
public void numberOfSides()
{
System.out.println("Hexagon-It is geometrical figure with an attribute of 6 sides");
}
}
これは、上記の実装を試みた後の私の新しいコードです。エラーが存在し、廃止されたメソッドであると言われています。
import java.io.*;
public abstract class Shape {
public abstract void numberOfSides();
public static void main(String[] args)
{
System.out.println("Enter the name of the Shape");
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
try {
String shapeName = br.readLine();
if (br.equals ("Trapezoid"))
{Trapezoid t = new Trapezoid();
t.numberOfSides();
}
else if (br.equals ("Triangle"))
{Triangle tg = new Triangle();
tg.numberOfSides();
}
else if (br.equals ("Hexagon"))
{Hexagon h = new Hexagon();
h.numberOfSides();
}
}
}
class Trapezoid extends Shape {
public void numberOfSides()
{
System.out.println("Trapezoid~It is geometrical figure with an attribute of 4 sides (Of which two are parallel and with no angles)");
}
}
class Triangle extends Shape {
public void numberOfSides()
{
System.out.println("Triangle~It is geometrical figure with an attribute of 3 sides");
}
}
class Hexagon extends Shape {
public void numberOfSides()
{
System.out.println("Hexagon-It is geometrical figure with an attribute of 6 sides");
}
}