Error with finding nth number of Fibonacci using recursion - java

I am having trouble with an error saying that my int variable final may not have been initialized however I thought I initialized it in the if statement.
import java.util.Scanner;
public class Fibo
{
static int fibNum(int num)
{
int finish;
if(num < 0)
{
finish = -1;
}
else if(num == 0 || num == 1)
{
finish = num;
}
else if(num > 1)
{
finish = fibNum(num-1) + fibNum(num-2);
}
return finish;
}//end fibNum
public static void main(String []args)
{
int num;
Scanner in = new Scanner(System.in);
num = in.nextInt();
fibNum(num);
System.out.println(" is the nth number in the fibbonaci sequence that you are looking for");
}//end main
}//end class
Error:
Fibo.java:20: error: variable finish might not have been initialized
return finish;

While the member fields (variables) can be initialized to default values implicitly, the local variables cannot, so you should initialize it explicitly by changing this:
int finish;
to this:
int finish = 0;

You have to initialize the variable.
Change:
int finish;
To
int finish = 0;

You Must Declare Return Variable. Because if condition can execute only when calling a method.
int finish = 0;

Local variables do not get default values. Their initial values are undefined without assignment by some means. Before you can use local variables they must be initialized.
That's why you have to intialize the local variable.
int finish = 0;

Yes, you must initialize the int variable finish in fibNum method

Related

Can't get my integer to increase constantly in method

I am fairly new to java (and most programming languages as a whole) and am trying to get some unfamiliar concepts down, one of which is recursion. To test it out I tried making it so that a number would constantly increase and get displayed.
public static void recursionTest() {
int numb = 0;
System.out.println(numb);
numb += 1;
recursionTest();
}
I tried writing it out like this, but it will only print the number 0 constantly with no increase. I then tried putting before println, but it only produced the number 1. I then tried replacing with a while loop.
public static void recursionTest() {
int numb = 0;
System.out.println(numb);
while (numb != -1) {
numb =+ 1;
}
recursionTest();
}
This ended up printing out just a single 0, I then tried moving it above println like I did before, but it then didn't display anything. Is there something I'm missing? Sorry if my question is stupid.
As you have defined it, numb is a local variable scoped within the method. It's created with the instruction int numb = 0 at the beginning of each call of the method, and destroyed at the end of it. Each time you call the method (even if the call is recursive) is a different variable. With the same name in the code, but it's a different one.
In order to achieve a counter you should either define numb as a static field of the class or pass as parameter to the method. You can do something like the following:
Option 1 (static field):
public class TheClass {
static int numb = 0;
public static void recursionTest() {
System.out.println(numb);
numb += 1;
recursionTest();
}
}
Option 2 (pass as parameter):
public class TheClass {
public static void recursionTest(int numb) {
System.out.println(numb);
numb += 1;
recursionTest(numb);
}
}
In the second attempt you are doing the System.out.println outside the while loop, so you are printing the value only once. Moreover, the recursive call is never being done due to the infinite while loop, but in the case you put a limit to the loop, the new call to the same method will result in the counter being reset because the same reason as before.
You should read up about the scope and lifespan of variables in Java. You declare a local variable inside the function, which is "created" and initiated (to 0) when you write:
int numb = 0;
This variable is only visible inside this current function call. After your function is executed the variable and its value is invalidated and destroyed. When calling the function again, the variable is created and initiated (to 0) again.
If your variable needs to "survive" multiple calls of your function consider to declare it outside of the function:
private static int numb = 0;
public static void recursionTest() {
System.out.println(numb);
while (numb != -1) {
numb =+ 1;
}
recursionTest();
}
Or pass it as a parameter:
public static void main(String[] args) {
recursionTest(0);
}
public static void recursionTest(int numb) {
System.out.println(numb);
while (numb != -1) {
numb =+ 1;
}
recursionTest(numb);
}

Problems with printing my instance variables

Im having a problem. when i write for example "18" in the console, it just prints "0". I also want to know how i am able to print all the instance variables when i create a new car object. Thanks a lot.
public class mad {
public static void main(String[] args) {
java.util.Scanner tastatur = new java.util.Scanner(System.in);
int answer = tastatur.nextInt();
car carA = new car();
carA.createCar(answer);
System.out.println(carA.number);
}
}
class car {
int number;
void createCar(int n) {
n = number;
}
}
You wrote "n = number" instead of "number = n"
Should be this:
void createCar(int n) {
this.number = n;
}
For printing all instance variables, see this: printing all variables
Your assignment in the class car is the opposite of what you wanted to do. You're assigning the field number (which is always 0) to the parameter n. What you wanted to do is assigning the parameter n to the field number.
So the solution is to replace
n = number;
with
number = n;
By the way: By convention class names should start with a capital letter.
In your car class, you're currently doing this:
int number;
void createCar(int n) {
n = number; // you're assigning n to the value of number
}
Switch those around so it reads:
int number;
void createCar(int n) {
this.number = n; // now you're assigning number to the value of n
}
In constructor line n = number; should be number = n;.
You are getting 0 because instance variables are initialized bt 0 or equivalent by default.
To print all variables you must have to define a function to do so. Best way is to override toString() method of Object class.

