TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Prime b
NA
810
346k
Circle class!
Mar 12 2012 5:22 PM
Seems like I am the only one who is posting in the java section =(
Well its not that I have the problems , I am just want you to look over my code and tell me what you think. You know, I just need a professional advice. I will delete the problem text so people wont cheat on the homework.
The problem:
Circle Class Write a Circle class that has the following fields: • radius: a double • PI: a final double initialized with the value 3.14159 The class should have the following methods: • Constructor. Accepts the radius of the circle as an argument. • Constructor. A no-arg constructor that sets the radius field to 0.0. • setRadius. A mutator method for the radius field. • getRadius. An accessor method for the radius field. • getArea. Returns the area of the circle, which is calculated as area = PI * radius * radius • getDiameter. Returns the diameter of the circle, which is calculated as diameter = radius * 2 • getCircumference. Returns the circumference of the circle, which is calculated as circumference = 2 * PI * radius Write a program that demonstrates the Circle class by asking the user for the circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference.
My solution:
THE CIRCLE CLASS
/*
* The Circle class hold data about a circle.
*
*/
/**
*
* @author Alexander Trowell
*/
public class Circle
{
// Fields
private double radius; // Radius of the circle
final private double PI = 3.14159; // The value of the PI
/**
* Constructor
*
* @param radius The circles radius
*/
public Circle(double radius)
{
this.radius = radius;
}
/**
* Constructor No arg constructor that the sets the radius field to 0.0
*/
public Circle()
{
radius = 0.0;
}
/**
* The setRadius method sets the Circles radius
* @param radius returns radius of the circle
*/
public void setRadius(double radius)
{
this.radius = radius;
}
/**
* The getRadius method
* @return the radius of the circle
*/
public double getRadius()
{
return radius;
}
/**
* The getArea method
* @return the area of the circle
*/
public double getArea()
{
return PI * radius * radius;
}
/**
* The getDiameter method
* @return the diameter of the circle
*/
public double getDiameter()
{
return radius * 2;
}
/**
* The getCircumference method
* @return the circumference of the circle
*/
public double getCircumference()
{
return 2 * PI * radius;
}
}
Main MEHOD
import java.util.Scanner;
/**
*
* @author Alexander Trowell
*/
public class CircleDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double radius; // To hold a radius
// Create a Scanner object for keyboard input
Scanner scan = new Scanner(System.in);
// Get the number for the radius
System.out.println("Please enter the circle's radius");
radius = scan.nextDouble();
// Create an instance of the Circle class,
// passing the data that was entered as arguments
// to the constructor.
Circle circle1 = new Circle(radius);
// Output
System.out.println("The area of the circle with radius of "+radius+" is "+ circle1.getArea()
+" with diameter of "+circle1.getDiameter()+" and with circumference of "+circle1.getCircumference());
}
}
Reply
Answers (
7
)
How to give transparency for JComboBox,JListBox and JTable ?
What is the major difference between Filter and servlet.