Results 1 to 3 of 3
  1. #1
    badness is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default 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

  2. #2
    clement1 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    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.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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
    Java Code:
    if(theName.equals("Stop"))
    {
    The next question is: what happens if the user types in "stop" instead of "Stop"?
    Part 1
    Java 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
    Java 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 );
            }
        }
    }

Similar Threads

  1. beginner to Java
    By notwist in forum New To Java
    Replies: 15
    Last Post: 04-18-2008, 09:41 AM
  2. Taking Java In School Need Help
    By xEuPhOrIcSx in forum New To Java
    Replies: 7
    Last Post: 02-04-2008, 08:02 AM
  3. Replies: 2
    Last Post: 01-07-2008, 07:45 PM
  4. Please help... assignment for school
    By confused2000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2007, 08:12 AM
  5. beginner needs help with OBD-II input
    By andrewos in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •