Question

In: Computer Science

Circle Class (This is in JAVA) /** * Defines a basic shape with just area *...

Circle Class (This is in JAVA)

/**
 * Defines a basic shape with just area
 *
 * @author Jo Belle
 * @version 0.2 (10/05/2020)
 */
public class Shape{
    private double area;

    public Shape(){ area = 0.0; }
    public Shape( double a ){ this.area = a; }
    public void setArea( double a ){ area = a; }
    public double getArea(){ return area; }
    public String toString(){
        return "Shape:\n\tarea: " + area;
    }
}
/**
 * Create a simple Circle object
 *
 * @author Jo Belle
 * @version 0.2 (10/12/2020)
 */
public class ShapeDriver{
    public static void main( String[] args ){
        Circle cir = new Circle( );
        cir.setRadius( 5.0 );
        System.out.println( cir.toString() );
    }
}

Given the code above, write a Circle class (and save it in a file named Circle.java) that inherits from the Shape class. Include in your Circle class, a single private field double radius. Also include a method void setRadius(double r) (which also sets area) and a method double getRadius() (which also returns the current radius). Change the accessibility modifier for area in the Shape class to be more appropriate for a base class. Make sure that ShapeDriver's main() method executes and produces the following output:

Shape:
        area: 78.53981633974483
        radius: 5.0

Submit both your Circle.java and your Shape.java files.

Solutions

Expert Solution

Answer.

Shape.java

public class Shape{

private double area;

public Shape(){
area = 0.0;
}

public Shape( double a ){
this.area = a;
}

public void setArea( double a ){
area = a;
}

public double getArea(){
return area;
}

public String toString(){
return "Shape:\n\tarea: " + area;
}

}

Circle.java

public class Circle extends Shape{

//declaring private field
private double radius;

//getter method
public double getRadius() {
return radius;
}

//setter method
public void setRadius(double radius) {
this.radius = radius;
this.setArea(Math.PI*this.radius*this.radius); //sets area using method in Shape class
}

//This is to be overridden only if you want to display radius in output
public String toString() {
return super.toString()+"\n\tradius: "+radius;
}

}

ShapeDriver.java

public class ShapeDrive{

public static void main( String[] args ){
Circle cir = new Circle( );
cir.setRadius(5.0 );
System.out.println( cir.toString() );
}

}

Output

Kindly upvote please,

Thank you.


Related Solutions

1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
use java The area of a circle is computed by ?? 2 , where r is...
use java The area of a circle is computed by ?? 2 , where r is the radius of the circle and π is 3.14. The area of a rectangle is computed by ??, where a and b correspond to the length and width of the rectangle respectively. The area of a triangle is computed by ah 2 , where a and h correspond to the length and height of the triangle respectively. Write a Java program: First it asks...
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
Area of Circle
Luther has a circular garden of radius 1.6 cm . If she plans to put artificial grass on it, How much area does she need to cover?
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of...
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of size 4 in a driver class using the following statement: Circle circleArr[] = new Circle[4]; Populate the array with four different radiuses and then, using a for loop from 0 to one less then the length of the array, print the area and the diameter of each of the four circles Circle Class: import java.text.DecimalFormat; public class Circle {    DecimalFormat dec = new...
Write a java class called circle that represents a circle. It should have following three fields:...
Write a java class called circle that represents a circle. It should have following three fields: int x (x-coordinate), int y (y-coordinate), double radius (radius of the circle). Your circle object should have following methods: public int getRadius () public int getX () public int getY () public double area () public double perimeter() public String toString() write a client program called CircleClient that creates objects of the circle class called c1 and c2. Assign values to the fields when...
Use BlueJ java to finish this PolkaDots class, class circle is included below as well. public...
Use BlueJ java to finish this PolkaDots class, class circle is included below as well. public class PolkaDots { private ArrayList<Circle> dots;// an arrayList field to hold an ArrayList. /** * Create a new list of Circles, starts with 5 default circles. */ public PolkaDots() { dots = new ArrayList<Circle>(); // an arrayList to hold a bunch of circles Circle circle1 = new Circle(30, 10, 10, "blue");//create the 1st circle dots.add(circle1);// add the 1st circle to the arrayList    Circle...
IN JAVA PLEASE, USE COMMENTS Following the example of Circle class, design a class named Rectangle...
IN JAVA PLEASE, USE COMMENTS Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. • A no-arg constructor that creates a default rectangle. • A constructor that creates a rectangle with specified width and height • A method name getWidth() return the value...
Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT