Skip to content

Commit

Permalink
Cleaned Up Code - Code Renaming of variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Aatmaj-Zephyr committed Sep 24, 2022
1 parent 003c8ee commit ef7a197
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 141 deletions.
37 changes: 18 additions & 19 deletions src/Getinput.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
public class Getinput
// This class generates the cities from the files.
{
public citylist set(String ObjectFile, String NeighbourFile)
// This is the method which is called.
// This does the job of calling the two functions of making city instances and setting the
// neighbours
{
return SetCityNeighbours(SetCityInstances(ObjectFile), NeighbourFile);
public cityList setCityNeighbours(String ObjectFile, String NeighbourFile)
// This is the method which is called.
// This does the job of calling the two functions of making city instances and
// setting the
// neighbours
{
return SetCityNeighboursFromCityListAndNeighbourFile(SetCityInstances(ObjectFile), NeighbourFile);
}

public citylist SetCityInstances(String ObjectFile) {
public cityList SetCityInstances(String ObjectFile) {
// This method make city instances with the names and the coordinates given
// in the ObjectFile, and stores them in a citylist which it returns.

citylist mycitylist = new citylist(); // Citylist to store the city instances.
cityList myCityList = new cityList(); // Citylist to store the city instances.

try // try catch to prevent the file not exists exception.
{
Expand All @@ -30,33 +31,31 @@ public citylist SetCityInstances(String ObjectFile) {

while ((line = reader.readLine()) != null) // While not the end of file.
{
String arr[] =
line.split(":"); // split the read string to bifercate name and two coordinates.
String arr[] = line.split(":"); // split the read string to bifercate name and two coordinates.

String name = arr[0]; // name of the city
double x = Double.parseDouble(arr[1]); // x coordinate
double y = Double.parseDouble(arr[2]); // y coordinate

city citytobeadded = new city(x, y);
citytobeadded.setname(name);
mycitylist.append(citytobeadded);
citytobeadded.setName(name);
myCityList.append(citytobeadded);
// Add the coty to the citylist which is to be returned.

}
reader.close(); // close the File

return mycitylist;
return myCityList;
} catch (Exception ex) {
System.out.println("File doesn't exist or has invalid entries"); // the file does not exist.
return null;
}
}

public citylist SetCityNeighbours(
citylist mycitylist,
String
NeighbourFile) { // This method is present to set the Neighbours of the cities in the
// citylist mycitylist
private cityList SetCityNeighboursFromCityListAndNeighbourFile(
cityList mycitylist,
String NeighbourFile) { // This method is present to set the Neighbours of the cities in the
// citylist mycitylist

try // try catch to prevent the file not exists exception.
{
Expand All @@ -77,7 +76,7 @@ public citylist SetCityNeighbours(
// Seperate the Neighbours which are seperated by commas.

for (int i = 0; i < nei.length; i++) {
mycitylist.get(name).neighbours.append(mycitylist.get(nei[i]));
mycitylist.getCityByCityName(name).neighbours.append(mycitylist.getCityByCityName(nei[i]));
// Set the Neighbours.
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static void main(String[] args) {
// We can use it for running the program on different different cities and different different
// paths.

citylist input = in.set("Cities.txt", "Neighbours.txt");
cityList input = in.setCityNeighbours("/Users/aatmaj/Travelling-salesman/src/Cities.txt", "/Users/aatmaj/Travelling-salesman/src/Neighbours.txt");
// This will make instances of the city objects with positions(coordinates) as given in the
// "coordinates.txt" file,
// and set the available paths from the "paths.txt" file. Then this cities instances are
Expand Down Expand Up @@ -66,7 +66,7 @@ public static void main(String[] args) {
}

System.out.print("The path is ");
mypathfinder.printpathname();
mypathfinder.printShortestPathName();
// directly print the shortest path. We can extract it using methods, buthere in this case
// direct printing is easier.

Expand Down
22 changes: 11 additions & 11 deletions src/city.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public class city {
complex coordinates;
// The position of the city stored as complex coordinates.

String name;
String cityName;
// name of the city.

citylist neighbours;
// the neighbours of the city stored in a citylist.
cityList neighbours;
// the neighbours of the city stored in a cityList.
// Note that the neighbors are those from whome path to the city can lead. This is useful for
// one-way aths as well.

Expand All @@ -17,21 +17,21 @@ public class city {
{
coordinates = new complex(x, y); // set coordinates

neighbours = new citylist(); // instantiate neighbours
name = new String(); // instantiate name.
neighbours = new cityList(); // instantiate neighbours
cityName = new String(); // instantiate name.
}

city(double x, double y, String myname) {
city(double x, double y, String cityName) {
// another parametrized constructor.
// In case someone wans to add name at the time of making instances.
this(x, y);
setname(myname);
setName(cityName);
}

public void setname(String myname)
public void setName(String cityName)
// To set the name of the city.
{
name = myname;
this.cityName = cityName;
}

/*The equals methods
Expand All @@ -52,15 +52,15 @@ public static boolean equals(city a, city b) {
}

public static boolean equals(city a, String b) { // Two cities are same if their names are equal
if (b.equals(a.name)) {
if (b.equals(a.cityName)) {
return true;
} else {
return false;
}
}

public static boolean equals(String b, city a) { // Two cities are same if their names are equal
if (b.equals(a.name)) {
if (b.equals(a.cityName)) {
return true;
} else {
return false;
Expand Down
154 changes: 78 additions & 76 deletions src/citylist.java
Original file line number Diff line number Diff line change
@@ -1,152 +1,154 @@
import java.util.*;

public class citylist
public class cityList
// This is the class which is made for storing city objects as a list.
{
ArrayList<city> listofcities;
ArrayList<city> listOfCities;
// This arraylist is the one which has all the city objects.

citylist() // default constructor.
{
listofcities = new ArrayList<city>(); // instantiate new ArrayList
cityList() // default constructor.
{
listOfCities = new ArrayList<city>(); // instantiate new ArrayList
}

public void append(city a) // Method to append in the list.
{
listofcities.add(a);
{
listOfCities.add(a);
}

/*
contains() Method is used to check if the citylist contains the list or not.
If citylist has the city, then return true, else return false.
We can give parameters of city, city name or coordinates.
*/
public boolean contains(city a) {

for (int j = 0; j < listofcities.size(); j++) {
if (city.equals(listofcities.get(j), a) == true) {
* hasCity() Method is used to check if the citylist contains the list or not.
* If citylist has the city, then return true, else return false.
* We can give parameters of city, city name or coordinates.
*/
public boolean hasCityByCityObj(city a) {

for (int j = 0; j < listOfCities.size(); j++) {
if (city.equals(listOfCities.get(j), a) == true) {
return true;
}
}
return false;
}

public boolean contains(String a) {
for (int j = 0; j < listofcities.size(); j++) {
if (city.equals(listofcities.get(j), a) == true) {
public boolean hasCityByStringName(String a) {
for (int j = 0; j < listOfCities.size(); j++) {
if (city.equals(listOfCities.get(j), a) == true) {
return true;
}
}
return false;
}

public boolean contains(double a, double b) {
return contains(new city(a, b));
public boolean hasCityByCoordinates(double a, double b) {
return hasCityByCityObj(new city(a, b));
}

public void set(citylist a)
// method to set values of another citylist into this citylist.
{
listofcities.clear();
for (int i = 0; i < a.listofcities.size(); i++) {
listofcities.add(a.listofcities.get(i));
public void copyFromCitylist(cityList a) {
// method to set values of another citylist into this citylist.

listOfCities.clear();
for (int i = 0; i < a.listOfCities.size(); i++) {
listOfCities.add(a.listOfCities.get(i));
}
}

public city last() // return the last element in the list
{
return listofcities.get(listofcities.size() - 1);
public city getLastCityElement() {
// return the last element in the list

return listOfCities.get(listOfCities.size() - 1);
}

public void pop() // pop from the list
{
listofcities.remove(listofcities.size() - 1);
{
listOfCities.remove(listOfCities.size() - 1);
}

public double sum()
// This method is present to calculate the path length.
// This method sums up the distances between all the two cities in the list.
{
public double getPathLength()
// This method is present to calculate the path length.
// This method sums up the distances between all the two cities in the list.
{
double temp = 0; // temparory variable to store the sum.
for (int i = 0; i <= (listofcities.size() - 2); i++) {
temp =
temp + complex.dist(listofcities.get(i).coordinates, listofcities.get(i + 1).coordinates);
for (int i = 0; i <= (listOfCities.size() - 2); i++) {
temp = temp + complex.distanceBetweenComplexPoints(listOfCities.get(i).coordinates, listOfCities.get(i + 1).coordinates);
}
return temp;
}

/*
Methods to print the elements (name of the cities) of the citylist.
print()- print the names.
println()- print the names on a new line .
print(String Seperator)- print with Seperator, like comma, etc.
*/
* Methods to print the elements (name of the cities) of the citylist.
* print()- print the names.
* println()- print the names on a new line .
* print(String Seperator)- print with Seperator, like comma, etc.
*/
public void print() {
for (int i = 0; i <= (listofcities.size() - 1); i++) {
System.out.print(listofcities.get(i).name);
for (int i = 0; i <= (listOfCities.size() - 1); i++) {
System.out.print(listOfCities.get(i).cityName);
}
}

public void println() {
for (int i = 0; i <= (listofcities.size() - 1); i++) {
System.out.println(listofcities.get(i).name);
for (int i = 0; i <= (listOfCities.size() - 1); i++) {
System.out.println(listOfCities.get(i).cityName);
}
}

public void print(String Seperator) {
for (int i = 0; i < (listofcities.size() - 1); i++) {
System.out.print(listofcities.get(i).name);
for (int i = 0; i < (listOfCities.size() - 1); i++) {
System.out.print(listOfCities.get(i).cityName);
System.out.print(Seperator); // print with Seperator
}
System.out.print(listofcities.get(listofcities.size() - 1).name);
System.out.print(listOfCities.get(listOfCities.size() - 1).cityName);
}

/*
posn-The method to find the position of the city in the list.
Can add parameters of city object, city name or coordinates.
*/
public int posn(city a) {
for (int j = 0; j < listofcities.size(); j++) {
if (city.equals(listofcities.get(j), a) == true) {
* The methods to find the position of the city in the list.
* Can add parameters of city object, city name or coordinates.
*/
public int getPositionByCity(city a) {
for (int j = 0; j < listOfCities.size(); j++) {
if (city.equals(listOfCities.get(j), a) == true) {
return j;
}
}
return -1;
}

public int posn(String a) {
for (int j = 0; j < listofcities.size(); j++) {
if (city.equals(listofcities.get(j), a) == true) {
public int getPositionByString(String a) {
for (int j = 0; j < listOfCities.size(); j++) {
if (city.equals(listOfCities.get(j), a) == true) {
return j;
}
}
return -1;
}

public int posn(double a, double b) {
return posn(new city(a, b));
public int getPositionByPoint(double a, double b) {
return getPositionByCity(new city(a, b));
}

/*
Method to get the city object from the citylist.
*/
public city get(int a) // get the a'th position of the citylist
{
return listofcities.get(a);
* Methods to get the city object from the citylist.
*/
public city getCityByPosition(int a) // get the a'th position of the citylist
{
return listOfCities.get(a);
}

public city get(city a) {
return get(posn(a));
public city getCityByCity(city a) {
return getCityByPosition(getPositionByCity(a));
}

public city get(String a) {
return listofcities.get(posn(a));
public city getCityByCityName(String a) {
return listOfCities.get(getPositionByString(a));
}

public city get(double a, double b) {
return get(new city(a, b));
public city getCityByCoordinates(double a, double b) {
return getCityByCity(new city(a, b));
}

public int size() // the size of the citylist
{
return listofcities.size();
public int getSizeOfCityList() // the size of the citylist
{
return listOfCities.size();
}
}
2 changes: 1 addition & 1 deletion src/complex.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class complex {
imaginary = b;
}

static double dist(complex a, complex b) {
static double distanceBetweenComplexPoints(complex a, complex b) {
// distance between two complex coordinates.
return complex.abs(complex.sub(a, b));
}
Expand Down

0 comments on commit ef7a197

Please sign in to comment.