testtheclassshape.java
// Program to test the class Shape
import java.io.*;
public class TestShape
{
public static void main (String args[]) throws IOException
{
int x ;
int y;
int z;
String s;
int menu; // for the swich case
String answer; // for the repetition loop
// set input as the the buffer reader
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.flush();
do
{ // sets up the do while loop to allow the user
to go around as many times as they like
// The users start up menu , to show what is available
System.out.println("Menu ");
System.out.println("Enter 1 for a Square");
System.out.println("Enter 2 for a Rectangle ");
System.out.println("Enter 3 for a Triangle ");
System.out.println("Enter nothing and it will default
to a shape 'Point'");
s=input.readLine(); // reads the users input in
menu = Integer.parseInt(s); // converts the input into
an integer
switch (menu)
{
case 1: // Square
System.out.println("You have selected a sqaure"
);
System.out.println("Please enter the side
of the square"); // promt the user for the value of x
System.out.flush();
s=input.readLine(); // reads the users value
in
x=Integer.parseInt(s); // converts the string
into an integer
Shape s1 = new Shape(x); // creates
a new instance of the class shape
System.out.println("For a " + s1.getShape()
+ " with the length of the side as " + x + ", the perimeter is " + s1.getPerimeter());
System.out.flush(); // clear the input stream;
break;
case 2: // Rectangle
System.out.println("You have selected a rectangle");
System.out.println("Please enter the first
side of the rectangle"); // prompts the user for the x value
System.out.flush();
s=input.readLine(); // reads the users input
in
x=Integer.parseInt(s); // converts the string
to an integer
System.out.flush();
System.out.println("Please enter the second
side of the rectangle"); // prompts the user for the y value
s=input.readLine(); // reads the users input
in
y=Integer.parseInt(s); // converts the input
into an integer
Shape s2 = new Shape(x,y); // creates an new instance of the class shape
System.out.println("For a " + s2.getShape()
+ " with sides of " + x + " and " + y + " the perimeter is " + s2.getPerimeter());
System.out.flush();// clears the input stream
break;
case 3: // Triangle
System.out.println("You have selected a triangle");
System.out.println("Please enter the first
side of the triangle "); // prompts user for vlaue of x
System.out.flush();
s=input.readLine(); // reads the users input
in
x=Integer.parseInt(s); // converts the input
string to an integer
System.out.println("Please enter the second
side of the triangle "); // prompts the user for the value of y
System.out.flush();
s=input.readLine(); // reads the users input
in
y=Integer.parseInt(s); // converts the input
string into an integer
System.out.println("Please enter the third
side of the triangle"); // prompts the user for the value of z
System.out.flush();
s=input.readLine(); // reads the users input
in
z=Integer.parseInt(s); // converts the input
string to an integer
Shape s3 = new Shape(x,y,z); // creates a
new instance of the class shape
System.out.println("For a " + s3.getShape()
+ " with sides " + x + ", " + y + " and " + z + " has a perimeter of "
+ s3.getPerimeter());
System.out.flush(); // clears the input stream
break;
default:
System.out.println("You have not selected
any input parameters, or an incorrect perimeter");
Shape s4 = new Shape(); // creates and new
instance of the class shape with no perimeters which will call the default
constructor
System.out.println("The default constructor
has a shape " + s4.getShape() + " with no sides, and a perimeter of " +
s4.getPerimeter());
break;
} // ends the switch
System.out.println("Enter a Y to continue, or anything
else to quit");
System.out.flush();
answer = input.readLine();
}while(answer.equalsIgnoreCase("Y"));
} // ends the main
} // ends the class
Return to : Java Programming Hints
and Tips