Lab Activity 2.1-1 A Letter Guessing Game. BufferedReader Lab Activity 2.1-2 Application of Event Controlled Loop.
Scanner
import [Link].*; import [Link];
public class Balance {
public class GuessingGame { public static void main(String[] args) {
public static void main(String[] args) throws IOException { Scanner scan = new Scanner([Link]);
char LETTER = 'M', guess = ' '; [Link]("Enter Amount Borrowed: ");
String playerGuess; double balance = [Link]();
BufferedReader reader = new BufferedReader(new InputStreamReader([Link])); int paymentNumber = 1;
[Link]("I am thinking of a CAPITAL Letter"); while (balance > 0) {
[Link]("What is your guess? "); [Link]("Payment # " + paymentNumber + ": ");
playerGuess = [Link](); double payment = [Link]();
guess = [Link](0);
if (payment > balance) {
while (guess != LETTER) { [Link]("Payment exceeds balance. Adjusting to remaining balance.");
[Link]("Your guess is incorrect."); payment = balance;
[Link]("What is your guess? "); }
playerGuess = [Link](); balance -= payment;
guess = [Link](0); [Link]("Current Balance: " + balance);
} paymentNumber++;
}
[Link]("Congratulations, you guessed my letter."); [Link]("YOU ARE PAID!");
} }
} }
Lab Activity 2.1-2 Application of Event Controlled Loop. BufferedReader Lab Activity 2.2-1 (A Program that will enter a username and password). BufferedReader
import [Link].*;
import [Link].*;
public class Password {
public class Balance { public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException { String username, password;
BufferedReader read = new BufferedReader(new InputStreamReader([Link])); boolean login = false;
[Link]("Enter Amount Borrowed: "); BufferedReader reader = new BufferedReader(new InputStreamReader([Link]));
double balance = [Link]([Link]()); do {
int paymentNumber = 1; [Link]("Please enter username: ");
while (balance > 0) { username = [Link]();
[Link]("Payment # " + paymentNumber + ": "); [Link]("Please enter password: ");
double payment = [Link]([Link]()); password = [Link]();
if ([Link]("Admin") && [Link]("admin")) {
if (payment > balance) { login = true;
[Link]("Payment exceeds balance. Adjusting to remaining balance."); [Link]("Login successful! Welcome, " + username + ".");
payment = balance; } else if ([Link]("Admin")) {
} login = false;
balance -= payment; [Link]("Incorrect password. Please try again.");
[Link]("Current Balance: " + balance); } else {
paymentNumber++; [Link]("Incorrect username or password. Please try again.");
} }
[Link]("YOU ARE PAID!"); } while (!login);
} }
} }
Lab Activity 2.2-2 Loop Application with Selection Structures .( TV Channel Survey Program). Scanner Lab Activity 2.2-2 Loop Application with Selection Structures .( TV Channel Survey Program). BufferedReader
import [Link]; import [Link].*;
public class Channel_survey { public class ChannelSurvey {
public static void main(String[] args) { public static void main(String[] args) throws IOException {
Scanner scan = new Scanner([Link]); BufferedReader read = new BufferedReader(new InputStreamReader([Link]));
int absCbn = 0, ptv = 0, tv5 = 0, gma = 0; int absCbn = 0, ptv = 0, tv5 = 0, gma = 0;
[Link]("Enter the number of respondents: "); [Link]("Enter the number of respondents: ");
int respondents = [Link](); int respondents = [Link]([Link]());
[Link]("Enter the votes (2 for ABS-CBN, 4 for PTV, 5 for TV5, 7 for GMA):"); [Link]("Enter the votes (2 for ABS-CBN, 4 for PTV, 5 for TV5, 7 for GMA):");
for (int i = 0; i < respondents; i++) { for (int i = 0; i < respondents; i++) {
int vote = [Link](); int vote = [Link]([Link]());
switch (vote) { switch (vote) {
case 2: case 2:
absCbn++; absCbn++;
break; break;
case 4: case 4:
ptv++; ptv++;
break; break;
case 5: case 5:
tv5++; tv5++;
break; break;
case 7: case 7:
gma++; gma++;
break; break;
default: default:
[Link]("Invalid vote! Please enter 2, 4, 5, or 7."); [Link]("Invalid vote! Please enter 2, 4, 5, or 7.");
i--; i--;
break; break;
} }
} }
[Link]("Survey Results:"); [Link]("Survey Results:");
[Link]("ABS-CBN 2: " + absCbn + " votes"); [Link]("ABS-CBN 2: " + absCbn + " votes");
[Link]("PTV 4: " + ptv + " votes"); [Link]("PTV 4: " + ptv + " votes");
[Link]("TV5: " + tv5 + " votes"); [Link]("TV5: " + tv5 + " votes");
[Link]("GMA 7: " + gma + " votes"); [Link]("GMA 7: " + gma + " votes");
} }
} }
Lab Activity 2.3 Factorial of A Number. BufferedReader Lab Activity 5.1 Probability of Winning a Lottery. Scanner
import [Link].*; import [Link];
public class Factorial { public class Lottery {
public static void main(String[] args) throws IOException { public static void main(String[] args) {
int ctr, num, factorial = 1; Scanner scan = new Scanner([Link]);
BufferedReader read = new BufferedReader(new InputStreamReader([Link])); [Link]("Enter the number of balls in the bin (n): ");
[Link]("Please enter a number: "); int n = [Link]();
num = [Link]([Link]()); [Link]("Enter the number of balls selected (r): ");
int r = [Link]();
for (ctr = 1; ctr <= num; ctr++) {
[Link](ctr); long result = 1;
if (num > 1 && ctr < num) for (int i = 0; i < r; i++) {
[Link]("*"); result *= (n - i);
factorial = factorial * ctr; }
} for (int i = 1; i <= r; i++) {
[Link]("\n" + num + "!=" + factorial); result /= i;
} }
} [Link]("The odds of winning the lottery: " + result);
}
}
Lab Activity 5.1 Probability of Winning a Lottery. BufferedReader
import [Link].*;
public class Lottery {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the number of balls in the bin (n): ");
int n = [Link]([Link]());
[Link]("Enter the number of balls selected (r): ");
int r = [Link]([Link]());
long result = 1;
for (int i = 0; i < r; i++) {
result *= (n - i);
}
for (int i = 1; i <= r; i++) {
result /= i;
}
[Link]("The odds of winning the lottery: " + result);
}
}