# java assignment

1

ASSIGNMENT 2

Note: The evaluation is based on the syntactic and semantic correctness, as well as the

pragmatic clarity of the solution.

PROBLEM 1 [8 MARKS].

Mass and Weight

Scientists measure an object’s mass in kilograms and its weight in Newtons. If you know

the amount of mass that an object has, you can calculate its weight, in Newtons, with the

following formula:

Weight = Mass * 9.8

Following proper programming style, write a TUI version and a GUI version of a

program that asks the user to enter an object’s mass, and then calculate its weight. If the

object weighs more than 1,000 Newtons, display a message indicating that it is too heavy.

If the object weighs less than 10 Newtons, display a message indicating that the object is

too light.

Hint:

 You would need an if-else statement for comparing weights.

 In the TUI version, use the methods of the Scanner class for input and output.

 In the GUI version, use the methods of the JOptionPane class for input and

output.

PROBLEM 2 [6 MARKS].

Sum of Numbers

(a) Following proper programming style, write a program that asks the user for a positive

nonzero integer value. The program should use a loop to get the sum of all the integers

from 1 up to the number entered. For example, if the user enters 50, the loop will find the

sum of 1, 2, 3, 4, …, 50.

Hint:

Use two loops, a while loop and a for loop.

2

 The condition in the while loop should test whether the number entered is nonnegative.

 The condition in the for loop should test whether the numbers have been summed

up to the number entered.

(b) Use a while loop instead of a for loop to test whether the numbers have been

summed up to the number entered.

PROBLEM 3 [6 MARKS].

Even-Odd

You can use the following logic to determine whether a number is even or odd:

if ((number % 2) == 0)

{

// The number is even.

}

else

{

// The number is odd.

}

Following proper programming style, write a program with a method named isEven

that accepts an int argument. The method should return true if the argument is even, or

false otherwise. The program’s main method should use a loop to generate 100 random

integers. It should use the isEven method to determine whether each random number is

even, or odd. When the loop is finished, the program should display the number of even

numbers that were generated, and the number of odd numbers.

Hint:

Use the nextInt() method of the Random class for generating random integers.

Tietoa työnantajasta:
( 2 arvostelua ) Montreal, Canada

Projektin tunnus: #21702953

## Myönnetty käyttäjälle:

Bobga

Hello. Thanks for your good job postiong... I am excited to work with you on this project. I have good experience with Java. So I think I can help you if you want. I am ready to start work immediately. Thanks. Best reg Lisää