Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 3 of 3
Thread: Need HELP with JAVA!!!!!

12072011, 06:52 AM #1
 Join Date
 Dec 2011
 Posts
 2
 Thanks
 0
 Thanked 0 Times in 0 Posts
Need HELP with JAVA!!!!!
Hey, I'm figmoomoo and I'm new here. I'm currently taking a Java course as a Comp Sci freshman and it's kicking my butt. I'm about to take a final in a couple of days but I have no idea what I'm doing... My professor gave us a practice final and I don't know how to do it. If anyone could help me understand it, I would greatly appreciate it.
Code:The Practice Test: 1 Problems Instruction: Each problem in this section is worth 20 points. A. Consider the code fragment below: Code Listing 1.1 int[] sigma = {0,0,0,0,0,0,0,0,0,0}; int[][] mask = {{ 9, 6, 3, 16}, { 4, 15, 10, 5}, {14, 1, 8, 11}, { 7, 12, 13, 2}}; int i,j; for (i=0; i < mask.length; i++) { for(j=0; j < mask.length; j++) { sigma[i] = sigma[i] + mask[i][j]; sigma[mask.length + j] = sigma[mask.length + j] + mask[i][j]; if (i + j == mask.length  1) sigma[sigma.length  1] = sigma[sigma.length  1] + mask[i][j]; if (i == j) sigma[sigma.length  2] = sigma[sigma.length  2] + mask[i][j]; } } boolean magic = true; for (i=1; i<sigma.length; i++) { if (sigma[i] != sigma[i  1]) magic = false; } if (magic) System.out.println("It’s magic!!"); else System.out.println("It isn’t magic."); (a) What would the contents of the sigma array be after Code Listing 1.1 is executed? (b) What would be displayed after Code Listing 1.1 is executed? B. Binary search is a divideandconquer algorithm. It works by repeatedly reducing the size of the array to be searched until a target is found or is determined not to be in the array. Trace the action of binary search, including listing the values of low, high, and mid, indices of the array. (In each table, use as many columns as needed.) For found, write Y when X, the search target, is found in the array below and N otherwise. Index 0 1 2 3 4 5 6 7 8 9 10 11 Value 2 3 5 7 11 13 17 19 23 29 31 37 (i) X = 6 low     mid     high     found     Number of elements examined: List the elements in the order examined: Return Value: (ii) X = 19 low     mid     high     found     Number of elements examined: List the elements in the order examined: Return Value: 2 Programming Instruction: This section of the exam is worth 60 points. Define the indicated classes. Import all relevant packages for each class. Use descriptive names for identifiers but no documentation is required. 2.1 The Weight Class Define a class called Weight, that models weight measured in the English system (in pounds and ounces). The Weight class should implement the parameterized Comparable interface. This class should consist of two instance variables, pounds and ounces, both of which are integers. Note that 1 pound is equivalent to 16 ounces. The class should consist of three methods: 1. A parameterized constructor that takes two explicit parameters, the number of pounds and the number of ounces. 2. Define a method called compareTo that takes one explicit parameter, a reference to a Weight object. This method should return a negative number when the object of the Weight class used to invoke the method is less than the object used as the explicit parameter, a positive number when the object of the Weight class used to invoke the method is greater than the object used as the explicit parameter, and 0 when the object of the Weight class used to invoke the method is equal to the object used as the explicit parameter. 3. Override the toString() method so that it returns a String representation of the Weight object. The string should be formatted as shown in these examples: Example If the pound variable is 10 and the ounces variable is 5, it returns the string “10 lbs 5 oz”. Example If the pound variable is 1 and the ounces variable is 0, it returns the string “1 lb 0 oz”. Example If the pound variable is 0 and the ounces variable is 3, it returns the string “0 lbs 3 oz”. Be sure to use the singular form, lb, after 1 and the plural form, lbs, for all other values. Use oz for all values. 2.2 The WeightSorter Class Suppose you are coaching wrestlers and you record the weights of your athletes in a text file with each line containing two integers. The first integer represents the number of pounds and the second represents the number of ounces for that athlete. Write a program that reads the weights from the file and stores them in an array list of objects of the Weight class. Your program should do the following: 1. prompt the user for the name of the input file, 2. read the data from the input file and store the weights in the array list, 3. print the unsorted data on the screen, as shown below, 4. sort the array list, 5. prompt the user for the name of the output file and then store the sorted weights in the output file, and 6. finally, your program should display the weights of the lightest and heav iest athletes on the screen, as shown below. The input may contain any number of weights. Your program should use a trycatch statement to handle IOException. For example, a sample input data file may be (unsortedweights.txt): 120 2 195 15 200 5 112 11 252 0 140 9 For the sample data file above, the program interaction would be: Enter the name of the input file>unsortedweights.txt Unsorted Weights  120 lbs 2 oz 195 lbs 15 oz 200 lbs 5 oz 112 lbs 11 oz 252 lbs 0 oz 140 lbs 9 oz Enter the name of the output file>sortedweights.txt The lightest athlete weighs 112 lbs 11 oz. The heaviest athlete weights 252 lbs 0 oz. After the program is executed, the sortedweights.txt file will contain: Sorted Weights  112 lbs 11 oz 120 lbs 2 oz 140 lbs 9 oz 195 lbs 15 oz 200 lbs 5 oz 252 lbs 0 oz
Last edited by figmoomoo; 12072011 at 06:55 AM.

12072011, 12:23 PM #2

12082011, 01:21 AM #3
 Join Date
 Dec 2011
 Posts
 2
 Thanks
 0
 Thanked 0 Times in 0 Posts
For part B, the Binary Search part. How do i use the table?