• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Help Needed - How to Create These Simply Programs

Joined
Jun 6, 2004
Messages
9
Can someone please help with the below stated code? I am trying to learn Java by myself and these were the two questions that were at the end of the chapter and I have no clue what to fill in and how to do it? Please help, thanks.



1. Fill in the blanks in the following progam that contains an integer n from the command-line argment and displays n! (where n! is the product of all numbers from 1 to n, pronounced "n factorial"):

public class NFactorial
{
_________________________ main __________________________
{
int n = Integer.parseInt( ________ );
int k;
long fact = 1;

for ( k = 2; _________________ )
{
____________________ ;
}
system.out.println(n + "! = " + _____________________ ) ;
}
}


_____________________________________________________________________________________________________________________________________


2. Write a class RedDisk derived from JApplet; make it show a red disk in the middle of a white rectangle (like the Japanese flag). Make sure your applet is scalable.

Hints:

You can get the current dimentions of the applet's content pane as follows:

Container c = getContentPane( );
int w = c.getWidth( );
int h = c.getHeight( );

You can use the following method to draw a disk (a filled circle) of adius r with the center at (x, y):

private void drawDisk (graphics g, int x, int y, int r)
{
g.fillOval (x - r, y - r, 2*r, 2*r);
}
 
I don't want to nag you or anything but, giving you the answers probably wouldn't be helping you. Instead, shouldn't you reread the chapters in the books until you figure out the answers. The only thing I will give you is, if you have any troubles or any questions regarding Java, I'll be happy to answer any and all questions.

Cheers!
 
I spent quite a bit of time on these questions, but never figured out the answer. It could be that the book I am reading assumes quite a bit of stuff, which is annoying.
 
you must be rushing through it becuase i know that pretty much anyone who has been coding in java for more than 2 weeks should know the little 'incantation' for the main function by now..

http://java.sun.com/docs/books/tutorial/getStarted/application/main.html

other than that, if you know how to compute a factorial you should be in business..

just think 'what integer would i want to convert from a string'

then ' how do i calculate a factorial with a for loop'

then 'what would the limit of a for loop be for x!'
 
Back
Top