21. What If - Link
- Task
- Output
- Question
Here is the next Java program you’ll enter, which introduces you to the if statement. Type this in, make it run exactly right and then we’ll see if your practice has paid off.
WhatIf.java
22. How Old Are You? - Link
- Task
- Answer
Make a program which displays a different message depending on the age given. Here are the possible responses:
- age is less than 16, say “You can’t drive.”
- age is less than 18, say “You can’t vote.”
- age is less than 25, say “You can’t rent a car.”
- age is 25 or over, say “You can do anything that’s legal.”
23. Else And If - Link
- Task
- Output
- Question
Type this one in and make it work, too.
ElseAndIf.java
24. Weekday Name - Link
- Task
- Answer
- Output
I have provided a function that is supposed to return the name of a day of the week given the day number.Files NeededUse
if and else to complete it according to the following table:| Number | Day of week |
|---|---|
| 1 | Sunday |
| 2 | Monday |
| 3 | Tuesday |
| 4 | Wednesday |
| 5 | Thursday |
| 6 | Friday |
| 7 | Saturday |
| 0 | Saturday |
| anything else | "error" |
25. How Old Are You, Specifically? - Link
- Task
- Answer
- Output
Using
Note that unlike the original “How Old Are You” assignment, this program must only display exactly one message for a given age and not multiple messages.
Sample Output:You can make up your own messages if you want, but you must have at least four messages, and you must use
if statements, else if, and else statements, make a program which displays a different message depending on the age given.| Age | Message |
|---|---|
| less than 16 | "You can't drive." |
| 16 to 17 | "You can drive but not vote." |
| 18 to 24 | "You can vote but not rent a car." |
| 25 or older | "You can do pretty much anything." |
else if statements to make sure only one of the messages is printed for any given age.26. Space Boxing - Link
- Task
- Answer
Julio Cesar Chavez Mark VII is an interplanetary space boxer, who currently holds the championship belts for various weight categories on many different planets within our solar system. However, it is often difficult for him to recall what his “target weight” needs to be on earth in order to make the weight class on other planets. Write a program to help him keep track of this.It should ask him what his earth weight is, and to enter a number for the planet he wants to fight on. It should then compute his weight on the destination planet based on the table below:
So, for example, if Julio weighs 128 lbs. on earth, then he would weigh just under 50 lbs. on Mars, since Mars’ gravity is 0.39 times earth’s gravity. (128 * 0.39 is 49.92)
| # | Planet | Relative gravity |
|---|---|---|
| 1 | Venus | 0.78 |
| 2 | Mars | 0.39 |
| 3 | Jupiter | 2.65 |
| 4 | Saturn | 1.17 |
| 5 | Uranus | 1.05 |
| 6 | Neptune | 1.23 |
27. ALittleQuiz - Link
- Task
- Answer
Write an interactive quiz. It should ask the user three multiple-choice or true/false questions about something. It must keep track of how many they get wrong, and print out a “score” at the end.
28. Modulus Animation - Link
- Task
- Question
In this program, you’ll use a loop to draw a simple ASCII-based animation on the screen, and you will use modulus (%) to determine which frame of the animation to show. (You will learn how to create your own loops later.)Files Needed(It will look cooler while it’s running.)
- ModulusAnimation.java - modify and turn in this one
- ModulusAnimationWorm.java - first example
- ModulusAnimationSymbols.java - second example
32. Twenty Questions… well, actually just Two - Link
- Task
- Answer
Make a program which plays a simple game of 20 2 Questions. The first question should be “animal, vegetable, or mineral?” Then, the second question should be “is it bigger than a breadbox?” Then, display one of six possible responses, depending on their answers. You can choose what answers to give for each of the six possibilities.Here’s a suggestion:
You will use nested
| size \ type | animal | vegetable | mineral |
|---|---|---|---|
| smaller than a breadbox | squirrel | carrot | paper clip |
| bigger than a breadbox | moose | watermelon | camaro |
if statements to do this.
Sample Output:33. Choose Your Own Adventure! - Link
- Task
- Answer
Make a short “Choose Your Own Adventure” game. The starting room should give the user two choices. Then the second room they travel to should give them two more choices. Finally the third room should give them two choices.This means your game will have eight possible “endings”. Your game will also have a total of fifteen rooms:You must use nested
if statements to do this.
Sample Output:34. Age Messages 3 - Link
- Task
- Answer
Using
You can make up your own messages if you want, but you must have at least four messages, and you must use
if statements with compound conditions (like &&), make a program that displays a single message depending on the age given.| Age | Message |
|---|---|
| less than 1 6 | "You can't drive." |
| 16 to 17 | "You can drive but not vote." |
| 18 to 24 | "You can vote but not rent a car." |
| 25 or older | "You can do pretty much anything." |
This output of this assignment is identical to the “How Old Are You, Specifically” assignment. However, this time you must accomplish it using if statements with compound conditions and you must not use else if or else. | |
| Sample Output: |
&& statements to make sure only one of the messages is printed for any given age.35. Two More Questions - Link
- Task
- Answer
Using
if statements with compound conditions (like &&), make a guessing game of two questions similar to the Twenty Questions assignment.However, this time you must accomplish it using if statements with compound conditions and you must not use else if or else or nested ifs.- Question 1: Does it belong inside or outside or both?
- Question 2: Is it alive?
| inside | outside | both | |
|---|---|---|---|
| alive | houseplant | bison | dog |
| not alive | shower curtain | billboard | cell phone |
| Sample Output: |
36. BMI Categories - Link
- Task
- Answer
- Bonus
(This assignment was suggested by Joel H in 2012.)The body mass index (BMI) is commonly used by health and nutrition professionals to estimate human body fat in populations. It is computed by taking the individual’s weight (mass) in kilograms and dividing it by the square of their height in meters.Start with the BMI Calculator you wrote previously (BMICalc.java). Then use some
Sample Output:It doesn’t matter whether you input the values in metric (kilos and meters) or Imperial measurements (feet/inches and pounds).
if statements to show the category for a given BMI.| BMI | Category |
|---|---|
| less than 18.5 | underweight |
| 18.5 to 24.9 | normal weight |
| 25.0 to 29.9 | overweight |
| 30.0 or more | obese |
Although BMI is a very good estimate of human body fat, the formula doesn’t work well for athletes with a lot of muscle, or people who are extremely short or very tall. If you are concerned about your BMI, check with your doctor.
37. Gender Game - Link
- Task
- Answer
Make a program which displays an appropriate name for a person, using a combination of nested Notice that in the example below, we never even ask the marriage question, because she is under 20 and so her marital status doesn’t change what we call her.
ifs and compound conditions. Ask the user for a gender, first name, last name and age.If the person is female and 20 or over, ask if she is married. If so, display “Mrs.” in front of her name. If not, display “Ms.” in front of her name. If the female is under 20, display her first and last name.If the person is male and 20 or over, display “Mr.” in front of his name. Otherwise, display his first and last name.Note that asking a person if they are married should only be done if they are female and 20 or older, which means you will have a single if and else nested inside one of your if statements.Also, did you know that with an if statement (or else), the curly braces are optional when there is only one statement inside?
Sample Output:38. compareTo() Challenge - Link
- Task
- Answer
- Output
Write a program that compares several Sample Output:
Strings using the compareTo() method. You should display the Strings and display the integer that compareTo() gives you.You must have five examples which result in a number less than 0, five examples which result in a number greater than 0, and two examples which give you exactly 0. This means you need a total of twelve examples.You may not just flip the Strings around; you must have twelve different examples.Here’s an example:CompareToChallenge.java
39. Alphabetical Order - Link
- Task
- Answer
- Output
Make a program that asks for the last name of the user. Depending on their last name, make a statement about how long they have to wait during roll call. You need to use
else ifs to make sure only one statement gets printed.Once you understand how compareTo() works, this is a pretty straightforward assignment, much like How Old Are You, specifically, except that it uses Strings instead of ints and so you must use the compareTo() method.- name is
"Carswell"or before: say “you don’t have to wait long” - name is
"Jones"or before: say “that’s not bad” - name is
"Smith"or before: say “looks like a bit of a wait” - name is
"Young"or before: say “it’s gonna be a while” - name is after
"Young": say “not going anywhere for a while?”
40. The Worst Number-Guessing Game Ever - Link
- Task
- Answer
- Output
Write a program that plays an incredibly stupid number-guessing game. The user will get one try to guess the secret number. Tell them if they got it right or wrong, and if they got it wrong, display what the secret number was.You must store the secret number in a variable, and use that variable throughout. The secret number itself must not appear in the program at all, except in the one line where you store it into a variable.I know it will be the same number every time the game is played.Sample Output: