Java Help Needed - How to Create These Simply Programs

Jun 18, 2004
101
0
0
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);
}
 

Casawi

Platinum Member
Oct 31, 2004
2,366
1
0
2.
public JPanel whiteTri = new JPanel ();
g.setColor(Red);
whiteTri.add();
window.add(whiteTri);
I know u need these lines the details so you ganna get some compiler errors
 

Casawi

Platinum Member
Oct 31, 2004
2,366
1
0
1.
public class NFactorial
{
public void static main ()
{
int n = Integer.parseInt( the argument from the command line here);
int k;
long fact = 1;

for ( k = 2; k<= n ; k++ )
{
fact = fact*k;
}
system.out.println(n + "! = " + fact ) ;
}
}
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
For 1), I think you need

public static void main( String[] args ) { ... }

Thus the command line argument will be stored into a array of Strings so a couple lines down it is asking you to retrieve it by parsing as an Int. So I think you'd need

int n = Integer.parseInt( args[0] );

which will parse the string at position zero in the array to an int so that the factorial can be computed with it.
 

znaps

Senior member
Jan 15, 2004
414
0
0
If you can't answer those yourself then either:

- You didn't actually read the chapter in question
- You're lying and you just want someone to do your homework for you

or both
 
Jun 18, 2004
101
0
0
1. I read the chapter quite a few times, but the book I am reading is not that great. It assumes quite a bit of things and even though I have taken a VB class, it still stumbles.
 
Jun 18, 2004
101
0
0
I don't know why is it that when you post a question people always think that you are cheating. I know there are quite a few people who do this, but not everyone is here to cheat.
 
Jun 18, 2004
101
0
0
wow! Number one was really easy once I see it.

However, I am having a bit of a hard with the second one. I tried entering the aforementioned lines with additional code, but it still gives me tons of errors that I can't fix now.
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Well he didn't give you everything you'll need for 2). Your class will need to extend JApplet ( public class blah extends JApplet {} ) for one thing. Your main method for a JApplet is public void init() {}, which is where you would put the Container, create a JPanel object (the class def for this will need to extend JPanel), etc.. You also need a paintComponent(Graphics g) method in the class that extended JPanel. In this method you need super.paintComponent(g) to get things to work right. Then you can use methods from the Graphics object such as g.fillOval() to actually draw the objects you need. When you draw the JPanel and the circle, it sounds like its asking you to use the .getWidth/.getHeight so that you can resize the window and still have everything draw correctly (as opposed to entering a constant width/height to draw with).

Maybe this helps??
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |