Question

I have given you a piece of code to test that your circle class works correctly....

I have given you a piece of code to test that your circle class works correctly. Add your circle class to the code.

public class Lab2Num1 {

public static class Circle
{ private double radius;
//your code goes here
  
//provide default constructor, constructor with one parameter, area, and circumference
  
}
public static void main(String[] args) {

Circle c = new Circle(1.5);


System.out.printf("The circumference of a circle of radius " + c.getRadius()+ " is %5.2f\n", c.circumference());
System.out.printf("The area of a circle of radius " +c.getRadius()+ " is %5.2f\n", c.area());
  
c.setRadius(5.0);
  
System.out.printf("The circumference of a circle of radius " + c.getRadius() + " is %5.2f\n", c.circumference());
System.out.printf("The area of a circle of radius " + c.getRadius() + " is %5.2f\n", c.area());
}//end main

}

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

Please find the code below:

Lab2Num1.java

package classes7;

public class Lab2Num1 {

   public static class Circle{

       private double radius;
       //your code goes here

       //provide default constructor, constructor with one parameter, area, and circumference
       public Circle() {
           radius = 0;
       }

       public Circle(double d) {
           radius = d;
       }

       public double getRadius() {
           return radius;
       }

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

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

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

   }
   public static void main(String[] args) {

       Circle c = new Circle(1.5);


       System.out.printf("The circumference of a circle of radius " + c.getRadius()+ " is %5.2f\n", c.circumference());
       System.out.printf("The area of a circle of radius " +c.getRadius()+ " is %5.2f\n", c.area());

       c.setRadius(5.0);

       System.out.printf("The circumference of a circle of radius " + c.getRadius() + " is %5.2f\n", c.circumference());
       System.out.printf("The area of a circle of radius " + c.getRadius() + " is %5.2f\n", c.area());
   }//end main

}

output:

Quick Access 6 . Java EE Java * Debug Java Brows @ Console X * B 1 R 9 - D o <terminated > Lab2Num1 [Java Application) C:\P

Add a comment
Know the answer?
Add Answer to:
I have given you a piece of code to test that your circle class works correctly....
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
  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are two parts for one code (one question) the two parts branch off, one part has 2 sheets of code, the other has 3 sheets of code, all are small code segments for one question. Pt.1 - 2 sheets of code: public class Computer { private String make; private String type; private String modelNumber; private double cpuSpeed_GHz; private int ramSize_GB; private int hardDriveSize_GB; private...

  • Write a java class definition for a circle object. The object should be capable of setting...

    Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { }    //get method (Accessor Methods ) public double getRadius (...

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

  • What is the output of the following code? public class Test { pub be static void...

    What is the output of the following code? public class Test { pub be static void main(String() args) { Object o 1 -new Object(); Object o2 = new Object(); System out.print((o1 = o2) + "" + (o1 equals(o2))); } } A. false false B. true true C. false true D. true false Analyze the following code. //Program 1: public class Test { public static void main(String[] args) { Object circle 1 = new Circle(); Object circle2 = new Circle(); System...

  • 2.1.1: Area of a Circle w/ Ra... SUBMIT CONTINUE RUN CODE TEST CASES ASSIGNMENT DOCS GRADE...

    2.1.1: Area of a Circle w/ Ra... SUBMIT CONTINUE RUN CODE TEST CASES ASSIGNMENT DOCS GRADE MORE Description Write a method that returns the area of a circle given the radius 1 import java.util.Scanner; 2. public class Main { 3. public static void main(String[] args) { Scanner io- new Scanner(System.in); System.out.println("Inout the radius of the circle: "); 6 double radius - io.nextDouble 7 System.out.println("Perimeter is -" + (2 radius - Math.PD)): 8 System.out.println("Area is - " . (Math.PI. radius radius)):...

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • What is wrong the following code? Explain. public class Test { public static void main(String[] args)...

    What is wrong the following code? Explain. public class Test { public static void main(String[] args) { A a = new A(5.0); } class A { int value = 2; } }

  • Design and implement class Circle to represent a circle object. The class defines the following attributes...

    Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1.      A private variable of type double named radius to represent the radius. Set to 1. 2.      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. 3.      Method getRadius() that returns the radius. 4.     ...

  • Below is the code from LE 6.2 that LE 7.1 is referring to. import java.util.Scanner; public...

    Below is the code from LE 6.2 that LE 7.1 is referring to. import java.util.Scanner; public class lastNameLE62 { private static Scanner in = new Scanner(System.in); private static int size; public static void arraySize() { System.out.printf("How many sports are you interested in? "); size = Integer.parseInt(in.nextLine()); } public static String[] setFavoriteSports() { String []favSports = new String[size]; for(int i=1; i<=size; i++) { System.out.printf("Enter sport #%d: ", i); favSports[i-1] = in.nextLine(); } return favSports; } public static String[] setFavoriteTeams(String[] sports) {...

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