I want to find the factorial of a number using a default and parameterize constructor. getting output as 0

I have declared a default and a parameterize constructor. Using a single object I am calling both the constructor and an function. When I run the program I am getting the output as 0 instead of getting the factorial. I have initialize f=1 still the output is 0.
class Factorial
{
int num, f;
Factorial()
{
f = 1;
}
Factorial(int n)
{
num = n;
}
public int getFactorial()
{
for(int i = 1; i <= num; i++)
{
f = f * i;
}
System.out.println("Factorial= " + f);
return f;
}
public static void main(int m)
{
Factorial obj = new Factorial();
obj = new Factorial(m);
obj.getFactorial();
}
}
This is because the initial value of f remains zero when the class is initialized with the factorial(int n) constructor.
Set f to 1 in the initializer to fix this problem:
int n, f = 1;
Factorial() {
}
Factorial(int n) {
num = n;
}
This leaves your class with a big problem: calling getfactorial multiple times will change the state of the class, increasing the value of the factorial. You can fix this by creating a boolean variable that indicates whether the factorial has been computed or not, and returning f after the computation has been performed.
If you do not need "lazy" computation, make f a local variable in the method.
As a good practice, a variable should have the minimal possible scope (preferred local, then instance). Or in other words: state variables should be used just when you want to share some date between several methods of the same class. But what in your case? Factorial is an operation that may be computed in just one step, from one single parameter: No need to further processing.
So, I recommend you to refactorize your class to:
Drop off the state variables: Convert them to local variables or parameters to the method.
Set the factorial method as static (precisely because it does not need state variables).
Another small detail: Parameters are passed from command line to the main method through an array of Strings - always.
Leave all the inputs and outputs to/from the user in just one method, preferrably the main method.
So it will remain like this:
class Factorial
{
public static int getFactorial(int num)
{
int f=1;
for (int i = 1; i <= num; i++)
{
f = f * i;
}
return f;
}
public static void main(String[] args)
{
int m=Integer.parseInt(args[0]);
int factorial=Factorial.getFactorial(m);
System.out.printf("factorial of %d is %d\n", m, factorial);
}
}
Much simplier, isn't it?

How do I implement "this" into my Java program?

It's a requirement of my school assignment that I use "this" in the following program. However, I can't quite figure out where I could put this. I keep getting a "non-static variable this cannot be referenced from a static context" error.
import java.util.Scanner;
public class PrimeNumber
{
public static void main(String args[])
{
System.out.println("Enter the upper limit for the prime numbers computation: ");
int upperLimit = new Scanner(System.in).nextInt();
int count = 0;
for(int number = 2; number<=upperLimit; number++)
{
if(isPrime(number))
{
System.out.println(number);
count++;
}
}
System.out.println("Number of primes generated: " + count);
}
public static boolean isPrime(int number)
{
for(int i=2; i<number; i++)
{
if(number%i == 0)
{
return false;
}
}
return true;
}
}
The Java keyword this refers to the instance of your class that invoked an instance method. A static method is general to its class, and so you cannot reference any instance (non-static) variables from within it. You can only access instance variables like this from within an instance method, that is, a method that is not defined as static.
So, you would need to create an instance method (of which there are none in your class), in order to use this.
This is nothing more than a reference to the object on which the method was called. Static methods on the other hand can operate without any instance of the class even exisiting, therefore they can't have reference to any object. That's why you can't use this in static method. If you really need this, you have to remove static keywords from your functions and use instance variables in those functions anyhow.
public class PrimeNumber
{
public int count = 0;
public int upperLimit;
public static void main(String args[])
{
PrimeNumber pn = new PrimeNumber();
System.out.println("Enter the upper limit for the prime numbers computation: ");
pn.upperLimit = new Scanner(System.in).nextInt();
pn.doCheck();
System.out.println("Number of primes generated: " + pn.count);
}
public void doCheck() {
for (int number = 2; number <= this.upperLimit; number++)
{
if (this.isPrime(number))
{
System.out.println(number);
count++;
}
}
}
public boolean isPrime(int number)
{
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
}

Fibonacci Sequence return argument

I need to generate a program that generates the Fibonacci Sequence
Here is what I have so far:
import java.util.Scanner;
public class FibonacciRunner
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter n:");
int n = in.nextInt();
EP64 fg = new EP64();
for (int i = 1; i <= n; i++)
System.out.println(fg.nextNumber());
}
}
public class EP64
{
public static void nextNumber(int n)
{
int fold1 = 1;
int fold2 = 1;
int fnew = fold1 + fold2;
fold1 = fnew;
}
}
I get an error on:
System.out.println(fg.nextNumber());
saying:
method nextNumber in class EP64 cannot be applied to given types:
required: int
found: no arguments
reason: actual and formal argument lists differ in length
and can someone also tell me if I am doing this program right? If not, help! I looked at other similar questions but I cannot make much sense of them
Thank you all!
method nextNumber in class EP64 cannot be applied to given types: required: int found: no arguments reason: actual and formal argument lists differ in length
Your
public static void nextNumber(int n)
^^^^^^^
says that any call to the method must provide an integer as argument. But here:
System.out.println(fg.nextNumber());
^^ you need to add an integer argument
you violate this by providing no argument.
As your code reads now, I'd probably drop the int n argument.
and can someone also tell me if I am doing this program right?
Naah, not really...
fold1 and fold2 should probably be member variables (so they don't get reset in every call to the method),
You're forgetting to update fold2 (you only update fold1),
Also, you probably want to return an int from the nextNumber method.
Read up on
Official Java Tutorial: Defining Methods
You are calling a static method to a object reference instead of the class itself.
And
Not passing any argument at all for nextNumber() method.
Make the method non-static as :
public void nextNumber(int n) {}
Pass arg to the method as :
for (int i = 1; i <= n; i++)
System.out.println(fg.nextNumber(n));
And also don't forget to return the processed number from your nextNumber method,which you collecting in System.out.println.
Your declaration of nextNumber says it takes an int argument, but you are calling it with no arguments.
Also, your code isn't going to do what you want. You probably should make fold1 and fold2 members of class EP64 and make the method an instance method rather than a static method. You also need to do fold2 = fold1; before you update fold1.
Finally, you need to declare nextNumber to return an int value, and then actually have it return an int value.
You have two problems. Firstly, your method doesn't return anything, i.e. it is void. You need to make it int and add a return fnew; at the end. The other problem is you are starting from scratch every time, it will return 2 each time. You need to make fold1 and fold2 fields by moving them above the nextNumber line. Oh, and drop the int n argument as it doesn't do anything.
I agree on the diagnostics of the other posts, but don't suggest a member variable, but a rename and local variables.
You can ask for the 5th Fibonacci-Number with 5 calls to
fib.next ();
or with a single call to
fib (5);
Since the fibonacci-sequence increases very rapidly, you have very few calls (54) before hitting the overflow boundary. So if you repeatedly recalc the same sequence, to print the sequence, it's not a big problem. A recursive solution would be fine.
Btw.: EP64 is a very bad name.
I think this is enough:
import java.util.Scanner;
public class Fibnocci
{
public static void main(String []abc)
{
int a=0,b=1,c;
Scanner in=new Scanner(System.in);
System.out.print("Enter the Range: ");
int n= in.nextInt();
System.out.print(a+" "+b);
for(int i=0;i<n-2;i++) //n-2 because we are showing 0,1 initially.
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}
}
}
If you want to call this as a method then:
import java.util.Scanner;
public class Fibnocci
{
public static void main(String []abc)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter the Range: ");
int n= in.nextInt();
callFibonocci(n);
}
public static void callFibonocci(int n)
{
int a=0,b=1,c;
System.out.print(a+" "+b);
for(int i=0;i<n-2;i++) //n-2 because we are showing 0,1 initially.
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}
}
}
You can call this method out of the class;
// Fibnocci Using c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeProject
{
class FibnocciSeries
{
public int[] FibonacciArray(int length)
{
int[] fseries = new int[length];
fseries[0] = 0;
fseries[1] = 1;
if (length == 0)
return null;
//Iterating through the loup to add adjacent numbers and create the memeber of series
for (int i = 2; i < length; i++)
{
fseries[i] = fseries[i - 1] + fseries[i - 2];
}
return fseries;
}
}
}
////////////////////
class Program
{
static void Main(string[] args)
{
FibnocciSeries fb = new FibnocciSeries();
Console.WriteLine("Please Enter Integer Length of Fibnocci series");
int length = Convert.ToInt32(Console.ReadLine());
int[] result = fb.FibonacciArray(length);
foreach(int i in result)
Console.Write(i.ToString()+ " ");
Console.ReadLine();
}
}
|

Categories