HWK2 Introduction to Computer Science for MAjors II Spring98

Due date: 2/25/99 Submit on-line.

Problem: Create a linked list to keep track of airport name, airport code

pair for a list given in Fig.3.28 of your text.

Steps:

1. Design a class Airport with two data values: airportName and airportCode.

The methods needed: constructors, equal, tostring

2. Write an application class with and instantiate a linked list object.

Read each pair of airport data into a Airport object and add this object

the list. Use jdk1.2 LinkedList class.

3. Add a method to the application that will take airport code as parameter,

search the linked list for airport object that has this code, and obtain

airport name corresponding to the code and print it out. You will have to

use iterator to traverse thru the list.
 
 

Answer: This answer is a tested Java program which gives lot more details that needed.

A Java copy is available in my directory cse116/examples on everest for you to run and

Observe operation.
 
 

import java.util.*;

import structure.*;
 
 

class hwk2 {

private static LinkedList airportList;

private static ReadStream r;

public static void main(String [] args) {

r = new ReadStream();

airportList = new LinkedList();

// make the airport list

readinAirports();

// get code from user and print out corresponding

// airport details

String code;

System.out.println("Lets search airport name given a code");

System.out.println("Please input airport code to search");

code = r.readString();

Airport temp = searchCode(code);

if (temp != null)

System.out.println(temp); // this will polymorphically call toString

// of Airport class

else

System.out.println("Not found");

String name;

System.out.println("Lets search airport name given a name");

System.out.println("Please input airport name ");

name = r.readString();

temp = searchName(name);

if (temp != null)

System.out.println(temp); // this will polymorphically call toString

// of Airport class

else

System.out.println("Not found");

}

//input and search methods

public static void readinAirports()

{

char more = 'y';

String name, code;

Airport temp; char junk;

while (more == 'y')

{

System.out.println("Please input airport name");

name = r.readLine();

System.out.println("Please input airport code");

code = r.readLine();

temp = new Airport(name,code);

airportList.add(temp);

System.out.println("Any more airports? (y or n)");

more = r.readChar(); junk = r.readChar();

}

}

public static Airport searchCode(String code)

{

ListIterator i = airportList.listIterator();

boolean found = false;

Airport a = null;

while (!found && i.hasNext())

{ a = (Airport)i.next();

if (a.getCode().equals(code))

found = true;

}

if (found)

return a;

else return null;

}

public static Airport searchName(String name)

{

ListIterator i = airportList.listIterator();

boolean found = false;

Airport a = null;

while (!found && i.hasNext())

{ a = (Airport)i.next();

if (a.getName().equals(name))

found = true;

}

if (found)

return a;

else

return null;

}

}

class Airport {

private String code;

private String name;

//constructor

public Airport(String n, String c)

{

code = c;

name = n;

}

public String toString()

{

return ("Airport Name: " + name +" " + "Airport Code: " + code);

}

public boolean equals(Airport a)

{

return (name.equals(a.getName()) && code.equals(a.getCode()));

}

public String getName()

{

return name;}

public String getCode()

{

return code;

}

}