Open Simonefleek opened 4 months ago
25.1 LAB: Random values
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(). 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).
Note: When submitted, your program will be tested against different input range to verify if the three randomly generated values are within range.
import java.util.Scanner;
public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);
// Create a VendingMachine object
VendingMachine vendingMachine = new VendingMachine();
// Read inputs from the user
//System.out.print("Inventory: 0 bottles");
int purchase1 = scnr.nextInt();
//System.out.println("Inventory: 0 bottles ");
int restock = scnr.nextInt();
//System.out.println("Inventory: 0 bottles");
int purchase2 = scnr.nextInt();
// Perform vending machine operations
vendingMachine.purchase(purchase1);
vendingMachine.restock(restock);
vendingMachine.purchase(purchase2);
// Report inventory
vendingMachine.report();
scnr.close(); // Close the scanner
}
}
simulates a simple vending machine with operations to purchase drinks and check inventory. public class VendingMachine {
// number of bottle in stock
private int bottles;
// initial inventory is 20
public VendingMachine(){
bottles = 20;
}
public void purchase(int amount){
bottles = bottles - amount;
}
public int getInventory(){
return bottles;
}
public void restock(int amount){
bottles = bottles + amount;
}
public void report(){
System.out.println("Inventory: " + bottles + " bottles");
}
}
Given three integers as user inputs that represent the number of bottles of drinks to purchase, to restock, and to purchase afterward, create a VendingMachine object that performs the following operations:
Purchase the first input number of bottles of drinks Restock the second input number of bottles of drinks Purchase the last input number of bottles of drinks Report inventory The VendingMachine is found in VendingMachine.java. A VendingMachine's initial inventory is 20 bottles of drinks.
Ex: If the input is:
import java.util.Scanner;
public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);
}