14 Replies - 1872 Views - Last Post: 24 September 2008 - 04:29 PM Rate Topic: -----

#1 bwat47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 08

Lost in calling methods

Post icon  Posted 23 September 2008 - 07:01 PM

/* Problem4
 * Brandon Watkins
 * 10/23/08
 * 
 * This program prompts the user to input the center of a circle and a point ona  circle. It then calculates the radius, diameter, circumference, and area.
 */


// Import Classes Uncomment as needed
import java.util.*;
import java.lang.*;



public class Problem4
{
  
  static Scanner console = new Scanner(System.in);
// create scanner to obtain input from the command line
  
  public static void main(String[] args)
  {
	
	//variable declaration
	
	double x1;
	double x2;
	double y1;
	double y2;
	double distance;
	
   
   
	
	//executable statements
	
	// Prompts for first double and stores value in x1
	System.out.print ("Enter the value for the first point on the circle: ");
	x1 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in x2
	System.out.print ("Enter the second point on the circle: ");
	x2 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in y1
	System.out.print ("Enter the third point on the circle: ");
	y1 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in y2
	System.out.print ("Enter the fourth point on the circle: ");
	y2 = console.nextDouble();
	System.out.println();
	
	
  }
  
  public static double distance (double x1, double x2, double y1, double y2)
  {
	double distance;
	distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
	return distance;
  }

  public static double radius (double x1, double x2, double y1, double y2)
  { 
  double radius;
  distance (double x1, double x2, double y1, double y2);
  radius = distance;
  return radius;
  }


} 



  


This is what I have so far. I am stuck hardcore on the radius and distance methods.

I have to make a program that out puts a circle's radious, area, and circumference when it is given the center and a point on the circle.

I have make and use these 5 methods:

a. distance: This method takes as its parameters four numbers that represent two points in the plane and returns the distance between them.

b. radius: This method takes as its parameters four numbers that represent the center and a point on the circle, calls the method distance to find the radius of the ricle, returns the circles radius

c. This method takes as its parameter a number that represents the radius of the circle and returns the circles circumference.

d. area: distance: This method takes as its parameters a number that represents the radius of a circle and returns the circles area.

e. Use the named constant PI from the math class to specifi the value of pi

I am getting these errors. I am a complete noob and am lost. I have been working on this for hours.

6 errors found:
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java [line: 70]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:70: '.class' expected
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java [line: 70]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:70: ';' expected
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java [line: 70]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:70: <identifier> expected
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java [line: 70]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:70: not a statement
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java [line: 70]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:70: ';' expected
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java [line: 70]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:70: ';' expected

Please help me

This post has been edited by bwat47: 23 September 2008 - 07:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Lost in calling methods

#2 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: Lost in calling methods

Posted 23 September 2008 - 07:12 PM

from a very quick look i can tell you the following statement is wrong

distance (double x1, double x2, double y1, double y2);


it should be something like this

double distance = distance(x1, x2, y1, y2);


the reason is the 'distance' method is going to return a double value; hence you need to set a variable equal to the double that it returns

This post has been edited by Mach1Guy: 23 September 2008 - 07:13 PM

Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Lost in calling methods

Posted 23 September 2008 - 07:13 PM

You are on the right track, you just had a few problems in your radius function...

  public static double radius (double x1, double x2, double y1, double y2)
  {
	double radius;
	double distancevar; // Create your variable
	
	// Don't forget to set the variable, don't just call the function
	// Also notice that we do not specify the types of variables in the parameters when CALLING the function
	distancevar = distance (x1, x2, y1, y2); 
	radius = distancevar;
	return radius;
  }



You need to create the variable to hold the return of the function call. Here we created "distancevar" and also notice that we DO NOT use the data types when CALLING the function. Data types are only used in parameters when you are defining functions etc.

This should get you compiling and moving on the right path. Enjoy!

"At DIC we be radius calculating code ninjas... I calculate the distance between your left ear and right ear to be -1.42... wait, that can't be right." :snap:
Was This Post Helpful? 1
  • +
  • -

#4 eskimo9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-September 08

Re: Lost in calling methods

Posted 23 September 2008 - 07:18 PM

[quote name='bwat47' date='23 Sep, 2008 - 07:01 PM' post='422907']
/* Problem4

  
  public static double distance (double x1, double x2, double y1, double y2)
  {
	double distance;
	distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
	return distance;
  }

  public static double radius (double x1, double x2, double y1, double y2)
  { 
  double radius;
  distance (double x1, double x2, double y1, double y2);
  radius = distance;
  return radius;
  }


}


The reason you're getting all the errors on line 70 (I think thats the line that calls the distance method within the radius method) is because I think when you call a method from within another, you don't need to declare the input parameter variable types, as they have already been declared.

When you execute the radius method, it will request those input parameters (x1, x2, y1, y2), and they will be stored as instance variables which will then be used as the inputs for the distance method.

If radius is always going to = distance, why not declare a field variable, and set radius = distance?
Was This Post Helpful? 0
  • +
  • -

#5 bwat47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 08

Re: Lost in calling methods

Posted 23 September 2008 - 07:20 PM

Thank you so much Martyr2, I was trying to fix that for more than an hour and it was such a simple thing lol, thank you so much. I don't even wanna know how much longer I coulda sat their attempting to figure that out. it now compiles perfectly.

This post has been edited by bwat47: 23 September 2008 - 07:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: Lost in calling methods

Posted 23 September 2008 - 07:23 PM

bwat....just curious but what IDE are you using for your Java programming?
Was This Post Helpful? 0
  • +
  • -

#7 bwat47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 08

Re: Lost in calling methods

Posted 23 September 2008 - 07:26 PM

I am using drjava, I also have jcreator.
Was This Post Helpful? 0
  • +
  • -

#8 bwat47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 08

Re: Lost in calling methods

Posted 23 September 2008 - 08:26 PM

alright, I am so stuck again haha. I finished all the methods, but I can't get it to print the results. It prompts me to enter the points on the circles and then nothing happens. here is my code:

/* Problem4
 * Brandon Watkins
 * 10/23/08
 * 
 * This program prompts the user to input the center of a circle and a point ona  circle. It then calculates the radius, diameter, circumference, and area.
 */


// Import Classes Uncomment as needed
import java.util.*;
import java.lang.*;



public class Problem4
{
  
  static Scanner console = new Scanner(System.in);
  public static final double PI = 3.14159;
// create scanner to obtain input from the command line
  
  public static void main(String[] args)
  {
	
	//variable declaration
	
	double x1;
	double x2;
	double y1;
	double y2;
	double distance;
	
   
	
	//executable statements
	
	// Prompts for first double and stores value in x1
	System.out.print ("Enter the value for the first point on the circle: ");
	x1 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in x2
	System.out.print ("Enter the second point on the circle: ");
	x2 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in y1
	System.out.print ("Enter the third point on the circle: ");
	y1 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in y2
	System.out.print ("Enter the fourth point on the circle: ");
	y2 = console.nextDouble();
	System.out.println();
	
	
	
	
  }
  
  public static double distance (double x1, double x2, double y1, double y2)
  {
	double distance;
	distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
	return distance;
  }

  public static double radius (double x1, double x2, double y1, double y2)  
  {  
   double radius;  
   double distancevar;  
   distancevar = distance (x1, x2, y1, y2);   
   radius = distancevar;  
   return radius;  
  }  
  
  
  
  public static double circumference (double radius)
  {
   double circumference;
   circumference = (2 * radius) * PI;
   return circumference;
	
  }
  
  public static double area (double radius)
  {
   double area;
   area = PI * (radius * radius);
   return area;
	
	
  }
  public static void printarea (double radius, double area, double circumference)
  {
	System.out.println ("The Radius is " + radius);
	System.out.println ("area: " + area);
	System.out.println ("circumference: " + circumference);
	
  }


} 



  


This is what happens when I enter my numbers:

Welcome to DrJava. Working directory is C:\Users\Brandon\Documents\Dr Java\Week4
> java Problem4
Enter the value for the first point on the circle: [DrJava Input Box]

Enter the second point on the circle: [DrJava Input Box]

Enter the third point on the circle: [DrJava Input Box]

Enter the fourth point on the circle: [DrJava Input Box]

>

please help, I'm so close to being finished!

This post has been edited by bwat47: 23 September 2008 - 08:31 PM

Was This Post Helpful? 0
  • +
  • -

#9 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: Lost in calling methods

Posted 23 September 2008 - 09:35 PM

the methods are never are being called that you have created. you must call the methods that you created from the main method that you have. right now you have, public static void main(String[] args), method. and everything is getting executed from that method, so now you just need to include the calls to the other methods within this method

only the code in the main() method is going to get executed when you start the program. the other methods must be called in order to be executed

This post has been edited by Mach1Guy: 23 September 2008 - 09:54 PM

