Question

In: Computer Science

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 DecimalFormat("#.##");
   private int radius;
  
   public Circle(int radius) {
       this.radius = radius;
   }

   public void setRadius(int r) {
       radius = r;
   }
  
   public int getRadius() {
       return radius;
   }
  
   public int getDiameter() {
       return radius*radius;
   }
  
   public double getArea() {
       return Math.PI*radius*radius;
   }
  
   public String toString() {
       String result = "A circle with radius " + getRadius() + " has an area of " + dec.format(getArea()) + " and a diameter of " + getDiameter();
       return result;
   }
}

Solutions

Expert Solution

import java.text.DecimalFormat;

public class Circle {
    DecimalFormat dec = new DecimalFormat("#.##");
    private int radius;

    public Circle(int radius) {
        this.radius = radius;
    }

    public void setRadius(int r) {
        radius = r;
    }

    public int getRadius() {
        return radius;
    }

    public int getDiameter() {
        return radius * radius;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

    public String toString() {
        String result = "A circle with radius " + getRadius() + " has an area of " + dec.format(getArea()) + " and a diameter of " + getDiameter();
        return result;
    }
}

class CircleTest {

    public static void main(String[] args) {
        Circle circleArr[] = new Circle[4];
        circleArr[0] = new Circle(5);
        circleArr[1] = new Circle(1);
        circleArr[2] = new Circle(10);
        circleArr[3] = new Circle(7);

        for (int i = 0; i < circleArr.length; i++) {
            System.out.println(circleArr[i]);
        }
    }
}


Related Solutions

JAVA: Create a circle that contains the following... Sample code to work from provided below. FIELDS:...
JAVA: Create a circle that contains the following... Sample code to work from provided below. FIELDS: a private double that holds the radius. CONSTRUCTORS a no argument constructor that sets the radius to zero a constructor that takes a single argument of type double which is the radius. METHODS a method called "getRadius" which returns the current radius a method called "setRadius" which takes a single parameter of type double containing the new radius a method called "getArea" which returns...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on...
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on Program 13-1 (see attachment Pr13-1.cpp) in the textbook, create a class name “Circle” with the following declarations (hint: you can use PI=3.14): //Circle class declaration class Circle { private: double radius; public: void setRadius(double); double getRadius() const; double getArea() const; double getPerimeter() const; }; 2: Based on Program 13-2 (see attachment Pr13-2.cpp) in the textbook, create two instances of the Circle class, pizza1 and...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside of main declare two static final variables and integer for number of days in the week and a double for the revenue per pizza (which is $8.50). Create a method called main Inside main: Declare an integer array that can hold the number of pizzas purchased each day for one week. Declare two additional variables one to hold the total sum of pizzas sold...
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 language how would I write the following? Create an array called “boyNames” and store...
In java language how would I write the following? Create an array called “boyNames” and store all names from the BoyNames.txt file Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file Create a text menu that allowing users to:          1. Enter a name and the application must display a message indicating if the name is   among the most popular (found on either array) If the name is found, tell the user if it is a boy’s...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept of a domain name, assuming a domain name has a single attribute: the domain name itself. Include the following: - Constructor: accepts the domain name as an argument. - getDomain: an accessor method for the domain name field. - setDomain: a mutator method for the domain name field. - prefix: a method returning whether or not the domain name starts with www. - extension:...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT