Introduction
Requirement
CSE114: Computer Science 1 Spring 2018
Homework2:
Due Date: Homework2 is due by 11:59 PM on Monday, March 12, 2018. Submit your work
(the .java source code files ONLY, not the compiled .class files!) through the “Homework2” link
on Blackboard. You may submit an unlimited number of times; we will only grade the last/latest
submission attempt, but be sure to attach all of your files to each submission attempt. Be sure to
include your name and Stony Brook ID number in a comment at the beginning of each file that
you submit.
Instructions: This assignment is worth 40 points (10 points per program).
1. Write a method that checks whether the input string or a sentence (a string with spaces) is a
palindrome or not. The method should be case insensitive and should ignore spaces. Write a
test program that prompts the user to input a string and invokes this method. Some example
runs are:
Enter the input string: madam
Input string madam is a palindrome
Enter the input string: banana
Input string banana is NOT a palindrome
Enter the input string: Race Car
Input string Race Car is a palindrome
Enter the input string: Too HOT to hoot
Input string Too HOT to hoot is a palindrome
2. Two strings are anagrams if they are written using the same exact letters. Write a method to
check if given two strings are anagrams or not. You have to ignore the case and space
characters. Write a test program for that prompts the user to input two strings and invokes this
method. Some example runs are:
Enter the first string: abbacba
Enter the second string: abcabba
abbacba and abcabba are anagrams
Enter the first string: banana
Enter the second string: cabana
banana and cabana are NOT anagrams
Enter the first string: Eleven plus two
Enter the second string: Twelve plus one
CSE114: Computer Science 1 Spring 2018
Eleven plus two and Twelve plus one are anagrams
3. Write down a sort method to sort an array of String using bubble-sort algorithm. Bubble sort
algorithm makes several passes through the array. On each pass, successive neighboring pairs
are compared. If a pair is not in order, its values are swapped; otherwise the value remains
unchanged. The technique is called bubble sort or sinking sort because the smaller values
gradually “bubble” their way to the top, and the larger values “sink” to the bottom.
Write a test program that reads in 10 String values and invokes the bubble sort method. Display
the sorted string array.
Example run:
Enter 10 strings: New York City, Austin, Dallas, Seattle, Washington D.C., Houston, Chicago,
Las Vegas, Charlotte, Denver,
Sorted strings: Austin, Charlotte, Chicago, Dallas, Denver, Houston, Las Vegas, New York
City, Seattle, Washington D.C.,
4. Write a method to multiply two matrices. The header of the method is:
?????? ?????? ??????[][] ??????????????(?????? [][] ?,??????[][] ?).
To multiply matrix ? by matrix ?, the number of columns in ? must be the same as the number
of rows in ?, and the two matrices must have elements of thsame or compatible types. Let ?
be the result of the multiplication. Assume the column size of matrix ? is ?. Each element
? ?? = ? ?1 × ? 1? + ? ?2 × ? 2? + ⋯+ ? ?? × ? ?? .
For example, the two 3 × 3 matrices ? and ?, ? is:
(
? 11
? 12 ? 13
? 21
? 31
? 22
? 32
? 23
? 33
) × (
? 11 ? 12 ? 13
? 21
? 31
? 22
? 32
? 23
? 33
) = (
? 11
? 12 ? 13
? 21
? 31
? 22
? 32
? 23
? 33
)
Where ? ?? = ? ?1 × ? 1? + ? ?2 × ? 2? + ? ?3 × ? 3? .
Write a test program that prompts the user to enter two 3 × 3 matrices and displays their
product. Here is a sample run:
Enter matrix1: 1 2 3 4 5 6 7 8 9
Enter matrix2: 0 2 4 1 4.5 2.2 1.1 4.3 5.2
Multiplication of the matrices is: 5.3 23.9 24
11.6 56.3 58.2
17.9 88.7 92.4