Java Help

Yz388

Limp Gawd
Joined
Feb 18, 2005
Messages
197
Alright heres the deal. Use to know some Java but its been a while and now its coming back to haunt me. BTW: I am aware that you guys dont give out answers for programs to students. Im looking for help, someone who can let me know what im doing wrong. So calm down before you jump down my throat.

I needed to write a circle class that has the following fields:
radius- a double
PI- a final double intitialized with the value 3.14159

Should have constructor, setRadius, getRadius, getArea, getDiameter, getcirumference.

Then write a program that demonstrates the Circle class by asking the user for the circles radius, creating a Circle object, and then reporting the circles area, diameter, and circumference.

This is what I got (after too much time, like I said, its been awhile)


As you can see, i thought this may of been correct since it does what I wanted, but i forgot that its all in one file. Its calling itself in its own program. I tried creating the two files, where I would place the methods in one, and call them in the other, but its not working at all. This method seems to work, but its not efficient according to my sources. Is there a simple way of converting this to a class and then a program? grr

Thanks guys

import java.util.Scanner;
import java.text.DecimalFormat;



public class practice
{
public static void main(String[] args)
{
double radius, // To hold a radius
area; // To hold an area
double diameter; // To hold a diameter
double circumference; // To hold a circumference
String inputLine; // To hold a line of input
char choice; // To hold the user's choice
final double PI = 3.14159;


// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Create a DecimalFormat object for output formatting.
DecimalFormat formatter = new DecimalFormat("0.00");

// Process the data for one or more circles.
do
{
// Get the circle's radius.
System.out.print("Please enter the circle's radius: ");
radius = keyboard.nextDouble();

// Calculate and display the area.
area = Math.PI * radius * radius;
System.out.println("The area is " +
formatter.format(area));

// Calculate and display the diameter.
diameter = radius * 2;
System.out.println("The diameter is " +
formatter.format(diameter));

// Calculate and display the circumference.
circumference = 2 * Math.PI * radius;
System.out.println("The circumference is " +
formatter.format(circumference));


// Repeat this?
System.out.print("Would you like to " +
"enter different values and try again? (Y or N) ");
keyboard.nextLine(); // Consume the remaining newline.
inputLine = keyboard.nextLine();
choice = inputLine.charAt(0); // Get the first char.

} while (Character.toUpperCase(choice) == 'Y');
}
}
 
Maybe this will help kick things off. Start with two files:

Circle.java:
Code:
public class Circle {
    // Class implementation here
}

CircleTest.java:
Code:
public class CircleTest {
    public static void main(String[] args) {
        // Test code that creates a Circle here
    }
}
 
I'll be happy to help you but please put code around your code, I can't read it well like that, my eyes get lost.
 
Back
Top