## CS 596: Client-Server Programming Spring Semester, 1997 Doc 8, Assignment 1 Comments

To Lecture Notes Index
San Diego State University -- This page last updated Feb 18, 1997

### Contents of Doc 8, Assignment 1 Comments

 Static Problem slide # 1 Constant Problem slide # 2 Print Problem slide # 3 Struct Problem slide # 4 The God Class Problem slide # 5 Classes in One File slide # 8 No Data Files slide # 9 Continually Reading Data Files slide # 10 Exception Problem slide # 11 Singleton Problem slide # 12 Documentation Problem slide # 13 Positional Data Problem slide # 18

`Doc 8, Assignment 1 Comments Slide # 1`

# Static Problem

```class AirlineSchedule
{
private static Table data

public static void doSomething()
{ blah }

public static void methodA()
{blah}
}

class Cities
{
private static SomeType ModeData

public static void methodC()
{blah }

public static void methodB()
{blah}
}```

`Doc 8, Assignment 1 Comments Slide # 2`

# Constant Problem

```class AirlineSchedule
{
private String scheduleFile = "schedule";
private Table data;

public AirlineSchedule()
{
InputStream cin = new FileInputStream( scheduleFile);
blah blah
}

public void seatsOnPlane( String planeType)
{
if ( planeType == "A")
return 5;
else if ( planeType.equals( "B" ) )
return 10;
else if  ( planeType.equals( "C" ) )
return 15;
}
}
```

`Doc 8, Assignment 1 Comments Slide # 3`

# Print Problem

``` class AirlineSchedule
{
Hashtable cityAndCodes;

{
Enumeration cities = cityAndCodes.keys();
while ( cities.hasMoreElements() )
System.out.print( cities.nextElement() );

System.out.println();
}

public void citiesServed_Better(OutputStream output)
{
Enumeration cities = cityAndCodes.keys();
while ( cities.hasMoreElements() )
output.print( cities.nextElement() );

output.println();
}

public Vector citiesServed_BetterYet()
{
Enumeration cities = cityAndCodes.keys();
Vector cityList = new Vector();
while ( cities.hasMoreElements() )
return cityList;
}
}```

`Doc 8, Assignment 1 Comments Slide # 4`

# Struct Problem

```class Flight
{
int flightNumber;
String originCity;
String destinationCity;
String planeType;

public void setOriginCity( String newValue )
{originCity = newValue; }
public getOriginCity( )
{return originCity;}

public void setDestinationCity( String newValue ) {blah }
public getDestinationCity(  )     {blah}

public void setPlaneType( String newValue ) {blah }
public getPlaneType(  )     {blah}

public void setFlightNumber( String newValue ) {blah }
public getFlightNumber( )     {blah}

}
```

`Doc 8, Assignment 1 Comments Slide # 5`

# The God Class Problem

```class AirlineSchedule
{
public String getManifest()
{
blah
blah
blah
blah
while ( blah )
{
blah
blah
blah
blah
if ( blah )
blah
else
{
blah
blah
blah
blah
}
blah
}
blah
blah
blah
blah
blah
do Doc 8, Assignment 1 Comments Slide # 6

```
The God Class Problem
```               {
blah
blah
blah
blah
blah
```
```               try
{
blah
blah
blah
}
catch ( blah )
{
blah
}
}
until ( blah )
blah
blah
blah
blah
switch ( blah )
{
blah
blah
blah
}
return blah;
}
```

`Doc 8, Assignment 1 Comments Slide # 7`
File Problem 1

# Classes in One File

file: p1.java

```class Flight
{
BLAH
}

class AirlineSchedule
{
BLAH
}

class Cities
{
BLAH
}

class Reservations
{
BLAH
}
Doc 8, Assignment 1 Comments Slide # 8
```
File Problem 2

# No Data Files

```class AirlineSchedule
{
private String scheduleFile = "schedule";
private Table data;

public AirlineSchedule()
{
InputStream cin = new FileInputStream( scheduleFile);
blah blah
}

public void seatsOnPlane( String planeType)
{
if ( planeType == "A")
return 5;
else if ( planeType.equals( "B" ) )
return 10;
else if  ( planeType.equals( "C" ) )
return 15;
}
}
Doc 8, Assignment 1 Comments Slide # 9
```
File Problem 3

# Continually Reading Data Files

```
```
```public String getManifest( in FlightNumber, Date ofFlight)
{
InputStream cin = new FileInputStream( reservations);
read to correct line in file

String manifest = cin.readLine();
cin.close();
return manifest
}
```

```public String getCityList()
{
InputStream cin = new FileInputStream( cities);
read to correct line in file

String cityList = cin.readLine();
cin.close();
return cityList;
}
```

`Doc 8, Assignment 1 Comments Slide # 10`

# Exception Problem

```class AirlineSchedule
{
private String scheduleFile = "schedule";
private LabeledData data;

{
try
{
InputStream cin = new FileInputStream( scheduleFile);
}
catch ( FileNotFoundException noFile )
{
System.out.println( "Could not find file" );
}
}
```

`Doc 8, Assignment 1 Comments Slide # 11`

# Singleton Problem

```// Only one object of this class should exist in the program
class IForgetWhatClassTheyDidThisIn
{
blah
}

```
```// Only one object of this class can be created
class SingleObject
{
private SingleObject instance = null;

private SingleObject()
{ blah }

public static SingleObject getInstance()
{
if (  instance == null )
instance = new SingleObject();

return instance;
}
}
```

`Doc 8, Assignment 1 Comments Slide # 12`

# Documentation Problem 1

 Comments none some not useful useful Document XX Class XX Method XX Inline XX Java Doc XX
```

```
 Comments none some not useful useful Document XX Class XX XX Method XX XX Inline XX Java Doc XX
```Doc 8, Assignment 1 Comments Slide # 13
```
Documentation Problem 2

"Table class, what Table class? You didn't tell us about that!"

```Doc 8, Assignment 1 Comments Slide # 14
```
Sample Use of Table

```class Reservations
{
Table reservationData;
String reservationFileName;

public static void newDataBase( String fileName ) throws
FileExistsException, IOException
{
if ( new File( fileName ).exists() )
throw new FileExistsException( fileName );

OutputStream  cout = new BufferedOutputStream (
new FileOutputStream( fileName ) );

Table dataBase = new Table();
Vector header = new Vector();

String nullMessage = null;
dataBase.save( cout, nullMessage );
cout.flush();
cout.close();
}

Doc 8, Assignment 1 Comments Slide # 15
```
Reservations # 2

```public Reservations( String fileName) throws
FileNotFoundException, IOException
{
reservationFileName = fileName;
reservationData = new Table();
InputStream  cin = new BufferedInputStream (
new FileInputStream( fileName ) );
cin.close();
}

public int seatsFilled( String flightNumber, String date )
{
return flightManifest( flightNumber, date ).numberOfRows();
}

public boolean hasReservation( String name,
String flightNumber,
String date )
{
Table flightManifest = flightManifest( flightNumber, date );
return flightManifest.contains( name );
}

public void makeReservation( String name,
String flightNumber,
String date ){
Vector passengerData = new Vector();
```
```reservationData.addRow( passengerData );
}Doc 8, Assignment 1 Comments Slide # 16
```
Reservations # 3
```
```
```     public Table flightManifest( String flightNumber, String date )
{
Table flightData = reservationData.rowsAt( flightNumber, 1);
return flightData.rowsAt( date, 2 );
}

public void saveReservations() throws IOException
{
OutputStream  cout = new BufferedOutputStream (
new FileOutputStream(
reservationFileName ) );

String nullMessage = null;
reservationData.save( cout, nullMessage );
cout.flush();
cout.close();
}
}

class FileExistsException extends Exception
{
public FileExistsException()
{ super(); }

public FileExistsException( String message )
{ super(message); }
}

```

`Doc 8, Assignment 1 Comments Slide # 17`

# Positional Data Problem

Variation 5

 From To DepartTime ArrivalTime FlightNumber AircraftType AMS, LAX, 04:15, 15:15, 602, C;

```static final int CITY_TO_COLUMN = 1;
static final int CITY_FROM_COLUMN = 0;
static final int ARRIVAL_TIME_COLUMN = 3;
static final int DEPARTURE_TIME_COLUMN = 2;
static final int FLIGHT_NUMBER_COLUMN = 4;
static final int AIRCRAFT_TYPE_COLUMN = 5;

Table schedule;

public string getArrivalTime( String FlightNumber )
{
Table myFlight = schedule.rowsAt( FlightNumber,
FLIGHT_NUMBER_COLUMN);

return myFlight.elementAt( 2, ARRIVAL_TIME_COLUMN );
}

in Table class (in case you haven't seen the table class)
/**
* Returns a new table containing all the rows that have the value
* "key" in the indicated column
*/
public Table rowsAt( Object key, int column )
```

`Doc 8, Assignment 1 Comments Slide # 18`
Positional Data Problem
Variation 5 - Solution

 From To DepartTime ArrivalTime FlightNumber AircraftType AMS, LAX, 04:15, 15:15, 602, C;

```class AirlineSchedule
{
Table schedule;
Vector  columnIndex;

public AirlineSchedule()
{
blah
columnIndex = schedule.rowAt( 0 );
}

public string getArrivalTime( String FlightNumber )
{
Table myFlight;
myFlight = schedule.rowsAt( FlightNumber,
columnIndex.indexOf( ```
`"FlightNumber"`
`));`

```     return myFlight.elementAt( 2,
columnIndex.indexOf(```
` "ArrivalTime")`
``` );
}
Doc 8, Assignment 1 Comments Slide # 19
```
Positional Data Problem
Variation 5 - A Better Solution

Stop writing programs
Build classes to use in programs
Then write programs

```class LabeledTable extends Table
{
public Table rowsAt( Object key, String columnLabel )
{
int columnNumber = getColumnIndex( columnLabel );
return super.rowsAt( key, columnNumber );
}
}

```