Question

*****language : java****** ceate a class RightTriangle with three instance variables of type double called base,...

*****language : java****** ceate a class RightTriangle with three instance variables of type double called base, height, and hypotenuse and three instance variables of type int called ID, xLoc and yLoc. Also add a static variable of type double called scaleFactor. This should be initialized to a default value of 1. Then define an appropriate constructor that takes initial values for all instance variables except hypotenuse and sets the values of all instance variables including calculating the hypotenuse. Then define get and set methods for all instance variables. Define a single SetBaseAndHeight which sets both base and height and calculates the hypotenuse. There should not be individual methods SetBase, SetHeight, and SetHypotenuse. All set methods should verify that no invalid data is set. Define methods GetArea and GetPerimeter. Define a new method ScaleShape() which multiplies the base and height instance variables by the scaleFactor and then updates the hypotenuse. 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 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//#################### PGM START ######################################

package dog;

import java.util.Scanner;

class RightTriangle{
   double height,base,hyp;
   int id,xLoc,yLoc;
   static double scaleFactor=1;
   RightTriangle(double h,double b,int id,int x,int y){
       this.height=h;
       this.id=id;
       this.base=b;
       this.xLoc=x;
       this.yLoc=y;
       this.hyp=Math.sqrt((h*h)+(b*b));
   }
   void SetBaseAndHeight(double h,double b){
       this.height=h;
       this.base=b;
       this.hyp=Math.sqrt((h*h)+(b*b));
   }
   double GetArea(){
       return (base*height)/2;
   }
   double GetPerimeter(){
       return(base+height+hyp);
   }
   void setxLoc(int x){
       this.xLoc=x;
   }
   void seyLoc(int y){
       this.yLoc=y;
   }
   void ScaleShape(){
       this.height=this.height*scaleFactor;
       this.base=this.base*scaleFactor;
       this.hyp=this.hyp*scaleFactor;
   }
}

public class Triangle {

   public static void main(String[] args) {
       boolean flag=true,delflag;
       int i=0,choice,index,xloc,yloc;
       double height,base;
       Scanner s=new Scanner(System.in);
       RightTriangle r1[]=new RightTriangle[10];
       int id=0;
      
      
       while(flag){
           System.out.println("1 – Enter a new right triangle \n"
                   + "2 – Delete a right triangle\n"
                   + "3 – Delete all right triangles\n"
                   + "4 – Display all right triangles\n"
                   + "5 – Move a triangle\n"
                   + "6 – Resize a triangle\n"
                   + "7 – Enter a scale factor\n"
                   + "8 – Scale all triangles\n"
                   + "9 – Exit program");
           System.out.print("Enter your choice: ");
           choice=Integer.parseInt(s.nextLine());
           delflag=true;
           switch(choice){
               case 1:if(i<10){
                           System.out.print("Enter the height: ");
                           height=s.nextDouble();
                           System.out.print("Enter the base : ");
                           base=s.nextDouble();
                           System.out.print("Enter the xLoc: ");
                           xloc=s.nextInt();
                           System.out.print("Enter the yLoc: ");
                           yloc=s.nextInt();
                           r1[i++]=new RightTriangle(height,base,id,xloc,yloc);
                           id+=1;
                       }else{
                           System.out.println("\nArray full , Max 10 triangles allowed");
                       }
                       break;
                      
               case 2: System.out.print("Enter the id: ");
                       index=s.nextInt();
                       for(int j=0;j<i;j++){
                           if(r1[j].id==index){
                               r1[j]=r1[i-1];
                               r1[i-1]=null;
                               i--;
                               delflag=false;
                           }
                       }
                       if(delflag){
                           System.out.println("\nWrong id entered!!!!");
                       }
                       break;
              
               case 3:for(int j=0;j<i;j++){
                           r1[j]=null;
                       }
                       i=0;
                       System.out.println("Deleted all triangles");
                       break;
                      
               case 4:for(int j=0;j<i;j++){
                           System.out.println("Triangle ID: "+r1[j].id+"\n\tBase: "+r1[j].base
                                   +"\n\tHeight: "+r1[j].height+"\n\tHypotenuse: "+r1[j].hyp
                                   +"\n\txLoc: "+r1[j].xLoc+"\n\tyLoc: "+r1[j].yLoc);
                       }
                       break;
                      
               case 5: System.out.print("Enter the id:");
                       index=s.nextInt();
                       for(int j=0;j<i;j++){
                           if(r1[j].id==index){
                               System.out.print("Enter the xLoc: ");
                               xloc=s.nextInt();
                               System.out.print("Enter the yLoc: ");
                               yloc=s.nextInt();
                               r1[j].setxLoc(xloc);
                               r1[j].seyLoc(yloc);
                               delflag=false;
                          
                           }
                       }if(delflag){
                           System.out.println("\nWrong id entered!!!!");
                       }
                       break;
                      
               case 6:System.out.print("Enter the id:");
                       index=s.nextInt();
                       for(int j=0;j<i;j++){
                           if(r1[j].id==index){
                               System.out.print("Enter the new base: ");
                               base=s.nextInt();
                               System.out.print("Enter the new height: ");
                               height=s.nextInt();
                               r1[j].SetBaseAndHeight(height, base);
                               delflag=false;
                          
                           }
                       }if(delflag){
                           System.out.println("\nWrong id entered!!!!");
                       }
                       break;
                      
               case 7:System.out.print("Enter the new scaling factor: ");
                       RightTriangle.scaleFactor=s.nextDouble();
                       System.out.println("New scale factor: "+RightTriangle.scaleFactor);
                       break;
                      
               case 8: for(int j=0;j<i;j++){
                           r1[j].ScaleShape();
                       }
                       break;
                      
               case 9: System.out.print("Are you sure you want to exit(y/n):");
                       char y=s.nextLine().charAt(0);
                       if(y=='Y' || y=='y')
                           flag=false;
                       break;
                      
               default: System.out.println("\nInvalid choice!!!!!!!");
                       break;
                  
                  
                  
           }
           System.out.println("\n");
       }
       s.close();
   }

}

