import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
private int baseNumber;
private int exponent;
private double result;
public GuessingGame() {
Random rand = new Random();
baseNumber = rand.nextInt(9) + 2;
exponent = rand.nextInt(4) + 1;
result = Math.pow(baseNumber, exponent);
}
public void provideHint() {
if (exponent > 0) {
System.out.println("Hint: The number is raised to a positive power.");
} else {
System.out.println("Hint: The number is raised to a negative power (root).");
}
System.out.println("Hint: The result of the operation is: " + result);
}
public static boolean checkGuess(int base, int exp, GuessingGame game) {
return base == game.baseNumber && exp == game.exponent;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
GuessingGame game = new GuessingGame();
System.out.println("Guess the base number and its exponent!");
game.provideHint();
System.out.print("Enter your guess for the base number: ");
int userBase = scanner.nextInt();
System.out.print("Enter your guess for the exponent: ");
int userExponent = scanner.nextInt();
if (checkGuess(userBase, userExponent, game)) {
System.out.println("Congratulations! You guessed correctly.");
} else {
System.out.println("Sorry, that's incorrect. The correct base was " + game.baseNumber + " and the exponent was " + game.exponent + ".");
}
}
}
GuessingGame.main(null)
Guess the base number and its exponent!
Hint: The number is raised to a positive power.
Hint: The result of the operation is: 216.0
Enter your guess for the base number: Enter your guess for the exponent: Sorry, that's incorrect. The correct base was 6 and the exponent was 3.
import java.util.ArrayList;
import java.util.List;
public class Store {
int id;
boolean inStock;
String name;
double price;
Store(int id, boolean inStock, String name, double price) {
this.id = id;
this.inStock = inStock;
this.name = name;
this.price = price;
}
public static void main(String[] args) {
List<Store> products = new ArrayList<>();
products.add(new Store(1, true, "Laptop", 999.99));
products.add(new Store(2, false, "Mouse", 19.99));
products.add(new Store(3, true, "Keyboard", 49.99));
products.add(new Store(4, true, "Monitor", 149.99));
products.add(new Store(5, false, "Headphones", 59.99));
System.out.println(products.get(0).name + " - $" + products.get(0).price);
System.out.println(products.get(2).name + " - $" + products.get(2).price);
System.out.println(products.get(4).name + " - $" + products.get(4).price);
}
}
Store.main(null)
Laptop - $999.99
Keyboard - $49.99
Headphones - $59.99
First Name: John
Last Name: Doe