Ass03 (1)

docx

School

Wayne State University *

*We aren’t endorsed by this school

Course

3020

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

7

Uploaded by JusticeMantisMaster1087

Report
CSC 3020 Java Programming Assignment 03 40 points Due 02/15/2024 (11:45 A.M.) Assignment Objectives: ■■ To define methods with formal parameters. ■■ To invoke methods with actual parameters (i.e., arguments). ■■ To define methods with a return value. ■■ To use method overloading and understand ambiguous overloading. ■■ To declare array reference variables and create arrays. ■■ To obtain array size using arrayRefVar.length and know default values in an array. ■■ To declare, create, and initialize an array using an array initializer. ■■ To copy contents from one array to another. ■■ To develop and invoke methods with array arguments and return values. ■■ To define a method with a variable-length argument list. ■■ To search elements using the linear or binary search algorithm. ■■ To sort an array using the selection sort approach. ■■ To use the methods in the java.util.Arrays class . ■■ To pass arguments to the main method from the command line. ■■ To declare variables for two-dimensional arrays, create arrays, and access array elements in a two-dimensional array using row and column indices. ■■ To program common operations for two-dimensional arrays (displaying arrays, summing all elements, finding the minimum and maximum elements, and random shuffling). ■■ To pass two-dimensional arrays to methods. Solution to this assignment will not be posted on Canvas; however, any question can be discussed in the class upon request of a student. All assignments must be submitted by the Canvas. No email or hard copy is accepted. You must follow the following format: a. For non-programming questions, use a word file to type your answers. Don’t use the text box on the Canvas to answer the questions or to write comments, we will not read it. b. State your answer clearly. c. For programming questions, include only the source file for each problem . d. Submit your file to the Canvas. You must submit your assignment on time; otherwise, you will receive zero. In addition, you cannot submit your file more than one time. e. There will be several folders on the Canvas. You need to upload your file(s) using the correct folder on the Canvas. f. Name each file: “Assignment Number(Question number(s))”. g. To upload your file(s): In Course Navigation, click the Assignments link. Click the title of the assignment. Click the   Submit   Assignment button. Add   File . ... Add Another   File . ... Submit   Assignment. ... View   Submission .
It is your responsibility to make sure that each file is uploaded correctly. If you uploaded a wrong file, you receive zero; files will not be accepted after due date even if you have a prove that the file is created before the due date. Make sure you review the Cheating & Plagiarism policy on Canvas. Answer questions 1 to 16 in a separate single .txt file; include only your answers with questions numbers. Write a program for each of Q.17 - Q.18; save each program in a .txt file. Submit total of 3 .txt files by the due date. Q 01. (3 points) Q 02. (2 points) Identify and correct the errors in the following program (4 errors): 1 public class Test { 2 public static method1( int n, m) { 3 n += m; 4 method2(3.4); 5 } 6 7 public static int method2( int n) { 8 if (n > 0) return 1; 9 else if (n == 0) return 0; 10 else if (n < 0) return -1; 11 } 12 } Q 03. (2.5 points)
Identify and fix the errors in the following code (5 errors): 1 public class Test { 2 public static void main(String[] args) { 3 double [100] r; 4 5 for ( int i = 0; i < r.length(); i++); 6 r(i) = Math.random * 100; 7 } 8 } Q 04. (0.5 points) Once an array is created, its size cannot be changed. Does the following code resize the array? int [] myList; myList = new int [10]; // Sometime later you want to assign a new array to myList myList = new int [20]; Q 05. (0.5 points) True or false? When an array is passed to a method, a new array is created and passed to the method. Q 06. (1 points) Show the output of the following two programs: (a) public class Test { public static void main(String[] args) { int number = 0; int [] numbers = new int [1]; m(number, numbers); System.out.println("number is " + number + " and numbers[0] is " + numbers[0]); } public static void m( int x, int [] y) { x = 3; y[0] = 3; } } (b) public class Test { public static void main(String[] args) { int [] list = {1, 2, 3, 4, 5}; reverse(list); for ( int i = 0; i < list.length; i++)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
System.out.print(list[i] + " "); } public static void reverse( int [] list) { int [] newList = new int [list.length]; for ( int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList; } } Q 07. (1.5 points) What is wrong with each of the following method headers? public static void print(String... strings, double ... numbers) public static void print( double ... numbers, String name) public static double ... print( double d1, double d2) Q 08. (0.5 points) What types of array can be sorted using the java.util.Arrays.sort method? Does this sort method create a new array? Q 09. (1 points) Show the output of the following code: int [] list1 = {2, 4, 7, 10}; java.util.Arrays.fill(list1, 7); System.out.println(java.util.Arrays.toString(list1)); int [] list2 = {2, 4, 7, 10}; System.out.println(java.util.Arrays.toString(list2)); System.out.print(java.util.Arrays.equals(list1, list2)); Q 10. (1.5 points) Show the output of the following program when invoked using  1. java Test I have a dream  2. java Test "1 2 3"  3. java Test public class Test { public static void main(String[] args) { System.out.println("Number of strings is " + args.length); for ( int i = 0; i < args.length; i++) System.out.println(args[i]); } } Q 11. (2 points)
1) Can the rows in a two-dimensional array have different lengths? 2) What is the output of the following code? int [][] array = new int [5][6]; int [] x = {1, 2}; array[0] = x; System.out.println("array[0][1] is " + array[0][1]); 3) Show the output of the following code: int [][] array = {{1, 2}, {3, 4}, {5, 6}}; for ( int i = array.length - 1; i >= 0; i--) { for ( int j = array[i].length - 1; j >= 0; j--) System.out.print(array[i][j] + " "); System.out.println(); } 4) Show the output of the following code: public class Test { public static void main(String[] args) { int [][] array = {{1, 2, 3, 4}, {5, 6, 7, 8}}; System.out.println(m1(array)[0]); System.out.println(m1(array)[1]); } public static int [] m1( int [][] m) { int [] result = new int [2]; result[0] = m.length; result[1] = m[0].length; return result; } } Q 12. (2 points) Q 13. (2 points)
Q 14. (1 points) Q 15. (2 points) Assume a,b, and c are initialized. Q 16. (3 points) Programming Questions
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Q 17. (7 points) Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidate’s name, the votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. In addition to main method, your program must have at least two methods; a method that returns the winner and a method that returns the sum of votes. Format the output using the format method. Name your class: Candidates. A sample output is: Q 18. (7 points) Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods: a. Method getData: This method reads and stores the data in the two-dimensional array. b. Method averageHigh: This method calculates and returns the average high temperature of the year. c. Method averageLow: This method calculates and returns the average low temperature of the year. d. Method indexHighTemp: This method returns the index of the highest temperature in the array. e. Method indexLowTemp: This method returns the index of the lowest temperature in the array. (These methods must all have the appropriate parameters.) Name your class: Temperatures A sample output is: