A. Introduction
We have learned standard library methods in 2.5. The other category of methods (a.k.a functions) is user-defined methods.
B. Watch (PPT)
What is a static method in Java?
Java is an object-oriented programming language but so far we have not used object-oriented concepts such as instances of a class. A "static" method means the method belongs to the class, not an instance of the class. For instance "main" is the start of all other methods, it is static. All the methods we create for now are static.
2. Where should I define my methods?
Inside the class but outside main, either before main or after main. The order doesn't matter, a method has to be called by main to run.
C. Try
1. Create two methods as described here
2. Create a static method outside main to return the average of an array of double values.
https://repl.it/@Brainseater905/ClientsideStainedState There are a bunch of problems, so can we review it on the next day?
import java.io.IOException;
import java.io.FileReader;
import java.util.*;
class Hours {
public static double getHours(int start, int end)
{
double hours=0;
hours=((end-start)*0.5);
return hours;
}
public static int getTime(String time)
{
int end = 0;
switch(time)
{
case "A": end = 10;break;
case "B": end = 11;break;
case "C": end = 12;break;
case "D": end = 13;break;
case "E": end = 14;break;
case "F": end = 15;break;
case "G": end = 16;break;
case "H": end = 17;break;
default:end = Integer.parseInt(time);
}
return end;
}
public static double getRate(int location)
{
double rate = 0;
if (location<=199 && location>=100)
{
rate = 10.00;
}
if (location<=299 && location>=200)
{
rate = 7.50;
}
if (location<=399 && location>=300)
{
rate = 9.25;
}
if (location<=499 && location>=400)
{
rate = 6.75;
}
if (location<=599 && location>=500)
{
rate = 8.00;
}
return rate;
}
public static double getORate(int location)
{
double Orate = 0;
if (location<=199 && location>=100)
{
Orate = 15.00;
}
if (location<=299 && location>=200)
{
Orate = 15.00;
}
if (location<=399 && location>=300)
{
Orate = 10.50;
}
if (location<=499 && location>=400)
{
Orate = 13.50;
}
if (location<=599 && location>=500)
{
Orate = 12.00;
}
return Orate;
}
public static void main(String[] args)throws IOException {
Scanner data =new Scanner(new FileReader("data/Data.TXT"));
//for(int i = 0;i<4;i++)
{
for(int e = 0; e<2;e++)
{
int location = data.nextInt();
System.out.println(location);
int day = data.nextInt();
System.out.println(day);
double hours = getHours(getTime(data.next()),getTime(data.next()));
System.out.println(hours);
if (location<=199 && location>=100)
{
if (hours>5)
{
System.out.println((getORate(location)*(hours-5)+(5*getRate(location))));
}
else
{
System.out.println((hours*getRate(location)));
}
}
if (location<=299 && location>=200)
{
if (hours>6)
{
System.out.println((getORate(location)*(hours-5)+(5*getRate(location))));
}
else
{
System.out.println((hours*getRate(location)));
}
}
if (location<=399 && location>=300)
{
if (hours>4)
{
System.out.println((getORate(location)*(hours-5)+(5*getRate(location))));
}
else
{
System.out.println((hours*getRate(location)));
}
}
if (location<=499 && location>=400)
{
if (day==1||day==7)
{
System.out.println((getORate(location)*(hours)));
}
else
{
System.out.println((hours*getRate(location)));
}
}
if (location<=599 && location>=500)
{
if (hours>6)
{
System.out.println((getORate(location)*(hours-6)+(6*getRate(location))));
}
else
{
System.out.println((hours*getRate(location)));
}
}
}
data.nextLine();
System.out.println();
}
}
}
import java.util.Scanner;
import java.io.*;
class Timesheet {
public static double getTime(String timeCode){
double time = 0;
switch(timeCode){
case "1": time = 9; break;
case "2": time = 9.5; break;
case "3": time = 10; break;
case "4": time = 10.5; break;
case "5": time = 11; break;
case "6": time = 11.5; break;
case "7": time = 12; break;
case "8": time = 12.5; break;
case "9": time = 13; break;
case "A": time = 13.5; break;
case "B": time = 14; break;
case "C": time = 14.5; break;
case "D": time = 15; break;
case "E": time = 15.5; break;
case "F": time = 16; break;
case "G": time = 16.5; break;
case "H": time = 17; break;
default: System.exit(1);
}
return time;
}
public static double subtractHours(String start, String end){
return getTime(end) - getTime(start);
}
public static double getRegularRate(int location){
double regularRate = 0;
if(location<=199){regularRate = 10;}
else if(location>=200 && location<=299){regularRate = 7.5;}
else if(location>=300 && location<=399){regularRate = 9.25;}
else if(location>=400 && location<=499){regularRate = 6.75;}
else{regularRate = 8;}
return regularRate;
}
public static double getOvertimeRate(int location){
double overtimeRate = 0;
if(location<=199){overtimeRate = 15;}
else if(location>=200 && location<=299){overtimeRate = 15;}
else if(location>=300 && location<=399){overtimeRate = 10.5;}
else if(location>=400 && location<=499){overtimeRate = 13.5;}
else{overtimeRate = 12;}
return overtimeRate;
}
public static double getHoursOfOvertime(int location, int day, double totalhours){
double hours = 0;
if(location<=199){
hours = Math.max(0, totalhours-5);
}
else if(location>=200 && location<=299){
hours = Math.max(0, totalhours-6);
}
else if(location>=300 && location<=399){
hours = Math.max(0, totalhours-4);
}
else if(location>=400 && location<=499){
if(day==1 || day==7){hours = totalhours;}
}
else{
hours = Math.max(0, totalhours-6);
}
return hours;
}
public static double getPay(int location, int day, String start, String end){
double hours = subtractHours(start, end);
double pay = (hours-getHoursOfOvertime(location, day, hours))*getRegularRate(location);
pay += getHoursOfOvertime(location, day, hours) * getOvertimeRate(location);
return pay;
}
public static void main(String[] args) throws IOException {
Scanner leslie = new Scanner(new FileReader("data/input.txt"));
for(int i=0; i<5; i++){
String inputs[] = leslie.nextLine().split(", ");
double pay = getPay(Integer.parseInt(inputs[0]), Integer.parseInt(inputs[1]), inputs[2], inputs[3]);
pay += getPay(Integer.parseInt(inputs[4]), Integer.parseInt(inputs[5]), inputs[6], inputs[7]);
System.out.print("$" + pay);
if((pay*100)%10 == 0){System.out.println("0");}
else{System.out.println();}
}
}
}
import java.util.Scanner;
class Mario {
public static void main(String[] args) {
Scanner karl=new Scanner (System.in);
double total=0.0;
for(int i=1;i<=4;i++){
int locationA = karl.nextInt();
int dayA = karl.nextInt();
String startA = karl.next();
String endA = karl.next();
int locationB =karl.nextInt();
int dayB =karl.nextInt();
String startB = karl.next();
String endB=karl.next();
double hoursA=(convert(endA)-convert(startA))*0.5;
double hoursB=(convert(endB)-convert(startB))*0.5;
double pay=0.0;
double payA=0.0;
double payB=0.0;
if (locationA>=400 && locationA<=499){
if (dayA==1||dayA==7)payA=hoursA*13.5;}
else if (hoursA>overtimeStart(locationA)){
payA=overtimeStart(locationA)*regularR(locationA)+(hoursA-overtimeStart(locationA))*overR(locationA);
}
else payA=hoursA*regularR(locationA);
if (locationB>=400 && locationB<=499){
if (dayB==1||dayB==7)payB=hoursB*13.5;}
else if (hoursB>overtimeStart(locationB)){
payB=overtimeStart(locationB)*regularR(locationB)+(hoursB-overtimeStart(locationB))*overR(locationB);
}
else payB=hoursB*regularR(locationB);
pay = payA+payB;
System.out.println(pay);
}
}
public static int convert(String ins){
int outint=0;
if(ins.equals("A"))outint=10;
else if(ins.equals("B"))outint=11;
else if(ins.equals("C"))outint=12;
else if(ins.equals("D"))outint=13;
else if(ins.equals("E"))outint=14;
else if(ins.equals("F"))outint=15;
else if(ins.equals("G"))outint=16;
else if(ins.equals("H"))outint=17;
else outint=Integer.parseInt(ins);
return outint;
}
public static double regularR(int location){
double rate=0.0;
if(location>=100 && location<=199)rate=10.0;
else if(location>=200 && location<=299)rate=7.5;
else if(location>=300 && location<=399)rate=9.25;
else if(location>=400 && location<=499)rate=6.75;
else if(location>=500 && location<=599)rate=8.0;
return rate;
}
public static double overR (int location){
double rate=0.0;
if(location>=100 && location<=199)rate=15.0;
else if(location>=200 && location<=299)rate=15.0;
else if(location>=300 && location<=399)rate=10.50;
else if(location>=400 && location<=499)rate=1.0;
else if(location>=500 && location<=599)rate=12.0;
return rate;
}
static public int overtimeStart (int location){
int overtimeStart=0;
if(location>=100 && location<=199)overtimeStart=5;
else if(location>=200 && location<=299)overtimeStart=6;
else if(location>=300 && location<=399)overtimeStart=4;
else if(location>=400 && location<=499)overtimeStart=0;
else if(location>=500 && location<=599)overtimeStart=6;
return overtimeStart;
}
}