Results 1 to 4 of 4
  1. #1
    rrjava is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default 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!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    rrjava is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    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;
    }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    any reason for the validator class?

    You could just have the following
    Java Code:
    System.out.println("Enter number");
    double area = sc.nextDouble();
    In your code you never actually extract nextDouble

    hasNextDouble returns boolean not a number. nextDouble returns the actual double.

Similar Threads

  1. java.system.class.loader Not Working
    By kg1 in forum Advanced Java
    Replies: 2
    Last Post: 12-06-2010, 09:41 PM
  2. Replies: 2
    Last Post: 03-01-2010, 03:19 PM
  3. TextIO class not working
    By trudy in forum New To Java
    Replies: 4
    Last Post: 12-30-2009, 07:48 PM
  4. Replies: 14
    Last Post: 05-28-2009, 05:44 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •