Question
Can anyone help me pass the exercise TestProgram.java: import java.io.*; public class Program { private int cityCount; private City cityArray[]; private CompressedArray array; private String
Can anyone help me pass the exercise TestProgram.java:
import java.io.*;
public class Program {
private int cityCount; private City cityArray[]; private CompressedArray array;
private String buffer = null; private BufferedReader in = null; /* Open the file whose name was give as parameter */ public Program(String fileName, boolean showMap) { try { in = new BufferedReader(new FileReader(fileName)); buffer = in.readLine(); // Store first line } catch (IOException e) { System.out.println("Cannot read file \'"+fileName+"\'"); System.exit(0); } } /* Returns true if the end of the file has been reached; it returns false otherwise. */ public boolean endOfFile() { if (buffer == null) return true; else return false; } /* Reads a line from the file. */ public String readString() { String line = buffer; if (buffer == null) { System.out.println("Error. The end of file was reached, so another string cannot be read from it"); return null; } try { buffer = in.readLine(); } catch (IOException e) { System.out.println("Error. Cannot read from file"); System.exit(0); } return line; } /* Reads an integer from the file. Returns -1 if no integer could be read. */ public int readInt() { String line = readString(); if (line == null) return -1; else return Integer.parseInt(line); } /* Reads an double from the file. Returns -1.0 if no integer could be read. */ public double readDouble() { String line = readString(); if (line == null) return -1.0; else return Double.parseDouble(line); }
public City[] getCityArray() { return cityArray; } public void expandCapacity() { City temp[]=cityArray; City cityArray[]=new City[temp.length+3]; for(int i=0;i for(int i=0;i public class TestProgram { public static void main(String[] args) { String[] files = new String[] { "cities1.txt", "cities2.txt", "cities3.txt", "cities4.txt", "cities5.txt" }; int[] sizes = new int [] { 21, 15, 10, 15, 36 }; String[] solns = new String[] { " 36.88 441.59 441.44 420.49 421.01 22.20 396.09 399.24 57.70 37.20 96.54 117.34 531.75 510.19 483.50 179.23 176.82 264.62 244.25 223.79 273.31 ", " 551.88 377.00 191.27 457.96 94.25 111.80 98.09 455.81 278.93 362.35 134.53 652.92 466.59 561.43 205.83 ", " 269.11 91.35 257.26 802.24 718.88 712.53 589.42 480.42 504.12 239.15 ", " 372.31 420.49 64.41 98.05 278.93 324.04 658.98 306.54 245.60 561.06 111.33 466.59 519.70 205.83 762.13 ", " 512.66 552.89 42.05 372.31 157.18 191.27 420.49 95.52 133.24 64.41 98.05 415.28 455.81 278.93 324.04 396.09 129.03 163.60 28.16 37.20 301.38 96.54 603.83 643.29 458.33 510.19 192.94 483.50 179.23 334.43 375.20 203.30 244.25 81.22 223.79 273.31 " }; double[] firsts = new double[] { 36.87817782917155, 551.881327823292, 269.1059270993487, 372.3130403303113, 512.6646077115134 }; for (int f = 0; f boolean t1 = ca.getLength() == sizes[f]; boolean t2 = ca.toString().equals(solns[f]); boolean t3 = ca.getElement(0) == firsts[f]; if (t1 && t2 && t3) { System.out.println("Test " + (f+1) + " Passed"); } else { System.out.println("Test " + (f+1) + " Failed"); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started