11 Replies - 932 Views - Last Post: 24 November 2009 - 06:22 PM Rate Topic: -----

#1 le_feu_et_glace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 09

Value returning method

Post icon  Posted 24 November 2009 - 05:45 PM

hi im having some problems with this program i did the program and compile it but when i run it , it wont work can someone please tell me what i did wrong pleaseee :D

THE QUESTIONS ASK TO "Write a value-returning method that takes as parameters two double values and returns the sum of the values. Write a program to test your method."


here's my code

import java.util.*;
import java.io.*;
import java.lang.Math.*;

public class ch3_PrExercise3
{
	static Scanner console = new Scanner(System.in);

	public static double sum(double n1, double n2)
	{
		
		
		System.out.print("Enter n1: ");
		  n1 = console.nextDouble();
		  System.out.println();

		System.out.print("Enter n2: ");
		  n2 = console.nextDouble();
		  System.out.println();

		return n1 + n2;	
		
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Value returning method

#2 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Value returning method

Posted 24 November 2009 - 05:49 PM

You never declared your variables...

You have to use this
double n1, n2;



before or after your scanner statement

This post has been edited by theautokustomizer: 24 November 2009 - 05:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 le_feu_et_glace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 09

Re: Value returning method

Posted 24 November 2009 - 05:52 PM

View Posttheautokustomizer, on 24 Nov, 2009 - 04:49 PM, said:

Do you have a tester program made up yet?



NO not yet
Was This Post Helpful? 0
  • +
  • -

#4 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Value returning method

Posted 24 November 2009 - 05:54 PM

Do you need two programs? Or can you just have one?
Was This Post Helpful? 0
  • +
  • -

#5 le_feu_et_glace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 09

Re: Value returning method

Posted 24 November 2009 - 05:55 PM

View Posttheautokustomizer, on 24 Nov, 2009 - 04:54 PM, said:

Do you need two programs? Or can you just have one?



it wasnt specified but i belive one
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Value returning method

Posted 24 November 2009 - 05:56 PM

View Posttheautokustomizer, on 24 Nov, 2009 - 04:49 PM, said:

You never declared your variables...

You have to use this
double n1, n2;



before or after your scanner statement

Nah :P They are declared here:

public static double sum(double n1, double n2)
Was This Post Helpful? 0
  • +
  • -

#7 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Value returning method

Posted 24 November 2009 - 05:56 PM

Okie Dokey :)

Create a main method, and write your testing data in there.:)

This post has been edited by theautokustomizer: 24 November 2009 - 05:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 le_feu_et_glace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 09

Re: Value returning method

Posted 24 November 2009 - 06:01 PM

yea i declared them try it different ways and it still wont work

View Posttheautokustomizer, on 24 Nov, 2009 - 04:56 PM, said:

Okie Dokey :)

Create a main method, and write your testing data in there.:)



ok ill try that
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Value returning method

Posted 24 November 2009 - 06:03 PM

public class ch3_PrExercise3
{
	static Scanner console = new Scanner(System.in);

	public static double sum(double n1, double n2)
	{
		
		
		System.out.print("Enter n1: ");
		  n1 = console.nextDouble();
		  System.out.println();

		System.out.print("Enter n2: ");
		  n2 = console.nextDouble();
		  System.out.println();

		return n1 + n2;	
		
	}

	public static void main(String[] args) {
		double x, y;
		System.out.println("The sum is: " + sum(x, y));
   }
}



But Theautokustomizer confusion is justified
There is no need to define in a main method variable that will be use temporarly by a method
the code would be better that way
public class ch3_PrExercise3
{
	static Scanner console = new Scanner(System.in);

	public static double sum()
	{
		double n1, n2;
		
		System.out.print("Enter n1: ");
		  n1 = console.nextDouble();
		  System.out.println();

		System.out.print("Enter n2: ");
		  n2 = console.nextDouble();
		  System.out.println();

		return n1 + n2;	
		
	}

	public static void main(String[] args) {
		
		System.out.println("The sum is: " + sum());
   }
}


and a lot better that way
public class ch3_PrExercise3
{
	static Scanner console = new Scanner(System.in);

	public static double sum(double n1, double n2)
	{
		
		return n1 + n2;	
		
	}

	public static void main(String[] args) {
		double x, y;
		System.out.print("Enter n1: ");
		  x = console.nextDouble();
		  System.out.println();

		System.out.print("Enter n2: ");
		  y = console.nextDouble();
		  System.out.println();

		System.out.println("The sum of " + x + " and " + y + " is: " + sum(x, y));
   }
}


Was This Post Helpful? 1
  • +
  • -

#10 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Value returning method

Posted 24 November 2009 - 06:05 PM

It was the way the code was written that got me, but thank you for the clarification Tai Pan... :)

This post has been edited by theautokustomizer: 24 November 2009 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

#11 le_feu_et_glace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 09

Re: Value returning method

Posted 24 November 2009 - 06:09 PM

THANKS for helping me im new with the whole java thing so its really confusing and i was trying differently but thanks i understand what i was missing now

thank u
Was This Post Helpful? 0
  • +
  • -

#12 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Value returning method

Posted 24 November 2009 - 06:22 PM

Your welcome, and I do see that you are new. When people help you, it is a nice gesture to click the "This post was helpful" for the people who did. Some people care, others don't. And in the future if you have any questions feel free to post, just remember to follow the rules and regulations. TY
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1