:::::::::::::: Clock.java :::::::::::::: class Clock { public static void main(String[] args) { ConsoleReader c = new ConsoleReader(System.in); int h, m; String hour, minu; String line; String clock; System.out.print("Where do we start? "); line = c.readLine(); hour = line.substring(0, 2); minu = line.substring(3, 5); h = Integer.parseInt(hour); m = Integer.parseInt(minu); hour = "00" + h; minu = "00" + m; hour = hour.substring(hour.length() - 2); minu = minu.substring(minu.length() - 2); clock = hour + ":" + minu; System.out.print(clock + ">"); line = c.readLine(); while (! line.equals("done")) { m = m + 1; if (m >= 60) { m = 0; h = h + 1; if (h >= 24) { h = 0; } } hour = "00" + h; minu = "00" + m; hour = hour.substring(hour.length() - 2); minu = minu.substring(minu.length() - 2); clock = hour + ":" + minu; System.out.print(clock + ">"); line = c.readLine(); } } } :::::::::::::: Digits.java :::::::::::::: class Digits { public static void main(String[] args) { ConsoleReader c = new ConsoleReader(System.in); String line; int sum, i; System.out.print("Type a number: "); line = c.readLine(); while (! line.equals("done")) { sum = 0; i = 0; while (i < line.length()) { sum = sum + Integer.parseInt(line.substring(i, i+1)); i = i + 1; } System.out.println("The sum of the digits is: " + sum); System.out.print("Type a number: "); line = c.readLine(); } System.out.println("Thanks for using this program."); } } :::::::::::::: GPA.java :::::::::::::: class GPA { public static void main(String[] args) { ConsoleReader c = new ConsoleReader(System.in); System.out.print("Please enter a grade: "); String line; line = c.readLine(); double num; double sum = 0; int count = 0; double avg; while (! line.equals("done")) { num = Double.parseDouble(line); if (num >= 0 && num <= 100) { sum = sum + num; count = count + 1; System.out.println(num + " entered, thank you."); } else { System.out.println(num + " is not a valid score. Disregarded."); } System.out.print("Please enter a grade: "); line = c.readLine(); } if (count > 0) { avg = sum / count; System.out.println(count + " scores, average is " + avg); if (avg >= 90) { System.out.println("Congratulations, the average is above 90."); } else { System.out.println("Average is not above 90, just letting you know."); } } else { System.out.println("No scores, no average."); } } } :::::::::::::: Investment.java :::::::::::::: class Investment { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); System.out.print("Initial balance: "); double initial, current; int i = 0; initial = console.readDouble(); System.out.print("Interest rate (a number between 0 and 1): "); double rate = console.readDouble(); current = initial; while (current < 2 * initial) { current = (1 + rate) * current; i = i + 1; System.out.println(i + ". The balance becomes: " + current + ", the ratio is " + current/initial); } } } :::::::::::::: Menu.java :::::::::::::: class Menu { public static void main(String[] args) { ConsoleReader c = new ConsoleReader(System.in); System.out.println("The menu is: 1, 2, 3, 4 (and done to quit): "); String line; System.out.print("Type something: "); line = c.readLine(); while (! line.equals("done")) { if (line.equals("1")) { System.out.println("You have typed option " + line); } else { if (line.equals("2")) { System.out.println("You have typed option " + line); } else { if (line.equals("3")) { System.out.println("You have typed option " + line); } else { if (line.equals("4")) { System.out.println("You have typed option " + line); } else { System.out.println("Sorry, I don't understand option " + line); } } } } System.out.print("Type something: "); line = c.readLine(); } } } :::::::::::::: PRS.java :::::::::::::: class PRS { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); int computerScore = 0, userScore = 0; int computer, user; String computerWord; String line; computer = (int)(Math.random() * 3); if (computer == 0) { computerWord = "paper"; } else { if (computer == 1) { computerWord = "rock"; } else { if (computer == 2) { computerWord = "scissors"; } else { computerWord = "... ooops, something went wrong!?"; } } } System.out.print("The computer has chosen " + computerWord + ". What do you choose? "); line = console.readLine(); while (! line.equals("done")) { if (line.equals("paper")) { user = 0; } else { if (line.equals("rock")) { user = 1; } else { if (line.equals("scissors")) { user = 2; } else { user = -1; // something went wrong. } } } if (user >= 0) { if ((user + 1) % 3 == computer) { System.out.print("The user has won. "); userScore += 1; System.out.println("The score is now user " + userScore + " - computer " + computerScore); } else { if ((computer + 1) % 3 == user) { System.out.print("The computer has won. "); computerScore += 1; System.out.println("The score is now user " + userScore + " - computer " + computerScore); } else { System.out.print("This is a tie. "); System.out.println("The score is still " + userScore + " - computer " + computerScore); } } } else { System.out.println("Sorry, I don't understand " + line); } computer = (int)(Math.random() * 3); if (computer == 0) { computerWord = "paper"; } else { if (computer == 1) { computerWord = "rock"; } else { if (computer == 2) { computerWord = "scissors"; } else { computerWord = "... ooops, something went wrong!?"; } } } System.out.print("The computer has chosen " + computerWord + ". What do you choose? "); line = console.readLine(); } System.out.println("Thanks for using this program."); } } :::::::::::::: Quiz.java :::::::::::::: class Quiz { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); int n1, n2, good = 0, total = 0; int answer; String line; n1 = (int)(Math.random() * 100 - 50); n2 = (int)(Math.random() * 100 - 50); System.out.print("What is " + n1 + " + " + n2 + "? "); line = console.readLine(); while (! line.equals("done")) { answer = Integer.parseInt(line); if (answer == n1 + n2) { good += 1; } total += 1; System.out.println("Your current score is: " + good + "/" + total); n1 = (int)(Math.random() * 100 - 50); n2 = (int)(Math.random() * 100 - 50); System.out.print("What is " + n1 + " + " + n2 + "? "); line = console.readLine(); } System.out.println("Thank you for using this program."); } } :::::::::::::: Reverse.java :::::::::::::: class Reverse { public static void main(String[] args) { ConsoleReader c = new ConsoleReader(System.in); String line; System.out.print("Type something: "); line = c.readLine(); while (! line.equals("done")) { int i = line.length() - 1; System.out.print(line + " reversed is "); while (i >= 0) { System.out.print(line.charAt(i)); i = i - 1; } System.out.print("\nType something: "); line = c.readLine(); } } } :::::::::::::: Square.java :::::::::::::: class Square { public static void main(String[] args) { ConsoleReader c = new ConsoleReader(System.in); System.out.print("Please enter a positive number: "); double n = c.readDouble(); double x = 1; while (Math.abs(x * x - n) >= 0.000001) { System.out.println("New guess: " + x); x = (x + n / x) / 2; } System.out.println(x + " squared is " + x * x); } } :::::::::::::: Vowels.java :::::::::::::: class Vowels { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); String line; System.out.print("Type something: "); line = console.readLine(); int i = 0, j = 0; while (! (line.equals("done"))) { j = 0; int v = 0; while (j < line.length()) { char c = line.charAt(j); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ) { v = v + 1; } j = j + 1; } i = i + 1; System.out.println(i + ". " + line + " has " + v + " vowels in it."); System.out.print("Type something: "); line = console.readLine(); } } }