Java Programming Lab 02: Variable Declarations, Arithmetic Operations, and Math Class, Study Guides, Projects, Research of Object Oriented Programming

Object-oriented programming (OOP) refers to a type of computer programming (software design) in which programmers define the data type of a data structure, and also the types of operations (functions) that can be applied to the data structure.

Typology: Study Guides, Projects, Research

2019/2020

Uploaded on 01/11/2020

samia-khan-2
samia-khan-2 🇵🇸

4.8

(4)

11 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LAB 02
Topics
Variable declarations and assignment.
Integer and floating-point division and the
remainder operator.
The Math class
The printf statement
EXERCISE01:
1. In BlueJ, create a new project called Lab2
2. Create a class in that project called ExpressionPalooza
3. Write a method in that class called compute
4. In compute, declare an integer variable named size
5. In a separate assignment statement, assign the value 348 to size
6. Print out the value as follows:
The size is 348
7. Compile the code and run the method.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java Programming Lab 02: Variable Declarations, Arithmetic Operations, and Math Class and more Study Guides, Projects, Research Object Oriented Programming in PDF only on Docsity!

LAB 02

Topics

**- Variable declarations and assignment.

  • Integer and floating-point division and the**

remainder operator.

**- The Math class

  • The printf statement**

EXERCISE01:

  1. In BlueJ, create a new project called Lab
  2. Create a class in that project called ExpressionPalooza
  3. Write a method in that class called compute
  4. In compute , declare an integer variable named size
  5. In a separate assignment statement, assign the value 348 to size
  6. Print out the value as follows: The size is 348
  7. Compile the code and run the method.

SOLUTION:

Figure 1

OUTPUT:

Figure 2

EXERCISE02:

  1. After printing the original value of size , assign it a new value of 876
  2. Then print it as follows: The size is now 876.

EXERCISE03:

1. Declare another integer variable named result 2. Using a separate assignment statement, compute the value 8642 times 4 minus 57 and store it in result. 3. Print the result: The result is xxx

SOLUTION:

Figure 5

OUTPUT:

Figure 6

EXERCISE04:

  1. Now copy and paste the last assignment and println statements.
  2. Change the assignment statement to force the subtrac3on to happen first.
  3. Change the output to: The result is now xxx

15 / 2 is xxx 15 / 5.0 is xxx 15 / 2.0 is xxx 15.0 / 2 is xxx

SOLUTION:

Figure 9

OUTPUT:

Figure 10

EXERCISE06:

  1. Again, compute and print the following values.
  2. Do these one at a 3me and predict the results each time: 14 % 5 is xxx 15 % 5 is xxx 32 % 5 is xxx 178 % 10 is xxx 5936 % 1000 is xxx

SOLUTION:

Figure 11

OUTPUT:

Figure 14

EXERCISE08:

  1. Declare an integer variable named radius and initialize it to 12 in The declaration.
  2. Declare a double variable named circumference.
  3. Use an assignment statement to compute the circumference of a circle with the given radius (2 times pi times the radius).
  4. Use the Math.PI constant in the calculation.
  5. Print the result: The circumference of a circle with radius xxx is xxx

SOLUTION:

Figure 15

OUTPUT:

Figure 16

EXERCISE09:

  1. Using the same radius, compute the area of the circle and store it in a double variable named area
  2. The formula for the area of a circle is pi times the radius squared.
  3. Use the Math.pow method to square the radius.
  4. Print the result to three decimal places using a print statement: The area of the circle is xxx