Question

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 for base and height and calculates the hypotenuse, get methods for all three instance variables, and methods getArea and getPerimeter.

Then create a test application which queries the user for the needed data to create two Circle objects and two RightTriangle objects. It will then display all information about the four objects. It should then display which Circle object has the largest area and which RightTriangle object has the largest area.

Shell:

import java.util.Scanner;

// Then create a test application
public class Lab5Shell
{
   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);

       // which queries the user for the needed data to create two Circle objects
       Circle c1, c2;

       // and two RightTriangle objects.
       RightTriangle r1, r2;

       // It will then display all information about the four objects.

       // It should then display which Circle object has the largest area

       // and which RightTriangle object has the largest area.
   }
}

//Create a class Circle
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
class RightTriangle
{
   // with three instance variables of type double called base, height, and hypotenuse.

   // Then define an appropriate constructor which takes initial values for the base and height and calculates the hypotenuse,

   // a single set method which takes new values for base and height and calculates the hypotenuse,

   // get methods for all three instance variables

   // and methods getArea and getPerimeter.
}

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

Following is the answer:

Circle.java

public class Circle {
    private double radius;
    Circle(double radius){
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea(){
        return 3.14 * this.radius * this.radius;
    }

    public double getPerimeter(){
        return 2 * 3.14 * this.radius;
    }

    @Override
    public String toString() {
        return "Circle{" +
                "radius=" + radius +
                '}' + "Area = " + this.getArea() + " Perimeter = " + this.getPerimeter();
    }
}

RightTriangle.java

public class RightTriangle {
    private double base;
    private double height;
    private double hypotenuse;

    RightTriangle(double base, double height){
        this.base = base;
        this.height = height;
        this.hypotenuse = Math.sqrt((base * base) + (height * height));
    }

    public double getBase() {
        return base;
    }

    public double getHeight() {
        return height;
    }

    public double getHypotenuse() {
        return hypotenuse;
    }

    public void setHypotenuse(double base, double height) {
        this.hypotenuse = Math.sqrt((base * base) + (height * height));
    }

    public double getArea(){
        return (this.base * this.height)/2;
    }

    public double getPerimeter(){
        return (this.base + this.height)+Math.sqrt((base * base) + (height * height));
    }

    @Override
    public String toString() {
        return "RightTriangle{" +
                "base=" + base +
                ", height=" + height +
                ", hypotenuse=" + hypotenuse +
                '}' + "Area = " + this.getArea() + " Perimeter = " + this.getPerimeter();
    }
}

Lab5Shell.java

import java.util.Scanner;

public class Lab5Shell {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        // which queries the user for the needed data to create two Circle objects
        Circle c1, c2;
        c1 = new Circle(3);
        c2 = new Circle(4);
        System.out.println(c1.toString());
        System.out.println(c2.toString());

        if(c1.getArea() > c2.getArea()){
            System.out.println("C1 has large area");
        }
        // and two RightTriangle objects.
        RightTriangle r1, r2;
        r1 = new RightTriangle(3,4);
        r2 = new RightTriangle(4,5);
        System.out.println(r1.toString());
        System.out.println(r2.toString());

        if(r1.getArea() > r2.getArea()){
            System.out.println("R1 has large area");
        }

    }
}

Output:

Add a comment
Know the answer?
Add Answer to:
Create a class Circle with one instance variable of type double called radius. Then define an...
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...

  • SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The...

    SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: A private variable of type double named radius to represent the radius. Set to 1. Constructor Methods: Python : An overloaded constructor method to create a default circle. C# & Java: Default Constructor with no arguments. C# & Java: Constructor method that creates a circle with user-specified radius. Method getRadius() that returns the radius. Method setRadius()...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • Create a class named Circle with fields named radius, diameter, and area. Include a constructor that...

    Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Also include methods named setRadius() and getRadius(). The setRadius() method not only sets the radius, but it also calculates the other two values. (The diameter of a circle is twice the radius, and the area of a circle is pi multiplied by the square of the radius. Use the Math class PI constant...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius;...

    #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius; // get function for radius public: double getRadius(){ return radius; } // set function for radius void setRadius(double rad){ radius=rad; } // returning area = 3.14159 * radius * radius double getArea(){ return (3.14159 * radius * radius); } }; // Sample run int main() { // Declaring object of Circle Circle myCircle; myCircle.setRadius(5); // printing radius of circle cout<<"Radius of circle is: "<<(myCircle.getRadius())<<endl;...

  • Define an interface named Shape with a single method named area that calculates the area of...

    Define an interface named Shape with a single method named area that calculates the area of the geometric shape:        public double area(); Next, define a class named Circle that implements Shape. The Circle class should have an instance variable for the radius, a constructor that sets the radius, an accessor and a mutator method for the radius, and an implementation of the area method. Define another class named Rectangle that implements Shape. The Rectangle class should have instance variables...

  • Write an ADT named circle in java containing the following. Include data: radius (double) Include methods:...

    Write an ADT named circle in java containing the following. Include data: radius (double) Include methods: A no-argument constructor A constructor that sets the data to passed values Getters and setters for each piece of data getArea (Math.PI * radius * radius … try using a Math method here) getDiameter (radius * 2) getCircumference (2 * Math.PI * radius) A toString( ) method to display the object data Include Javadoc comments for the class and every method, and generate the...

  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 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...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

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
ADVERTISEMENT