Java

TOPIC 07 – ARITHMETIC EXPRESSIONS

 

LESSON WORK

 

 

QUESTION 1

What value will the variable t get?  You must test each one in a Java program to make sure!  Because you cannot have the same variable twice inside the same program, you can rename the variable so that you can test all four in one program.

 

Careful, some of these results are surprising at first!

 

a) int t = 1 + 6 * 5;

b) int t = 12 + 60 / (2 * 3);

c) int t = 3 + (int)(2.0/3.0);

d) int t = 100 % 3;

e) double t = 8;

 

f) double t = 7.0 + (int)4.1 + 2.2;

 

g) double t = 5/2;

Save your work inside the file Question1.java in the topic07 folder.
 

QUESTION 2

Write a program called RectangleArea that will implement the following pseudo-code:

 

  • A variable named length gets the value 5.5.
  • A variable named width gets the value 4.2.
  • A variable named area gets the area of the rectangle.  This is calculated by multiplying length by width.
  • An output statement displays the area on the screen.

 

Save your work inside the RectangleArea.java file in the topic07 folder.

 

QUESTION 3

Write a program called YearlySalary that will implement the following pseudo-code:

 

  • A variable named hourlyWage is set to 11.50
  • A variable named weeklyHours is set to 15
  • A variable named weeklyWage gets the value of hourlyWage x weeklyHours
  • A variable named yearlyWage gets the value of weeklyWage x 52
  • A variable named holidayPay gets 4% of yearlyWage
  • A variable named yearlySalary gets yearlyWage +  holidayPay
  • An output statement displays the yearlySalary.

 

Save your work in a file called YearlySalary.java inside the topic07 folder.