PDA

View Full Version : help with class


raptrex
05-02-2009, 09:51 PM
so i made a circle class that sets the radius, and should output the radius, circumference and area however, it doesnt
heres my Circle.java

public class Circle
{
private double radius;
private double PI = 3.14159;

public void setRadius(double rad)
{
radius = rad;
}

public double getRadius()
{
return radius;
}

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

public double getDiameter()
{
return 2 * radius;
}

public double getCircumference()
{
return 2 * PI * radius;
}

}

and heres circledemo.java

import java.util.Scanner;
public class circledemo
{
public static void main(String[] args)
{
mainMenu();
}

public static void mainMenu()
{
boolean flag = true;
boolean checkOne = false;
int option;
double radius;
Scanner keyboard = new Scanner(System.in);
while (flag == true)
{
System.out.println("\nCircle");
System.out.println("\n1.\tSet Radius");
System.out.println("2.\tGet the Radius");
System.out.println("3.\tCompute the Area of the circle");
System.out.println("4.\tCompute the Diameter");
System.out.println("5.\tCompute the Circumference");
System.out.println("6.\tExit");
System.out.print("\nSelect an option: ");
option = keyboard.nextInt();

Circle one = new Circle();

switch(option)
{
case 1:
System.out.print("Enter the radius: ");
radius = keyboard.nextDouble();
one.setRadius(radius);
checkOne = true;
break;

case 2:
if (checkOne == false)
System.out.println("Error: Do option 1 first");
else
{
System.out.println("The radius is " + one.getRadius());
}
break;

case 3:
if (checkOne == false)
System.out.println("Error: Do option 1 first");
else
{
System.out.println("The area is " + one.getArea());
}
break;

case 4:
if (checkOne == false)
System.out.println("Error: Do option 1 first");
else
{
System.out.println("The diameter is " + one.getDiameter());
}
break;

case 5:
if (checkOne == false)
System.out.println("Error: Do option 1 first");
else
{
System.out.println("The circumference is " + one.getCircumference());
}
break;

case 6:
System.out.println("Thanks for using the Circle - good Bye!");
flag = false;
break;
}//switch

}//while
}//mainMenu
}//end program

shyam
05-03-2009, 02:31 PM
so i made a circle class that sets the radius, and should output the radius, circumference and area however, it doesnt
heres my Circle.java
what doesn't work? remember not everyone has an environment to run ur programs...btw, you should try initializing ur circle outside the while loop

raptrex
05-04-2009, 12:34 AM
initializing circle outside the while loop fixed the problem