|
Assignment 3
Task
Explore numerical input and conditionals.
Steps
Write a program that asks the user to enter an integer. Your output should then display the following details for that integer:
- whether the integer is postive, negative, or zero.
- whether the integer is odd or even.
- whether the integer is divisible by 3, 5, or 7.
- whether the integer is divisible by 3, 5, and 7.
Use a Scanner to read in an integer from the user. (Remember to import java.util.Scanner; at the beginning of your file.) Use conditionals (including if , else if , and else , as appropriate) to determine what output to display for the current input.
You can use the % operator to determine whether one integer is divisible by another. For example, the expression (num % 3 == 0) evaluates to true when num is divisible by 3.
I will enter only integers when testing your code. (What happens if you try to enter a string or decimal number? You will learn later this semester how to handle such error cases.)
Here is some sample output with user input in green:
D:\TA\hw\A3>java Assignment3
Enter an integer: 4
You entered the integer 4.
4 is positive.
4 is even.
D:\TA\hw\A3>java Assignment3
Enter an integer: -105
You entered the integer -105.
-105 is negative.
-105 is odd.
-105 is divisible by 3, 5, or 7.
-105 is divisible by 3, 5, and 7.
D:\TA\hw\A3>java Assignment3
Enter an integer: 100
You entered the integer 100.
100 is positive.
100 is even.
100 is divisible by 3, 5, or 7.
D:\TA\hw\A3>java Assignment3
Enter an integer: 0
You entered the integer 0.
0 is zero.
0 is even.
0 is divisible by 3, 5, or 7.
0 is divisible by 3, 5, and 7.
What to submit
Attach Assignment3.java to an email.
FAQs
- Where's that LegalAge demo code from lab?
- Here: LegalAge.java
Note how in the first section where I determine the user's age category, everything is in a single if/else block. In this case, only one of these four println statements will execute, based on which condition is found to be true.
On the other hand, in the second section where I determine what the user can legally do, I use a series of three separate if statements. This means any number of these println s could execute, based on the result of each conditional test.
- How do a determine if a number is even or odd?
- If it is divisible by 2 (with a remainder == 0), then it is an even number. Otherwise, it is odd.
- How do I test for divisibility by 3, 5, or 7? And for divisibility by 3, 5, and 7?
- I recommend you have a look at the logical and (&&) and logical or (||) operators. I didn't mention these in lab, so look in your book, the Sun tutorial, or your lecture notes. Otherwise, you can use a series of
if s or nested if s (one if statement inside another) to achieve the same thing.
Grading
Out of 10 points:
- 1 - Submission
- Follows required submission policies.
- 2 - Compiles
- 2 - Input
- Clearly prompts the user and then reads in an integer.
- 5 - Output
- Correctly displays whether the inputted integer is positive/negative/zero; odd/even; divisible by 3, 5, or 7; divisible by 3, 5 and 7.
|