Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 25, 2008, 07:07 PM   #1
soccersquirt82
macrumors 6502
 
Join Date: Mar 2008
Number doubling program

I get an error: "The local variable y may not have been initialized." How would I initialize it?

[code]
package numberdoub;
import java.util.*;
public class Numberdoub
{
static double redouble (double x, double y){

//equation
while (y < 1000){
y = 2*x;}
return x;
}

public static void main(String[] args) {

//defines variable
double x, y;

Scanner sc = new Scanner (System.in);

//user input
System.out.print("x = ");
x = sc.nextDouble();

//answers
System.out.println("The answer is: " + y);
}
}
__________________
 Common sense is the collection of prejudices acquired by age eighteen 
soccersquirt82 is offline   0 Reply With Quote
Old Aug 25, 2008, 07:07 PM   #2
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
y = 0;
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote
Old Aug 25, 2008, 07:22 PM   #3
kpua
macrumors 6502
 
Join Date: Jul 2006
The problem is, you never set y to anything in main() before you print it out. What do you expect the program to print out there?
kpua is offline   0 Reply With Quote
Old Aug 25, 2008, 07:34 PM   #4
soccersquirt82
Thread Starter
macrumors 6502
 
Join Date: Mar 2008
That would make the answer 0 which isn't what I want.
__________________
 Common sense is the collection of prejudices acquired by age eighteen 
soccersquirt82 is offline   0 Reply With Quote
Old Aug 25, 2008, 07:39 PM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
I think i understand what you're supposed to do, which is read in a value from the user, and if that number is less than 1000, double it. If the result is less than 1000, double it again, and so on. Once you've gotten a value that is greater than or equal to 1000, display it.

Your redouble function only needs to take one argument, the original value.

You're pretty close on this one. After you've read a number from your scanner, you just need to pass it to your function.

Your function to do the redoubling is almost there, but it needs some tweaks. It only needs to deal with a single value that will eventually be returned.

In your main function, you can either print the result of that thing, or store it and print that.

-Lee
lee1210 is offline   0 Reply With Quote
Old Aug 25, 2008, 08:19 PM   #6
soccersquirt82
Thread Starter
macrumors 6502
 
Join Date: Mar 2008
I tried to only have one variable, but then the answer is the same as what I put in. Here is the code:
[code]
package numberdoub;
import java.util.*;
public class Numberdoub
{
static double redouble (double x){

//rd method
while (x < 1000){
x = 2*x;}
return x;
}

public static void main(String[] args) {

//defines variable
double x;

Scanner sc = new Scanner (System.in);

//user input
System.out.print("x = ");
x = sc.nextDouble();

//answers
System.out.println("The answer is: " + x);
}
}
__________________
 Common sense is the collection of prejudices acquired by age eighteen 
soccersquirt82 is offline   0 Reply With Quote
Old Aug 25, 2008, 09:15 PM   #7
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by soccersquirt82 View Post
I tried to only have one variable, but then the answer is the same as what I put in. Here is the code:
[code]
package numberdoub;
import java.util.*;
public class Numberdoub
{
static double redouble (double x){

//rd method
while (x < 1000){
x = 2*x;}
return x;
}

public static void main(String[] args) {

//defines variable
double x;

Scanner sc = new Scanner (System.in);

//user input
System.out.print("x = ");
x = sc.nextDouble();

//answers
System.out.println("The answer is: " + x);
}
}
redouble looks great. Now you need to use it. You never call redouble. Just because you called the variable in redouble x, and you have an x in main, doesn't mean they are the same variable. You will need to call redouble from main, and pass it main's x. What it returns will be your answer, so you'll need to do something with it.

-Lee
lee1210 is offline   0 Reply With Quote
Old Aug 25, 2008, 09:23 PM   #8
soccersquirt82
Thread Starter
macrumors 6502
 
Join Date: Mar 2008
I think I used redouble in the right way. The answer is still the same, though.
[code]
package numberdoub;
import java.util.*;
public class Numberdoub
{
static double redouble (double x){

//rd method
while (x < 1000){
x = 2*x;}
return x;
}

public static void main(String[] args) {

double redouble = 0;
double x;

Scanner sc = new Scanner (System.in);

//user input
System.out.print("x = ");
redouble = sc.nextDouble();

//answers
System.out.println("The answer is: " + x);
}
}
__________________
 Common sense is the collection of prejudices acquired by age eighteen 
soccersquirt82 is offline   0 Reply With Quote
Old Aug 25, 2008, 09:46 PM   #9
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by soccersquirt82 View Post
I think I used redouble in the right way. The answer is still the same, though.
[code]
package numberdoub;
import java.util.*;
public class Numberdoub
{
static double redouble (double x){

//rd method
while (x < 1000){
x = 2*x;}
return x;
}

public static void main(String[] args) {

double redouble = 0;
double x;

Scanner sc = new Scanner (System.in);

//user input
System.out.print("x = ");
redouble = sc.nextDouble();

//answers
System.out.println("The answer is: " + x);
}
}
I know for sure we went over this before. =)
You don't need to declare the type of functions in java. this is not fortran.
You were assigning sc.nextDouble() to x. That was good. You then need to call the function redouble on x, and do something with its result. Calling a function that returns a value looks like:
Code:
double myResult = myDoubleFunction(myArgument);
-Lee
lee1210 is offline   0 Reply With Quote
Old Aug 25, 2008, 09:56 PM   #10
soccersquirt82
Thread Starter
macrumors 6502
 
