Java Assigment 3 - Intro to Java Programming, Assignments of Java Programming

This assignment will focus on the use of random numbers, if statements and the swapping of values in two different variables.

Typology: Assignments

2021/2022

Available from 04/25/2023

alaminenquaries
alaminenquaries 🇧🇩

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Assigment 3 - Intro to Java Programming
Assignment 3 Preparation
This assignment will focus on the use of random numbers, if statements and the swapping of
values in two different variables.
Assignment 3
Assignment 3 Submission
Follow the directions below to submit Assignment 3:
Create a Java program.
The class name for the program should be 'ValueSwap'.
In the main method you should perform the following:
Prompt the user to input an integer value using the Scanner class.
Call Math.random() to create a second integer value between 0 - 99.
Compare the two variable's values. If the first is greater than the second then swap their
values. If the first is not greater than the second then do nothing.
Output the following "<first variable value> is less than or equal to <second variable
value>".
pf2

Partial preview of the text

Download Java Assigment 3 - Intro to Java Programming and more Assignments Java Programming in PDF only on Docsity!

Java Assigment 3 - Intro to Java Programming

Assignment 3 Preparation

This assignment will focus on the use of random numbers, if statements and the swapping of

values in two different variables.

Assignment 3

Assignment 3 Submission

Follow the directions below to submit Assignment 3:

Create a Java program.

The class name for the program should be 'ValueSwap'.

In the main method you should perform the following:

Prompt the user to input an integer value using the Scanner class.

Call Math.random() to create a second integer value between 0 - 99.

Compare the two variable's values. If the first is greater than the second then swap their

values. If the first is not greater than the second then do nothing.

Output the following " is less than or equal to <second variable

value>".

Answer

Code:

import java.util.Scanner; public class ValueSwap { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Prompt user to enter an integer value and store it in a variable System.out.println("Enter an integer value:"); int firstValue = scanner.nextInt(); // Generate a random integer between 0 and 99 and store it in a variable int secondValue = (int) (Math.random() * 100 ); // Check if the first value is greater than the second value if (firstValue > secondValue) { // If true, swap the values using a temporary variable int temp = firstValue; firstValue = secondValue; secondValue = temp; } // Output the result of the comparison using the two variables System.out.println(firstValue + " is less than or equal to " + secondValue); } }