Was This Post Helpful? 0
  • +
  • -

#10 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: Lost in calling methods

Posted 23 September 2008 - 09:51 PM

	//variable declaration
	
	double x1;
	double x2;
	double y1;
	double y2;
	// double distance; remove this variable (delete this line)
	double radius; //add this variable
	double circumference; //add this variable
	double area; //add this variable
	
	//executable statements
	
	// Prompts for first double and stores value in x1
	System.out.print ("Enter the value for the first point on the circle: ");
	x1 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in x2
	System.out.print ("Enter the second point on the circle: ");
	x2 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in y1
	System.out.print ("Enter the third point on the circle: ");
	y1 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in y2
	System.out.print ("Enter the fourth point on the circle: ");
	y2 = console.nextDouble();
	System.out.println();
	
	//Make the calls to the methods that you created
	radius = radius(x1,x2,y1,y2);
	circumference = circumference(radius);
	area = area(radius);
	printarea(radius,area,circumference);
 


just another quick note...i'm not exactly sure on the math on all this so i can't check you there, but i'm suspicious of the distance and radius method

   distancevar = distance (x1, x2, y1, y2);   
   radius = distancevar;  
   return radius;  



from the code above, essentially your radius is getting set to whatever you are calculating for distance because you are saying:
radius = whatever is returned from your distance method
Was This Post Helpful? 1
  • +
  • -

#11 bwat47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 08

Re: Lost in calling methods

Posted 24 September 2008 - 02:17 PM

Yeah distance between those two points is supposed to be the raduis (or so it says in the book)

I am still at a loss of how to get it to print any results though, I tried to make a method that would call the others and print their values but I get errors. Here's what I have right now:

/* Problem4
 * Brandon Watkins
 * 10/23/08
 * 
 * This program prompts the user to input the center of a circle and a point ona  circle. It then calculates the radius, diameter, circumference, and area.
 */


// Import Classes Uncomment as needed
import java.util.*;
import java.lang.*;



public class Problem4
{
  
  static Scanner console = new Scanner(System.in);
  public static final double PI = 3.14159;
// create scanner to obtain input from the command line
  
  public static void main(String[] args)
  {
	
	//variable declaration
	
	double x1;
	double x2;
	double y1;
	double y2;
	double distance;
	double radius;
	double area;
	double circumference;
	
	
   
	
	//executable statements
	
	
	
	
	
	// Prompts for first double and stores value in x1
	System.out.print ("Enter the value for the first point on the circle: ");
	x1 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in x2
	System.out.print ("Enter the second point on the circle: ");
	x2 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in y1
	System.out.print ("Enter the third point on the circle: ");
	y1 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in y2
	System.out.print ("Enter the fourth point on the circle: ");
	y2 = console.nextDouble();
	System.out.println();
	
	printarea (radius, area, circumference);
	
	
   
	
	
  } // end main
  
  public static double distance (double x1, double x2, double y1, double y2)
  {
	double distance;
	distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
	return distance;
  }

  public static double radius (double x1, double x2, double y1, double y2)  
  {  
	double radius;  
	double distancevar;  
	distancevar = distance (x1, x2, y1, y2);   
	radius = distancevar;  
	return radius;  
  }  
  
  
  
  public static double circumference (double radius)
  {
	double circumference;
	circumference = (2 * radius) * PI;
	return circumference;
	
  }
  
  public static double area (double radius)
  {
	double area;
	area = PI * (radius * radius);
	return area;
	
	
  }
  public static void printarea (double radius, double area, double circumference)
  {

	
	System.out.println ("radius: " + radius (x1, x2, y1, y2));
	System.out.println ("area: " + area (radius));
	System.out.println ("circumference: " + circumference (radius));
	
  }


} 



Here's the errors I get when I compile:

4 errors found:
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java  [line: 111]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:111: cannot find symbol
symbol  : variable x1
location: class Problem4
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java  [line: 111]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:111: cannot find symbol
symbol  : variable x2
location: class Problem4
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java  [line: 111]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:111: cannot find symbol
symbol  : variable y1
location: class Problem4
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java  [line: 111]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:111: cannot find symbol
symbol  : variable y2
location: class Problem4


Was This Post Helpful? 0
  • +
  • -

#12 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: Lost in calling methods

Posted 24 September 2008 - 04:03 PM

look at the post directly above your last post....i included the calls to all your methods at the very bottom of it (you have to scroll down). i included everything you need in your main method in the first code box above

you do have the correct idea above though...almost there :)
let me know when you get it or if you have more problems...good luck, you can also IM me if needed

This post has been edited by Mach1Guy: 24 September 2008 - 04:05 PM

Was This Post Helpful? 0
  • +
  • -

#13 bwat47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 08

Re: Lost in calling methods

Posted 24 September 2008 - 04:14 PM

Oh I didn't scroll down far enough haha. That really helped me understand thanks.

for some reason it is giving me an error about the x and y variables still though which i don't quite get.

/* Problem4
 * Brandon Watkins
 * 10/23/08
 * 
 * This program prompts the user to input the center of a circle and a point ona  circle. It then calculates the radius, diameter, circumference, and area.
 */


// Import Classes Uncomment as needed
import java.util.*;
import java.lang.*;



public class Problem4
{
  
  static Scanner console = new Scanner(System.in);
  public static final double PI = 3.14159;
// create scanner to obtain input from the command line
  
  public static void main(String[] args)
  {
	
	
	//variable declaration
	
	double x1;
	double x2;
	double y1;
	double y2;
	
	// double distance; remove this variable (delete this line)
	double radius; //add this variable
	double circumference; //add this variable
	double area; //add this variable
	
	//executable statements
	
	// Prompts for first double and stores value in x1
	System.out.print ("Enter the value for the first point on the circle: ");
	x1 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in x2
	System.out.print ("Enter the second point on the circle: ");
	x2 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in y1
	System.out.print ("Enter the third point on the circle: ");
	y1 = console.nextDouble();
	System.out.println();
	
	// Prompts for first double and stores value in y2
	System.out.print ("Enter the fourth point on the circle: ");
	y2 = console.nextDouble();
	System.out.println();
	
	//Make the calls to the methods that you created
	radius = radius(x1,x2,y1,y2);
	circumference = circumference(radius);
	area = area(radius);
	printarea(radius,area,circumference);
	
	
   
	
	
  } // end main
  
  public static double distance (double x1, double x2, double y1, double y2)
  {
	double distance;
	distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
	return distance;
  }

  public static double radius (double x1, double x2, double y1, double y2)  
  {  
	double radius;  
	double distancevar;  
	distancevar = distance (x1, x2, y1, y2);   
	radius = distancevar;  
	return radius;  
  }  
  
  
  
  public static double circumference (double radius)
  {
	double circumference;
	circumference = (2 * radius) * PI;
	return circumference;
	
  }
  
  public static double area (double radius)
  {
	double area;
	area = PI * (radius * radius);
	return area;
	
	
  }
  public static void printarea (double radius, double area, double circumference)
  {

	
	System.out.println ("radius: " + radius (x1, x2, y1, y2));
	System.out.println ("area: " + area (radius));
	System.out.println ("circumference: " + circumference (radius));
	
  }


} 



  


here is the erros:

4 errors found:
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java [line: 110]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:110: cannot find symbol
symbol : variable x1
location: class Problem4
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java [line: 110]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:110: cannot find symbol
symbol : variable x2
location: class Problem4
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java [line: 110]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:110: cannot find symbol
symbol : variable y1
location: class Problem4
File: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java [line: 110]
Error: C:\Users\Brandon\Documents\Dr Java\Week4\Problem4.java:110: cannot find symbol
symbol : variable y2
location: class Problem4

This post has been edited by bwat47: 24 September 2008 - 04:20 PM

Was This Post Helpful? 0
  • +
  • -

#14 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: Lost in calling methods

Posted 24 September 2008 - 04:26 PM

	System.out.println ("The Radius is " + radius);
	System.out.println ("area: " + area);
	System.out.println ("circumference: " + circumference);
 


go back to your original code that you had in the printarea method as noted above

the reason it is complaining is because when you defined those variables (x1,x2,y1,y2), you did so in the main method. therefore there scope is only going to be within that method (not recognized outside of it). to allows those variables to be used in the printarea method you would have to pass them in as arguments to the method (as you did with the other variables such as radius, area, and circumference)

i believe you should just go back to your original code for printarea as noted above

p.s. you can remove the following line from your code because it's not being used:
import java.lang.*;

This post has been edited by Mach1Guy: 24 September 2008 - 04:29 PM

Was This Post Helpful? 0
  • +
  • -

#15 bwat47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 08

Re: Lost in calling methods

Posted 24 September 2008 - 04:29 PM

thank you! works perfectly!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1