Assignment
University
Stony Brook UniversityCourse
CSE 114 | Introduction to Object-Oriented ProgrammingPages
6
Academic year
2023
Riley
Views
78
CSE114: Introduction to Object Oriented Programming Spring 2022 Homework3: Due Date: Homework3 is due by 11:59 PM on Tuesday, April 13, 2022 . Submit your work (the .java source code files ONLY, not the compiled .class files!) through the “Homework3” 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. Submission Guidelines: 1. All the files should be inside a folder named as ‘LastName_FirstName_SBUID’ and upload it as zip file. [Example: A student named John Smith with SBUID 100200 will name the folder as Smith_John_100200 and should upload the zipped folder named Smith_John_100200.zip] 2. Each file should be named as HW*problem* (Example: ‘HW2problem1’) 3. If there are multiple classes for a problem, put each problem in a separate folder. Ensure that you compile the program before uploading (as structure mentioned above). If the program does not compile, you will lose points. Instructions: This assignment is worth 40 points (10 points per program). 1. Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: 𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑎𝑡𝑖𝑐 𝑖𝑛𝑡 [] 𝑒𝑙𝑖𝑚𝑖𝑛𝑎𝑡𝑒𝐷𝑢𝑝𝑙𝑖𝑐𝑎𝑡𝑒 ( 𝑖𝑛𝑡 [] 𝑙𝑖𝑠𝑡 ) Write a test program that reads in 10 integers, invokes the method, and displays the distinct numbers separated by exactly one space. Here is a sample run of the program: Enter 10 integers: 1 2 3 2 1 6 3 4 5 2 The distinct integers are: 1 2 3 6 4 5 2. Write the following method that merges two sorted lists into a new sorted list: 𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑎𝑡𝑖𝑐 𝑖𝑛𝑡 [] 𝑚𝑒𝑟𝑔𝑒 ( 𝑖𝑛𝑡 [] 𝑙𝑖𝑠𝑡 1, 𝑖𝑛𝑡 [] 𝑙𝑖𝑠𝑡 2) Write a test program that prompts the user to enter two sorted lists and displays the merged list. Here is a sample run. Enter list1 size and contents: 5 1 5 16 61 111 Enter list2 size and contents: 4 2 4 5 6 List1 is 1 5 16 61 111 List2 is 2 4 5 6
The merged list is 1 2 4 5 5 6 16 61 111 [Note the first number in the input indicates the number of the elements in the list. This number is not the part of the list.] 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. Sample 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. [Note that in the sample run the whole input is provided horizontally, which doesn’t mean that your input should be a single String containing all the cities names. In fact, you program should read (input) one String (representing a city) at a time.] 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 the same 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 𝑏 11 𝑏 12 𝑏 13 𝑐 11 𝑐 12 𝑐 13 ( 𝑎 21 𝑎 22 𝑎 23 ) × ( 𝑏 21 𝑏 22 𝑏 23 ) = ( 𝑐 21 𝑐 22 𝑐 23 ) 𝑎 31 𝑎 32 𝑎 33 𝑏 31 𝑏 32 𝑏 33 𝑐 31 𝑐 32 𝑐 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
Part 1 import java.util.Scanner ; public class HW3problem1 { public static void main(String[] args) { int [] list = new int [ 10 ] ; Scanner stdin = new Scanner(System.in) ; System.out.print( "Enter 10 integers: " ) ; for ( int i = 0 ; i < list.length ; i++) list[i] = stdin.nextInt() ; System.out.print( "The distinct integers are: " ) ; for ( int i : eliminateDuplicate(list)) { if (i == 0 ) continue; System.out.print(i + " " ) ; } } public static int [] eliminateDuplicate( int [] list) { int [] newList = new int [list.length] ; for ( int i = 0 ; i < list.length ; i++) { if (distinctElement(newList , list[i])) newList[i] = list[i] ; } return newList ; } public static boolean distinctElement( int [] list , int n) { for ( int i : list) if (n == i) return false; return true; } }
Part 2 import java.util.Scanner ; public class HW3problem2 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in) ; System.out.print( "Enter list1 size and contents: " ) ; int size1 = stdin.nextInt() ; int [] list1 = new int [size1] ; for ( int i = 0 ; i < size1 ; i++) list1[i] = stdin.nextInt() ; System.out.print( "Enter list2 size and contents: " ) ; int size2 = stdin.nextInt() ; int [] list2 = new int [size2] ; for ( int i = 0 ; i < size2 ; i++) list2[i] = stdin.nextInt() ; System.out.print( "List1 is " ) ; for ( int k : list1) System.out.print(k + " " ) ; System.out.println() ; System.out.print( "List2 is " ) ; for ( int k : list2) System.out.print(k + " " ) ; System.out.println() ; System.out.print( "The merged list is " ) ; for ( int k : merge(list1 , list2)) System.out.print(k + " " ) ; } public static int [] merge( int [] list1 , int [] list2) { int l1 = list1.length ; int l2 = list2.length ; int i1 = 0 ; int i2 = 0 ; int i ; int [] list = new int [l1+l2] ; for (i = 0 ; i1 < l1 && i2 < l2 ; i++) { if (list1[i1] < list2[i2]) { list[i] = list1[i1] ; i1++ ; } else { list[i] = list2[i2] ; i2++ ; } } if (i1 < l1) for ( int j = i1 ; j < l1 ; j++ , i++) { list[i] = list1[j] ; } else for ( int j = i2 ; j < l2 ; j++ , i++) { list[i] = list2[j] ; } return list ; } }
Part 3 import java.util.Scanner ; public class HW3problem3 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in) ; String[] cities = new String[ 10 ] ; System.out.print( "Enter 10 Strings: " ) ; for ( int i = 0 ; i < 10 ; i++) cities[i] = stdin.nextLine() ; sort(cities) ; System.out.print( "Sorted Strings: " ) ; for ( int i = 0 , l = cities.length ; i < l ; i++) { if (i < l- 1 ) System.out.print(cities[i] + ", " ) ; else System.out.print(cities[i] + "." ) ; } } public static void sort(String[] list) { String temp ; for ( int i = 0 , l = list.length ; i < l ; i++) for ( int j = 0 ; j < l- 1 ; j++) if ((list[j].compareTo(list[j+ 1 ]) > 0 )) { temp = list[j] ; list[j] = list[j+ 1 ] ; list[j+ 1 ] = temp ; } } }
Part 4 import java.util.Scanner ; public class HW3problem4 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in) ; double [][] m1 = new double [ 3 ][ 3 ] ; double [][] m2 = new double [ 3 ][ 3 ] ; System.out.print( "Enter matrix1: " ) ; for ( int i = 0 ; i < 3 ; i++) for ( int j = 0 ; j < 3 ; j++) m1[i][j] = stdin.nextDouble() ; System.out.print( "Enter matrix2: " ) ; for ( int i = 0 ; i < 3 ; i++) for ( int j = 0 ; j < 3 ; j++) m2[i][j] = stdin.nextDouble() ; System.out.print( "Multiplication of the matrices is: " ) ; for ( double [] i: multiply(m1 , m2)) { for ( double j : i) System.out.print(j + " " ) ; System.out.println() ; for ( int n = 0 ; n < 35 ; n++) System.out.print( " " ) ; } } public static double [][] multiply( double [][] m1 , double [][] m2) { double [][] newMatrix = new double [m2.length][m1[ 0 ].length] ; for ( int k = 0 ; k < newMatrix.length ; k++) { for ( int i = 0 , j ; i < newMatrix[k].length ; i++) { double total = 0 ; for (j = 0 ; j < newMatrix.length ; j++) { total += m1[k][j] * m2[j][i] ; if (j == 2 ) newMatrix[k][i] = Math.round(total* 10.0 )/ 10.0 ; } } } return newMatrix ; } }
CSE114 Spring 2022: Homework 3 Submission Guidelines
Please or to post comments