0% found this document useful (0 votes)
15 views

Java_CH6_Exercise

Uploaded by

wwangyibo17
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java_CH6_Exercise

Uploaded by

wwangyibo17
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER 6

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);

System.out.print("Enter 1 to play Again:");


int again = input.nextInt();
if(again != 1) {
won = true;
break;
}else {
won = false;
}
}
}

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

Congratulation!. You Guessed The Number 35 within 4 tries


Enter 1 to play Again:0
Thank You For Playing!
2. Write a java program that determines and displays all the prime numbers less than 100. A
positive integer is prime if it’s divisible by only 1 and itself. The number 1, by definition, is
not prime. Implement a method that determine whether a number is prime by using the square
root of n approach.

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();
}
}
}

public static boolean isPrimeRefined(int n){


double max = Math.floor(Math.sqrt(n));
for(int i=2; i<=max; i++){
if(n % i == 0) {
return false;
}
}
return true;
}
}
Output:
Prime numbers between 0 to 100 are
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97,

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;

private static double total = 0.0f;

public static void main(String[] args){


Scanner input = new Scanner(System.in);
double time = 0.0f;

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{

double exceed = fullTime - 3;


fee = BASE_FEE + (exceed * HOURLY_FEE);
}
if(fee > MAX_FEE) {
fee = MAX_FEE;
}
total += fee;
return fee;
}
}

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

You might also like