Skip to main content
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
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.”
Here’s a sample run. Notice that a person who is under 16 will display three messages, one for being under 16, one for also being under 18, and one for also being under 25.Sample Output:
Type this one in and make it work, too.
ElseAndIf.java
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:
Using if statements, else if, and else statements, make a program which displays a different message depending on the age given.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 else if statements to make sure only one of the messages is printed for any given age.
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)
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.
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 NeededIf you download, compile and run ModulusAnimationWorm.java it will look roughly like this.
(It will look cooler while it’s running.)
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 if statements to do this. Sample Output:
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:
Using if statements with compound conditions (like &&), make a program that displays a single message depending on the age given.
You can make up your own messages if you want, but you must have at least four messages, and you must use && statements to make sure only one of the messages is printed for any given age.
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?
Again, here are some sample responses, for the non-creative among you.
(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 if statements to show the category for a given BMI.
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.
Sample Output:
It doesn’t matter whether you input the values in metric (kilos and meters) or Imperial measurements (feet/inches and pounds).
Make a program which displays an appropriate name for a person, using a combination of nested 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:
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.
Write a program that compares several 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
Sample 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?”
Sample 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: