### CS170, Homework 5

• Overview

• This homework will reinforce the concept of loops and re-introduce you to the concept of methods that you have seen in lab 3

• You have used methods in your last homework assignment, e.g.:

 ``` x = input.nextInt(); // Read in an integer y = input.nextInt(); // Read in another integer ```

We have also used methods in class:

 ``` y = Math.sin( x ); // Compute sin(x) b = Math.sin( a ); // Compute sin(a) ```

When we want to perform the same task over and over again, we define a method to perform that task.

After writing the definition of the method, we can invoke (call or use) the method over and over again.

• Because we have not studied how to define methods yet, we will give you the structure of the method definition.

All you need to do in this homework is to write the statements inside the body of the method to make the method perform the task.

• Remember that:

• A method can receive information from its input parameters.

Example:

 ``` public static double cube(double a) // a = input parameter of method { double b; b = a * a * a; // Compute a^3 // a is given some value when // the method is called (invoked), see main() below: // cube( 2.0 ) will give value 2.0 to a // cube( 5.0 ) will give value 2.0 to a return(b); // Return the value b (see below for explanation) } public static void main(String[] args) { double x; x = cube( 2.0 ); // When you call cube with 2.0, you will // assign 2.0 to the input parameter a x = cube( 5.0 ); // When you call cube with 5.0, you will // assign 5.0 to the input parameter a } ```

• When the method executes a return statement:

 ``` return expression ; ```

the method is exited immediately and the value given in expression is returned.

• Preparation

• Make a hw5 directory and copy over the prepared files:

 ``` mkdir ~/cs170/hw5 cp ~cs170002/share/hw5/*.java ~/cs170/hw5 cd ~/cs170/hw5 ```

 You can use BlueJ or gedit If you use BlueJ from your dorm/apartment, it can be very slow... If you use gedit, you need to use javac ... to compile and java ... to run the programs.

• Part 1: write a method that return the Least Common Multiple of 2 (input) numbers

• In the first part of hw5 assignment you will write a method LCM(x,y) inside the program LCM.java that return the Least Common Multiple (LCM) of the 2 input numbers x and y.

• The LCM of a and b is the smallest integer value that is divisible by both a and b

Examples:

 LCM(5, 7) = 35    (because 35 is the smallest value that is divisible by 5 and 7) LCM(4, 6) = 12    (because 12 is the smallest value that is divisible by 4 and 6)

• You will use this brute force search to find the LCM:

 ``` for ( cand = first_number; cand <= last_number; x++ ) { check if cand is the LCM of input numbers x and y; if cand IS the LCM, what should you do ? if cand IS NOT the LCM, when should you do ? } ```

Before you can write this algorithm, you need to answer these questions first:

 Which number should you start with when looking for the LCM ? Which number should you stop with when looking for the LCM ? See a hint below ! How can you tell if a candidate number is the LCM of the (input) numbers x and y ???

• Helpful Property: (when should you stop searching)

 ``` LCM(a, b) ≤ a × b ```

because the number a × b is divisble by a and by b.

Where to put your code and how to compile and run

• Where to put the LCM algorithm:

• Put your algorithm inside the LCM method inside the LCM.java program:

 ``` /** * This method finds the LCM of a and b * * @param a first input number * @param b second input number * @return the LCM of a and b */ public static int LCM(int a, int b) { int ans = 0; // Let max = max(a,b) // // For each number x in the list (max, max+1, max+2, ....., a*b) // { // test if the number x can be divided by a AND b // } // // The FIRST number that is divisible by a AND b // is the LCM - return this value return ans; } ```

Do not make any changes to the main() method !!!

• How to compile and run LCM.java:

 ``` cd ~/cs170/hw5 javac LCM.java java LCM Correct output: LCM of 5 and 7 = 35 LCM of 4 and 6 = 12 LCM of 6 and 21 = 42 LCM of 35 and 49 = 245 ```

• Part 2: write a method to determine if a string is a palindrome

• In the second part of hw5, you will write a method isPalindrome(s) inside the program Palindrome.java that returns:

 ``` true if the input string s is a palindrome false if the input string s is NOT a palindrome ```

• A palindrome is a string that is equal to itself when read backwards

Examples:

 ``` civic eve ```

• Algorithm to find out if a string is a palindrome:

 ``` Example: suppose the variable s contains: s = "atlanta" 1. The algorithm need to keep some information.... We define 2 index variables: i and j i "points" to the left character that you want to test j "points" to the right character that you want to test We can make i and j "point" to a position by storing the position index in the variable ! OK, let's look at the algorithm next. 2. Start: make i point to the left most character make j point to the right most character Example: if we set: i = 0; j = 6; we will achieve this step: 0123456 "atlanta" ^ ^ | | i=0 j=6 3. You can get the character of the string at the i-th position and the character at the j-th position in string s using these expressions: s.charAt(i) s.charAt(j) You want to compare these characters next.... Because characters are just integer (Unicode), you can simply use: s.charAt(i) == s.charAt(j) to compare them ! 4. If the characters are the same, you must continue the test: 0123456 "atlanta" ^ ^ | | i=0 j=6 they are both 'a', continue To continue, move i to right and move j to left: 0123456 "atlanta" ^ ^ | | i=1 j=5 5. Since this pair of characters (s.charAt(i) is 't' and s.charAt(j) is also 't') is also equal, we move i and j: 0123456 "atlanta" ^ ^ | | i=2 j=4 When the characters are NOT equal, you KNOW the word is NOT a palindrome and you can return IMMEDIATELY. You can use a "return(false);" to return the false value immediately. 6. How will the algorithm behave if you have a palindrome: "kayak" i is the LEFT character and j is the RIGHT character. We keep moving i to the right and j to the left.... If the string is a palindrome, then sooner or later, i and j will PASS each other: i > j This is the indication that we have check ALL pairs of letters and each pair MATCH UP. When this happens, you must exit the loop and then return TRUE !!! ```

• Again, to write a loop, you must find:

 The starting values of the variables that you use to check every solution. In this problem, you must use 2 variables (I used i and j in the description above) to go through every possible pairs of characters to compare. When do you stop (I gave you the hint that: i > j !)

• Hint on where to start the positions i and j:

 ``` Input string s; s = "................" ^ ^ | | left most right most position position left most character position of a string s = 0 right most character position of a string s = s.length() - 1 ```

• Where to put the code:

• Put your algorithm inside the isPalindrome() method inside the Palindrome.java program:

 ``` public static boolean isPalindrome( String s ) { // ********************************************** // Write your algorithm inside this method // ********************************************** // Return true if input s is a palindrome // Return false if input s is NOT a palindrome // // DO NOT make a Scanner (no input from keyboard used) // // Algorithm summary: // 0 n=s.length()-1 // s = "..............." // ^ ^ // | | // s.charAt(i) == s.charAt(j) ??? // // Where should you start i and j ? // How can you tell when you are done ??? // What do you do if s.charAt(i) == s.charAt(j) ??? // What do you do if s.charAt(i) != s.charAt(j) ??? // // Note: you can use "return(true);" or "return(false);" // to make the method exit immediately with // return value specified. } ```

• Turnin

• Use the following commands to turn in your homework:

 ``` cd ~/cs170/hw5 /home/cs170002/turnin-hw LCM.java hw5 /home/cs170002/turnin-hw Palindrome.java hw5a ```