Java

TOPIC 15 – IF STATEMENTS – PART 2

 

LESSON WORK

 

 

QUESTION 1

Copy and paste the code below into a Java program and find the error (if there is one).

 

 

a)

int a = 41;

if (a == 42);

{

   System.out.println("Yo");

}

 

b)

double b = 1.0/3.0;

if (b == 0.3333333)

{

   System.out.println("Yo");

}

 

c)

boolean c = true;

if (c == false)

{

   String cool = "Campeau";  //oh yes I did!

}

System.out.println(cool);

 

d)

int x = 5;

if (x = 4)

{

   System.out.println("Yo" + x);

}

 

e)

int awesome;

int coco = 4;

if (coco >= 4)

{

   awesome = 7;

}

System.out.println(awesome);

 

f)
Scanner scr = new Scanner(System.in);
System.out.println("Enter an NHL team.");

String team = scr.next();


if (team.equals("Toronto"))

{

   int lastCup = 1967;

}

else if (team.equals("Montreal")

{

   int lastCup = 1993;

}
else

{

   System.out.println("Sorry, pick another team.");

}

System.out.println("Last cup was in " + lastCup);


QUESTION 2

Write the program that will convert a numeric mark into its equivalent grade letter.

 

QUESTION 3

Ask the user for two integer values.  One will be the numerator and one will be the denominator.  As long as the denominator is not zero, you will output the decimal value of the fraction.  If it is zero, you should output that one cannot divide by zero.

 

QUESTION 4

Write the program that will take an order from the user and output the total price.  Include also a 13% tax on the price.  Finally, the tip should be the same as the tax amount but rounded up to the nearest dollar.

 

Do not worry about the number of decimal places being shown.  It is fine to have too many!

 

You can change the menu items but must keep the same prices.

 

Welcome to our restaurant.

*****

To start, what would you like to drink?

 

A-Tea  $2
B-Coffee $2

C-Water $1
D-Juice $2

 

Choice:

A

 

Excellent choice.  Your total is now at 2 dollars.

*****

Could I interest you in an appetizer?

 

A-Escargot $8

B-Mushroom Caps $6

C-Cheese Sticks $6

 

Choice:

B

 

Very well.  Your total is now at 8 dollars.

*****

What would you like for your main meal?

A-Steak $25

B-Pasta Special $18

C-Liver and Onions $2

 

Choice:

A

 

Very nice.  Your total is now at 33 dollars.

*****

What type of salad would you like with that?

 

A-Garden $0 extra

B-Greek $2 extra

C-Ceasar $3 extra

 

Choice:

C

 

Ok then.  Your total is now at 36 dollars.

 

****

What would you like for desert?

 

A-Chocolate cake  $6

B-Apple pie  $5

C-Nothing

 

Choice:

B

 

Excellent.  Your total is now at $41 dollars.

*****

Total: 41.00

HST: 5.33

Total: 46.33

Tip: 6.0

Grand Total: 52.33

 

I hope you liked it!