ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am going through the "complete reference" code by code and there are so many things that I don't understand and post here. Hope I will be up someday.
Here is my simple factorial code:
Factorial.java
Code:
package javaapplication8;
public class Factorial
{
int fact (int n)
{
int result;
if(n==1) return 1;
result = fact(n-1)*n;
return result;
}
}
main.java
Code:
package javaapplication8;
public class Main
{
public Main()
{
}
public static void main(String[] args)
{
Factorial f = new Factorial();
System.out.println("Factorial of 3 is " + f.fact(3));
System.out.println("Factorial of 5 is " + f.fact(5));
System.out.println("Factorial of 9 is " + f.fact(9));
}
}
When I compile main.java, I get the "BUILD SUCCESSFUL" result. But when I run the code, following is what I have:
I have written the code from scratch many times, but got the same error. Don't know why.
I also feel I am bugging the community too much with my newbie questions. I will be grateful if someone can point me through a real newbie java-teaching website. I began to lose control with the book.
The problem is regarding the package statement.
I tried compiling the program by removing the package statement from both the files and it worked fine.
Code:
mann@Manish:~/javaapplication8$ javac Factorial.java
mann@Manish:~/javaapplication8$ javac Main.java
mann@Manish:~/javaapplication8$ java Main
Factorial of 3 is 6
Factorial of 5 is 120
Factorial of 9 is 362880
mann@Manish:~/javaapplication8$
I previously knew about how to make it work with packages, but, it has been years I haven't coded java. Anyway, it's good to remind all that. I will let u know as soon as I find out the solution about packages. Till then, code this way.
Wouldn't it be easier to put main() in the Factorial class, or is there a specific need to use a package here?
It is considered a good coding practice to group related classes into packages. But, yes while learning, the practice to use package is not required.
When I used to learn java, I put all the classes of a program in one single file to avoid confusions among several programs.
Alright. I found it. If u want to complie/run your programs with package <packagename>;, u can see this example. I have tested these methods on ur program itself and following is what I have come up with.
When u use
Code:
package javaapplication8;
you should have to put the Factorial.java, Main.java files in a directory called "javaapplication8" and this directory should be inside ur current working directory.
Then, u can compile these files by using
Thank you all for your replies. Tonight I came home and once tried to run the code and it worked fine. I don't know why I was getting the error message. Weird
I tried to modify some of my code, so that the factorial is computed for the number that the user has entered. So, here is my modified code:
main.java
Code:
package javaapplication8;
public class Main
{
public Main()
{
}
public static void main(String[] args)
throws java.io.Exception
{
char n;
Factorial f = new Factorial();
do
{
System.out.println("Enter a number to compute its factorial: ");
n = (char) System.in.read();
}
while (n == 0);
System.out.println("\n");
System.out.println("Factorial of " +n " is " + f.fact(int n));
}
}
Factorial.java
Code:
package javaapplication8;
public class Factorial
{
int fact (int n)
{
int result;
if(n==1) return 1;
result = fact(n-1)*n;
return result;
}
}
Now, the red line is line 30 in my code. The error message is as follows:
Quote:
init:
deps-jar:
Compiling 1 source file to /home/diablo/java_codes/JavaApplication8/build/classes
/home/diablo/java_codes/JavaApplication8/src/javaapplication8/Main.java:30: ')' expected
System.out.println("Factorial of " +n " is " + f.fact(n));
1 error
BUILD FAILED (total time: 0 seconds)
There is no *unmatching* parantheses in the subject line 30. Whatever I do, I couldn't get rid of the error.
A couple of things:
Do not assume that I know what throws java.io.Exception is. I just learnt in the book that it is required to use System.in.read() to read user input from the console.
System.out.println("Factorial of " +n " is " + f.fact(int n)); does not work whether f.fact(int n) or f.fact(n). The error message is the same.
If my code worked, I thought the do-while statement would do much better if I could test if the value entered was integer. So, I had written while (n != int). But it didn't work. Seems like I am making a logical or syntax error.
I am using NetBeans 5.0 on a SuSE 9.3 Pro box.
I am a complete n00b in Java.
Edit: Added item no 2 in the list.
Last edited by chief_officer; 05-30-2006 at 01:28 PM.
I have marked the changes which I have done to Main.java
Code:
package javaapplication8;
import java.io.*;
public class Main {
public Main() {
}
public static void main(String[] args)
throws Exception {
char n;
Factorial f = new Factorial();
do {
System.out.println("Enter a number to compute its factorial: ");
n = (char) System.in.read(); } while (n == 0);
System.out.println("\n");
System.out.println("Factorial of " + n + " is " + f.fact(n));
}
}
Now, it compliles and run well. But, u need to check the logic to find the factorial as it's not working as expected. This is what I see here.
Code:
mann@Manish:~$ javac javaapplication8/Factorial.java
mann@Manish:~$ javac javaapplication8/Main.java
mann@Manish:~$ java javaapplication8/Main
Enter a number to compute its factorial:
5
Factorial of 5 is 0
mann@Manish:~$
Last edited by manishsingh4u; 05-30-2006 at 01:59 PM.
Thank you very much for your kind assistance and most importantly, your patience.
I have rechecked the file and saw that the logic behind the factorial is fine. However reading the input from the console does not pass an integer value to the fact class. See the following code and the outputs:
Code:
package javaapplication8;
import java.io.*;
public class Main
{
public Main()
{
}
public static void main(String[] args)
throws Exception
{
int n;
Factorial f = new Factorial();
do
{
System.out.println("Enter a number to compute its factorial: ");
n = (int) System.in.read();
}
while (n == 1);
System.out.println("\n");
System.out.println("Factorial of " + n + " is " + f.fact(n));
System.out.println("Factorial of 5 is " + f.fact(5));
}
}
Here I added the System.out.println("Factorial of 5 is " + f.fact(5)); just to see what is going on. The results are as follows:
int n ---> char n;
n = (int) System.in.read() ---> n = (char) System.in.read();
The output becomes Factorial of 3 is 0 for n=3. The 0 is independent of the n value; if I input 8, then the output is Factorial of 8 is 0. I even tried with character x, and the output is Factorial of x is 0.
Both is wrong.
I went back to the former case, where I receive 48+n as the computed output of the factorial and tried it with a character. For x, the output is Factorial of 120 is 0. For d, the output is Factorial of 100 is 0. So, the code somehow changes my integer input to its ASCII value. How does it do it when I force the input to be integer, I couldn't figure out...
Well, I never got stuck with this problem yet because I always used the following way. For some reason (which I am not able to figure out), n is storing the ASCII value for 5 instead of it's decimal value. Anyway, this code works fine.
Code:
package javaapplication8;
import java.io.*;
public class Main {
public Main() {
}
public static void main(String[] args)
throws Exception {
int n;
Factorial f = new Factorial();
do {
System.out.println("Enter a number to compute its factorial: ");
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
n = Integer.parseInt (br.readLine());
} while (n == 1);
System.out.println("\n");
System.out.println("Factorial of " + n + " is " + f.fact(n));
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.