META HTTP-EQUIV="expires" CONTENT="0">
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.
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.
|
mkdir ~/cs170/hw5
cp ~cs170002/share/hw5/*.java ~/cs170/hw5
cd ~/cs170/hw5
|
|
Examples:
|
for ( cand = first_number; cand <= last_number; cand++ )
{
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:
|
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
|
Do not make any changes to the main() method !!!
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
|
true if the input string s is a palindrome false if the input string s is NOT a palindrome |
Examples:
civic eve |
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 !!!
|
|
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
|
|
cd ~/cs170/hw5
/home/cs170002/turnin-hw LCM.java hw5
/home/cs170002/turnin-hw Palindrome.java hw5a
|