import static dit948.SimpleIO.*; import static dit948.Random.*; public class Solutions_Sep5 { public static void main(String[] args) { //Choose the program you want to run here ex12(); } /** * Write a program that randomly generates an integer between 1 and 12 and * displays the English month name January, February, ..., December for the * number 1, 2, ..., 12, accordingly. * * Hint: Use functions from Random library as described in the IOCheatSheet. */ public static void ex1() { int number = randomInt(12) + 1; if (number == 1) System.out.println("Month is Januaray"); else if (number == 2) System.out.println("Month is Feburary"); else if (number == 3) System.out.println("Month is March"); else if (number == 4) System.out.println("Month is April"); else if (number == 5) System.out.println("Month is May"); else if (number == 6) System.out.println("Month is June"); else if (number == 7) System.out.println("Month is July"); else if (number == 8) System.out.println("Month is August"); else if (number == 9) System.out.println("Month is September"); else if (number == 10) System.out.println("Month is October"); else if (number == 11) System.out.println("Month is November"); else // if (number == 12) System.out.println("Month is December"); } /** * Write an interactive quiz. It should ask the user three multiple-choice * or true/false questions about something. It must keep track of how many * they get right, and print out a "score" at the end. */ public static void ex2() { print("Are you ready for a quiz? (Y\\N): "); char inputChar; int inputInt; int score = 0; inputChar = readChar(); if (inputChar != 'Y') return; println("\nOkay, here it comes!\n"); println("Q1) What is the capital of Albania?\n" + "\t1) Sofia\n" + "\t2) Tirana\n" + "\t3) Budapest\n"); print("> "); inputInt = readInt(); if (inputInt == 2) { score += 1; println("\nThat's right!\n"); } else { println("Nope, the right answer is Tirana."); } println("Q2) Can you store the value \"dog\" in a variable of type int?\n" + "\t1) yes\n" + "\t2) no"); print("> "); inputInt = readInt(); if (inputInt == 2) { score += 1; println("\nThat's right!\n"); } else { println("Sorry, \"dog\" is a string. ints can only store numbers.\n"); } println("Q3) What is the result of 6+4/2?\n" + "\t1) 5\n" + "\t2) 8\n" + "\t3) 4\n"); print("> "); inputInt = readInt(); if (inputInt == 2) { score += 1; println("\nThat's correct!\n"); } else { println("Sorry, 5 is the right answer. \n"); } println("Overall, you got " + score + " out of 3 correct."); println("Thanks for playing!"); } /** * Write a program that prompts the user to enter an integer and determines * whether it is divisible by 5 and 6, whether it is divisible by 5 or 6, * and whether it is divisible by 5 or 6, but not both. */ public static void ex3() { // Enter an integer print("Enter an integer: "); int number = readInt(); println("Is " + number + " divisible by 5 and 6? " + ((number % 5 == 0) && (number % 6 == 0))); println("Is " + number + " divisible by 5 or 6? " + ((number % 5 == 0) || (number % 6 == 0))); println("Is " + number + " divisible by 5 or 6, but not both? " + ((number % 5 == 0) ^ (number % 6 == 0))); } /** * Write a program that simulates picking a (random) card from a deck of 52 * cards. Your program should display the rank ( Ace, 2, 3, 4, 5, 6, 7, 8, * 9, 10, Jack, Queen, King ) and suit ( Clubs, Diamonds, Hearts, Spades) of * the card. */ public static void ex4() { final int NUMBER_OF_CARDS = 52; // Pick a card int number = randomInt(NUMBER_OF_CARDS); print("The card you picked is "); if (number % 13 == 0) print("Ace of "); else if (number % 13 == 10) print("Jack of "); else if (number % 13 == 11) print("Queen of "); else if (number % 13 == 12) print("King of "); else print((number % 13) + " of "); if (number / 13 == 0) println("Clubs"); else if (number / 13 == 1) println("Diamonds"); else if (number / 13 == 2) println("Hearts"); else if (number / 13 == 3) println("Spades"); } /** * Write a program that prompts the user to enter the first 9 digits and * displays the 10-digit ISBN (including leading zeros). Your program should * read the input as an integer. */ public static void ex5() { // Prompt the user to enter an integer print("Enter the first 9 digits of an ISBN as integer: "); int number = readInt(); // Calculate checksum. You may also write a loop int checksum = ((number / 100000000 % 10) * 1 + (number / 10000000 % 10) * 2 + (number / 1000000 % 10) * 3 + (number / 100000 % 10) * 4 + (number / 10000 % 10) * 5 + (number / 1000 % 10) * 6 + (number / 100 % 10) * 7 + (number / 10 % 10) * 8 + (number % 10) * 9) % 11; print("The ISBN-10 number is "); // Display leading zeros, improve the solution using loops if (number / 100000000 == 0) { print("0"); if (number / 10000000 == 0) { print("0"); if (number / 1000000 == 0) { print("0"); if (number / 100000 == 0) { print("0"); if (number / 10000 == 0) { print("0"); if (number / 1000 == 0) { print("0"); if (number / 100 == 0) { print("0"); if (number / 10 == 0) { print("0"); if (number == 0) { print("0"); } } } } } } } } } print(number); if (checksum == 10) print("X"); else print(checksum); } /** * Use a while loop to find the smallest integer n such that n^2 (n square) * is greater than 12000. */ public static void ex6() { int i = 1; while (i * i <= 12000) { i++; } println("This number is " + i); } /** * Write a program that reads an unspecified number of integers, determines * how many positive and negative values have been read, and computes the * total and average of the input values (not counting zeros). Your program * ends with the input 0. Display the average as a real number. */ public static void ex7() { int countPositive = 0, countNegative = 0; int count = 0, total = 0, num; print("Enter integers ending with 0: "); num = readInt(); while (num != 0) { if (num > 0) countPositive++; else if (num < 0) countNegative++; total += num; count++; // Read the next number num = readInt(); } if (count == 0) println("no numbers are entered except 0"); else { println("The number of positives is " + countPositive); println("The number of negatives is " + countNegative); println("The total is " + total); println("The average is " + total * 1.0 / count); } } /** * Write a program that asks for a positive integer until the uses enters * one, and calculates the binary representation of that integer. Hint: Use * the modulo operator % */ public static void ex8() { int input, inputOriginal; do { print("> Enter a positive integer: "); input = readInt(); inputOriginal = input; } while (input < 0); String bitstring = ""; while (input > 0) { int bit = input % 2; int quotient = input / 2; bitstring = bit + bitstring; input = quotient; } println("The binary representation of " + inputOriginal + " is " + bitstring); } /** * Write a program that prompts the user to enter a string and displays the * string in reverse order. */ public static void ex9() { print("Enter a string: "); String s = readString(); String result = ""; for (int i = 0; i < s.length(); i++) { result = s.charAt(i) + result; } println("The reversed string is " + result); } /** * Write a program that reads an integer and displays all its smallest * factors in increasing order. For example, if the input integer is 120, * the output should be as follows: 2, 2, 2, 3, 5. */ public static void ex10() { // Prompt the user to enter a positive integer print("Enter a positive integer: "); int number = readInt(); // Find all the smallest factors of the integer println("The factors for " + number + " is"); int factor = 2; while (factor <= number) { if (number % factor == 0) { number = number / factor; println(factor); } else { factor++; } } } /** * Write a program that prompts the user to enter the number of students and * each student's name and score, and finally displays the name of the * student with the highest score. */ public static void ex11() { // Prompt the user to enter the number of students print("Enter the number of students: "); int numOfStudents = readInt(); print("Enter a student name: "); String student1 = readLine(); print("Enter a student score: "); double score1 = readDouble(); for (int i = 0; i < numOfStudents - 1; i++) { print("Enter a student name: "); String student = readLine(); print("Enter a student score: "); double score = readDouble(); if (score > score1) { student1 = student; score1 = score; } } println("Top student " + student1 + "'s score is " + score1); } /** * Write a program that prompts the user to enter an integer from 1 to 15 * and displays a pyramid */ public static void ex12() { // Prompt the user to enter the number of lines print("Enter the number of lines: "); int numberOfLines = readInt(); if (numberOfLines < 1 || numberOfLines > 15) { println("You must enter a number from 1 to 15"); System.exit(1); //terminates the program } // Print lines for (int row = 1; row <= numberOfLines; row++) { // Print (NUMBER_OF_LINES - row) leading spaces for (int column = 1; column <= numberOfLines - row; column++) print(" "); // Print leading numbers row, row - 1, ..., 1 for (int num = row; num >= 1; num--) print((num >= 10) ? " " + num : " " + num); // Print ending numbers 2, 3, ..., row - 1, row for (int num = 2; num <= row; num++) print((num >= 10) ? " " + num : " " + num); // Start a new line println(""); } } }