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:

  1. On Mac, open the Terminal program. On Windows, open the PowerShell program.

  2. Log into goldengate by using the ssh command from your personal laptop: ssh your-username@goldengate.cs.brynmawr.edu.

  3. 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:

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:

  1. Day of the week (ex: Sunday, Monday)

  2. Month (ex: June, July)

  3. The day of the month (ex: 10)

  4. The year (ex: 1994)

Then create the following two methods:

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:

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:


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:

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 }
 70 void foo() {
 71   int a = 0;
 72   double b = a + 5.0;
 73   return;
 74 }

Grading Rubric

This assignment will be out of 10 points:


Submitting

In a text file (call vim readme.txt) answer the following questions:

  1. How much time did you spend on the homework?

  2. 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.

  1. Dates.java

  2. Payments.java

  3. answers.txt

  4. controlFlow.txt

  5. readme.txt