Join Date: Mar 2008
Like this?

[code]
package numberdoub;
import java.util.*;
public class Numberdoub
{
static double redouble (double x){

//rd method
while (x < 1000){
x = 2*x;}
return x;
}

public static void main(String[] args) {

double redouble = 0;
double x;

Scanner sc = new Scanner (System.in);

//user input
System.out.print("x = ");
redouble = sc.nextDouble();

x = redouble(x);

//answers
System.out.println("The answer is: " + redouble(x));
}
}
__________________
 Common sense is the collection of prejudices acquired by age eighteen 
soccersquirt82 is offline   0 Reply With Quote
Old Aug 25, 2008, 10:01 PM   #11
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by soccersquirt82 View Post
Like this?

[code]
package numberdoub;
import java.util.*;
public class Numberdoub
{
static double redouble (double x){

//rd method
while (x < 1000){
x = 2*x;}
return x;
}

public static void main(String[] args) {

double redouble = 0;
double x;

Scanner sc = new Scanner (System.in);

//user input
System.out.print("x = ");
redouble = sc.nextDouble();

x = redouble(x);

//answers
System.out.println("The answer is: " + redouble(x));
}
}
Very nearly. For one, get rid of double redouble = 0;
Next, you need to assign the value from your Scanner to what you're going to pass to redouble.
Lastly, you only need to call redouble on x once. Now you're doing it, and assigning the result to x (so x has the result from redouble in it), then you're calling it again in your print statement. You can do it in one or the other. In this case it won't matter, because calling redouble on something over 1000 should result in the same value as you passed in, in general you want to call a function like this only once.

-Lee
lee1210 is offline   0 Reply With Quote
Old Aug 25, 2008, 10:09 PM   #12
soccersquirt82
Thread Starter
macrumors 6502
 
Join Date: Mar 2008
IT WORKS!!!! YAY!! Thanks so much. Just one more to go!
[code]
package numberdoub;
import java.util.*;
public class Numberdoub
{
static double redouble (double x){

//rd method
while (x < 1000){
x = 2*x;}
return x;
}

public static void main(String[] args) {

double x;

Scanner sc = new Scanner (System.in);

//user input
System.out.print("x = ");
x = sc.nextDouble();

//answers
System.out.println("The answer is: " + redouble(x));
}
}
__________________
 Common sense is the collection of prejudices acquired by age eighteen 
soccersquirt82 is offline   0 Reply With Quote
Old Aug 25, 2008, 10:17 PM   #13
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by soccersquirt82 View Post
IT WORKS!!!! YAY!! Thanks so much. Just one more to go!
[code]
package numberdoub;
import java.util.*;
public class Numberdoub
{
static double redouble (double x){

//rd method
while (x < 1000){
x = 2*x;}
return x;
}

public static void main(String[] args) {

double x;

Scanner sc = new Scanner (System.in);

//user input
System.out.print("x = ");
x = sc.nextDouble();

//answers
System.out.println("The answer is: " + redouble(x));
}
}
Great! =)
One tip, though... what if x is entered as -2? I am not sure if you're supposed to consider this, but in reality it is very important, as no matter how many times you double a negative number, it will never get over 1000 (unless there is an overflow, but i believe java will die with an exception instead of overflowing).

There is a Math.abs() function that will give you the absolute value, otherwise it's pretty easy to fake yourself.

-Lee

Edit: I was wrong, even for integers Java allows for +/-Infinity. So this will lead to an infinite loop instead of a crash condition, which is probably worse depending on who you ask.

Last edited by lee1210; Aug 25, 2008 at 10:25 PM.
lee1210 is offline   0 Reply With Quote
Old Aug 26, 2008, 02:07 AM   #14
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
Quote:
Originally Posted by soccersquirt82 View Post
That would make the answer 0 which isn't what I want.
No it wouldn't. When you declare a variable such as:

double y;

you have no idea what it contains. It is garbage, so what you do is initialise the variable so that it contains a safe value like so:

double y = 0;

that way you know what it contains from the start. Then you just put the new value that you want it to contain in as normal. It is good practice to always initialise variables to a safe value when you declare them.
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How to find out program serial number through Terminal? kathrynk MacBook Pro 3 Nov 26, 2013 06:50 PM
iPad: RetinaPad not doubling some images cclloyd Jailbreaks and iOS Hacks 2 Mar 4, 2013 04:22 PM
I cannot stand the pixel doubling on the retina! curtisvadnais MacBook Pro 40 Dec 27, 2012 09:56 PM
Doubling our efforts on secrecy ... davidgnomo iPhone 79 Sep 25, 2012 12:00 AM
Pixel Doubling apolloa MacBook Pro 51 Jun 12, 2012 08:57 PM

Forum Jump

All times are GMT -5. The time now is 08:35 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC