Homework 3: Recursion, Loops, and Conditionals

Set Up

In this assignment you will solve some problems using both recursion and loops.

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 hw3 to create your homework 3 directory. Finally, call cd hw3.

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 NumbersRecursion.java.

Remember your vim commands:

NOTE: For problems 2 and 3, you will need to get input from the user. For problem 1, you do not need to do this, just test out your function in your main function.


1. Numbers

Write a program called Numbers.java that implements the following methods. Make sure to test the methods in the file’s main method.


1.1 Previous Even

For this problem, we want to write two functions that solve the same problem, first using recursion, and then using loops. The methods should each take an integer and print out all the previous even numbers.

For example, previousEven(6) should print out 6 4 2, previousEven(9) should print out 8 6 4 2. Make sure to put a space between the numbers and print all the numbers on one line. It is okay if there is a space after the last number.

Be sure to try calling the function from your main function to test it out. You do not need to accept user input, but can if you would like to use it for testing purposes.

TODO: Write a method called previousEvenRec that solves this problem recursively. TODO: Write a method called previousEvenLoop that solves this problem with loops.


1.2 Product of Previous Odd

For this problem, we want to write two functions that solve the same problem, first using recursion, and then using loops. The methods should each take an integer and print out the product of the previous odd numbers.

For example, productOfPreviousOdd(9) would return 945 because it would multiply 9 by 7 by 5 by 3 by 1.

TODO: Write a method called productOfPreviousOddRec that solves this problem recursively. TODO: Write a method called productOfPreviousOddLoop that solves this problem with loops.


2. Calendar:

Write a program called Month.java that gets an integer representing a month from the user and prints the corresponding month name.

For example:

$ java Month
Enter an integer: 1
January

$ java Month
Enter an integer: 10
October

3. Timer

Write a program, Timer.java that computes the time when the user will finish all of their tasks. The program should ask the user for the following information:

  1. The current time

  2. How many tasks they need to complete

  3. How long a task takes (you can assume that each task takes the same amount of time).

Below is an example of how your program should work.

$ javac Tasks.java
$ java Tasks
Enter the current hour:
23
Enter the number of tasks:
1
Enter the number of hours a task takes:
5

You will finish at 4:00 o'clock.

$ javac Tasks.java
$ java Tasks
Enter the current hour:
9
Enter the number of tasks:
3
Enter the number of hours a task takes:
2

You will finish at 15:00 o'clock.

Grading Rubric

This assignment will be out of 12 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: Numbers.

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/hw3/ .

Make sure to replace <username> with your username. There is a space between hw3/ 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 HW03 on Gradescope.

  1. Numbers.java

  2. Calendar.java

  3. Tasks.java

  4. readme.txt