Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed

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"); } } }

}

City.java X i public class City { private String name; private int x; private int y; private CityMarker marker; public City (String name, int x, int y) { this.name = name; this.x = x; this.y = y; } 13 public String getName() { return name; } } 2 3 4 5 6 7 80 9 10 11 12 13 140 15 16 17 180 19 20 21 220 23 24 25 260 27 28 29 300 31 32 } public int getX() { return x; } public int gety () { return y; } "Program.java CompressedArray.java X TestProgram.java *TestCompressed Array.java CityMarker.java 1 import java.util.Arrays; 2 public class CompressedArray ( 3 private int origArraySize; 4 private double array[]; 5 60 public CompressedArray (double[][] aArray) { 7 int n = aArray.length; int total = (n * (n - 1)) / 2; 9 int i, j, k = 0; 10 origArraySize = total; 11 array = new double[total]; 12 for (i = 0; i ) 16 array(k++] = aArray[i][i]; 17 18 19 } 20 210 public int getLength () { 22 return origArraySize; 23 } 24 250 public double getElement(int i) { 26 return array[i]; 27 } 28 290 @Override A 30 public boolean equals(object obj) { 31 if (this == obj) { 32 return true; } 34 if (obj == null) { 35 return false; 36 } 37 if (getClass() != obj.getClass()) { 38 return false; 39 } 40 final CompressedArray other = (CompressedArray) obj; 41 if (this.origArraySize != other.origArraySize ) { 42 return false; 43 } 44 if (!Arrays.equals(this.array, other.array)) { 45 return false; 46 } 47 return true; 48 } 49 500 @Override 51 public String toString() { 52 String t = ""; 53 for (int i = 0; i ) 16 array(k++] = aArray[i][i]; 17 18 19 } 20 210 public int getLength () { 22 return origArraySize; 23 } 24 250 public double getElement(int i) { 26 return array[i]; 27 } 28 290 @Override A 30 public boolean equals(object obj) { 31 if (this == obj) { 32 return true; } 34 if (obj == null) { 35 return false; 36 } 37 if (getClass() != obj.getClass()) { 38 return false; 39 } 40 final CompressedArray other = (CompressedArray) obj; 41 if (this.origArraySize != other.origArraySize ) { 42 return false; 43 } 44 if (!Arrays.equals(this.array, other.array)) { 45 return false; 46 } 47 return true; 48 } 49 500 @Override 51 public String toString() { 52 String t = ""; 53 for (int i = 0; i <>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions