Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Making a java program into an applet

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Making a java program into an applet

    Greetings,

    I am working on my assignment and I can get the Commission program to work just fine. When I try to move over to an applet I keep getting the same error and I am following the book exactly as described. Once I get this figured out I can modify the programming for my assignment but I simply want to get this part working.


    /*
    * Week 05 Project Assignment.
    * James Turnham
    */







    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;

    public class Commission
    {
    public static void main(String[] args)
    {
    double dollars, answer;
    int empCode;

    dollars = getSales();
    empCode = getCode();
    answer = getComm(dollars, empCode);
    output(answer, dollars);
    finish();
    }

    public static double getSales()
    {
    double sales = 0.0;
    boolean done = false;

    while(!done)
    {
    String answer = JOptionPane.showInputDialog(null, "Enter the sales amount\n(do not use comma's or dollar signs)\n or click cancel to exit:");

    if (answer == null) finish();

    try
    {
    sales = Double.parseDouble(answer);
    if (sales <=0) throw new NumberFormatException();
    else done = true;
    }
    catch(NumberFormatException e)
    {
    JOptionPane.showMessageDialog(null, "Your entry was not in the proper format. ", "Error" ,JOptionPane.INFORMATION_MESSAGE);
    }
    }
    return sales;
    }

    public static void finish()
    {
    System.exit(0);
    }

    public static int getCode()
    {
    int code = 0;
    boolean done = false;

    while(!done)
    {
    try
    {
    String message = "Enter the commission code:" + "\n\n) Telephone Sales\n2) In-Store Sales\n3) Ouside Sales\n\n";

    code = Integer.parseInt(JOptionPane.showInputDialog(null, message));

    if (code<1 || code>3) throw new NumberFormatException();
    else done = true;
    }
    catch(NumberFormatException e)
    {
    JOptionPane.showMessageDialog(null, "Please enter a 1, 2, or 3,","Error",JOptionPane.INFORMATION_MESSAGE);
    }
    }
    return code;
    }

    public static double getComm(double employeeSales, int employeeCode)
    {

    double commission = 0.0;

    switch(employeeCode)
    {
    case 1:
    commission = .10 * employeeSales;
    break;

    case 2:
    commission = .14 * employeeSales;
    break;

    case 3:
    commission = .18 * employeeSales;
    break;
    }
    return commission;
    }

    public static void output(double commission, double sales)
    {
    DecimalFormat twoDigits = new DecimalFormat("$#,000.00");

    JOptionPane.showMessageDialog(null, "Your commission on sales of "+twoDigits.format(sales) + " is " + twoDigits.format(commission),"Commission Totals", JOptionPane.INFORMATION_MESSAGE);
    }
    }


    Applet work

    /* Commission Applet


    */

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;

    public class CommissionApplet extends Applet implements ItemListener Where I get my first error.
    {
    double dollars, answer;
    int empCode;
    Image dollarSign;
    Color darkRed = new Color(160, 50, 0);

    public void init()
    {

    }

    public void itemStateChange(ItemEvent choice)
    {
    setBackground(darkRed);
    setForeground(Color.white);
    add(promptLabel);
    add(salesField);
    salesField.requestFocus();
    salesField.setForeground(Color.black);
    add(codeLabel);
    add(telphoneBox);
    telephoneBox.addItemListener(this);
    add(inStoreBox);
    inStoreBox.addItemListener(this);
    add(outsideBox);
    outsideBox.addItemListener(this);
    add(outputLabel);
    }
    Label promptLabel = new Label("Enter the sales amount (do not use commas or dollar signs):");
    TextField salesField = new TextField(20);

    Label codeLabel = new Label("Select the appropriate commission code:");

    CheckboxGroup codeGroup = new CheckboxGroup();
    Checkbox telephoneBox = new CheckboxGroup("Telephone Sales",false,codeGroup);
    Checkbox inStoreBox = new CheckboxGroup("In-Store Sales",false,codeGroup);
    Checkbox outsideBox = new Checkbox("Outside Sales", false,codeGroup);
    Checkbox hiddenBox = new Checkbox("",true,codeGroup);

    Label outputLabel = new Label ("Click an option button to calculate the sales commission.");


    public void itemStateChange(ItemEvent choice)
    {
    try
    {
    dollars = getSales();
    empCode = getCode();
    answer = getComm(dollars,empCode);
    output(answer, dollars);
    }

    catch(NumberFormatException e)
    {
    outputLabel.setText("You must enter a dollar amount greater then zero.");
    hiddenBox.setState(true);
    salesField.setText("");
    salesField.requestFocus();
    }
    }

    public double getSales()
    {
    double sales = Double.parseDouble(salesField.getText());

    if (sales <= 0) throw new NumberFormatException();

    return sales;
    }

    public int getCode()
    {
    int code = 0;
    if (telephoneBox.getState()) code = 1;
    else
    if (inStoreBox.getState()) code = 2;
    else
    if (outsideBox.getState()) code = 3;
    return code;
    }
    public double getComm(double sales, int code)
    {
    double commission = 0.0;
    switch (code)
    {
    case 1:
    commission = .10 * sales;
    break;

    case 2:
    commission = .14 * sales;
    break;

    case 3:
    commission = .18 * sales;
    break;

    }
    return commission;
    }

    public void output (double commission, double sales)
    {
    DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
    outputLabel.setText("Your commission on sales of " + twoDigits.format(sales) + " is " + twoDigits.format(commission));
    }

    public void paint(Graphics g)
    {
    dollarSign = getImage(getDocumentBase(), "dollarSign.gif");
    g.drawImage(dollarSign,12,28,this);
    }
    }


    5 errors found:
    File: C:\Users\James Turnham\Documents\Rasmussen School Folder\Week 05 assignment.java [line: 11]
    Error: class CommissionApplet is public, should be declared in a file named CommissionApplet.java
    File: C:\Users\James Turnham\Documents\Rasmussen School Folder\Week 05 assignment.java [line: 32]
    Error: cannot find symbol
    symbol: variable telphoneBox
    location: class CommissionApplet
    File: C:\Users\James Turnham\Documents\Rasmussen School Folder\Week 05 assignment.java [line: 46]
    Error: constructor CheckboxGroup in class java.awt.CheckboxGroup cannot be applied to given types;
    required: no arguments
    found: java.lang.String,boolean,java.awt.CheckboxGroup
    reason: actual and formal argument lists differ in length
    File: C:\Users\James Turnham\Documents\Rasmussen School Folder\Week 05 assignment.java [line: 47]
    Error: constructor CheckboxGroup in class java.awt.CheckboxGroup cannot be applied to given types;
    required: no arguments
    found: java.lang.String,boolean,java.awt.CheckboxGroup
    reason: actual and formal argument lists differ in length
    File: C:\Users\James Turnham\Documents\Rasmussen School Folder\Week 05 assignment.java [line: 54]
    Error: method itemStateChange(java.awt.event.ItemEvent) is already defined in class CommissionApplet

    Any help would be appreciated.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Making a java program into an applet

    Your posted code has lost its formatting making it very difficult to read. Consider editing your original post adding [code=java] [/code] tags around your code. The first tag goes above your posted code block and the second tag goes below the code block.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Making a java program into an applet

    Also, the error messages are telling you what your errors are. You need to name the file that holds the CommissionApplet class CommissionApplet.java. Next, there is no variable named "telphoneBox". My guess -- you're misspelling it. Have a look at your other error messages and try to interpret them yourself. You'll be surprised at how informative they really are.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a java program into an applet

    Quote Originally Posted by curmudgeon View Post
    Also, the error messages are telling you what your errors are. You need to name the file that holds the CommissionApplet class CommissionApplet.java. Next, there is no variable named "telphoneBox". My guess -- you're misspelling it. Have a look at your other error messages and try to interpret them yourself. You'll be surprised at how informative they really are.
    I've got the errors fixed down to 1 and i've named the file CommissionApplet.java and it's in the same folder as the Commission.java so I don't understand what the message is. I fixed my spelling errors and formatting and just have the one.

    Is there a specific place I need to put this CommissionApplet.java?

    Thanks

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Making a java program into an applet

    Please show your latest CommissionApplet file, using code tags, and the latest error message, again using code tags.

Similar Threads

  1. [SOLVED] Help with my Java program: Applet that draws 5 random circles.
    By iDizzle in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 27th, 2012, 02:20 AM
  2. [SOLVED] Help with my Java program: Making change from an entered double.
    By iDizzle in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 18th, 2012, 04:16 PM
  3. Replies: 0
    Last Post: October 13th, 2011, 08:05 PM
  4. So I'm making this program...
    By SkyAphid in forum The Cafe
    Replies: 2
    Last Post: August 29th, 2011, 06:55 PM
  5. java applet program question (on getting path fails)
    By hellocheese in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 30th, 2011, 05:34 PM