Question

In: Computer Science

java create a class for triangle and also driver(area and perimeter) also write its getters and...

java

create a class for triangle and also driver(area and perimeter)

also write its getters and setters

for the right triangle

Solutions

Expert Solution

// RightTriangle.java : Java class to represent a right angled triangle
public class RightTriangle {
  
   // variables to store the base and height of the triangle
   private double base;
   private double height;
  
   // default constructor to set base and height to 0
   public RightTriangle()
   {
       this(0,0);
   }
  
   // parameterized constructor to set the base and height of the triangle to the passed arguments
   public RightTriangle(double base, double height)
   {
       this.base = base;
       this.height = height;
   }
  
   // setters
   // method to set the height of the triangle
   public void setHeight(double height)
   {
       this.height = height;
   }
  
   // method to set the base of the triangle
   public void setBase(double base)
   {
       this.base = base;
   }
  
   // getters
   // method to return the height of the triangle
   public double getHeight()
   {
       return height;
   }
  
   // method to return the base of the triangle
   public double getBase()
   {
       return base;
   }
  
   // method to compute and return the area of the triangle
   public double area()
   {
       return (base*height)/2;
   }
  
   // method to compute and return the perimeter of the triangle
   public double perimeter()
   {
       return(base+height+Math.sqrt(Math.pow(base, 2) + Math.pow(height, 2)));
   }
}
//end of RightTriangle.java

// RightTriangleDriver.java : Driver program to test the RightTriangle class

public class RightTriangleDriver {
  
   public static void main(String[] args) {

       // test the RightTriangle class
       // create an object of RightTriangle class
       RightTriangle t1 = new RightTriangle(3,4);
      
       // display its bae, height , area and perimeter
       System.out.println("Base : "+t1.getBase());
       System.out.println("Height : "+t1.getHeight());
       System.out.println("Area : "+t1.area());
       System.out.println("Perimeter : "+t1.perimeter());
   }

}
//end of RightTriangleDriver.java

Output:


Related Solutions

In java. Prefer Bluej Create a program in java that calculates area and perimeter of a...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Create a program in java that calculates area and perimeter of a square - use a...
Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
Using Java Create the class RightTriangle which is a triangle with one of its angles equal...
Using Java Create the class RightTriangle which is a triangle with one of its angles equal to 90º – a “right angle”. Its constructor will require the lengths only of its two legs. It will then calculate the length of the third side (its “hypotenuse”) using the Pythagorean Theorem: a2 + b2 = c2. This class will have no instance variables itself, so it will need to set the appropriate variables in its parent class, Triangle. It will inherit equals...
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT