CISC181 S2015 Lab2
Contents
Preliminaries
- Either on your machine or on an eCalc machine in lab:
- Just like last time, make a new project (but now n = 2) following these instructions.
- Make sure your existing project is closed first
- This time instead of calling your class "MyClass", make it "Lab2". This happens when you are creating a new module, in the Java class name field
- Make sure you can compile and run a "Hello, world" program before you start to code the exercises below
- If you have trouble creating or running your new project, don't get hung up on this step! Here's a temporary solution:
- Create a new public class Lab2 in MyClass.java in your old MyLab_1 project
- This will prompt Android Studio to put a squiggly red line underneath it. If you click on it a light bulb icon should appear nearby. Click this and choose Move class 'Lab2' to 'Lab2.java' from the pop-up menu
- Now put a public static void main(String[] args) { ... } into the Lab2 class in Lab2.java
- In the Select Run/Debug Configuration pop-up menu next to the green triangle button, select Edit Configurations and change the Main class field to end in "Lab2" instead of "MyClass". You should now be able to code in Lab2!
- When you have time, talk to your classmates, the TA, or the instructor to figure out where you are going wrong on the new project creation. This approach is only a hack to get you started
- Just like last time, make a new project (but now n = 2) following these instructions.
Instructions
Modify Lab2.java as follows:
- Add your name and section number in a comment before the Lab2 class body
- Besides the main method, create two other methods in Lab2: static void convertSeconds() { ... } and static boolean isItPrime(int n) { ... } (explained in the subsections below)
- Your main() should initially contain the following:
- Tell the user (using println()) that they can choose either of the two functions above
- Prompt the user to enter a number to choose one of the options (1 for first option, 2 for second option, etc.), then use a Scanner to read it
- If the user chooses convertSeconds(), call that function immediately
- If the user chooses isItPrime(), ask them to enter n, read it with your Scanner as an integer, and call isItPrime() with n as the argument. Depending on the return value of isItPrime(), either print "Yes" or "No"
- Once that is working, do the following
- Put the choices above inside a while loop, and add a "quit program" option
- If the user chooses to quit, exit the loop and let the program end
- Otherwise, call the chosen method and afterward the loop should return to the top and ask the user again what they want to do
static void convertSeconds()
This method should ask the user to enter an integer which represents a length of time t in seconds, and then compute and print out the number of d days, h hours, m minutes, and s seconds corresponding to t on separate lines.
When you are satisfied that you can calculate d, h, m, and s correctly, use branching to modify your printing so that (1) you start with the first non-zero time unit (i.e. do not print leading zero values), and (2) you print "1 day" instead of "1 days", "1 hour" instead of "1 hours", etc. where appropriate (i.e., do not use plural for a value of 1). Some example outputs are below:
If the user inputs "67", the output should be
1 minute 7 seconds
and if they input "8880" the output should be
2 hours 28 minutes 0 seconds
For this function you may assume that t is positive, and you do not have to use a long
static boolean isItPrime(int n)
This method will calculate whether the value n passed in is a prime number or not. The answer will be returned in the form of a boolean. The method itself should not print to or read from the console.
To determine whether n is prime, first do some basic error checking: make sure that it is bigger than 1. If not, print an error message of some sort and return false.
If there's no error, then loop through the possible integer factors from 2 up to n / 2 to see if any of them evenly divide n. Remember: this is integer division. If you find no such factor, then n is prime and you can return true. If you do find a factor, then you can stop looping and return false right away.
Finishing up
Remember to use proper naming and formatting style throughout your code, and submit your Lab2.java on Sakai by Friday, February 20