Java Problem Set 1 – Variables, Math, Output
Create a new class called VarsAndMath. Write the code for 1-4 in the same class/program. Label each section with a System.out.println() statement.
1. Write code that has two integers (num1 and num2), adds them, stores the result in a variable (sum) then prints out in the format below.
Number 1: 30
Number 2: 20
Sum: 50
2. Write code that has two doubles (num3 and num4), subtracts them, stores the
result in a variable (diff) then prints out in the format below.
Number 1: 30.0
Number 2: 20.0
Difference: 50.0
3. Write code that takes 10 integers (grade1, grade2, etc.) and prints out the
average in both integer and decimal form. See output below.
Ten Grades: 90, 50, 83, 74, 99, 100, 0, 96, 89, 93, 91
Average (whole): 86
Average (decimal): 86.5
4. Write code that computes the Pythagorean theorem. Given is A and B. Find C.
C = math.sqrt(A*A + B*B)
C = math.sqrt(A*A + B*B)
A: 5.0
B: 12.0
C: 13.0
Output should look like:
VARIABLES AND MATH PROBLEM SET
PROBLEM 1
-----------------
Number 1: 30
Number 2: 20
Sum: 50
PROBLEM 2
-----------------
Number 1: 30.0
Number 2: 20.0
Difference: 50.0
PROBLEM 3
-----------------
Grades: 90, 50, 83, 74, 99, 100, 0, 96, 89, 93, 91
Average (whole): 86
Average (decimal): 86.5
PROBLEM 4
-----------------
A: 5.0
B: 12.0
C: 13.0