Famcraft

Author Topic: Java help  (Read 1977 times)

Offline eGI

  • Retired
  • Hero Member
  • *
  • Posts: 538
    • View Profile
Java help
« Opened on March 31, 2014, 12:34:39 AM (Edited March 31, 2014, 03:34:39 AM) »
So I'm working on a project for my programming class and I am completely stumped.  The point of the program is to be given the radius of a circle and to have the program compute the area, diameter, and circumference.  The program is required to use a constructor which is the part that has me completely stumped as I have everything, but no matter what I put in for the radius, the area, diameter, and circumference come out to 0. Any help would be much appreciated.

Here is what I have so far. (I have to have both Circle.java and CircleDemo.java to satisfy the requirements of the assignment)

Circle.java
Code: [Select]
public class Circle
{
    private double Radius;
    private final double Pi = 3.14159;
   
    public Circle()
    {
       
    }
   
    public double getRadius()
    {
        return Radius;
    }
   
    public double getArea()
    {
        return Pi * Radius * Radius;
    }
   
    public double getDiameter()
    {
        return Radius * 2;
    }
   
    public double getCircumference()
    {
        return Pi * 2 * Radius;
    }
   

}

CircleDemo.java
Code: [Select]
import java.util.Scanner;

public class CirlceDemo
{
    public static void main(String[] args)
    {
        double Radius;
        double Pi;
        double Area;
        double Diameter;
        double Circumference;
       
        Scanner keyboard = new Scanner(System.in);
       
        Circle r = new Circle();
     
        System.out.print("What is the radius of the circle? ");
        Radius = keyboard.nextDouble();
       
        System.out.println("The area of the circle is " + r.getArea());
        System.out.println("The diameter of the circle is " + r.getDiameter());
        System.out.println("The circumference of the circle is " + r.getCircumference());

    }
}

EDIT by Kealper: Added code into code tags.
Head Coach Middle School Cross Country and Track
Assistant Coach High School Cross Country and Track

I know running... and a few other things...

Offline 1869_Flame

  • Retired
  • Hero Member
  • *
  • Posts: 611
  • Home is where my dirt-hut is.
    • View Profile
Re: Java help
« Reply #1 on March 31, 2014, 02:59:32 AM (Edited March 31, 2014, 03:26:48 AM) »
though not in Java, but quickly made in C#, this might help (see attachements)

Quickly made a very simple c# program that will calculate the radius, circumference and area of a circle with a given diameter.
As C# looks a lot like Java, you might find this helpfull.

In case you dont have C#, you can use the free express one from microsoft, im using that one too. Link here and look for the "Visual C# 2010 Express"

Offline Kealper

  • Administrator
  • Hero Member
  • *
  • Posts: 1349
    • View Profile
  • Minecraft Name: Kealper
Re: Java help
« Reply #2 on March 31, 2014, 03:52:04 AM (Edited March 31, 2014, 04:03:22 AM) »
There were a few things wrong with the code, which I'll explain below.

The first problem was in both Circle.java and CircleDemo.java. In Circle.java, you never provided a way to initialize the private variable Radius, so it was always defaulting to 0.0 after making a new Circle(). To fix this, I've made Circle() require a radius parameter on initialization, which is a double. In the public method Circle, it sets its own instance of Radius to the parameter given at initialization.
In CircleDemo.java, the modification to that was to initialize a new Circle() after getting the user's input and setting it to Radius. The Radius variable is then passed as an argument to Circle() during initialization, to set that instance up properly.

The second issue is simpler, and was a typo when you defined CircleDemo.java's class, you put public class CirlceDemo;  :P

Fixed code for Circle.java:
Code: [Select]
public class Circle
{
    private double Radius;
    private final double Pi = 3.14159;

    public Circle(double r)
    {
        this.Radius = r;
    }

    public double getRadius()
    {
        return Radius;
    }

    public double getArea()
    {
        return Pi * Radius * Radius;
    }

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

    public double getCircumference()
    {
        return Pi * 2 * Radius;
    }


}

Fixed code for CircleDemo.java:
Code: [Select]
import java.util.Scanner;

public class CircleDemo
{
    public static void main(String[] args)
    {
        double Radius;
        double Pi;
        double Area;
        double Diameter;
        double Circumference;

        Scanner keyboard = new Scanner(System.in);

        System.out.print("What is the radius of the circle? ");
        Radius = keyboard.nextDouble();

        Circle r = new Circle(Radius);

        System.out.println("The area of the circle is " + r.getArea());
        System.out.println("The diameter of the circle is " + r.getDiameter());
        System.out.println("The circumference of the circle is " + r.getCircumference());

    }
}


EDIT: For full disclosure, I'm not a Java developer, the code above appears to compile and function on my computer with limited testing, so if it's actually incorrect, that's why :P
Also, if you haven't learned the "this" keyword in your class yet, then uhh... You should be able to remove that "this." part and just modify Radius as-is, but it's typically frowned upon to do that since in any of the object-oriented languages I've used, not specifying it can cause headaches later on, on more complex programs. But... Don't use that keyword if you haven't been taught it yet, unless you're allowed to get outside help on that project :P

Offline eGI

  • Retired
  • Hero Member
  • *
  • Posts: 538
    • View Profile
Re: Java help
« Reply #3 on March 31, 2014, 09:56:38 AM »
Thanks, that helped a lot.  When we went over constructors it was in the last ten minutes of a class and most people really had no idea what was going on as our professor just flew right through how to do them.
Head Coach Middle School Cross Country and Track
Assistant Coach High School Cross Country and Track

I know running... and a few other things...