Hey all,
I'm new to Java and am attempting to write my first program that will determine the circumference of a circle with a given radius. The user will enter only a 'double' number as there is no error handling in this program - it's mainly to get me comfortable using helper classes, objects and methods as well as variable handling. Both classes compile, but it doesn't appear as if the radius is being calculated inside the circumference formula. Here is the code I've written so far:
import java.util.Scanner; /** * Write a description of class Circumerence here. * * @author me * @version 1.0 */ public class Circumerence { public static void main(String[] args) { double radiusInput = 0.0; Scanner keyInput = new Scanner(System.in); CircleFormula cf1 = new CircleFormula(); System.out.print("Enter a radius: "); radiusInput = keyInput.nextDouble(); System.out.println("The circumerence of a circle with a radius of " + radiusInput + " is " + cf1.getRadius()); } }
/** * Write a description of class CircleFormulas here. * * @author me * @version 1.0 */ public class CircleFormula { // radiusInput holds the radius private double userRadius; // circleOutput holds the circumference private double circumferenceOut; /** * Default constructor for CircleFormula sets the default * value of the userRadius object to 0.0. */ public CircleFormula() { // initialise instance variables this(0.0); } /** * An example of a method - replace this comment with your own * * This formula will calculate the circumerence of a circle, given a radius. */ public CircleFormula(double radius) { // put your code here setRadius(radius); } /** * The setRadius method sets the radius then calculates the circumerence * of the circle. */ public void setRadius(double radius) { radius = userRadius; circumferenceOut = 2 * 3.14 * radius; } public double getRadius() { return circumferenceOut; } }
I have a feeling I've confused variables and objects but I think I'm thoroughly confused now. If anyone has some pointers of where I might start looking to debug this, that'd be awesome. Not looking for someone to fix it completely, because I want to learn what I've done wrong. :-) Thank you!