/***
Cafeteria *********************************** Ronald works in the school cafeteria. He'd like to have a program that lets him easily type in the names of food items, and then display the prices and add them up. He decided to use a 2-letter abbreviation for each item. Here is a first version of the program. **************************************************/ String food, more ; float price, total; void setup() { do // repeat for each new customer { total = 0.00; do { // repeat for each food item food = input("Food item"); if( food.equals("ha") ) // hamburger { price = 3.50; } else if (food.equals("ff")) // french fries { price = 1.75; } else if (food.equals("ap")) // apple { price = 0.90; } else if (food.equals("dr")) // drink { price = 1.40; } else { price = 0.00; } // unknown item println(food + "\t" + price); total = total + price; } while(!food.equals("")); // press ENTER after last item println("Total = " + total); more = input("Next customer (or type 'quit')"); } while (!more.equals("quit")); } public String input(String prompt) { return javax.swing.JOptionPane.showInputDialog(null,prompt); } |
ha
3.5 ff 1.75 dr 1.4 0.0 Total = 6.65 |
A String variable is an
Object. This makes
it different from int and
float variables, which are
fundamental data types.
A fundamental type only contains data. An Object contains both data and methods. Methods provide
a behavior that
the Object can perform. Other methods perform data
manipulations.
Fundamental types are compared by boolean operators : = = ,
!= , > , < , >= , <= . That checks
the data values in the same way
that people compare numbers. So ( 5 < 25/4 )
is true.
Java is case-sensitive.
That means it thinks that capital and small letters are different
- not equal.
In most situations, people don't care about capital and small
letter differences. We can do a non-case-sensitive
comparison
by using .equalsIgnoreCase,
like this:
String
name = "Farmer";
String job = "farmer";
if( name.equalsIgnoreCase(job) )
{ output("That is a coincidence"); }