215k views
5 votes
Program 7 - Circle You write ALL the code, then run it - Produce the correct output. Turn in code and screen print of successful run, for credit * Write a class for a Circle * Input only the radius. * Write functions that Calculate the circles Circumference, Area and Diameter, and print out the value of the radius * Include error checking for radius, must be greater than zero. Test all combinations

User Aown Raza
by
5.5k points

1 Answer

3 votes

Answer:

Here is the Circle class:

public class Circle { // class definition

private int radius; // private data member of type int of class Circle named radius that stores the value of radius

public Circle(int r) { // parameterized constructor of class Circle that takes radius as argument

radius = r; } // sets value of radius as r

public int getRadius() { // accessor method to get the value of radius

return radius; } // returns the current value of radius

public int Diameter() { // method to compute diameter of a circle

return radius * 2; } // multiply radius value by 2 to compute diameter of Circle

public double Area() { // method to compute area of a circle

return Math.PI * Math.pow(radius, 2); } //the formula of area is

π x radius² where value of π is get using Math.PI

public double Circumference() { // // method to compute circumference of a circle

return 2* Math.PI * radius; } } //the formula of circumference is

2 x π x radius where value of π is get using Math.PI

Step-by-step explanation:

Here is the Main class:

import java.util.Scanner; //to accept input from user

public class Main { //definition of Main class

public static void main(String[] args) { //start of main method

Scanner scanner = new Scanner (System.in); //creates Scanner object to take input from user

System.out.println("Enter radius: "); //prompts user to enter radius

int r = scanner.nextInt(); //reads the value of radius from user

Circle c = new Circle(r); // calls Constructor of Circle passing r as argument to it using the object c of class Circle

if(c.getRadius()<=0){ //calls getRadius method to get current value of radius using objec and checks if this value (input value of r ) is less than or equal to 0

System.out.println("Error!"); } //when above if condition evaluates to true then print this Error! message

else { //if the value of radius is greater than 0

System.out.println("the radius of this Circle is: " +c.getRadius()); //calls getRadius method to return current value of r (input value by user)

System.out.println("the diameter of this Circle is: " + c.Diameter()); //calls Diameter method to compute the diameter of Circle and display the result on output screen

System.out.printf("the circumference of this Circle is: %.2f", c.Circumference()); //calls Circumference method to compute the Circumference of Circle and display the result on output screen

System.out.println(); //prints a line

System.out.printf("the Area of this Circle is: %.2f", c.Area()); } } }

//calls Area method to compute the Area of Circle and display the result on output screen

The program and its output is attached.

Program 7 - Circle You write ALL the code, then run it - Produce the correct output-example-1
Program 7 - Circle You write ALL the code, then run it - Produce the correct output-example-2
User Aeldron
by
6.1k points