ITEC 120
L03a: Conditionals

Objectives

To successfully complete this lab you will develop programs that make decisions based on conditions specified in IF statements.

Assignment

Create a workspace for the L03a lab. Create a program called Museum with a main method, where you will do the work for this lab.

You will write a program that will ask the user a few questions, and based on their answers, tell them what the admission to an art museum will be. Here's the fee structure:

Admission

There are many options for admission. Notice that all members are free, and all children 12 and under are free. If one is not a member, they have a choice: they may either pay admission for one day only, or, they may purchase a membership. All students get in for the student price, no matter what their age. (For this lab, you may assume if they say they are a student that they can produce a valid ID.) But notice that the youth price is the same as the student price, so it is only 18 and older for whom the student price matters.

This can seem like an overwhelming problem if you try to program it all at once. Try to break it down into pieces. Ask one question first, and get that working, then add in other parts. There is more than one right way to do this. You might want to ask about age first, or you might want to ask about membership first.

Read their age as an int, and read their answers to yes/no questions as a String ("Y" or "y", "N" or "n"). Use constants to store the various admission prices.

A sample run of this program might look like this (user input is in red):

Welcome to the Radford museum of Fine Art!
Are you a member? (Y or N) : N

Would you like to become a member today? (Y or N) : N

What is your age? 24

Are you a student (with a valid ID)? (Y or N) : N

Your admission today will be $20.

Enjoy your day at the Radford museum of Fine Art! 

 

Or this:

Welcome to the Radford museum of Fine Art!
Are you a member? (Y or N) : Y

Enjoy your day at the Radford museum of Fine Art! 

 

Another correct solution to this problem might look like this:

Welcome to the Radford museum of Fine Art!
What is your age? 24

Are you a member? (Y or N) : N

Would you like to become a member today? (Y or N) : Y

Are you a student (with a valid ID)? (Y or N) : Y

Your admission today will be $40, good for a whole year.

Enjoy your day at the Radford museum of Fine Art! 

Or this:

Welcome to the Radford museum of Fine Art!
What is your age? 11

Enjoy your day at the Radford museum of Fine Art! 

Testing

You want to make sure your program works for all possible scenarios. When you think you are done, test your program for:

 

Helpful Hints

There are two problems you will probably run into in this lab. It's best to understand them now, because you will continue to have problems with these two particular things until you really know how they work. They have to do with comparing Strings, and reading different types of information with the Scanner.

Comparing Strings.

Often, we want to know if a String is "equal" to something, in other words, does it contain a certain character or characters. When we compare primitive data types, we use handy operators : ==, !=, <=, and >=. We're tempted to use those operators to compare Strings. But, in Java, a String is an object, so a String variable doesn't contain the String itself. Instead it contains an address in memory where that String is stored. When we use == to compare Strings, we are comparing addresses, and not the characters in the String. Thus, str1 == str2 will be true if and only if they point to the exact same String in memory.

To compare the contents of Strings, we must use String methods: .equals(String s) and .equalsIgnoreCase(String s)

This is so important, I'm going to repeat it:

To compare the contents of Strings, we must use String methods:
.equals(String s)
and .equalsIgnoreCase(String s)

 

Scanner methods

For this lab, use .next() to read String input and .nextInt() for integer input.

The Scanner has methods to read information from an input stream. The keyboard is one input stream, but a Scanner can also be used to read input from a File or tokens from a String. Mysterious problems can occur when you don't understand exactly how each Scanner method deals with CRLFs (Carriage return line feed, or, the enter key from the keyboard.)

.nextInt() returns the next int it finds, but does not skip a CRLF after that int

.next() returns the next token it finds, as a String, but does not skip a CRLF after that int

.nextLine() returns the rest of the input on the current line, as a String, and skips the CRLF after it

What's a token?
In this case, a token is a group of characters separated by whitespace, which includes CRLFs. A token is an important concept that will be explored in greater detail later in class.

 

Submit Your Assignment

Submit your Museum.java to the L03a dropbox on D2L.