1

Our teacher asks us to create a java program about the situation given which is the pay rate per hour using if/else this is what I came out with and Im happy about it (Im just a beginner). Then the next instruction is to replace the if/else with switch statements. He hasn't discussed switch statements yet. I try to search them but I couldn't understand. Please help. Much appreciated.

This is the exact instruction: 1.Using an if/else construct write a program that will calculate a persons pay based on the number of hours worked. Take note of the ff: The pay rate is Php 35 per hour. Any hours worked after 40 hours is paid at the rate of 1.5 times the normal hour rate. Calculate and display the normal pay, the overtime pay and the total pay. 2. Modify your previous work by replacing the if/else construct with the switch statement.

   import java.util.Scanner;
    public class Payment2 {

    public static void main (String [] args){
    Scanner input=new Scanner(System.in);
    double hours;
    double Prph = 35;  //Prph means Pay rate per hour//


    System.out.println("Enter your working hours:");
    hours=input.nextDouble();
    if (hours <= 40){
        System.out.println("Your pay rate for the day is Php "+hours * Prph);
    }
    else if (hours > 40){
        double Ovtime= hours - 40;
        double NPay= hours * Prph;
        double OvPay= Ovtime * 52.5;

        System.out.println("Normal Pay:             Php "+         NPay);
        System.out.println("Overtime Pay:           Php " +       OvPay);
        System.out.println("\nYour total pay rate is: Php "+ (NPay+OvPay));
    }

  }
}
Jevi
  • 1
  • 5
  • 3
    Are you sure about the instructions? This `if` does not lend itself to a `switch`. Maybe they want to offer different rates (like for PHP, for Java, for car washing, etc)? – Thilo Sep 29 '16 at 10:15
  • This is the exact instruction: 1.Using an if/else construct write a program that will calculate a persons pay based on the number of hours worked. Take note of the ff: The pay rate is Php 35 per hour. Any hours worked after 40 hours is paid at the rate of 1.5 times the normal hour rate. Calculate and display the normal pay, the overtime pay and the total pay. 2. Modify your previous work by replacing the if/else construct with the switch statement. thats it... or maybe my program is wrong??????? – Jevi Sep 29 '16 at 10:17
  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – 001 Sep 29 '16 at 10:17
  • You can only use **switch case** with constant **case**.And here the conditions are not constant. – Mehraj Malik Sep 29 '16 at 10:18

3 Answers3

2

Ill write this out to maybe illustrate, but I think your question needs to be revisited.

int a = hours<=40?0:1;
switch(a){
    case 0:
        //do <=40 stuff.
        break;
    case 1:
       //do >40 stuff.
       break;
}

In this way you could describe any number (well an integer number) of cases and switch on them.

matt
  • 10,892
  • 3
  • 22
  • 34
1

Besides in Java you can not define ranges for switch directly (look at this QA: In Java,Using switch statement with a range of value in each case?), in this particular case you can try do such trick, just to show your teacher, that you do understand switch operator:

switch((int)(hours/40)) {
   case 0: case 1: {
     System.out.println("Your pay rate for the day is Php "+hours * Prph); 
     break; 
   }
   default: {
      double Ovtime= hours - 40;
      double NPay= hours * Prph;
      // other stuff
   }
}
Community
  • 1
  • 1
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
1
String TimeUsed = (hours<=40)?"Normal":"Overtime";
switch(TimeUsed)
{
case "Normal":
// 35 to pay 
break;

case "Overtime":
// 1.5 times of 35  to pay
break;

}

In your case, you can try string , int , char or Enum to practice switch case

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Venkat
  • 2,549
  • 2
  • 28
  • 61