Homework 2: Methods
Set Up
In this assignment you will be working with variables, and reading in user data with the Scanner class, and creating methods.
To work on this homework, follow these steps:
-
On Mac, open the Terminal program. On Windows, open the PowerShell program.
-
Log into goldengate by using the ssh command from your personal laptop:
ssh your-username@goldengate.cs.brynmawr.edu. -
Run these commands:
$ cd cs113
$ ls
If you see a homeworks directory, cd into it. Then, call mkdir hw2 to create your homework 2
directory. Finally, call cd hw2.
For each of the problems below, we will use the vim editor. You will need to create a java file
for each problem. For example, for the first problem you will call vim Dates.java.
Remember your vim commands:
-
vim Filename.java: creates a file named Filename.java -
i: enter insert mode -
esc: exit insert mode (you must exit insert mode in order to save and quit) -
:wq: save the file and close vim
NOTE: Every method you create should be tested by calling it from the main method.
1. Dates
Write a program called Dates.java that will print out dates in
different formats. The program should ask the user for the
following information:
-
Day of the week (ex: Sunday, Monday)
-
Month (ex: June, July)
-
The day of the month (ex: 10)
-
The year (ex: 1994)
Then create the following two methods:
-
printEuropean: this prints the information in the European format. -
printAmerican: this prints the information in the American format.
Both methods should take in four parameters. For example:
public static void printAmerican(String dayOfWeek, String month, String dayOfMonth, String year)
Notice that you do not need to parse the inputs to integers. This is because we are simply taking in user input and printing it in a certain format.
Your main function should get the week, month, day of the month,
and year from the user. It should then call printAmerican()
and printEuropean().
Here is an example of how your program should work.
$ javac Dates.java
$ java Dates
Give me a day of the week.
Monday
Give me a month.
September
Give me a day of the month.
16
Give me a year.
2024
American format: Monday, September 16, 2024
European format: Monday 16 September 2024
Your program should be able to handle numbers with decimal points.
2. Payments
BMC needs a program to calculate how much to pay their hourly employees. The US Department of Labor requires that employees get paid time and a half for any hours over 40 that they work in a single week. For example, if an employee works 45 hours, they get 5 hours of overtime, at 1.5 times their base pay. BMC employees are paid at $15.00 an hour. Employees are not allowed to work more than 60 hours in a week.
Rules:
-
An employee gets paid (hours worked) × (base pay), for each hour up to 40 hours.
-
For every hour over 40, they get overtime = (base pay) × 1.5.
-
The base pay must not be less than the minimum wage ($15.00 an hour). If it is, print a message.
-
If the number of hours is greater than 60, print an error message.
Directions:
Create a new file called Payments: vim Payments.java.
Paste in this base code:
class Payments {
public static void main(String[] args) {
}
public static void pay(double basePay, int hours) {
}
}
In your main() method, call pay() for each of these employees, and print out their payment amount:
-
Base pay: $22.50 Hours: 48
-
Base pay: $14.90 Hours: 38
-
Base pay: $30.92 Hours: 70
-
Base pay: $16.75 Hours: 29
3. More Mysteries
For parts 3.1 and 3.2, put your answers in a text file. Call vim answers.txt
to create an answers.txt file where you can put your answers.
For each program below, identify the error, label the type of error it is, and explain how it can be fixed.
As a reminder, these are the different types of errors we have discussed in class:
-
Syntax errors are issues that a compiler will catch that will prevent the program from compiling
-
Runtime errors are issues that cause the program to crash when the program is running.
-
Logic errors are issues in the algorithm. For example, say we set minimum wage to $13.00 / hr instead of $15.00 as is specified by the rules in problem 2 (payments).
3.1 Mystery 2
import java.util.Scanner;
public class Mystery2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userInput = input.nextLine();
int value = Integer.parseInt(input);
double fraction = value / 10;
System.out.println("The fraction is "+fraction);
}
}
What is the error? Is the error is syntax error, runtime error, or logic error? How can we fix the problem?
Hint: what happens when you divide two integers, and save the result as a double?
3.2 Mystery 3
import java.util.Scanner;
public class Mystery3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userInput = input.nextLine();
int value = Integer.parseInt(input);
double fraction = value / 0;
System.out.println("The fraction is "+fraction);
}
}
What is the error? Is the error is syntax error, runtime error, or logic error? How can we fix the problem?
4. Control Flow
For this problem you will walk through the flow of execution
of a program with multiple methods.
Read the following code and answer the questions in a
file called controlFlow.txt.
1 public static int leaveHome(int num) {
2 System.out.printf("%d little ducks went out one day\n", num);
3 return num - 1;
4 }
5
6 public static void whereTheyGo() {
7 System.out.printf("over the hill and far away,\n");
8 }
9
10 public static void mommaSay() {
11 System.out.printf("Momma duck said quak quak quak quak\n");
12 }
13
14 public static void comeHome(int returnDucks) {
15 System.out.printf("but only %d ducks came back", returnDucks);
16 }
17
18 public static void whereTheyGo(String terrain) {
19 System.out.printf("over the %s and far away,\n", terrain);
20 }
21
22 public static void jumping(String animal, int num) {
23 System.out.printf("There were %d %s jumping on the bed", num, animal);
24 }
25
26 public static void song(String animal, String place, int num) {
27 num = leaveHome(num);
28 whereTheyGo();
29 mommaSay();
30 comeHome(num);
31 num = leaveHome(num);
32 whereTheyGo(place);
33 mommaSay();
34 comeHome(num) // was accidently num = comeHome(num);
35 jumping(animal, num);
36 comeHome(num) // was accidently num = comeHome(num);
37 }
38
43 public static void main(String[] args) {
44 song("monkeys", "mountain", 4);
45 }
46 }
-
Write down the line numbers in the order of which they are executed. For example, if java we executed the foo method shown below, you would write just 71, 72, 73 - you do not need to include the method signature or the lines with just closing brackets.
70 void foo() {
71 int a = 0;
72 double b = a + 5.0;
73 return;
74 }
-
What is the value of the parameter
numin thevoid jumping(String animal, int num)method the first time that method is called? -
What is the output of this program?
Grading Rubric
This assignment will be out of 10 points:
-
Dates: 2 points
-
Payments: 3 points
-
Mysteries: 2 points
-
Control Flow: 3 points
Submitting
In a text file (call vim readme.txt) answer the following questions:
-
How much time did you spend on the homework?
-
What did you learn from the homework?
For 2 extra credit points: Run this command on your java files before submitting: astyle --style=java Example.java.
Replace Example with the name of your java file, for example: Dates.
Finally, you need to copy your files from the server to your personal computer for submission.
Close your connection to goldengate (type exit), and then run the following command:
$ scp -r <username>@goldengate.cs.brynmawr.edu:/home/<username>/cs113/homeworks/hw2/ .
Make sure to replace <username> with your username. There is a space between hw2/ and .. scp is a command to secure-copy files from one server to another. In this case, from goldengate to your own machine.
Submit the following files to the assignment called HW02 on Gradescope.
-
Dates.java -
Payments.java -
answers.txt -
controlFlow.txt -
readme.txt