Forums Register Login

Review

+Pie Number of slices to send: Send
In reference to a message posted on Feb 07 at 8:11pm with the subject "Need Help."
I've taking CSC 160 (Java) and I'm still teaching myself programming and Java. I'm since transferred to another university that will not teaching java, but rather, C++. I thought I would give this problem a shot. Can someone review my program and let me know how I did on my approach.
//Gross.java
//Will calculate payrol for 3 employees
import java.awt.Container;
import javax.swing.*;
import java.text.DecimalFormat;
public class Gross {
public static void main( String args [] )
{
String employee1, employee1Hours, employee1Rate,
employee2, employee2Hours, employee2Rate,
employee3, employee3Hours, employee3Rate;
int e1, e2, e3;
double e1R, e1H, e2H, e3H, e2R, e3R, e1P, e2P, e3P;
// Prompt user for first employee name
employee1 = JOptionPane.showInputDialog(
"Enter the first employee's name. ");
// Prompt user for the first employee hours
employee1Hours = JOptionPane.showInputDialog(
"Enter the first employee's hours\n" +
"for the week. ");
// Prompt user for the first employee's rate
employee1Rate = JOptionPane.showInputDialog(
"Enter the first employee's pay rate. ");

// Prompt user for second employee name
employee2 = JOptionPane.showInputDialog(
"Enter the second employee's name. ");
// Prompt user for the second employee hours
employee2Hours = JOptionPane.showInputDialog(
"Enter the second employee's hours\n" +
"for the week. ");
// Prompt user for the second employee's rate
employee2Rate = JOptionPane.showInputDialog(
"Enter the second employee's pay rate. ");
// Prompt user for third employee name
employee3 = JOptionPane.showInputDialog(
"Enter the third employee's name. ");
// Prompt user for the third employee hours
employee3Hours = JOptionPane.showInputDialog(
"Enter the third employee's hours\n" +
"for the week. ");
// Prompt user for the third employee's rate
employee3Rate = JOptionPane.showInputDialog(
"Enter the third employee's pay rate. ");

// Conversion of strings to doubles
e1H = Integer.parseInt( employee1Hours );
e2H = Integer.parseInt( employee2Hours );
e3H = Integer.parseInt( employee3Hours );
e1R = Double.parseDouble( employee1Rate );
e2R = Double.parseDouble( employee2Rate );
e3R = Double.parseDouble( employee3Rate );
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
// Calculation of all employees Pay
if ( e1H <= 40 )
e1P = (double) e1H * e1R;
else
e1P = (double) ((e1H * 2) + (e1H/2)) * e1R;
if ( e2H <= 40 )
e2P = (double) e2H * e2R;
else
e2P = (double) ((e2H * 2) + (e2H / 2)) * e2R;
if ( e3H <= 40 )
e3P = (double) e3H * e3R;
else
e3P = (double) ((e3H * 2) + (e3H / 2)) * e3R;
JTextArea outputArea = new JTextArea( 4, 20 );
outputArea.setText( "Name\tHours\tPay" +
"\n" + employee1 + "\t" + e1H + "\t" + "$" + twoDigits.format( e1P ) +
"\n" + employee2 + "\t" + e2H + "\t" + "$" + twoDigits.format( e2P ) +
"\n" + employee3 + "\t" + e3H + "\t" + "$" + twoDigits.format( e3P ) );
JOptionPane.showMessageDialog( null, outputArea,
"Last Week's Pay",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}



+Pie Number of slices to send: Send
Jeff,
You program will work, however there are several things I would do differently:
1. When asking for the # of hours worked, do not split the lines. This will mess up the display. Just put the entire thing in one string "Enter the first employee's hours for the week."
2. You don't need the explicitly cast the result back to a double, because the JRE will do it for you automatically.

A couple of questions.
* Are you allowed to create other classes?
* Have you learned looping yet? E.g. for() loop.
* Have you learned about methods?
I don't know how much you know before I can make some more recommendation.
-Peter
+Pie Number of slices to send: Send
Yes. I know how to use the for, while, and do while control structures. I've learned methods also. The problem that I'm having diffuclty on with using a for loop is: How do I assign each result to a different variable without overwriting my variable every time it loops.
*I'm not sure what you mean when you ask, "Are you allowed to create other classes?"
I've studied arrays, but I'm weak in that area, so I'm going to do some more studying on arrays.
+Pie Number of slices to send: Send
Jeff,
Here's my version:

There are a couple of things I would have done differently, but I didn't want to be any more terse.
-Peter
[This message has been edited by Peter Tran (edited February 08, 2001).]
[This message has been edited by Peter Tran (edited February 08, 2001).]
+Pie Number of slices to send: Send
Peter,
I compiled the program that you wrote in order to see how it worked. It compiles, but when I try to execute it, is says
"Exception in thread "main" java.lang.NoClassDefFoundError: Gross"
What am I doing incorrectly?
+Pie Number of slices to send: Send
I tried it and it worked fine.
Make sure you enter
java Gross
+Pie Number of slices to send: Send
Also make sure that your CLASSPATH includes the directory that you .class files are in.
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1518 times.
Similar Threads
Passing data to methods
Else without if error
Identifier expected
clearing the applet window
Help with Println and Scanner
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:39:21.