2013 Sky View
<< 2013 GridWorldUtilities | APQuestionsTrailIndex | 2012 ClimbClub >>
Click here to see the questions from 2013
SkyView.java
public class SkyView { /** A rectangular array that holds the data representing a rectangular area of the sky. */ private double[][] view; /**Constructs a SkyView object from a 1-dimensional array of scan data. * @param numRows the number of rows represented in the view * Precondition: numRows > 0 * @param numCols the number of columns represented in the view * Precondition: numCols > 0 * @param scanned the scan data received from the telescope, stored in telescope order * Precondition: scanned.length == numRows * numCols * Postcondition: view has been created as a rectangular 2-dimensional array * with numRows rows and numCols columns and the values in * scanned have been copied to view and are ordered as * in the original rectangular area of sky. */ public SkyView(int numRows, int numCols, double[] scanned) { /* to be implemented in part (a) */ } /**Returns the average of the values in a rectangular section of view. * @param startRow the first row index of the section * @param endRow the last row index of the section * @param startCol the first column index of the section * @param endCol the last column index of the section * Precondition: 0 <= startRow <= endRow < view.length * Precondition: 0 <= startCol <= endCol < view[0].length * @return the average of the values in the specified section of view */ public double getAverage(int startRow, int endRow, int startCol, int endCol) { /* to be implemented in part (b) */ return 0; } // There may be instance variables, constructors, and methods that are not shown public double[][] getView(){ return this.view; } public String toString(){ String result=" "; for(int i=0; i<view[0].length;i++) result+=String.format("%3d ", i); result+="\n"; for(int i=0; i<view.length;i++){ result+=i+" "; for(double d:view[i]) result+=String.format("[%3.1f]", d); result+="\n"; } return result; } }
SkyViewTester.java
public class SkyViewTester { /** * @param args */ public static void main(String[] args) { double[] scanned1 = {0.3, 0.7, 0.8, 0.4, 1.4, 1.1, 0.2, 0.5, 0.1, 1.6, 0.6, 0.9}; printArray(scanned1); double[] scanned2 = {0.3, 0.7, 0.8, 0.4, 1.4, 1.1}; SkyView sv1 = new SkyView(4,3,scanned1); if(sv1.getView()==null){ System.out.println("Your constructor returned a null"); }else { System.out.println("\nSkyView Example 1:"); System.out.println(sv1); String expected = " 0 1 2 \n0 [0.3][0.7][0.8]\n1 [1.1][1.4][0.4]\n2 [0.2][0.5][0.1]\n3 [0.9][0.6][1.6]\n"; if(sv1.toString().equals(expected)) System.out.print("No "); System.out.println("problems found.\n"); printArray(scanned2); System.out.println("\nSkyView Example 2:"); SkyView sv2 = new SkyView(3,2,scanned2); System.out.println(sv2); expected = " 0 1 \n0 [0.3][0.7]\n1 [0.4][0.8]\n2 [1.4][1.1]\n"; if(sv2.toString().equals(expected)) System.out.print("No "); System.out.println("problems found.\n"); System.out.print("\nSkyView Part B:\n getAverage(1, 2, 0, 1) should return 0.8\nYour returns "); System.out.println(sv1.getAverage(1, 2, 0, 1)); } } public static void printArray(double[] a){ for(int i=0; i<a.length;i++) System.out.print(String.format("%3d ", i) ) ; System.out.println(); for(double d:a) System.out.print( String.format("[%3.1f]", d)); System.out.println(); } public static String toString(double[][] a){ String result=" "; for(int i=0; i<a[0].length;i++) result+=String.format("%3d ", i); result+="\n"; for(int i=0; i<a.length;i++){ result+=i+" "; for(double d:a[i]) result+=String.format("[%3.1f]", d); result+="\n"; } return result; } }