简体   繁体   中英

Java Program that shows the number of shapes in a geometric figure interactively

I have this java program that is supposed to show the number of sides of geometrical shapes of Triangle, Trapezoid and Hexagon. I managed to create the codes such that when I ran the java Shape its shows all the Shapes and the sides all at once, but I want to make it interactive such that a user can be prompted to enter the shape she wants and the return is the number of sides of it, which functionality in Java can best do this?

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");
    }
}

This is my new cord after trying to implement above, its says errors exist, obsolete methods..

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");
}
}

To read input from the user you are going to want to use the Buffered Reader. Then you need to conditional call your methods based on the user input.

ex.

System.out.println("Enter the name of the shape");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String shapeName = br.readLine();

if(br.equals("Trapezoid") {
   Trapezoid t = new Trapezoid();
   t.numberOfSides();
}
//...

Once you have that code written you could implement a menu for the user to select the shape they want. Code for that will look something like this.

System.out.println("Select one of the following shapes. \n 1) Trapezoid \n 2) Triangle \n 3) Hexagon");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String shapeName = br.readLine();

if(br.equals("Trapezoid" || br.equals("1") {
   Trapezoid t = new Trapezoid();
   t.numberOfSides();
}
// ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM