Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-24-2007, 10:02 AM
Member
 
Join Date: Nov 2007
Posts: 8
badness is on a distinguished road
Beginner Needs Help w/ Program for School
Hello everyone,

I am completely new to this site and also to Java and programming. I am attending college online and am in my thirties. I have a Java class as part of my pursuit of an IT degree and am having a bit of trouble grasping all the concepts completely. I would appreciate some help, as my time is very limited for my reading for class and it is hard for me to read it all, though I am very desirous to learn. Below I am describing my assignments, which are already turned in for grading and I know aren't completely correct but I want to make them so, in order to understand and catch up. Thanks for any help you guys offer and I hope to be able to return the favor to others in the future, as I believe in paying it forward.

The program is a payroll program that each week we add to until we turn in the final project. I am using Netbeans 5.5.1 from the JDK I downloaded, f.y.i.

Payroll part 1 assignment req's:

Create a non-GUI based Java application that calculates weekly pay for an
employee. The application should display text that requests the user input the name of the employee, the hourly rate, and the number of hours worked for that week. The application should then print out the name of the employee and the weekly pay amount. In the printout, display the dollar symbol ($) to the left of the weekly pay amount and format the weekly pay amount to display currency. I came up with this using textbook info and guesswork:[/color] // Payroll.java
// Payroll program that displays the weekly pay of the employee.
import java.util.Scanner; // program uses class Scanner

public class Payroll
{
// main method begins execution of Java application
public static void main( String args[] )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );

int number1; // hours worked
int number2; // hourly pay rate
int sum; // total of hours worked multiplied by hourly pay rate

system.out.print ( “Enter employee name: “ ); // prompt
String theName = input.nextLine(); // read a line of text
Payroll.setEmployeeName( theName ); // set the employee name

System.out.print( "Enter hours worked: " ); // prompt
number1 = input.nextInt(); // read first number from user

System.out.print( "Enter hourly pay rate: " ); // prompt
number2 = input.nextInt(); // read second number from user

sum = number1 * number2; // multiply hours worked * hourly pay rate
System.out.printf( "Employee Name:,Weekly pay is $ %d\n", sum );

// display employee name and weekly pay total

} // end method main

} // end class Payroll

Can you help me with this and explain my errors?

Payroll part 2 req's are as follows:
Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should
prompt the user to enter a positive amount. Here is what I came up with, but wonder if the where loop statement would have been the right way:

// Payroll.java
// Payroll program that displays the weekly pay of the employee.
import java.util.Scanner; // program uses class Scanner

public class Payroll
{
// main method begins execution of Java application
public static void main( String args[] )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );

int number1; // hours worked
int number2; // hourly pay rate
int sum; // total of hours worked multiplied by hourly pay rate

system.out.println ( “Enter employee name: “ ); // prompt
String theName = input.nextLine(); // read a line of text
Payroll.setEmployeeName( theName ); // set the employee name

if (theName = Stop);
{
system.out.println( "Enter hours worked: " ); // prompt
number1 = input.nextInt(); // read first number from user
} //end if

else // Stop was not entered
system.out.println( “Re-enter employee name:” ); //prompt

if (number1 >= 0)
{
system.out.println( "Enter hourly pay rate: " ); // prompt
number2 = input.nextInt(); // read second number from user
} //end if
else
system.out.println(“Enter a positive number: );// prompt

`if(number2 >= 0)
{

sum = number1 * number2; // multiply hours worked * hourly pay rate

system.out.println( "Employee Name:,Weekly pay is $ %d\n", sum );

// display employee name and weekly pay total

}
else
system.out.println(“Enter a positive number: );// prompt

} // end method main

} // end class Payroll


Thank you all so much for any help, I will be eternally grateful. As I said these assignments are already turned in and I am just hopeful that someone here can aid me in fixing my mistakes and explain a little. Thank you.

Doug/Badness
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-24-2007, 02:09 PM
Member
 
Join Date: Nov 2007
Posts: 8
clement1 is on a distinguished road
if (theName = Stop);
{
system.out.println( "Enter hours worked: " ); // prompt
number1 = input.nextInt(); // read first number from user
} //end if

else // Stop was not entered
system.out.println( “Re-enter employee name:” ); //prompt

For this i would use a while loop instead of if statements.

while(theName != "Stop")
{
system.out.println( "Enter hours worked: " ); // prompt
number1 = input.nextInt(); // read first number from user
}

Basically it will keep asking unless the entered name is stop, its a more efficient way than using if statements.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-24-2007, 08:51 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
if (theName = Stop); {
The condition inside the if statement is not a boolean but reassigns the String "theName" to the variable "Stop" which if undeclared will result in a compile error "cannot find symbol".
Use the equals method to test for String equality. The "==" operator does not do well for testing String equality.
The semi–colon following the if statement counts as an empty statement and will cause the code inside the following curley braces to be skipped.
So change it to look lie this
Code:
if(theName.equals("Stop")) {
The next question is: what happens if the user types in "stop" instead of "Stop"?
Part 1
Code:
import java.util.Scanner; public class PayrollRx { public static void main( String[] args ) { Scanner input = new Scanner( System.in ); int hoursWorked; int hourlyPayRate; int weeklyPay; System.out.print ( "Enter employee name: " ); String theName = input.nextLine(); // Does this class have methods yet? // If so you would not refer to them this // way inside the main method. // Payroll.setEmployeeName( theName ); System.out.print( "Enter hours worked: " ); hoursWorked = input.nextInt(); System.out.print( "Enter hourly pay rate: " ); hourlyPayRate = input.nextInt(); // multiply hours worked * hourly pay rate weeklyPay = hoursWorked * hourlyPayRate; // display employee name and weekly pay total System.out.printf( "Employee Name: %s, Weekly pay is $%d%n", theName, weeklyPay ); } }
Part 2
Code:
import java.util.Scanner; public class PayrollRx { public static void main( String[] args ) { Scanner input = new Scanner( System.in ); boolean enterMoreData = true; while(enterMoreData) { System.out.print ( "Enter employee name: " ); String theName = input.nextLine(); if(theName.equalsIgnoreCase("stop")) { input.close(); break; } int hoursWorked; do { System.out.println("Enter hours worked: "); String line = input.nextLine(); hoursWorked = Integer.parseInt(line); if(hoursWorked < 0) System.out.println("number must be positive"); } while(hoursWorked < 0); int hourlyPayRate; do { System.out.println("Enter hourly pay rate: "); String line = input.nextLine(); hourlyPayRate = Integer.parseInt(line); if(hourlyPayRate < 0) System.out.println("number must be positive"); } while(hourlyPayRate < 0); // multiply hours worked * hourly pay rate int weeklyPay = hoursWorked * hourlyPayRate; // display employee name and weekly pay total System.out.printf( "Employee Name: %s, Weekly pay is $%d%n", theName, weeklyPay ); } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner to Java notwist New To Java 15 04-18-2008 10:41 AM
Taking Java In School Need Help xEuPhOrIcSx New To Java 7 02-04-2008 09:02 AM
Need help with a program for high school regarding multi-dimensional arrays. Torque New To Java 2 01-07-2008 08:45 PM
Please help... assignment for school confused2000 New To Java 3 11-12-2007 09:12 AM
beginner needs help with OBD-II input andrewos New To Java 3 07-30-2007 10:46 AM


All times are GMT +3. The time now is 11:27 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org