Midterm_REPAIR_Part_B

Midterm_REPAIR_Part_B - Repair of Midterm Part B Part B...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Repair of Midterm Part B Part B: Problem 1: Triangle Numbers (10 points) Triangle.java import java.util.*; public class Triangle { private int width; public Triangle( int n) {width = n;} // constructor public static void main(String args) { System.out.println("\nEnter a nonnegative number:"); Scanner keyboard = new Scanner(System.in); int number = keyboard.nextInt( ); Triangle myTriangle = new Triangle(number); //1. Find the area using the recursive method. int area1 = //2. Find the area using the iterative method. int area2 = // 9 and 10. If the two areas are the same, print out the message "Both area methods give ... " } // end of main method public int getArea() // recursive method { if (width <= 0) return 0; //3. Construct the smallerTriangle object. //Stopping condition Triangle smallerTriangle = //4. Find the area of the smallerTriangle object recursively. int smallerArea = //5. Return the area of the original calling triangle. return } // end of recursive getArea() method public int getArea(int n) // iterative method using a for loop { if (n <= 0) return 0; //6. Construct a for loop to add the numbers from 1 to n. Use k for the index variable. int total = 0; for ( ; ; ; ; ) //7. Update the total as we progress through the for loop. total = //8. Return the total area. return } } // end of class Page 1 Problem 2a: Zero Counter Complete the static method numberOfZeros(int n) in the program below. The program gives the number of zeros in an integer entered by the user at the keyboard. Example 2020 has two zeros. The main method is already completed. ZeroCounter.java import java.util.*; public class ZeroCounter { public static void main(String args) { System.out.println("Enter a nonnegative number:"); Scanner keyboard = new Scanner(System.in); int number = keyboard.nextInt( ); System.out.println(number + " contains " + numberOfZeros(number) + " zeros."); } // End of main. // Complete each of the following cases below. Note the number 0 itself contains 1 zero. // The two cases n=0 and n<10 together constitute the stopping condition. public static int numberOfZeros(int n) // Check the number is not negative. { if (n<0) n = -n; // First handle the single digit numbers 0-9. There are two cases. if (n == 0) .................................................................................................................................... else if (n < 10) //and not 0 .................................................................................................................................... // Now handle multi-digit numbers. ; ; else if (n%10 == 0) // Handle the case where the number ends in a 0. .................................................................................................................................... else // n%10 != 0 The last digit is not a 0. ; .................................................................................................................................... } // end of method numberOfZeros. } // end of class ; Page 2 Problem 2b: Zero Counter without Recursion Rewrite the method numberOfZeros without using any recursion! It must return the same numbers as the above method. Non-Recursive static method: public static int numberOfZeros(int n) { // Assure the number is not negative by changing sign if necessary. numberOfZeros if (n<0) n = -n; // 1. Handle the special case of n = 0. //2. Declare and initialize the variable "zeros" to keep track of the number of zeros. // 3, 4: Now use a while loop and update two quantities in its body. while (n > 0) { // 5: Return the number of zeros. } } // End of NON_RECURSIVE method numberOfZeros. Page 3 Problem 3: Flanagan's Matrix.java (You may examine his file.) You will need to place the folder "Flanagan" inside your working folder before compiling. MatrixTesterStudent.java import flanagan.math.Matrix; public class MatrixTesterStudent { public static void main(String args) { /* PROBLEM 1: Create a 3*3 identity matrix A. Find its number of rows and columns using Flanagan's methods. Use the methods identityMatrix(), getnumberOfRows(), getnumberOfColumns() and getElement(). */ int n = 3; Matrix A = //2. Find the number of rows of A using A as the invoking object. ; //1 int nr = //3. Find the number of columns of A using A as the invoking object. int nr = System.out.println("\nThe matrix A is: \n"); for (int i = 0; i<nr; i++) // select each row {for (int j = 0; j<nc;j++) System.out.printf("%6.2f",A.getElement(i,j)); System.out.println("\n"); } // end of for loop /* PROBLEM 2: Enter the following matrix C and compute its transpose Ct. Start with a 2D array as given. Print out the transpose. Use the instance method transpose. C = | 1 2 3 | | 4 5 6 | | 7 8 9 | */ // 4. Enter the 2D array of components. double carray = ......................................................................................................................................................... ; // 5. Convert the 2D array named carray to the matrix C using a constructor. // Do not forget the keyword new! // 6. Find Ct, the transpose of C using the instance method transpose. // 7 and 8. Now update nr and nc, the number of rows and columns of Ct. // 9 and 10. Use nested for loops to print out the Matrix Ct. System.out.println("The transpose of the matrix C is: \n"); // Select each row in the outer for loop. // Select each column in the inner for loop. System.out.printf("%6.2f",Ct.getElement(i,j)); System.out.println("\n"); } // end of for loop } } // end of main // end of class Page 4 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online