File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java Assignment Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Assignment Problem" Watch "Java Assignment Problem" New topic
Author

Java Assignment Problem

taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
Hi everyone first time poster here .
Im having an error on my java assignment i have most of the program right but I cant figure out how to fix the errors i have. any help would be appreciated.

The errors im getting are in the Dialog File.

errors:
error: variable sum might not have been initialized
return sum;
^
error: unreachable statement
System.exit(0); // Terminate
^\
error: missing return statement
}
^
3 errors
1st file. I believe this one is correct and i dont have any errors in it.

Fibonacci.java
-------------------------


This is the second file. This is the one im getting errors for.
FibonacciJDialog.java

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

There aren't any lines that assign a value to sum so it is right. Perhaps you are missing a line of code?

There are a couple of problems for the second one:
1) Check the return type of your main method against a book.
2) Main methods don't typically return a value
3) When the main method ends, the program ends. So you don't need a System.exit call there anyway.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
Thanks for the help. The only remaining error is the return sum not being initialized. I have to use the coding Fibonacci.java. It was supplied by the instructor so i dont understand what i can do with sum. Iv'e been trying everything i know to get this program to work. Im also a noob to java tbh.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2273
    
  28

What are you trying to do here? Show the sum of Fibonacci numbers in the dialog? Shouldn't you call the fib function from your main function? Did you want to put the return value of fib function into the sum variable before calling the dialog?
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
The Fibonacci.java file is what we are supposed to use provided by the Professor. So i guess i would have to put the return value of fib function into the sum variable.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2273
    
  28

Right. Try it
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
The problem is I've never had to do that in this class yet. We have always pulled the method like fib.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

You have the variable 'sum' and you have a Fibonacci object, the variable named 'box' refers to it.

Step 1: You want to call the 'Fib' method on the Fibonacci object. How would you do that? (Note, you already know how to call methods on objects, because you're already doing that in your code).
Step 2: Assign the result of the method call to the variable 'sum'. You also already know how to do that (that's also already in your code!).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
I have no way to compile this right now. But would this be what your talking about?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

taylor Lynch wrote:I have no way to compile this right now. But would this be what your talking about?

No, this is not valid Java. Remove the "System.out.println()" that's around the call to box.Fib, and remove the "int".

Also, you want line 17, which shows the result, under the call to box.Fib, and you'll want to show the value of 'sum' there.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2273
    
  28

You are showing the message "That Fibonacci Number is" in a dialog box, and the sum in System out. Why is that?
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
Ok I removed the system.println and I moved everything up to line 17 . But how am I supposed to call fib like this (box.fib(n)); and I'm also lost on what to do with return sum;
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
I called the method using the show message "that Fibonacci number is" + box.Fib(n)); when I try to compile with return sum I still get the variable message. It compiles fine without the return sum; but then when you run the program after you input a n it stops.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

you changed the code, so we don't know what you are really running.
"The variable message" means nothing to us.

Please cut-and-paste the full error message you get, as well as your current code. Without both of those all anyone here can do is guess what you have, and guess at a solution.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
Yeah i know i was mobile at the time and i posted that on my phone but now im home and heres the code....


taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
I'd Like to add that I really appreciate the support guys. Being it's my first time using this site im very impressed by everyone. =D First impressions are great.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

And what is the error you are getting?

Update: I was able to get your code to run. It does exactly what you told it do to, assuming this is still your current code:



What do you think happens inside your Fib() method?>
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
When i run it like this without the return sum; statement, it compiles just fine. But when i run the program it stops right after you input a number for the variable n.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

taylor Lynch wrote:When i run it like this without the return sum; statement, it compiles just fine. But when i run the program it stops right after you input a number for the variable n.
sorry - i was updating my reply when you posted this. See my previous post.
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
When i have the return sum; statement in the code, I get an error like "variable sum wasnt initialized" or something close to that.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

Look VERY CLOSELY at lines 8-14 of the Fibonacci code I posted. Here they are:


What would you think this does?
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
I dont know what it means exactly. I didnt write that coding. It was supplied by my professor, I am supposed to make the jdialog.java work with the Fibonacci.java code. From what i understand it takes the number that is given by the user and comes up with the corresponding number in the Fibonacci sequence.
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
Can anyone explain what it means?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

anything preceded by two slashes is a comment. it is exactly the same as if there was nothing there. So, you effectively have this:


I am assuming you pare passing in a value - something like 7. So, this says "while index is less than n...do nothing".

You have set index to 1, and n is what you pass in - so it is something like 7. so..."while 1 is less than 7, do nothing".

your code is stuck in an infinite loop. You need to write the code so that something HAPPENS on each iteration, and you eventually break out.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

I am amused at this type of assignment! no worries...you'll get solution though
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
I knew it was a comment im not that much of a noob to java. I just dont understand what it means for the code. Also i cant change anything from the Fibonacci.java file it was supplied by the Professor and cant be changed.
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
And the coding is supposed to take input from the user to create a fibonassi sequence. This means if someone was to input 5 the answer would be 8 b/c 3+5=8.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

taylor Lynch wrote:I just dont understand what it means for the code. Also i cant change anything from the Fibonacci.java file it was supplied by the Professor and cant be changed.

I don't know how to help you then. IF - and I want to emphasize the IF - you can't change the Fibonacci class, and what I posted is indeed the real and actual code, then you are screwed. the method Fib(int n) defined in that class is an infinite loop. If you call it (and you do on line 17 of your FibonacciJDialog class), your code goes into an infinite loop. It will sit there, doing basically nothing until you kill the JVM. So either

a) You misunderstood your professor
b) This is NOT the real code
c) You need a miracle of some kind.

