Results 1 to 12 of 12
Thread: Java Applet
- 12-16-2010, 12:41 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
Java Applet
Ok so i was given the task to create a java applet in which you type a number, and then check if the number is Perfect, Deficient, Abundant and Prime.
Here is the source text of the java console application i wrote:
I have no idea how to turn it into an applet. Can somebody please help me with it, I'm still a beginner.Java Code:import java.util.Scanner; public class NumberTester { public static void main (String[] args) { Scanner console = new Scanner (System.in); int n; n = console.nextInt(); if (isPerfectNumber (n)) System.out.println (" " + n + " Perfect"); else System.out.println (" " + n + " Not Perfect"); if (isDeficientNumber (n)) System.out.println (" " + n + " Deficient"); else System.out.println (" " + n + " Not Deficient"); if (isDeficientNumber (n)) System.out.println (" " + n + " Abundant"); else System.out.println (" " + n + " Not Abundant"); if (isAbundantNumber (n)) System.out.println (" " + n + " Prime"); else System.out.println (" " + n + " Not Prime"); } private static boolean isPerfectNumber (int n) { int total = 0; for (int i=1; i<n; i++) if (n % i == 0) total += i; return (total == n); } private static boolean isDeficientNumber (int n) { int total = 0; for (int i=1; i<n; i++) if (n % i == 0) total += i; return (total < n); } private static boolean isAbundantNumber (int n) { int total = 0; for (int i=1; i<n; i++) if (n % i == 0) total += i; return (total > n); } private static boolean isPrimeNumber (int n) { for(int i=3;i*i<=n;i+=2) { if(n%i==0) return false; } return true; } }
- 12-16-2010, 12:45 AM #2
well, the hard part of doing the number processing to determine if the number is the abundant, deficient, prime is done. Now you just need to applet-ize that.
How about taking a look at some existing applet that has a kind of input and push button to compute some output, like this Celsius Fahrenheit conversion Java applet sample
- 12-16-2010, 12:48 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
you do have google though
Lesson: Applets (The Java™ Tutorials > Deployment)
- 12-16-2010, 12:56 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
Well i tried to look at an example of an applet to get the idea, but I still can't get it even close to working. Problem is i need it for tomorrow, but i'm going to try it again with that celciustofarenheit example.
Thanks.
- 12-16-2010, 01:01 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
What problems are you running into exactly with the applet implementation?
- 12-16-2010, 01:48 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
Well this is what i came up with for just one type of number:
these are the errors i'm getting:Java Code:import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Tester5 extends Applet implements ActionListener{ TextField txtNumber; Label lblNumber; Button cmdTest; public void init(){ lblNumber = new Label("Enter the Number: "); add(lblNumber); txtNumber = new TextField(5); add(txtNumber); cmdNumber = new Button("Test"); add(cmdNumber); cmdNumber.addActionListener(this); } public void actionPerformed(ActionEvent e){ private static boolean isPerfectNumber (int n) { String str; int n; str = txtNumber.getText(); n = Integer.parseInt(str); int total = 0; for (int i=1; i<n; i++) if (n % i == 0) total += i; return (total == n); } public void paint(Graphics g) { g.clearRect(0, 50, 200, 80); if (isPerfectNumber (n)) g.drawString("" + n + " is perfect", 10, 60); else g.drawString("" + n + " is not perfect ", 10, 60); } } }
Tester5.java:23: illegal start of expression
private static boolean isPerfectNumber (int n) {
^
Tester5.java:23: illegal start of expression
private static boolean isPerfectNumber (int n) {
^
Tester5.java:23: ';' expected
private static boolean isPerfectNumber (int n) {
^
Tester5.java:23: '.class' expected
private static boolean isPerfectNumber (int n) {
^
Tester5.java:23: ';' expected
private static boolean isPerfectNumber (int n) {
^
Tester5.java:34: illegal start of expression
public void paint(Graphics g)
^
Tester5.java:34: illegal start of expression
public void paint(Graphics g)
^
Tester5.java:34: ';' expected
public void paint(Graphics g)
^
Tester5.java:34: ';' expected
public void paint(Graphics g)
^
9 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
- 12-16-2010, 02:01 AM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
This really does pain me to say but can you please put your code in tags?
[code]
// your code
[/cod] but with an 'e' after 'd' to make the word code ok?
- 12-16-2010, 02:05 AM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
semi colon problem? ;
re-investigate your code...compiler don't lie
- 12-16-2010, 02:07 AM #9
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
This isn't very helpful...
- 12-16-2010, 02:09 AM #10
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
what is not helpful?
- 12-16-2010, 03:07 PM #11
well, you can't have the methods private static boolean isPerfectNumber (int n) and
public void paint(Graphics g) inside the actionPerformed() method..
- 12-16-2010, 03:42 PM #12
also, the isPerfect, you cannot declare variable n when parameter is also n.
And its not likely necessary to have the static modifier, that won't be able to read the non static reference. And we shouldn't e reading the text field from the fuction, the action handler should read this. And I believe it is good practice to always use block delimiters { } for single line if and for statements.
well, for example,
and then (Tester5.html)Java Code:import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Tester5 extends Applet { TextField txtNumber; Label lblNumber; Button cmdNumber; // declare a result text field Label resultLabel; TextField result; public void init() { Panel p = new Panel(); p.setLayout(new GridBagLayout() ); lblNumber = new Label("Enter the Number: "); p.add(lblNumber, new GridBagConstraints(0,0,1,1,1.0,1.0,GridBagConstraints.EAST,GridBagConstraints.NONE,new Insets(2,2,2,2),0,0)); txtNumber = new TextField(5); p.add(txtNumber, new GridBagConstraints(1,0,1,1,2.0,1.0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(2,2,2,2),4,0)); cmdNumber = new Button("Is Perfect Number?"); p.add(cmdNumber, new GridBagConstraints(0,1,2,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(2,2,2,2),0,0)); resultLabel = new Label("Result: "); p.add(resultLabel, new GridBagConstraints(0,2,1,1,1.0,1.0,GridBagConstraints.EAST,GridBagConstraints.NONE,new Insets(2,2,2,2),0,0)); result = new TextField(); result.setEditable(false); p.add(result, new GridBagConstraints(1,2,1,1,2.0,1.0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(2,2,2,2),4,0)); setLayout(new BorderLayout()); add(p, BorderLayout.CENTER); // declare button handler as anonymous inner class. cmdNumber.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // fetch the text from input int input = Integer.parseInt(txtNumber.getText()); // invoke computation boolean perfect = isPerfectNumber(input); // put result up if (perfect) { result.setText("number is perfect"); } else { result.setText("number is not perfect"); } } }); } private boolean isPerfectNumber (int n) { int total = 0; for (int i=1; i<n; i++) { if (n % i == 0) { total += i; } } return (total == n); } }
and thenJava Code:<HTML> <BODY> <applet code="Tester5.class",height="200" width="500"> </applet> </BODY> </HTML>
Java Code:javac Tester5.java appletviewer Tester5.html
Similar Threads
-
applet class inside java.applet.* or java.awt.*
By Manish87 in forum Java AppletsReplies: 1Last Post: 09-27-2010, 02:15 PM -
java applet
By encyclopedia in forum New To JavaReplies: 6Last Post: 09-24-2010, 04:24 PM -
Help with this java applet
By dementedchild686 in forum Java AppletsReplies: 5Last Post: 03-14-2010, 11:53 AM -
What're the differences between JSP, Java Script, and Java Applet?
By meili100 in forum New To JavaReplies: 3Last Post: 07-23-2008, 08:07 AM -
Java Applet Help
By Nuluvius in forum New To JavaReplies: 0Last Post: 03-01-2008, 03:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks