Skip to main content

Homework 2: Methods, Methods, Methods

In this assignment you will be working with variables, and reading in user data with the Scanner class, and creating methods

Requirements:

Note, in the examples, * indicates user input.

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:

  1. Day of the week, e.g. Sunday, Monday, …
  2. Month, e.g. June, July
  3. The day of the month, e.g. 10, 29, …
  4. The year 2023, 1990, 1754

Then create the following two methods:

Both methods should have four paramaters. The order of the parameters should match the order listed above.

$ javac Dates.java; java Dates
What is a day of the week? *Thursday*
What is the month? *January*
What is the day of the month? *26*
What is the year? *2023*

European format: Thursday, January 26, 2023
American format: Thursday 26 January 2023

2. 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. We removed public static for stylistic sakes, you can assume every method signature starts with public static

  1   int fallOf(int num) {
  2     System.out.println("1 fell off and bumped their head");
  3     return num - 1;
  4   }
  5 
  6   void callDoctor(String animal) {
  7     System.out.println("Momma called the doctor and the doctor said\nNo more " + animal + "jumping on the bed");
  8   }
  9 
 10   void jumping(int num, String animal) {
 11     System.out.printf("There were %d %s jumping on the bed", num, animal);
 12   }
 13 
 14   void jumping(String animal, int num) {
 15     System.out.printf("There were %d %s jumping on the bed", num, animal);
 16   }
 17 
 18   void song(String animal, int num) {
 19     jumping(animal, num);
 20     num = fallOf(num);
 21     callDoctor(animal);
 22     jumping(animal, num);
 23     num = fallOf(num);
 24     callDoctor(animal);
 25     jumping(animal, num);
 26     num = fallOf(num);
 27     callDoctor(animal);
 28   }
 29 
 30   void main() {
 31     song("monkeys", 4);
 32   }
 33 
 34   void main(String[] args) {
 35     song("monkeys", 4);
 36   }
 37 }
  1. Write down the line numbers in the order of which they are executed. For example, if java executed each line one at a time from the top of a file to the bottom, you would write something like 1, 2, 3, 4, ....

  2. What is the value of the parameter num in the void jumping(String animal, int num) method the first time that method is called.

  3. What is the output of this program.

Requirement: make to sure to seperate each line number by a comma (,), otherwise our autograder wont work and you will lose points.

3. 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 employess are paid at $15.00 an hour. Employees are not allowed to work more than 60 hours in a week.

Rules:

Directions

Create a new class called Payments. Write a method called pay() that takes the base pay and hours worked as parameters, and prints the total pay or an error. Write a main method that calls this method for each of these employees:

  Base Pay Hours Worked
Employee 1 $22.50 48
Employee 2 $14.90 38
Employee 3 $30.92 70
Employee 4 $16.75 29

README.txt

In a text file called README.txt answer the following questions:

  1. How much time did you spend on the homework
  2. What did you learn from this homework
  3. optional: What did you struggle with during this homework
  4. optional: any other feedback you would like to share

Dont forget: make sure to fill in the header in all of your java files.

Submitting

Submit the following files to the assignment called HW02 on Gradescope:

  1. Dates.java
  2. ControlFlow.txt
  3. Payments.java
  4. README.txt

Make sure to name these files exactly what we specify here. Otherwise, our autograders might not work and we might have to take points off.