Results 1 to 12 of 12

Thread: Java Applet

  1. #1
    mex2005 is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default 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:


    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;
    }
    }
    I have no idea how to turn it into an applet. Can somebody please help me with it, I'm still a beginner.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    11

    Default

    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

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    10

  4. #4
    mex2005 is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    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.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    10

    Default

    What problems are you running into exactly with the applet implementation?

  6. #6
    mex2005 is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    Well this is what i came up with for just one type of number:

    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);
    				
    
    }
    }
    }
    these are the errors i'm getting:

    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.

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    10

    Default

    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?

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    10

    Default

    semi colon problem? ;

    re-investigate your code...compiler don't lie

  9. #9
    mex2005 is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    This isn't very helpful...

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    10

    Default

    what is not helpful?

  11. #11
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    11

    Default

    well, you can't have the methods private static boolean isPerfectNumber (int n) and
    public void paint(Graphics g) inside the actionPerformed() method..

  12. #12
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    11

    Default

    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,

    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 then (Tester5.html)
    Java Code:
    <HTML>
    <BODY>
    <applet code="Tester5.class",height="200" width="500">
    </applet>
    </BODY>
    </HTML>
    and then
    Java Code:
       javac Tester5.java
       appletviewer Tester5.html

Similar Threads

  1. applet class inside java.applet.* or java.awt.*
    By Manish87 in forum Java Applets
    Replies: 1
    Last Post: 09-27-2010, 03:15 PM
  2. java applet
    By encyclopedia in forum New To Java
    Replies: 6
    Last Post: 09-24-2010, 05:24 PM
  3. Help with this java applet
    By dementedchild686 in forum Java Applets
    Replies: 5
    Last Post: 03-14-2010, 12:53 PM
  4. Replies: 3
    Last Post: 07-23-2008, 09:07 AM
  5. Java Applet Help
    By Nuluvius in forum New To Java
    Replies: 0
    Last Post: 03-01-2008, 04:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •