EDIT: All Fixed
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
EDIT: All Fixed
Last edited by LoganC; September 27th, 2012 at 10:38 PM.
Are you sure? What does your test run show? A sample run would show: Area of Circle is 1.0
For your code sample take the role of the computer running your program, and go through it.
(init happens)...
main is called..
(some variabels declared)...
double circOutput; //declared
(more variable work)...
circOutput = 1; //value set
(more work, 4 numbers input)...
System.out.print("Area of Circle is "); // Area of Circle is
System.out.println(circOutput); // 1.0 //value read
Yes I mistyped that. It is showing up as Area of circle is 1.0 but that is not the correct answer, i have circOutput set to an equation that shouldn't equal zero. Im confused on what to do and why circOutput is equalling zero.
You have a parameter named circOutput in a method that is never called and never used. But that localized variable is only visible within the method and shadows the visibility of your instance variable with the same name.
Read my last post. Do like it says and play the role of a computer running the program. Step through the program one line at a time and see what takes place.
In my previous post I stepped through your program and made a list of notes as I went showing you what happens and why 1.0 is printed every time. I did not explain why, but that does show you the problem.
I'd like to recommend looking into scope and visibility. I think you should try looking at this website and hopefully it'll solve your problem. Scope and Visibility in Java
*edit* Well I guess I took too long trying to type up a response. Pretty much listen to jps
I believe I am close, now I just cant get main to read what theAnswer is being computed as. The calculations work, but the area is dependant on what theAnswer is set to instead of what the user inputs.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package exercisefiveone; /** * * @author student */ import java.util.Scanner; public class Exercisefiveone { /** * @param args the command line arguments */ public static void main(String[] args) { double dist = distance (1.0, 2.0, 4.0, 6.0); double area = area (1.0, 2.0, 4.0, 6.0); double radius; double theAnswer; theAnswer = 1; Scanner reader = new Scanner (System.in); System.out.println("Radius of Circle?"); radius = reader.nextInt(); System.out.println(area(theAnswer)); area (radius); } public static double distance (double x1, double y1, double x2, double y2) { double dx = x2 - x1; double dy = y2 - y1; double dsquared = dx*dx + dy*dy; double result = Math.sqrt (dsquared); return result; } public static double area (double dx, double yc, double xp, double yp) { double radius = distance (dx, yc, xp, yp); double circArea = Math.PI*radius; return circArea; } public static double area (double radius) { double theAnswer = Math.PI*radius; return theAnswer; } }
Why exactly are you using theAnswer in the main? Think about what the input is for your area method. When you tested for your other methods, how did you test for them? Did you try printing out area(circArea)? or did you try something different?
I am using theAnswer in main for the output at the bottom of main. Which area method are you referring to? I have two.