//########################## PGM END #################################
OUTPUT

Add a comment
Know the answer?
Add Answer to:
*****language : java****** ceate a class RightTriangle with three instance variables of type double called base,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two...

    Lab 7 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...

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

  • Create a class called Retangle.java that contains two double-precision instance variables named width and height. The...

    Create a class called Retangle.java that contains two double-precision instance variables named width and height. The class should include all kinds of overloaded constructors. Additionally, there should be two accessor methods, mutator methods, class method named area() that returns the area of a Rectangle object. Inside main(),fully test all methods.

  • JAVA PLEASE Create a class called Account with the following instance data Integer id Double balance...

    JAVA PLEASE Create a class called Account with the following instance data Integer id Double balance Provides the following methods Default constructor (defaults balance to 100) Constructor with parameters for the ID and the starting balance Accessor and mutator methods for id and balance Method to perform a withdrawal Method to perform a deposit Create a class called Bank which has an array of Account objects. This class will manage the accounts. It must provide methods Do withdrawal Do deposits...

  • this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea {...

    this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);    Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); // Read and set base and height for triangle1 (use setBase() and setHeight())    // Read and set base and height for triangle2 (use setBase() and setHeight())    // Determine larger triangle (use getArea())    private int base; private int height; private...

  • Java Write a Temperature class that has two private instance variables: • degrees: a double that...

    Java Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have - four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set...

  • In Java, write a class for a Triangle Object that contains, Private instance variables: - three...

    In Java, write a class for a Triangle Object that contains, Private instance variables: - three doubles: a,b,c, representing the three sides of the Triangle - constructor with three double as args, which are put into three instances variables Public Instance Methods: - getPerimeter: computes the perimeter of the triangle(the sum of the three sides), returns a double - getArea: computes the area of the triangle using Heron's formula: Area = (s*(s-a)*(s-b)*(s-c))^0.5, where s = 0.5*(the triangle's perimeter), returns a...

  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • 1- Create the base class Book that has the following instance variables, constructor, and methods title...

    1- Create the base class Book that has the following instance variables, constructor, and methods title ( String) isbn ( String) authors (String) publisher (String) edition ( int) published_year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method // that return sting representation of Book object. 2- Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title ( String) isbn...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT