Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Review

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 );
}
}



 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Pickett
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Jeff Pickett
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it and it worked fine.
Make sure you enter
java Gross
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also make sure that your CLASSPATH includes the directory that you .class files are in.
 
I am mighty! And this is a mighty small ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic