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

QUESTION

First are the instructions and then will be the Lab Shell. Thanks Add three instance variables to your class RightTriangle from lab 5.

First are the instructions and then will be the Lab Shell. Thanks

Add three instance variables to your class RightTriangle from lab 5. The first two are of type int and are called xLoc and yLoc and the third is of type int called ID. Also add a static variable of type double called scaleFactor. This should be initialized to a default value of 1. Update the constructor to set the three new instance variables and add appropriate get and set methods for the four new variables. All set methods including those from lab 5 should verify that no invalid data is set. Also define a new method ScaleShape() which multiplies the base and height instance variables by the scaleFactor and then updates the hypotenuse.

An application needs written which shows the user the following menu and implement all options. The program should continue to run and process requests until the user selects 9. The program should double-check that the user really wants to exit. You may use a limit of 10 possible triangles to simplify implementation. The program should assign new ids to assure their uniqueness, the user should not enter an id for a new object in option 1. All input must be validated and appropriate feedback given for invalid input. Option 4 should display all info about the object. This is 8 method calls.

             1 - Enter a new right triangle

             2 - Delete a right triangle

             3 - Delete all right triangles

             4 - Display all right triangles

             5 - Move a triangle

             6 - Resize a triangle

             7 - Enter a scale factor

             8 - Scale all triangles

             9 - Exit program

import java.util.Scanner;

public class Lab7Shell

{

public static void main(String[] args)

{

// DATA

RightTriangle [] triangles = new RightTriangle[10];

int nextIDNumber = 1;

boolean exit = false;

int selection=0;

Scanner input = new Scanner(System.in);

int id=0;

int x=0, y=0;

double base=0, height=0;

boolean found = false;

// ALGORITHM

// loop until user exits

do

{

// display options

// get selection (validate)

// switch on selection

switch(selection)

{

case 1:

// get size from user (two variables only,

calculate the hypotenuse)

// get location from user (two variables, X,Y

location)

// set found to false

// loop through array

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

{

// if this is an empty spot

if (triangles[i] == null)

{

// create new RightTriangle

object and assign to current array element

triangles[i] = new

RightTriangle(nextIDNumber++, base, height, x, y);

// set found to true

found = true;

// break out of loop

break;

}

}

// if not found, give error message

// break out of switch statement

break;

case 2:

// get id number to delete

// set found to false

// loop through array

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

{

// if this is a valid object and the

correct object

if (triangles[i] != null &&

triangles[i].GetID() == id)

{

// delete object

triangles[i] = null;

// set found to true

// break out of loop

}

}

// if not found, give error message

// break out of switch statement

case 3:

// loop through array

// if this is a valid object

// delete object

// break out of switch statement

break;

case 4:

// display header

// loop through array

// if this is a valid object

// display all info about

object. This should call and display all 8 get methods that return

information.

// break out of switch statement

break;

case 5:

// get id number to move

// get location from user (two variables new

X,Y location)

// 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

break;

case 6:

// get id number to resize

// get size from user (two variables)

// set found to false

// loop through array

// if this is a valid object and the

correct object

// call SetBaseAndHeight

// set found to true

// break out of loop

// if not found, give error message

// break out of switch statement

case 7:

// get new scale factor (validate)

// call SetScaleFactor to set the new scale

factor

// break out of switch statement

break;

case 8:

// loop through array

// if this is a valid object

// call ScaleShape method on

object

// break out of switch statement

break;

case 9:

// confirm user wants to exit

// set variable to break out of loop

// break out of switch statement

break;

}

// End loop

} while (!exit);

}

}

class RightTriangle

{

// with three instance variables of type double called base, height,

and hypotenuse.

private double base;

private double height;

private double hypotenuse;

// add three new instance variables

// add static scaleShape

// update constructor to take the new instance variables

public RightTriangle(int i, double b, double h, int x, int y)

{

SetBaseAndHeight(b, h);

// set the other instance variables

}

// a single set method which takes new values for base and height

and calculates the hypotenuse,

public void SetBaseAndHeight(double b , double h)

{

if (b > 0.0 && h > 0.0)

{

base = b;

height = h;

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

height);

}

}

// add set methods for the three new instance variables

// get methods for all three instance variables

public double GetBase()

{

return base;

}

public double GetHeight()

{

return height;

}

public double GetHypotenuse()

{

return hypotenuse;

}

// add get methods for the three new instance variables

// and methods getArea and getPerimeter.

public double GetArea()

{

return 0.5 * base * height;

}

public double GetPerimeter()

{

return base + height + hypotenuse;

}

// add ScaleShape method

}

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