/** * Created by karou on 15/09/2015. */ import java.util.Scanner; public class JavaExercises5To8 { /** * "Global" variable used for error checking. * Always check if the value is false after an input * operation, otherwise the result can be incorrect! */ public static boolean IOError = false; public static void main(String[] args) { // ex5(); // ex6(); // ex7(); ex8(); } /** * 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 ex5() { 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 asks the user for a string and returns the number * of vowels in that string. Hint. You may want to use the method: * s.indexOf(c) which returns -1 if character c is not in string s * Example: * > Enter a string: Musard * > The string "Musard" contains 2 vowels. */ public static void ex6() { String input; int vowelsNum = 0; print("> Enter a string: "); input = readString(); for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); // handle both uppercase and lowercase characters c = Character.toLowerCase(c); switch (c) { case 'a': // fall through case 'e': // fall through case 'i': // fall through case 'o': // fall through case 'u': // increment vowels count vowelsNum += 1; } } println("The string \"" + input + "\" contains " + vowelsNum + " vowels."); } /** * 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 % * Example: * * > Enter a positive integer: -3 * > Enter a positive integer: -88 * > Enter a positive integer: -8 * > The binary representation of 8 is 100 */ public static void ex7() { int input, inputOriginal; print("> Enter a positive integer: "); input = readInt(); inputOriginal = input; // rerun function if input is small than 0 if (input < 0) ex7(); 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 subroutine (function) that given an input string, returns * true or false depending on whether or not that string is a palindrome. * A palindrome is a sequence of characters which reads the same backward * or forward. For instance, "abba" and "bob" are palindromes, while "aab" * is not. Use the subroutine in the main method to check the words above. */ public static void ex8() { String input; print("> Enter a string: "); input = readString(); // assume the input is palindrome unless proven otherwise boolean isPalindrome = true; // initialise two counters // counter i starts at the beginning of the word // counter j starts at the end of the word // compare characters at both counter positions // if characters are different, set palindrome flag to false // loop through half the word for (int i = 0, j = input.length() - 1; i < input.length() / 2; i++, j--) { // exit loop when palindrome already false to save resources if (! isPalindrome) break; if (input.charAt(i) != input.charAt(j)) isPalindrome = false; } if (isPalindrome) { println("String " + input + " is a palidinrome."); } else { println("String " + input + " is NOT a palidinrome."); } } /** * Generic print. * @param o object to print */ public static void print(Object o) { System.out.print(o); IOError = false; } /** * Generic println. * @param o object to print */ public static void println(Object o) { System.out.println(o); IOError = false; } /** * Reads next char from standard input. * Should perhaps set IOResult on EOF, but currently doesn't. * @return next char read from standard input. */ public static char readChar() { IOError = false; char result = 0; try { result = (char)System.in.read(); } catch (Exception e) { IOError = true; } return result; } /** * Reads next int from standard input. * Sets IOResult in case of error. * @return next int read from standard input. */ public static int readInt() { IOError = false; Scanner scan = new Scanner(System.in); int result = 0; try { result = scan.nextInt(); } catch (Exception e) { IOError = true; } return result; } /** * Reads next string from standard input. * Stops at the first empty space. * Sets IOResult in case of error. * @return next string read from standard input. */ public static String readString() { IOError = false; Scanner scan = new Scanner(System.in); String result = ""; try { result = scan.next(); } catch (Exception e) { IOError = true; } return result; } }