You are here:

Java/Coding does not work

Advertisement


Question
*** The Question: ***

Design, code and test a program which inputs a code for the name of a day ("Sun" for Sunday, "Wk" for other Day) together with 2 numbers representing normal hours working and over time hours worked.
Output the day name and the pay according to the following rules:
*Sunday: ordinary hours @ $15 per hour; overtime @ $20 per hour.
*Other Day: ordinary hours @ $8 per hour; overtime @ $12 per hour.

*** My Questions ***
1. I'd appreciate if you could tell me how many variable there are in this question/ program? I ended up coming up two variable for String and two variable for double.

2. I have tried my coding but it fails constantly. I would appreciate if you could direct me with the coding in this question.

My coding:

package chapter04Selection;

import java.util.Scanner;

//This program decides the amount of pay according to the day worked and the number of hours worked.

public class Payday
{

  public static void main(String[] args)
  {
     Scanner input = new Scanner(System.in);
     
     String dayCode, time;
     double normalHour, overtimeHour;
        
     System.out.println("Is it a Sunday (Sun) or a working day (Wk)?");
     dayCode = input.next();
     
     //Determine if the Day is a Sun or a WK
     
     if (dayCode.equals("Sun"))
     {
        System.out.println("Have you worked Over time (Y/N)");
        time = input.next();
        
        //Determine if person has worked over time or not.
        
        if (time.equals("Y"))
     
         //Input Number of hours worked.
        
         System.out.println("Enter Normal Working Hours");
        
         normalHour = input.nextDouble();
        
         //Input Overtime Hours worked
        
         System.out.println("Enter Overtime Hours");
        
         overtimeHour = input.nextDouble();
        
         //Display the Day and Pay the person will receive Overtime on Sun.
        
         System.out.println("Day worked: " + dayCode + " ." + "Your pay is: " + ((normalHour*15)+(overtimeHour*20)) );
        
        else if (time.equals("N"))
        
         //Display the Day and Pay the person will receive for Normal hours on Sun.
        
         System.out.println("Day worked: " + dayCode + " ." + "Your pay is: " + (normalHour*15));
        
        else
         System.out.println("Please enter Y or N");
        
     }
     else if (dayCode.equals("Wk"))
     {
        System.out.println("Have you worked Over time (Y/N)");
        time = input.next();
        
        //Determine if person has worked over time or not.
        
        if (time.equals("Y"))
        
         //Input Number of hours worked.
        
         System.out.println("Enter Normal Working Hours");
        
         normalHour = input.nextDouble();
        
         //Input Overtime Hours worked
        
         System.out.println("Enter Overtime Hours");
        
         overtimeHour = input.nextDouble();
        
         //Display the Day and Pay the person will receive Overtime on Wk.
        
         System.out.println("Day worked: " + dayCode + " ." + "Your pay is: " + ((normalHour*8)+(overtimeHour*12)) );
        
        else if (time.equals("N"))
        
         //Display the Day and Pay the person will receive for Normal hours on Sun.
        
         System.out.println("Day worked: " + dayCode + " ." + "Your pay is: " + (normalHour*8));
        
        else
         System.out.println("Please enter Y or N");
     }
     else
        System.out.println("Please enter Sun on Wk");
     

  }

}

Answer
1. Your missing input variable which is of type Scanner.

2.
You should move regular hours worked out of the if loop for overtime, as it applies for both overtime and non-overtime. That is, first ask the user their regular hours, then ask if they did over time, and if they use Y, ask for the overtime hours.

Also, don't just say your program fails... say why it fails, then investigate. Determine which lines of code where executed and which aren't.

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.