Java_CH6_Exercise
Java_CH6_Exercise
1. Write an application that plays “guess the number”. The program chooses the number to be
guessed by selecting a random integer in the range 1 to 100. The player inputs a first guess. If
the player's guess is incorrect, your program should display Too high. Try again. or Too low.
Try again. to help the player guess the correct answer. The program should prompt the user
for the next guess. When the user enters the correct answer, display Congratulations. You
guessed the number!, and allow the user to choose whether to play again.
Program:
import java.util.Scanner;
import java.util.Random;
public class GuessNumber {
private static final Random randomNumber = new Random();
public static void main(String[] args) {
int secretNumber = 1 + randomNumber.nextInt(100);
int guessedNumber = 0;
int triesCount = 0;
boolean won = false;
Scanner input = new Scanner(System.in);
while(true) {
System.out.print(" Guess the number between 1 to 100 (-1 to exit) : ");
guessedNumber = input.nextInt();
if(guessedNumber == -1) {
break;
}
triesCount += 1;
if(guessedNumber > secretNumber) {
System.out.print("Too high. Try again!.");
continue;
}
if(guessedNumber < secretNumber) {
System.out.print("Too low. Try again!.");
continue;
}
if(guessedNumber == secretNumber){
System.out.printf("\nCongratulation!. You Guessed The Number %d
within %d tries\n",secretNumber,triesCount);
if(won) {
System.out.print("Thank You For Playing!");
}else {
System.out.print("Player gave up");
}
}
Output:
Guess the number between 1 to 100 (-1 to exit): 10
Too high. Try again!. Guess the number between 1 to 100 (-1 to exit): 40
Too high. Try again!. Guess the number between 1 to 100 (-1 to exit): 30
Too low. Try again!. Guess the number between 1 to 100 (-1 to exit): 35
Program:
public class PrimeNumbers {
public static void main(String[] args){
int count = 0;
System.out.printf("Prime numbers between 0 to 100 are\n\n");
for(int i=2; i<100; i++){
boolean prime = isPrimeRefined(i);
if(prime == true){
System.out.printf("%d, ", i);
count++;
}
if(count == 10){
count = 0;
System.out.println();
}
}
}
3. A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage
charges an additional $0.50 per hour for each hour or part thereof in excess of three hours.
The maximum charge for any given 24-hour period is $10.00. Write an application that
calculates and displays the parking charges for each customer who parked in the garage.
Program:
import java.util.Scanner;
public class ParkingCharges{
private static final double BASE_FEE = 2.00;
private static final double HOURLY_FEE = 0.50;
private static final double MAX_FEE = 10.00;
while(time != -1){
System.out.print("Enter hours parked (-1 to exit): ");
time = input.nextDouble();
if(time > 0) {
System.out.printf("Charges: $%.2f\n", calculateCharges(time));
}
}
System.out.printf("Total charges for the day: $%.2f\n", total);
}
public static double calculateCharges(double time){
double fullTime = Math.ceil(time);
double fee = 0;
if(fullTime < 3.0){
fee = BASE_FEE;
}else{
Output:
Enter hours parked (-1 to exit): 2
Charges: $2.00
Enter hours parked (-1 to exit): 3
Charges: $2.00
Enter hours parked (-1 to exit): 4
Charges: $2.50
Enter hours parked (-1 to exit): 10
Charges: $5.50
Enter hours parked (-1 to exit): 21
Charges: $10.00
Enter hours parked (-1 to exit): -1
Total charges for the day: $22.00