Jump to content

Problem when calling a function

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Qistina Tajuddin

Qistina Tajuddin

    Newbie

  • Members
  • Pip
  • 2 posts
I wrote a function Factorial (which runs fine on its own) to be used in SampleOutputPanel.

When I compile, I get this error for some reason.

C:\Java\Assignment\Factorial>javac SampleOutputPanel.java
SampleOutputPanel.java:12: cannot find symbol
symbol : constructor Factorial(int)
location: class Factorial
f = new Factorial (5);
^
1 error


My code is as follows.

Factorial.java


import javax.swing.*;

import java.awt.*;


public class Factorial {


	static int currentVal;


	public static int Factorial (int n)     {

	

        if (n == 0) 

			return 1;

			

		currentVal = n * Factorial (n - 1);

		System.out.println (currentVal);

		

        return (currentVal);

		

    }



}

SampleOutputPanel.java


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

	

class SampleOutputPanel extends JPanel {


	Factorial f;

	

	public SampleOutputPanel () {

		

		f = new Factorial (5);

		

		setPreferredSize (new Dimension (496, 120));

		setBorder (BorderFactory.createTitledBorder ("Sample Output"));

		add (new JLabel ("Output" + f));

		

	}

   

}



Please help out, thanks!

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
you don't have a constructor in you Factorial class. So you can't call new Factorial(5).
In this case, the static way is probably the most reasonable way to calculate the factorial, so

int f = Factorial.Factorial(5);
(Java convention says, that methods are lowerCamelCase, so you should change the Factorial() method to factorial())

you cant use Factorial f, unless you actually create the instance of Factorial object.

Also this will probably not work for you, if you are still looking to create the Factorial object:
new JLabel ("Output" + f)
because creating String from the f, which was a Factorial, calls the toString() method, which you have not implemented and results in calling the Object's toString() method instead, because of the inheritance.
If you are returning the int, then you should be just fine, because primitives will be easily converted to String.

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Actually you can use Factorial f for static methods, but it's considered bad practice. The compiler will turn any static calls on an instance into the proper code anyway.
It's easily tested by doing:
public void someMethod() {
  Factorial f = null;
  f.Factorial(5);
}
will not result in a nullpointer because the compiler will see it's a static method and compile Factorial.Factorial(5) instead.
(Don't write static calls on instances like I just did)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users