Results 1 to 4 of 4
Thread: New to java - class not working
- 03-10-2011, 02:44 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
New to java - class not working
Hey guys, I'm extremely new to java, OOP in general, and these forums, so please bare with me. I created a circle class to use with my circle app, but for some reason its not calling the methods correctly. Im not sure if it is a problem with my app or my class. My app isnt completely done yet, but it's enough to get the program running.
Code for my App:
public class CircleApp
{
public static void main(String[] args)
{
//print to console
System.out.println("Welcome to the Circle Tester");
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
//get input from the user
double radius = Validator.getDouble(sc, "Enter radius: ");
Circle cir = new Circle(radius);
double circumference = cir.getCircumference();
double area = cir.getArea();
System.out.println(circumference);
System.out.println(area);
System.out.println("Would you like to continue?: y/n");
choice = sc.next();
sc.nextLine();
}
}
}
Code for class:
package Circle;
import java.util.Scanner;
import java.lang.Math;
import java.text.NumberFormat;
public class Circle
{
//the instance variables
private double radius;
private double circumference;
private double area;
//the constuctor
public Circle(double radius)
{
this.radius = radius;
circumference = 2 * Math.PI * radius;
area = Math.PI * Math.pow(radius,2);
}
//methods
public double getCircumference()
{
return circumference;
}
public String getFormattedCircumference()
{
NumberFormat number = NumberFormat.getNumberInstance();
number.setMaximumFractionDigits(2);
return number.format(this.getCircumference());
}
public double getArea()
{
return area;
}
public String getFormattedArea()
{
NumberFormat number = NumberFormat.getNumberInstance();
number.setMaximumFractionDigits(2);
return number.format(this.getArea());
}
private String formatNumber(double x)
{
NumberFormat number = NumberFormat.getNumberInstance();
number.setMaximumFractionDigits(2);
return number.format(x);
}
}
There is also a validator class, which I didn't show here. When I call the getCircumference() method, I get 0.0 as the result. Is there something wrong with where I'm coding my calculations in the class (in the constructor)? I'm a little confused about if I can code calculations in my accessor methods or not. Any help would be appreciated, thanks!
- 03-10-2011, 03:02 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Im not sure what validator is but your code looks fine, you aren't getting any errors, just incorrect results?
I copied your circle class code, added a toString method, and created a main method in the class, printing the circle, and printing the get cir and get area methods worked.
I got the following output for a circle with 10 as radius
Java Code:Enter a radius: 10 Circumference 62.83185307179586, area 314.1592653589793 62.83185307179586 314.1592653589793
Last edited by sunde887; 03-10-2011 at 03:08 AM.
- 03-10-2011, 03:39 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Hmm i see, here is my code for getdouble(), I have a feeling this might be where the problem lies. I call this method in the Circle App.
public static double getDouble(Scanner sc, String prompt)
{
double d = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine(); //discard any other data entered on the line.
}
return d;
}
- 03-10-2011, 03:46 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
java.system.class.loader Not Working
By kg1 in forum Advanced JavaReplies: 2Last Post: 12-06-2010, 09:41 PM -
inner class Mouse listener not working in MVC model
By Grant in forum AWT / SwingReplies: 2Last Post: 03-01-2010, 03:19 PM -
TextIO class not working
By trudy in forum New To JavaReplies: 4Last Post: 12-30-2009, 07:48 PM -
E:\IT 215 Java Programming\public class Inventory.java:39: class, interface, or enum
By tlouvierre in forum New To JavaReplies: 14Last Post: 05-28-2009, 05:44 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks