Answered You can hire a professional tutor to get the answer.

QUESTION

Create an abstract base class called Shape and move the static variable scaleFactor and the three instance variables id, xLoc and yLoc from

Create an abstract base class called Shape and move the static variable scaleFactor and the three instance variables id, xLoc and yLoc from RightTriangle to Shape along with the get and set methods for those variables.  Implement a Shape constructor and update the RightTriangle constructor to call the Shape constructor for the variables moved to Shape. Then add four abstract methods to Shape called getArea(), getPerimeter(), displayDimensions() and scaleShape(). 

Define three classes that extend Shape called Circle, Rectangle and RightTriangle. For each class define the needed instance variables and an appropriate constructor to initializes those instance variables and call the Shape constructor for the instance variables defined on Shape, and then override the four abstract methods defined on Shape. 

Write an application which shows the user the following menu and implements all options. The program should continue to run and process requests until the user chooses to exit. The program should double-check that the user really wants to exit. You may use a limit of 10 possible shapes to simplify implementation. All input must be validated and appropriate feedback given for invalid input.

You must use an array of Shapes to hold all objects. You cannot have an array of Circle, Rectangle or RightTriangle objects. Additionally, only option 1 can use the Circle type, only option 2 can use the Rectangle type and only option 3 can use the RightTriangle type. Options 4-7 may only use the Shape type.

            1 - Add a new circle

            2 - Add a new rectangle

            3 - Add a new right triangle

            4 - Delete a shape

            5 - Delete all shapes

            6 - Display all shapes

            7 - Move a shape

            8- Enter a scale factor

            9- Scale all shapes

            10- Exit program

Shell to be used for this program:

import java.util.Scanner;

public class Lab8Shell

{

public static void main(String args[])

{

// DATA

Shape[] shapes = new Shape[10];

int nextIDNumber = 1;

boolean exit = false;

int selection;

Scanner input = new Scanner(System.in);

int id;

int x, y;

double base, height, width, radius;

boolean found = false;

// ALGORITHM

// loop until user exits

do

{

// display menu

System.out.println("1 Add a new circle");

System.out.println("2 Add a new rectangle");

System.out.println("3 Add a new right triangle");

System.out.println("4 Delete a shape");

System.out.println("5 Delete all shape");

System.out.println("6 Display all shapes");

System.out.println("7 Move a shape");

System.out.println("8 Enter a scale factor");

System.out.println("9 Scale all shapes");

System.out.println("10 Exit program");

// get user choice

selection = input.nextInt();

while (selection < 1 || selection > 10)

{

System.out.println("Please enter valid choice");

selection = input.nextInt();

}

// switch on selection

switch(selection)

{

case 1:

// ADD NEW CIRCLE

// get size from user (one variable)

// get location from user (two variables)

// set found to false

// loop through array

// if this is not a valid object

// create new Circle object and assign to current array element

// set found to true

// break out of loop

// if not found, give error message

// break out of switch statement

case 2:

// ADD NEW RECTANGLE

// get size from user (two variables)

// get location from user (two variables)

// set found to false

// loop through array

// if this is not a valid object

// create new Rectangle object and assign to current array element

// set found to true

// break out of loop

// if not found, give error message

// break out of switch statement

case 3:

// ADD NEW RIGHT TRIANGLE

// get size from user (two variables)

System.out.println("Enter the base");

base = input.nextDouble();

while (base <= 0)

{

System.out.println("Invalid number, please enter positive number");

base = input.nextDouble();

}

System.out.println("Enter the height");

height = input.nextDouble();

while (height <= 0)

{

System.out.println("Invalid number, please enter positive number");

height = input.nextDouble();

}

// get location from user (two variables)

System.out.println("Enter X location");

x = input.nextInt();

while (x < 0)

{

System.out.println("Invalid number, please enter positive number or zero");

x = input.nextInt();

}

System.out.println("Enter Y location");

y = input.nextInt();

while (y < 0)

{

System.out.println("Invalid number, please enter positive number or zero");

y = input.nextInt();

}

// set found to false

found = false;

// loop through array

for (int i = 0; i < shapes.length; i++)

{

// if this is not a valid object

if (shapes[i] == null)

{

// create new RightTriangle object and assign to current array element

shapes[i] = new RightTriangle(nextIDNumber++, x, y, base, height);

// set found to true

found = true;

// break out of loop

break;

}

}

// if not found, give error message

if (!found)

{

System.out.println("No more room");

}

// break out of switch statement

break;

case 4:

// DELETE ONE SHAPE

// get id number to delete

// set found to false

// loop through array

// if this is a valid object and the correct object

// delete object

// set found to true

// break out of loop

// if not found, give error message

// break out of switch statement

case 5:

// DELETE ALL SHAPES

// loop through array

// if this is a valid object

// delete object

// break out of switch statement

case 6:

// DISPLAY ALL SHAPES

// print header

System.out.println("Here are our shapes");

// loop through array

// if this is a valid object

// display ID, X, Y, Perimeter and Area

// call displayDimensions to display its dimensions

// break out of switch statement

case 7:

// MOVE A SHAPE

// get id number to move

// get location from user (two variables)

// set found to false

// loop through array

// if this is a valid object and the correct object

// call two set methods

// set found to true

// break out of loop

// if not found, give error message

// break out of switch statement

case 8:

// ENTER A SCALE FACTOR

// get scale factor

// call SetScaleFactor to set the new scale factor

// break out of switch statement

case 9:

// SCALE ALL SHAPES

// loop through array

// if this is a valid object

// call ScaleShape

// break out of switch statement

break;

case 10:

// EXIT PROGRAM

// confirm user wants to exit

// set variable to break out of loop

// break out of switch statement

}

} while (!exit);

}

}

abstract class Shape

{

// declare static variable

// declare instance variables

// one constructor

// get/set methods

// abstract methods

}

class Circle extends Shape

{

// declare instance variable

private double radius;

// one constructor

// get/set methods

public double GetRadius()

{

return radius;

}

public void SetRadius(double r)

{

if (r > 0.0)

{

radius = r;

}

}

// override abstract methods

// GetPerimeter method

public double GetPerimeter()

{

return 2 * Math.PI * radius;

}

// GetArea method

public double GetArea()

{

return Math.PI * radius * radius;

}

// ScaleShape method

// DisplayDimensions method

}

class Rectangle extends Shape

{

// declare instance variables

private double base;

private double height;

// one constructor

// get/set methods

public double GetBase()

{

return base;

}

public void SetBase(double b)

{

if (b > 0.0)

{

base = b;

}

}

public double GetHeight()

{

return height;

}

public void SetHeight(double h)

{

if (h > 0.0)

{

height = h;

}

}

// override abstract methods

// GetPerimeter method

public double GetPerimeter()

{

return (2 * base + 2 * height);

}

// GetArea method

public double GetArea()

{

return base * height;

}

// ScaleShape method

// DisplayDimensions method

}

class RightTriangle extends Shape

{

// declare instance variables

private double base;

private double height;

private double hypotenuse;

// one constructor

// get/set methods

public void SetBaseAndHeight(double b, double h)

{

if (b > 0.0 && h > 0)

{

base = b;

height = h;

hypotenuse = Math.sqrt(base*base + height*height);

}

}

public double GetBase()

{

return base;

}

public double GetHeight()

{

return height;

}

public double GetHypotenuse()

{

return hypotenuse;

}

// override abstract methods

// GetPerimeter method

public double GetPerimeter()

{

return base + height + hypotenuse;

}

// GetArea method

public double GetArea()

{

return 0.5 * base * height;

}

// ScaleShape method

// DisplayDimensions method

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question