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.
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.
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.
Re: Making a java program into an applet
Quote:
Originally Posted by
curmudgeon
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
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.