---------------------------------------------------------------------------------------------------------------- :::::::::::::: Calculate.java :::::::::::::: class Calculate { public static void main(String[] args) { ConsoleReader in = new ConsoleReader(System.in); System.out.print("Please enter a string of up to 10 digits: "); String num = in.readLine(); if (num.length() > 10) { System.out.println("Sorry, the string is too long."); } else { num = "0000000000".substring(0, 10 - num.length()) + num; int sum = 0; sum += num.charAt(0) - '0'; sum += num.charAt(1) - '0'; sum += num.charAt(2) - '0'; sum += num.charAt(3) - '0'; sum += num.charAt(4) - '0'; sum += num.charAt(5) - '0'; sum += num.charAt(6) - '0'; sum += num.charAt(7) - '0'; sum += num.charAt(8) - '0'; sum += num.charAt(9) - '0'; System.out.println("The sum of the digits is: " + sum); } } } ---------------------------------------------------------------------------------------------------------------- :::::::::::::: Clock.java :::::::::::::: class Clock { public static void main(String[] args) { ConsoleReader in = new ConsoleReader(System.in); System.out.print("Please enter the time: "); String time = in.readLine(); String hour = time.substring(0, 2), minute = time.substring(3); int hr = Integer.parseInt(hour), mn = Integer.parseInt(minute); if (hr > 24 || hr < 0 || mn > 59 || mn < 0) { System.out.println("Sorry, that's not a valid time."); } else { String rH = "00" + hr, rM = "00" + mn; System.out.println(rH.substring(rH.length() - 2) + ":" + rM.substring(rM.length() - 2)); mn += 1; if (mn == 60) { mn = 0; hr += 1; if (hr == 24) { hr = 0; } } rH = "00" + hr; rM = "00" + mn; System.out.println(rH.substring(rH.length() - 2) + ":" + rM.substring(rM.length() - 2)); mn += 1; if (mn == 60) { mn = 0; hr += 1; if (hr == 24) { hr = 0; } } rH = "00" + hr; rM = "00" + mn; System.out.println(rH.substring(rH.length() - 2) + ":" + rM.substring(rM.length() - 2)); mn += 1; if (mn == 60) { mn = 0; hr += 1; if (hr == 24) { hr = 0; } } rH = "00" + hr; rM = "00" + mn; System.out.println(rH.substring(rH.length() - 2) + ":" + rM.substring(rM.length() - 2)); mn += 1; if (mn == 60) { mn = 0; hr += 1; if (hr == 24) { hr = 0; } } rH = "00" + hr; rM = "00" + mn; System.out.println(rH.substring(rH.length() - 2) + ":" + rM.substring(rM.length() - 2)); mn += 1; if (mn == 60) { mn = 0; hr += 1; if (hr == 24) { hr = 0; } } rH = "00" + hr; rM = "00" + mn; System.out.println(rH.substring(rH.length() - 2) + ":" + rM.substring(rM.length() - 2)); mn += 1; if (mn == 60) { mn = 0; hr += 1; if (hr == 24) { hr = 0; } } rH = "00" + hr; rM = "00" + mn; System.out.println(rH.substring(rH.length() - 2) + ":" + rM.substring(rM.length() - 2)); mn += 1; if (mn == 60) { mn = 0; hr += 1; if (hr == 24) { hr = 0; } } rH = "00" + hr; rM = "00" + mn; System.out.println(rH.substring(rH.length() - 2) + ":" + rM.substring(rM.length() - 2)); mn += 1; if (mn == 60) { mn = 0; hr += 1; if (hr == 24) { hr = 0; } } rH = "00" + hr; rM = "00" + mn; System.out.println(rH.substring(rH.length() - 2) + ":" + rM.substring(rM.length() - 2)); mn += 1; if (mn == 60) { mn = 0; hr += 1; if (hr == 24) { hr = 0; } } rH = "00" + hr; rM = "00" + mn; System.out.println(rH.substring(rH.length() - 2) + ":" + rM.substring(rM.length() - 2)); mn += 1; if (mn == 60) { mn = 0; hr += 1; if (hr == 24) { hr = 0; } } rH = "00" + hr; rM = "00" + mn; System.out.println(rH.substring(rH.length() - 2) + ":" + rM.substring(rM.length() - 2)); } } } ---------------------------------------------------------------------------------------------------------------- :::::::::::::: Divisible.java :::::::::::::: class Divisible { public static void main(String[] args) { ConsoleReader in = new ConsoleReader(System.in); System.out.print("Please enter the first number: "); int n1 = in.readInt(); if (n1 % 2 == 0) { System.out.println(" " + n1 + " is divisible by 2"); } else { System.out.println(" " + n1 + " is not divisible by 2"); } if (n1 % 3 == 0) { System.out.println(" " + n1 + " is divisible by 3"); } else { System.out.println(" " + n1 + " is not divisible by 3"); } if (n1 % 5 == 0) { System.out.println(" " + n1 + " is divisible by 5"); } else { System.out.println(" " + n1 + " is not divisible by 5"); } System.out.print("Please enter the second number: "); int n2 = in.readInt(); if (n2 % 2 == 0) { System.out.println(" " + n2 + " is divisible by 2"); } else { System.out.println(" " + n2 + " is not divisible by 2"); } if (n2 % 3 == 0) { System.out.println(" " + n2 + " is divisible by 3"); } else { System.out.println(" " + n2 + " is not divisible by 3"); } if (n2 % 5 == 0) { System.out.println(" " + n2 + " is divisible by 5"); } else { System.out.println(" " + n2 + " is not divisible by 5"); } System.out.print("Please enter the third number: "); int n3 = in.readInt(); if (n3 % 2 == 0) { System.out.println(" " + n3 + " is divisible by 2"); } else { System.out.println(" " + n3 + " is not divisible by 2"); } if (n3 % 3 == 0) { System.out.println(" " + n3 + " is divisible by 3"); } else { System.out.println(" " + n3 + " is not divisible by 3"); } if (n3 % 5 == 0) { System.out.println(" " + n3 + " is divisible by 5"); } else { System.out.println(" " + n3 + " is not divisible by 5"); } System.out.print("Please enter the fourth number: "); int n4 = in.readInt(); if (n4 % 2 == 0) { System.out.println(" " + n4 + " is divisible by 2"); } else { System.out.println(" " + n4 + " is not divisible by 2"); } if (n4 % 3 == 0) { System.out.println(" " + n4 + " is divisible by 3"); } else { System.out.println(" " + n4 + " is not divisible by 3"); } if (n4 % 5 == 0) { System.out.println(" " + n4 + " is divisible by 5"); } else { System.out.println(" " + n4 + " is not divisible by 5"); } System.out.print("Please enter the fifth number: "); int n5 = in.readInt(); if (n5 % 2 == 0) { System.out.println(" " + n5 + " is divisible by 2"); } else { System.out.println(" " + n5 + " is not divisible by 2"); } if (n5 % 3 == 0) { System.out.println(" " + n5 + " is divisible by 3"); } else { System.out.println(" " + n5 + " is not divisible by 3"); } if (n5 % 5 == 0) { System.out.println(" " + n5 + " is divisible by 5"); } else { System.out.println(" " + n5 + " is not divisible by 5"); } System.out.print("Please enter the sixth number: "); int n6 = in.readInt(); if (n6 % 2 == 0) { System.out.println(" " + n6 + " is divisible by 2"); } else { System.out.println(" " + n6 + " is not divisible by 2"); } if (n6 % 3 == 0) { System.out.println(" " + n6 + " is divisible by 3"); } else { System.out.println(" " + n6 + " is not divisible by 3"); } if (n6 % 5 == 0) { System.out.println(" " + n6 + " is divisible by 5"); } else { System.out.println(" " + n6 + " is not divisible by 5"); } } } ---------------------------------------------------------------------------------------------------------------- :::::::::::::: Fats.java :::::::::::::: class Fats { public static void main(String[] args) { ConsoleReader in = new ConsoleReader(System.in); System.out.print("Please enter total number of calories: "); double cal = in.readDouble(); System.out.print("Please enter the total grams of fat: "); double fat = in.readDouble(); double calFat = 9 * fat; if (calFat > cal || cal < 0 || calFat < 0) { System.out.println("Sorry, your data is not consistent. "); } else { if (calFat < 0.3 * cal) { System.out.println("Food low in fat."); } else { System.out.println("Food not low in fat."); } } } } ---------------------------------------------------------------------------------------------------------------- :::::::::::::: GPA.java :::::::::::::: class GPA { public static void main(String[] args) { ConsoleReader in = new ConsoleReader(System.in); System.out.print("Please enter the first number: "); double n1 = in.readDouble(); if (n1 < 0 || n1 > 100) { System.out.println("Sorry, " + n1 + " is not a valid test score."); } else { System.out.print("Please enter the second number: "); double n2 = in.readDouble(); if (n2 < 0 || n2 > 100) { System.out.println("Sorry, " + n2 + " is not a valid test score."); } else { System.out.print("Please enter the third number: "); double n3 = in.readDouble(); if (n3 < 0 || n3 > 100) { System.out.println("Sorry, " + n3 + " is not a valid test score."); } else { double avg = (n1 + n2 + n3) / 3; avg = Math.round(avg * 100) / 100.0; System.out.println("The average of " + n1 + ", " + n2 + ", " + n3 + " is: " + avg); if (avg >= 90) { System.out.println("Congratulations. Your average " + avg + " is bigger than 90."); } else { System.out.println("Your average " + avg + " is not above 90."); } } } } } } ---------------------------------------------------------------------------------------------------------------- :::::::::::::: Grades.java :::::::::::::: public class Grades { public static void main(String[] args) { double score; ConsoleReader console = new ConsoleReader(System.in); System.out.print("Enter numeric score then press Enter : "); score = console.readDouble(); if (score >= 4.0) { System.out.println("A+"); } else if (score >= 3.85) { System.out.println("A"); } else if (score >= 3.5) { System.out.println("A-"); } else if (score >= 3.15) { System.out.println("B+"); } else if (score >= 2.85) { System.out.println("B"); } else if (score >= 2.5) { System.out.println("B-"); } else if (score >= 2.15) { System.out.println("C+"); } else if (score >= 1.85) { System.out.println("C"); } else if (score >= 1.5) { System.out.println("C-"); } else if (score >= 1.15) { System.out.println("D+"); } else if (score >= 0.85) { System.out.println("D"); } else if (score >= 0.35) { System.out.println("D-"); } else { System.out.println(""); } } } ---------------------------------------------------------------------------------------------------------------- :::::::::::::: PRS.java :::::::::::::: class PRS { public static void main(String[] args) { int usr, cmp; int scoreU = 0, scoreC = 0; String user, computer; ConsoleReader in = new ConsoleReader(System.in); usr = -1; cmp = (int)(Math.random() * 3); if (cmp == 0) { computer = "paper"; } else { if (cmp == 1) { computer = "rock"; } else { computer = "scissors"; } } System.out.println("The computer has chosen: " + computer); System.out.print("What do you choose (paper, rock or scissors): "); user = in.readLine(); if (user.equals("paper")) { usr = 0; } else { if (user.equals("rock")) { usr = 1; } else { if (! user.equals("scissors")) { System.out.println("I am afraid I don't understand your input."); } else { usr = 2; } } } if (usr == 0 || usr == 1 || usr == 2) { if ((usr + 1) % 3 == cmp) { System.out.println("You win."); scoreU += 1; } else { if ((cmp + 1) % 3 == usr) { System.out.println("You lose."); scoreC += 1; } else { System.out.println("It's a tie."); } } } System.out.println("The score is currently: Computer (" + scoreC + ") - User (" + scoreU + ")"); usr = -1; cmp = (int)(Math.random() * 3); if (cmp == 0) { computer = "paper"; } else { if (cmp == 1) { computer = "rock"; } else { computer = "scissors"; } } System.out.println("The computer has chosen: " + computer); System.out.print("What do you choose (paper, rock or scissors): "); user = in.readLine(); if (user.equals("paper")) { usr = 0; } else { if (user.equals("rock")) { usr = 1; } else { if (! user.equals("scissors")) { System.out.println("I am afraid I don't understand your input."); } else { usr = 2; } } } if (usr == 0 || usr == 1 || usr == 2) { if ((usr + 1) % 3 == cmp) { System.out.println("You win."); scoreU += 1; } else { if ((cmp + 1) % 3 == usr) { System.out.println("You lose."); scoreC += 1; } else { System.out.println("It's a tie."); } } } System.out.println("The score is currently: Computer (" + scoreC + ") - User (" + scoreU + ")"); usr = -1; cmp = (int)(Math.random() * 3); if (cmp == 0) { computer = "paper"; } else { if (cmp == 1) { computer = "rock"; } else { computer = "scissors"; } } System.out.println("The computer has chosen: " + computer); System.out.print("What do you choose (paper, rock or scissors): "); user = in.readLine(); if (user.equals("paper")) { usr = 0; } else { if (user.equals("rock")) { usr = 1; } else { if (! user.equals("scissors")) { System.out.println("I am afraid I don't understand your input."); } else { usr = 2; } } } if (usr == 0 || usr == 1 || usr == 2) { if ((usr + 1) % 3 == cmp) { System.out.println("You win."); scoreU += 1; } else { if ((cmp + 1) % 3 == usr) { System.out.println("You lose."); scoreC += 1; } else { System.out.println("It's a tie."); } } } System.out.println("The score is currently: Computer (" + scoreC + ") - User (" + scoreU + ")"); usr = -1; cmp = (int)(Math.random() * 3); if (cmp == 0) { computer = "paper"; } else { if (cmp == 1) { computer = "rock"; } else { computer = "scissors"; } } System.out.println("The computer has chosen: " + computer); System.out.print("What do you choose (paper, rock or scissors): "); user = in.readLine(); if (user.equals("paper")) { usr = 0; } else { if (user.equals("rock")) { usr = 1; } else { if (! user.equals("scissors")) { System.out.println("I am afraid I don't understand your input."); } else { usr = 2; } } } if (usr == 0 || usr == 1 || usr == 2) { if ((usr + 1) % 3 == cmp) { System.out.println("You win."); scoreU += 1; } else { if ((cmp + 1) % 3 == usr) { System.out.println("You lose."); scoreC += 1; } else { System.out.println("It's a tie."); } } } System.out.println("The score is currently: Computer (" + scoreC + ") - User (" + scoreU + ")"); usr = -1; cmp = (int)(Math.random() * 3); if (cmp == 0) { computer = "paper"; } else { if (cmp == 1) { computer = "rock"; } else { computer = "scissors"; } } System.out.println("The computer has chosen: " + computer); System.out.print("What do you choose (paper, rock or scissors): "); user = in.readLine(); if (user.equals("paper")) { usr = 0; } else { if (user.equals("rock")) { usr = 1; } else { if (! user.equals("scissors")) { System.out.println("I am afraid I don't understand your input."); } else { usr = 2; } } } if (usr == 0 || usr == 1 || usr == 2) { if ((usr + 1) % 3 == cmp) { System.out.println("You win."); scoreU += 1; } else { if ((cmp + 1) % 3 == usr) { System.out.println("You lose."); scoreC += 1; } else { System.out.println("It's a tie."); } } } System.out.println("The score is currently: Computer (" + scoreC + ") - User (" + scoreU + ")"); usr = -1; cmp = (int)(Math.random() * 3); if (cmp == 0) { computer = "paper"; } else { if (cmp == 1) { computer = "rock"; } else { computer = "scissors"; } } System.out.println("The computer has chosen: " + computer); System.out.print("What do you choose (paper, rock or scissors): "); user = in.readLine(); if (user.equals("paper")) { usr = 0; } else { if (user.equals("rock")) { usr = 1; } else { if (! user.equals("scissors")) { System.out.println("I am afraid I don't understand your input."); } else { usr = 2; } } } if (usr == 0 || usr == 1 || usr == 2) { if ((usr + 1) % 3 == cmp) { System.out.println("You win."); scoreU += 1; } else { if ((cmp + 1) % 3 == usr) { System.out.println("You lose."); scoreC += 1; } else { System.out.println("It's a tie."); } } } System.out.println("The score is currently: Computer (" + scoreC + ") - User (" + scoreU + ")"); } } ---------------------------------------------------------------------------------------------------------------- :::::::::::::: Quiz.java :::::::::::::: class Quiz { public static void main(String[] args) { ConsoleReader in = new ConsoleReader(System.in); int n1, n2; int good = 0; int total = 0; double answer; n1 = (int) Math.round(Math.random() * 100 - 50); n2 = (int) Math.round(Math.random() * 100 - 50); System.out.print("What is " + n1 + " + " + n2 + ": "); answer = in.readInt(); if (answer == n1 + n2) { good += 1; System.out.println("Your answer was correct."); } else { System.out.println("Sorry, the right answer was: " + (n1 + n2)); } total += 1; System.out.println("Your total so far is: " + good + " out of " + total); n1 = (int) Math.round(Math.random() * 100 - 50); n2 = (int) Math.round(Math.random() * 100 - 50); System.out.print("What is " + n1 + " + " + n2 + ": "); answer = in.readInt(); if (answer == n1 + n2) { good += 1; System.out.println("Your answer was correct."); } else { System.out.println("Sorry, the right answer was: " + (n1 + n2)); } total += 1; System.out.println("Your total so far is: " + good + " out of " + total); n1 = (int) Math.round(Math.random() * 100 - 50); n2 = (int) Math.round(Math.random() * 100 - 50); System.out.print("What is " + n1 + " + " + n2 + ": "); answer = in.readInt(); if (answer == n1 + n2) { good += 1; System.out.println("Your answer was correct."); } else { System.out.println("Sorry, the right answer was: " + (n1 + n2)); } total += 1; System.out.println("Your total so far is: " + good + " out of " + total); n1 = (int) Math.round(Math.random() * 100 - 50); n2 = (int) Math.round(Math.random() * 100 - 50); System.out.print("What is " + n1 + " + " + n2 + ": "); answer = in.readInt(); if (answer == n1 + n2) { good += 1; System.out.println("Your answer was correct."); } else { System.out.println("Sorry, the right answer was: " + (n1 + n2)); } total += 1; System.out.println("Your total so far is: " + good + " out of " + total); n1 = (int) Math.round(Math.random() * 100 - 50); n2 = (int) Math.round(Math.random() * 100 - 50); System.out.print("What is " + n1 + " + " + n2 + ": "); answer = in.readInt(); if (answer == n1 + n2) { good += 1; System.out.println("Your answer was correct."); } else { System.out.println("Sorry, the right answer was: " + (n1 + n2)); } total += 1; System.out.println("Your total so far is: " + good + " out of " + total); n1 = (int) Math.round(Math.random() * 100 - 50); n2 = (int) Math.round(Math.random() * 100 - 50); System.out.print("What is " + n1 + " + " + n2 + ": "); answer = in.readInt(); if (answer == n1 + n2) { good += 1; System.out.println("Your answer was correct."); } else { System.out.println("Sorry, the right answer was: " + (n1 + n2)); } total += 1; System.out.println("Your total so far is: " + good + " out of " + total); } } ---------------------------------------------------------------------------------------------------------------- :::::::::::::: Songs.java :::::::::::::: class Songs { public static void main(String[] args) { ConsoleReader in = new ConsoleReader(System.in); System.out.println(" Welcome to the Music Program. Please choose from the menu below: "); System.out.println(" [1] The Great Beyond "); System.out.println(" [2] Fretless "); System.out.println(" [3] Losing My Religion "); System.out.println(" [4] It's a Free World, Baby "); System.out.print("What do you want to listen to? \n\nPlease enter a number: "); int choice = in.readInt(); if (choice == 1) { System.out.println("\n I've watched the stars fall silent from your eyes"); System.out.println(" All the sights that I have seen"); System.out.println(" I can't believe that I believed I wished"); System.out.println(" That you could see"); System.out.println(" There's a new planet in the solar system"); System.out.println(" There is nothing up my sleeve\n"); System.out.println(" I'm pushing an elephant up the stairs"); System.out.println(" I'm tossing up punchlines that were never there"); System.out.println(" Over my shoulder a piano falls"); System.out.println(" Crashing to the ground\n"); } else { if (choice == 2) { System.out.println("\n He's got his work and she comes easy"); System.out.println(" They each come around when the other is gone"); System.out.println(" Me, I think I got stuck somewhere in between"); System.out.println(" I wouldn't confide in the Prodigal Son"); System.out.println(" The die has been cast, the battle is won"); System.out.println(" The bullets were blanks, a double aught gun"); System.out.println(" I couldn't admit to a minute of fun\n"); } else { if (choice == 3) { System.out.println("\n Life is bigger"); System.out.println(" It's bigger than you"); System.out.println(" And you are not me"); System.out.println(" The lengths that I will go to"); System.out.println(" The distance in your eyes"); System.out.println(" Oh no I've said too much"); System.out.println(" I set it up\n"); System.out.println(" That's me in the corner"); System.out.println(" That's me in the spotlight"); System.out.println(" Losing my religion"); System.out.println(" Trying to keep up with you"); System.out.println(" And I don't know if I can do it"); System.out.println(" Oh no I've said too much"); System.out.println(" I haven't said enough"); System.out.println(" I thought that I heard you laughing"); System.out.println(" I thought that I heard you sing"); System.out.println(" I think I thought I saw you try\n"); } else { if (choice == 4) { System.out.println("\n I was hungry when I said"); System.out.println(" I never got to sleep"); System.out.println(" You go ahead"); System.out.println(" You can get whatever you want"); System.out.println(" There's a feeling in my belly"); System.out.println(" The new tomorrow scene"); System.out.println(" It's an interesting job"); System.out.println(" It's the fireworks\n"); System.out.println(" (chorus 1)"); System.out.println(" I don't need it (I don't need it)"); System.out.println(" I hit my head (he hit his head)"); System.out.println(" I can't feel anything"); System.out.println(" You gave too much away"); System.out.println(" You came to say"); System.out.println(" It's a free world baby"); System.out.println(" And you gave too much away.\n"); } else { System.out.println("\n I am sorry, I don't think this is one of the options I list.\n"); } } } } } } ---------------------------------------------------------------------------------------------------------------- :::::::::::::: Taxes.java :::::::::::::: class Taxes { public static void main(String[] args) { ConsoleReader in = new ConsoleReader(System.in); System.out.print("Please enter the income: "); double income = in.readDouble(); double percent = 0; if (income > 10000 || income < 4000) { System.out.println("Sorry the income doesn't sound right."); } else { if (income > 8000) { percent = 0.3; } else { if (income > 6000) { percent = 0.2; } else { percent = 0.1; } } System.out.println("For an income of " + income + " the taxes are " + Math.round(percent * 100) + "%, that is: " + Math.round(income * percent * 100) / 100.0); } } } ----------------------------------------------------------------------------------------------------------------