Those are the only options I see.
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
Ok in the post he said something about making the max value 40. So what if i change index to 40?
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
Ok i changed the index to 40 and it ran the program. Then i took the variable sum and changed it to sum=in1+in2 and when i put the number 5 in it came back with 2. So something is still wrong. When it's correct it should come back with 8 because 5+3=8.
Heres the new code:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
You sure he said 40? Last time I tried Fibonacci numbers, I got an overflow error, but I think it occurred at fib(34). It was a long time ago, though.

BTW: fib(5) ≠ 8. fib(1) = 1, fib(2) = 1, ∴ fib(6) = 8.
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
This is the post from my professor:
The Fibonacci sequence consists of the numbers 1,1,2,3,5,8,13 ...... in which each number (except the first 2) is the sum of the two preceding numbers. Write a class which that you will call Fibonacci class with a method fib(N) that prints the corresponding Fibonacci value. Remember to use a long as a value for the returned fibonacci result because the number could be very large.

Design and write a Dialog box for Input/Ouput that allows the users to input a number like 5 and the program will produce an output of 8, which is the fibonacci of 5 because the fib(5)=8. Remember to call the program: FibonacciJDialog.java.

(Limit the number N to the value 40 maximum).
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

you professor said this:
Write a class which that you will call Fibonacci class with a method fib(N)

That means you need to write the class. It does NOT mean that you can't edit or change it.
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
I realize taht now. I changed the code in my previous post but as i said im still having a problem with the output being wrong.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

taylor Lynch wrote:Ok i changed the index to 40 and it ran the program. Then i took the variable sum and changed it to sum=in1+in2 and when i put the number 5 in it came back with 2. So something is still wrong. When it's correct it should come back with 8 because 5+3=8.

So...I really think you need to read this page. You seem to be writing and changing your code here and there, without understanding what you need to do.

I would recommend you do do this...Forget about the dialog box, forget about your FibonacciJDialog class. let's focus on your Fibonacci class. I would put a main() method in it, so you can concentrate on nothing but it.

I would suggest you throw away the entire Fibonacci code, and start over. Before you write a SINGLE line of code, you need to spend some time THINKING about what your code should do. If I asked YOU to give me the value of the 6th fibonacci number, how would YOU compute it? Explain it in ENGLISH, not in java. Explain it in clear steps that a 10yr old could follow. Then, when you feel confident you know how to do it...

Write a Fibonacci class with a main method that prints "I'm in main".

Once that works (and I literally mean compile and TEST it to be sure it wors), write a "void Fib()" method that does nothing but prints "I'm in Fib". Change your main to call it. Compile and test to make sure that works.

Then change your Fib() method to take an int parameter, and print out what that parameter was...something like "Fib was passed the value 7".

Once that works, change the method to return an int (for now, it may only return what it was passed in). Print out the value it returns in your main, compile and test.

Keep going like this. You should only ever add 2-3 lines of code AT MOST before you re-compile and test.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
If the 1st number is 1, then the 6th number is 8. Your question has been skewed to match 0‑based Java arrays. [Or he is one of those people who think there is a 0‑th element = 1.]
I have tried it, and you don’t get overflow for fib(40). I obviously had forgotten what happened previously.
Fred is right. That code cannot be what you were provided with. You have misunderstood something and changed something before you showed us it.
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
I sent the code to my Professor and he said that i needed to code the while loop in the Fibonacci.java code. The Fibonacci code that i've been using is copied and pasted directly from the site nothing has been changed except the things i changed in my previous post.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

taylor Lynch wrote:The Fibonacci code that i've been using is copied and pasted directly from the site nothing has been changed except the things i changed in my previous post.

This was your previous post:
taylor Lynch wrote:I realize taht now. I changed the code in my previous post but as i said im still having a problem with the output being wrong.

Your post prior to that was a quote of your assignement.
The post prior to THAT says
Ok i changed the index to 40 and it ran the program.

So at this point...
I have no idea WHAT your code looks like.
I have no idea WHAT the issue you are having is.

Your job, when you post here, is to make it EASY for me to help you. Post your current code. Post the EXACT and COMPLETE error message (if any). Post what output you GET. Post what output you EXPECT. Follow the advice you have been given (or at least, ACKNOWLEDGE you've seen it and why you don't think it will solve your problem).

I've given you the best advice I can (in my post at 14:20:25). If you aren't going to follow that advice, there's not much more I can do.
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
Ok i've worked on understanding what the comments are telling me and i've re-written the code. In the comments it says something about increment index. I dont understand what increment index is can someone explain?

Heres the new code there are no errors when it compiles the problem lies in when I run the program it doesn't provide the correct answer. This tells me that there is something wrong in the coding that needs to be changed.

srinivas chintala
Greenhorn

Joined: Oct 29, 2012
Posts: 3
Hi,

Try the following code !!!

class Fibonacci
{
void fib(int n)
{
int in1=0;int in2=1;
System.out.println("the fibonacci is");
System.out.println(in1);
System.out.println(in2);
int sum=0;//initial value
int index=1;
while (index<n)
{
sum=in1+in2;
System.out.println(sum);
in1=in2;
in2=sum;
index++;
}

}
}
public class Fib
{
public static void main(String ar[])
{
Fibonacci fb=new Fibonacci();
fb.fib(10);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Assignment Problem
 
Similar Threads
please help me with Recursion
help with assignment
Percentage issue
Fibonacci Sequence Problem
Inheritance error?