Answer to Question #308652 in Java | JSP | JSF for rose

Question #308652

In the file RandomNumbers.java, write a class called RandomNumbers that has three integer instance variables: var1, var2, and var3. Write getter method for each variable: getVar1(), getVar2() and getVar3() and a RandomNumbers constructor that set the initial values of all the instance variables to 0. Then write the following 2 instance methods:


• setRandomValues( ) - accepts a low and high integer values as parameters, and sets var1, var2, and var3 to random numbers (generated using the Random class) within the range of the low and high input values (inclusive).


• getRandomValues( ) - prints out the 3 random numbers in the format: "Random values: var1 var2 var3"


Ex: If the input is:


15 20


the output may be:


Random values: 17 15 19


where 17, 15, 19 can be any random numbers within 15 and 20 (inclusive).



1
Expert's answer
2022-03-10T10:19:06-0500


import java.util.*;


class RandomNumbers {
	private int var1;
	private int var2;
	private int var3;


	public RandomNumbers() {
		this.var1 = 0;
		this.var2 = 0;
		this.var3 = 0;
	}


	public RandomNumbers(int var1, int var2, int var3) {
		this.var1 = var1;
		this.var2 = var2;
		this.var3 = var3;
	}


	/***
	 * setRandomValues( ) - accepts a low and high integer values as parameters, and
	 * sets var1, var2, and var3 to random numbers (generated using the Random
	 * class) within the range of the low and high input values (inclusive).
	 * 
	 * @param low
	 * @param high
	 */
	public void setRandomValues(int low, int high) {
		Random r = new Random();
		this.var1 = r.nextInt(high - low) + low;
		this.var2 = r.nextInt(high - low) + low;
		this.var3 = r.nextInt(high - low) + low;
	}


	/**
	 * getRandomValues( ) - prints out the 3 random numbers in the format: "Random
	 * values: var1 var2 var3"
	 */
	public void getRandomValues() {
		System.out.println("Random values: " + this.var1 + " " + this.var2 + " " + this.var3);
	}


	/**
	 * @return the var1
	 */
	public int getVar1() {
		return var1;
	}


	/**
	 * @param var1 the var1 to set
	 */
	public void setVar1(int var1) {
		this.var1 = var1;
	}


	/**
	 * @return the var2
	 */
	public int getVar2() {
		return var2;
	}


	/**
	 * @param var2 the var2 to set
	 */
	public void setVar2(int var2) {
		this.var2 = var2;
	}


	/**
	 * @return the var3
	 */
	public int getVar3() {
		return var3;
	}


	/**
	 * @param var3 the var3 to set
	 */
	public void setVar3(int var3) {
		this.var3 = var3;
	}


}


class App {


	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);
		RandomNumbers randomNumbers = new RandomNumbers(5, 15, 10);
		System.out.print("Enter low number: ");
		int low = keyboard.nextInt();
		System.out.print("Enter high number: ");
		int high = keyboard.nextInt();
		randomNumbers.setRandomValues(low, high);
		randomNumbers.getRandomValues();
		keyboard.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS