OK. You have
learned a lot in previous lessons. By using what you learned so far,
you could ask me to do a lot of things Let us play a little bit. Let us pretend that you are a doctor.
Your dog catch a cold. He got fever, red eye, running nose and sneezing
a lot! You gave a prescription and ask me to calculate how much money
the medcines will cost.
The prescription is:
5 yellow pills
for fever
4 blue pills
for allergy symptoms (sneezing, running nose)
I called pharmacy and find out that yellow pill
cost 0.83 per pill and blue pill
is 1.95 per pill. So
I type the following code to calculate the cost:
class medicine
{
public static void main(String[] args)
{
int NoOfYellowPills=5;
int NoOfBluePills=4;
double priceOfYellowPill=0.83;
double priceOfBluePill=1.95;
double totalCost=NoOfYellowPills*priceOfYellowPill
+NoOfBluePills*priceOfBluePill;
System.out.println("Total medicine cost="+totalCost);
}
}
Save it as medicine.java. Then I compile the code: javac
medicine.java.
Then I run the code: java
medicine
Now i get the result: Total
medicine cost=11.95
In the above code, you may see that a statement may write into serveral
lines(highlighted in red).
OK, doctor, I finish my job.
Homework
Suppose you will have a birthday party next Saturday. You got $234
dollars from your grandparentsa. You plan to invite friends to
see "Star
War (3)".. Every one needs a ticket ($5), a drink ($1.1) and
popcorn($2.3). How many friends you could invite at most (Don't
forget yourself)?
Please print out something like "Maximum number of friends="...
(Solution is here). |