A. Introduction
Compare the following two programs, which is easier to read? why?
Program 1:
import java.util.*;
import java.io.*;
public class Timesheet {
public static void main (String[] args) throws IOException{
String input;
String inp[];
int location;
double start= 0;
double end =0;
double worked;
double paid ;
double salary = 0;
Scanner sc = new Scanner(new File("data/timeSheet.txt"));
for (int j=0;j<=3;j++) {
input = sc.nextLine();
inp = input.split(",");
inp[0] = inp[0].trim();
inp[1] = inp[1].trim();
inp[2] = inp[2].trim();
location=Integer.parseInt(inp[0]);
if(inp[1].equals("1"))
start = 9;
else if(inp[1].equals("2"))
start = 9.5;
else if(inp[1].equals("3"))
start = 10;
else if(inp[1].equals("4"))
start = 10.5;
else if(inp[1].equals("5"))
start = 11;
else if(inp[1].equals("6"))
start = 11.5;
else if(inp[1].equals("7"))
start = 12;
else if(inp[1].equals("8"))
start = 12.5;
else if(inp[1].equals("9"))
start = 13;
else if(inp[1].equals("A"))
start = 13.5;
else if(inp[1].equals("B"))
start = 14;
else if(inp[1].equals("C"))
start = 14.5;
else if(inp[1].equals("D"))
start = 15;
else if(inp[1].equals("E"))
start = 15.5;
else if(inp[1].equals("F"))
start = 16;
else if(inp[1].equals("G"))
start = 16.5;
else if(inp[1].equals("H"))
start = 17;
if(inp[2].equals("1"))
end = 9;
else if(inp[2].equals("2"))
end = 9.5;
else if(inp[2].equals("3"))
end = 10;
else if(inp[2].equals("4"))
end = 10.5;
else if(inp[2].equals("5"))
end = 11;
else if(inp[2].equals("6"))
end = 11.5;
else if(inp[2].equals("7"))
end = 12;
else if(inp[2].equals("8"))
end = 12.5;
else if(inp[2].equals("9"))
end = 13;
else if(inp[2].equals("A"))
end = 13.5;
else if(inp[2].equals("B"))
end = 14;
else if(inp[2].equals("C"))
end = 14.5;
else if(inp[2].equals("D"))
end = 15;
else if(inp[2].equals("E"))
end = 15.5;
else if(inp[2].equals("F"))
end = 16;
else if(inp[2].equals("G"))
end = 16.5;
else if(inp[2].equals("H"))
end = 17;
worked = end-start;
if( location <= 9)
paid = worked * 10;
else if( location >= 10 && location <= 19 ){
if (worked>4){
worked = worked - 4;
paid = 32;
paid += worked * 12;
}
else
paid = worked * 8;
}
else
{
if(worked<=4)
{
paid = worked * 12;
}
else
{
worked = worked - 4;
paid = 48;
paid += worked * 24;
}
}
System.out.printf("$%.2f\n", paid);
salary = salary + paid;
}
System.out.printf("$%.2f\n", salary);
}
}
Program 2:
import java.util.*;
import java.io.*;
class Timesheet2 {
public static void main (String[] args) throws IOException{
String input;
String inp[];
int location;
double startHour= 0;
double endHour =0;
double hours;
double paid ;
double salary = 0;
Scanner sc = new Scanner(new File("data/timeSheet.txt"));
for (int j=0;j<=3;j++) {
input = sc.nextLine();
inp = input.split(",");
inp[0] = inp[0].trim();
inp[1] = inp[1].trim();
inp[2] = inp[2].trim();
location=Integer.parseInt(inp[0]);
startHour = getStartHour(inp[1]);
endHour=getEndHour(inp[2]);
hours = endHour-startHour;
paid = calculatePay(hours, location);
System.out.printf("$%.2f\n", paid);
salary = salary + paid;
}
System.out.printf("$%.2f\n", salary);
}
public static double getStartHour(String s){
double start=0;
if(s.equals("1"))
start = 9;
else if(s.equals("2"))
start = 9.5;
else if(s.equals("3"))
start = 10;
else if(s.equals("4"))
start = 10.5;
else if(s.equals("5"))
start = 11;
else if(s.equals("6"))
start = 11.5;
else if(s.equals("7"))
start = 12;
else if(s.equals("8"))
start = 12.5;
else if(s.equals("9"))
start = 13;
else if(s.equals("A"))
start = 13.5;
else if(s.equals("B"))
start = 14;
else if(s.equals("C"))
start = 14.5;
else if(s.equals("D"))
start = 15;
else if(s.equals("E"))
start = 15.5;
else if(s.equals("F"))
start = 16;
else if(s.equals("G"))
start = 16.5;
else if(s.equals("H"))
start = 17;
return start;
}
public static double getEndHour(String e){
double end=0;
if(e.equals("1"))
end = 9;
else if(e.equals("2"))
end = 9.5;
else if(e.equals("3"))
end = 10;
else if(e.equals("4"))
end = 10.5;
else if(e.equals("5"))
end = 11;
else if(e.equals("6"))
end = 11.5;
else if(e.equals("7"))
end = 12;
else if(e.equals("8"))
end = 12.5;
else if(e.equals("9"))
end = 13;
else if(e.equals("A"))
end = 13.5;
else if(e.equals("B"))
end = 14;
else if(e.equals("C"))
end = 14.5;
else if(e.equals("D"))
end = 15;
else if(e.equals("E"))
end = 15.5;
else if(e.equals("F"))
end = 16;
else if(e.equals("G"))
end = 16.5;
else if(e.equals("H"))
end = 17;
return end;
}
public static double calculatePay(double hours, int location){
double paid=0;
if( location <= 9)
paid = hours * 10;
else if( location >= 10 && location <= 19 ){
if (hours>4){
hours = hours - 4;
paid = 32;
paid += hours * 12;
}
else
paid = hours * 8;
}
else
{
if(hours<=4)
{
paid = hours * 12;
}
else
{
hours = hours - 4;
paid = 48;
paid += hours * 24;
}
}
return paid;
}
}
B. Some benefit of using user-defined functions
Breaking down a big task into small steps so the code is easier to read, maintain and debug.
It made team collaboration possible, and promote better designs.
C. Try:
Improve the three functions above: getStartHour(String s), getEndHours(String e), calculatePay(double hours, int location)