Question

In: Computer Science

In this program, you are modifying given code so that the class is object-oriented. 2. Write...

In this program, you are modifying given code so that the class is object-oriented.

2. Write a Java class called CityDistancesOO in a class file called CityDistancesOO.java.   

3. Your class will still make use of two text files. a. The first text file contains the names of cities with the first line of the file specifying how many city names are contained within the file.   

b. The second text file contains the distances between the cities in the file described above without containing an entry for how many distances are within the file.   



4. The CityDistancesOO class will have two class attributes: a. The first attribute, called cities, is a one-dimensional array of String containing the city names. b. The second attribute, called distances, is a two-dimensional array (n x n array where n is the number of cities) of double and organized such that each row corresponds to the distances from a particular city.   

5. The CityDistancesOO class contains a constructor and four non-static methods: c. The constructor takes two arguments both of type String. The first argument, called nameFile, is the file name of the list of city names. The second argument, called distanceFile, is the file name of the list of city distances. The constructor then calls the helper methods loadCities() and loadDistances() to create and populate the two class attributes. d. The method called loadCities() is a helper method and should be private. The method takes a single argument called filename of type String that is the name of the file containing the city names. This method opens the file, reads in the data into cities attribute. The first item read from the text file should be the number of city names within the file (read as an integer). If done correctly, the cities attribute should be the correct size to store all city names without any “extra” space within the array. If the file does not exist or a problem reading the file is encountered, then an IOException is thrown. Hint: Be aware that using the nextInt() method from the Scanner class will read the number but not the newline character found after the number. Your method should correctly handle the newline character.

e. The method called loadDistances() is a helper method and should be private. The method takes an argument called filename of type String that is the name of the file containing the list of distances. The method opens the file, reads in the data into the distances attribute. If the file does not exist or a problem reading the file is encountered, then an IOException is thrown.

f. A public method called getNumberOfCities() that returns the number of cities stored in the cities attribute.

g. A private method called getCityIndex() that takes a single argument called cityName of type String (that is, the name of a particular city). The method iterates through the cities attribute and returns the index of the location within the array that contains the value of cityName. If the string specified by cityName is not found in cities, then the method returns the value -1.


h. A public method called getCity() that takes a single argument called index of type int. This method returns the city name as a String corresponding to the provided index.

i. A public method called findDistance that takes two arguments: the first is called start of type String; and the second is called end of type String. The method makes use of the getCityIndex() helper method to retrieve the indices of the city names corresponding to start and end arguments. Then, the correct distance is retrieved from the distances attribute and returned to the caller.

Given Code:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class CityDistances {
  

public static String[] loadCities(String filename) throws IOException{

Scanner fileIn = new Scanner(new File(filename));
int n = fileIn.nextInt();
String[] cities = new String[n];

fileIn.nextLine();

int i = 0;
while (fileIn.hasNextLine()) {
cities[i] = fileIn.nextLine();
i++;
}

fileIn.close();

return cities;

}
  
  
public static double[][] loadDistances(String filename,int numCities) throws IOException{

double distances[][] = new double[numCities][numCities];
Scanner fileIn = new Scanner(new File(filename));

for (int i = 0; i < numCities; i++) {
for (int j = 0; j < numCities; j++) {

distances[i][j] = fileIn.nextDouble();

}

}

fileIn.close();

return distances;

}


private static int getCityIndex(String[] cities,String cityName){
int cityIndex = -1;
  
for(int i = 0; i < cities.length ; i++){
if(cities[i].equals(cityName)){
cityIndex = i;
break;
}
}
  
return cityIndex;
}
  


public static double findDistance(String[] cities, double[][] distances,String start,String end){
  
int startCityIndex = getCityIndex(cities, start);
int endCityIndex = getCityIndex(cities, end);

if(startCityIndex!=-1 && endCityIndex!=-1){
return distances[startCityIndex][endCityIndex];
}else{
return -1;
}
}
}

Solutions

Expert Solution

/***************************************CityDistancesOO.java******************************/

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class CityDistancesOO {

   private String[] cities;
   private double[][] distances;

   public CityDistancesOO(String nameFile, String distanceFile) throws IOException {

       loadCities(nameFile);
       loadDistances(distanceFile, 10);
   }

   public String[] loadCities(String filename) throws IOException {

       Scanner fileIn = new Scanner(new File(filename));
       int n = fileIn.nextInt();
       cities = new String[n];

       fileIn.nextLine();

       int i = 0;
       while (fileIn.hasNextLine()) {
           cities[i] = fileIn.nextLine();
           i++;
       }

       fileIn.close();

       return cities;

   }

   public int getNumberOfCities() {

       return cities.length;
   }

   public String getCity(int index) {

       return cities[index];
   }

   public double[][] loadDistances(String filename, int numCities) throws IOException {

       distances = new double[numCities][numCities];
       Scanner fileIn = new Scanner(new File(filename));

       for (int i = 0; i < numCities; i++) {
           for (int j = 0; j < numCities; j++) {

               distances[i][j] = fileIn.nextDouble();

           }

       }

       fileIn.close();

       return distances;

   }

   private static int getCityIndex(String[] cities, String cityName) {
       int cityIndex = -1;

       for (int i = 0; i < cities.length; i++) {
           if (cities[i].equals(cityName)) {
               cityIndex = i;
               break;
           }
       }

       return cityIndex;
   }

   public static double findDistance(String[] cities, double[][] distances, String start, String end) {

       int startCityIndex = getCityIndex(cities, start);
       int endCityIndex = getCityIndex(cities, end);

       if (startCityIndex != -1 && endCityIndex != -1) {
           return distances[startCityIndex][endCityIndex];
       } else {
           return -1;
       }
   }

}

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Write a C++ program that will use good object-oriented principles. You have been tasked to write...
Write a C++ program that will use good object-oriented principles. You have been tasked to write an application that will allow a user to change their system password. The XYZ Corporation has the following rules for passwords: each password should have a minimum of 8 characters each password should have a minimum of 2 uppercase characters (A - Z) each password should have a minimum of 2 lowercase characters (a - z) each password should have a minimum of 2...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class with public and private members and multiple constructors.  Gain a better understanding of the building and using of classes and objects.  Practice problem solving using OOP. Overview You will implement a date and day of week calculator for the SELECTED calendar year. The calculator repeatedly reads in three numbers from the standard input that are interpreted as month, day of month, days...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses? Explain. -What is a class diagram? How is it used in object-oriented programming? -What is an attribute in OOP? What is a data member? -What is a method in OOP? What is a member function? -What is the difference between private members and public members of a...
This program should be done in python. This must use the principles of object oriented program....
This program should be done in python. This must use the principles of object oriented program. Create one or more classes to play Four-in-a-Row (also called Connect Four) with a user. It’s similar to tic-tac-toe but the board is of size 7×6 and discs fall straight through so the legal moves are more stringent than tic-tac-toe. The state of the board should be printed to the terminal after each legal move. You can represent the different colored discs as X’s...
Problem Write a movie management system using object-oriented design principles. The program will read from the...
Problem Write a movie management system using object-oriented design principles. The program will read from the supplied data file into a single array list. The data file (movies.txt) contains information about the movies. Each movie record has the following attributes: - Duration (in minutes) - Title - Year of release Each record in the movies.txt file is formatted as follows: - Duration,Title,Year - e.g.: 91,Gravity,2013 Specifically, you have to create an interactive menu driven application that gives the user the...
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition.
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition. (Similar to what we used in class) 2. Use a package in your project, the package name should be xxxprojectname. xxx is your initials taken from the first three characters of your Cal Poly email address. 3. Read in the following from the JOptionPane input window: a. Customer First Name b. Customer Last Name c. Customer Phone Number...
summer/** * This Object-Oriented version of the "Summer" class * is a simple introduction to constructors...
summer/** * This Object-Oriented version of the "Summer" class * is a simple introduction to constructors / * private data members / static vs. not static / and the * "toString" method. * * SKELETON FOR LAB TEST. * * @author Raymond Lister * @version April 2015; */ public class SummerOO { public static int numSummers = 0; // The above variable is used to count the number of // instances of the class SummerOO that have been created. //...
Write a program to print the number of class assignments you have done so far, the...
Write a program to print the number of class assignments you have done so far, the total grades, and average grade. Prompt a 0 input to end the program. Java Using while statements in the lecture this was provided. Wording confused me, Total grade is sum of all grades.
Describe how you would develop object-oriented features of Java for the Quiz program developed in the...
Describe how you would develop object-oriented features of Java for the Quiz program developed in the Programming Assignments. In particular, describe how the program could use each of the following: class variables, instance variables, inheritance, polymorphism, abstract classes, "this", "super", interfaces, and event listeners.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT