/* * An intro to the workings and built-in keywords of Java */ import java.lang.Math; // allows us to use the Math class of the library import java.util.Scanner; // ditto for the input Scanner class class JavaInteraction { public static void main(String[] args) { Scanner inputter = new Scanner( System.in ); // a small user-interaction loop while (true) { System.out.print("\nAsk me anything! > "); String user_line = inputter.nextLine(); // a Scanner method returning a whole line System.out.println("You typed: " + user_line); System.out.println("Your line had " + user_line.length() + " characters."); System.out.println("If you were shouting, it'd be " + user_line.toUpperCase() + "\n"); System.out.print("Checking if your input was a palindrome with spaces : " + isPal(user_line) + "\n"); /* * these are lines to use once you write the three methods * isPalNoSpace * canEval * eval System.out.print("Checking if your input was a palindrome without spaces: " + isPalNoSpace(user_line) + "\n"); System.out.print("Checking if your input can be evaluated: " + canEval(user_line) + "\n"); if (canEval(user_line)) { int value = eval( user_line ); System.out.println( "Your line evaluates to " + value ); } */ if (user_line.equals("quit")) { break; // get out of the while (true) loop! } } // Your task: create a more capable conversationalist! // - palindromes, using a function // - a small calculator // - anything else you'd like... } // end of the main method /* * These functions should be commented with a few lines explaining * - the name and its purpose * - details on the inputs * - details on what it outputs */ // function: isPal, checking for palindromes // inputs: a String, s, the input to check // output: true or false, whether s is a palindrome // // This is not quite correct -- be sure to fix it! // public static boolean isPal(String s) { int L = s.length(); if (L < 2) return true; if (s.charAt(0) != s.charAt(L-1)) return false; return true; } }