Skip to main content

Homework 2: Methods, Methods, Methods

Due: 2024-09-23 23:59:00EDT

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, … (e.g. stands for exempli gratia, meaning for example)
  2. Month, e.g. June, July
  3. The day of the month, e.g. 10, 29, …
  4. The year, e.g. 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? *Monday*
What is the month? *September*
What is the day of the month? *16*
What is the year? *2024*

American format: Monday, September 16, 2024
European format: Monday 16 September 2024

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 leaveHome(int num) {
  2     System.out.printf("%d little ducks went out one day\n", num);
  3     return num - 1;
  4   }
  5 
  6   void whereTheyGo() {
  7     System.out.printf("over the hill and far away,\n");
  8   }
  9
 10   void mommaSay() {
 11     System.out.printf("Momma duck said quak quak quak quak\n");
 12   }
 13 
 14   void comeHome(int returnDucks) {
 15     System.out.printf("but only %d ducks came back", returnDucks);
 16   }
 17 
 18   void whereTheyGo(String terrain) {
 19     System.out.printf("over the %s and far away,\n", terrain);
 20   }
 21 
 22   void jumping(String animal, int num) {
 23     System.out.printf("There were %d %s jumping on the bed", num, animal);
 24   }
 25 
 26   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
 39   void main() {
 40     song("monkeys", "mountain", 4);
 41   }
 42 
 43   void main(String[] args) {
 44     song("monkeys", "mountain", 4);
 45   }
 46 }
  1. 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 }
    
  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.