/* Airport class Anderson, Franceschi */ public class Airport { // 1. ***** Defi
ID: 3835405 • Letter: #
Question
/* Airport class
Anderson, Franceschi
*/
public class Airport
{
// 1. ***** Define the instance variables *****
// airportCode is a String
// gates is an integer
// Part 1 student code starts here:
// Part 1 student code ends here.
// 2. ***** Write this method *****
// Default constructor:
// method name: Airport
// return value: none
// parameters: none
// function: sets the airportCode to an empty String
// Part 2 student code starts here:
// Part 2 student code ends here.
// 3. ***** Write this method *****
// Overloaded constructor:
// method name: Airport
// return value: none
// parameters: a String startAirportCode and an int startGates
// function:
// calls the the setAirportCode method,
// passing startAirportCode parameter;
// calls the setGates method, passing the startGates parameter
// Part 3 student code starts here:
// Part 3 student code ends here.
// 4. ***** Write this method *****
// Accessor method for the airportCode instance variable
// method name: getAirportCode
// return value: String
// parameters: none
// function: returns airportCode
// Part 4 student code starts here:
// Part 4 student code ends here.
// 5. ***** Write this method *****
// Accessor method for the gates instance variable
// method name: getGates
// return value: int
// parameters: none
// function: returns gates
// Part 5 student code starts here:
// Part 5 student code ends here.
// 6. ***** Write this method *****
// Mutator method for the airportCode instance variable
// method name: setAirportCode
// return value: void
// parameters: String newAirportCode
// function: assigns airportCode the value of the
// newAirportCode parameter
// Part 6 student code starts here:
// Part 6 student code ends here.
// 7. ***** Write this method *****
// Mutator method for the gates instance variable
// method name: setGates
// return value: void
// parameters: int newGates
// function: validates the newGates parameter.
// if newGates is greater than or equal to 0,
// sets gates to newGates;
// otherwise, prints an error message to System.err
// and does not change value of gates
// Part 7 student code starts here:
// Part 7 student code ends here.
} // end of Airport class definition
/* A client program to display Airport object values
Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class AirportClient extends JFrame
{
String action1, action2;
boolean firstTime = true;
double animationPause = 5.0; // 5 seconds between animations
Airport airport1, airport2; // declare Airport object references
public void workWithAirports( )
{
animate( "Two airport object references declared:",
"Airport airport1, airport2;" );
/* Instantiate the airport using an empty argument list */
airport1 = new Airport( );
animate( "Instantiated airport1 using default constructor:",
"airport1 = new Airport( );" );
/* Set the airport code to IAD */
animate( "Set the airport1 code to IAD:",
"airport1.setAirportCode( "IAD" );" );
/* Set the number of gates to 30 */
airport1.setGates( 30 );
animate( "Set the number of gates to 30:" ,
"airport1.setGates( 30 );" );
/* Set the number of gates to -1, an invalid value */
airport1.setGates( -1 );
animate( "Attempted to set the number of gates to -1:",
"airport1.setGates( -1 );" );
/* Instantiate a second airport object using non-default constructor*/
airport2 = new Airport( "JFK", 69 );
animate( "Instantiated airport2 using overloaded constructor:",
"airport2 = new Airport( "JFK", 69 );" );
/* Finished */
animate( "Actions are complete, exiting", "" );
System.exit( 1 );
}
public AirportClient( )
{
super( "Using the Airport Class" );
setSize( 300, 350 );
setVisible( true );
}
public void paint( Graphics g )
{
super.paint( g );
if ( firstTime )
firstTime = false;
else
{
int boxL = 75, boxH = 20;
int sX = 50;
int sY = 75;
if ( airport1 != null )
{
// object reference box
g.setColor( Color.BLACK );
g.drawRect( sX, sY, boxL, boxH );
g.drawString( "airport1", sX, sY - 10 );
draw( g, sX, sY, airport1 ); // draw airport object
}
else
{
// indicate null reference
g.setColor( Color.BLACK );
g.drawRect( sX, sY, boxL, boxH );
g.drawString( "airport1", sX, sY - 10 );
g.setColor( Color.BLUE );
g.drawString( "null", sX + 15, sY + 15 );
}
sY = 200;
if ( airport2 != null )
{
// object reference box
g.setColor( Color.BLACK );
g.drawRect( sX, sY, boxL, boxH );
g.drawString( "airport2", sX, sY - 10 );
draw( g, sX, sY, airport2 ); // draw airport object
}
else
{
// indicate null reference
g.setColor( Color.BLACK );
g.drawRect( sX, sY, boxL, boxH );
g.drawString( "airport2", sX, sY - 10 );
g.setColor( Color.BLUE );
g.drawString( "null", sX + 15, sY + 15 );
}
// display action at bottom of screen
g.setColor( Color.BLUE );
g.drawString( action1, 20, 320 );
g.drawString( action2, 35, 335 );
}
}
private void draw( Graphics g, int sX, int sY, Airport a )
{
int boxL = 75, boxH = 20;
// arrow
g.setColor( Color.BLACK );
g.drawLine( sX + boxL, sY + boxH / 2,
sX + boxL + 25, sY + boxH / 2 );
g.drawLine( sX + boxL + 25, sY + boxH / 2,
sX + boxL + 25, sY + boxH * 2 );
g.drawLine( sX + boxL + 25 - 5, sY + boxH * 2 - 5,
sX + boxL + 25, sY + boxH * 2 );
g.drawLine( sX + boxL + 25 + 5, sY + boxH * 2 - 5,
sX + boxL + 25, sY + boxH * 2 );
// airportCode
g.setColor( Color.BLACK );
g.drawString( "airport code", sX + boxL - 75, sY + 2 * boxH + 15 );
g.drawRect( sX + boxL, sY + 2 * boxH, boxL, boxH );
g.setColor( Color.BLUE );
g.drawString( a.getAirportCode( ),
sX + boxL + 5, sY + 2 * boxH + 15 );
// gates
g.setColor( Color.BLACK );
g.drawString( "gates", sX + boxL - 75, sY + 3 * boxH + 15 );
g.drawRect( sX + boxL, sY + 3 * boxH, boxL, boxH );
g.setColor( Color.BLUE );
g.drawString( Integer.toString( a.getGates( ) ),
sX + boxL + 5, sY + 3 * boxH + 15 );
}
private void animate( String a1, String a2 )
{
action1 = a1;
action2 = a2;
repaint( );
Pause.wait( animationPause );
}
public static void main( String [] args )
{
AirportClient app = new AirportClient( );
app.workWithAirports( );
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
/* Pause class
* Anderson, Franceschi
*/
public class Pause
{
/** wait method
* @param seconds number of seconds to pause
*/
public static void wait( double seconds )
{
try
{
Thread.sleep( (int)( seconds * 1000 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
}
}
Explanation / Answer
Hi,
Please see the updated class.
Please comment for any queries/feedbacks.
Thanks.
Airport.java
import java.io.PrintStream;
/* Airport class
Anderson, Franceschi
*/
public class Airport
{
// 1. ***** Define the instance variables *****
// airportCode is a String
// gates is an integer
// Part 1 student code starts here:
private String airportCode;
private int gates;
// Part 1 student code ends here.
// 2. ***** Write this method *****
// Default constructor:
// method name: Airport
// return value: none
// parameters: none
// function: sets the airportCode to an empty String
// Part 2 student code starts here:
public Airport() {
this.airportCode = "";
this.gates = 0;
}
// Part 2 student code ends here.
// 3. ***** Write this method *****
// Overloaded constructor:
// method name: Airport
// return value: none
// parameters: a String startAirportCode and an int startGates
// function:
// calls the the setAirportCode method,
// passing startAirportCode parameter;
// calls the setGates method, passing the startGates parameter
// Part 3 student code starts here:
public Airport(String startAirportCode, int startGates) {
super();
setAirportCode(startAirportCode);
setGates(startGates);
}
// Part 3 student code ends here.
// 4. ***** Write this method *****
// Accessor method for the airportCode instance variable
// method name: getAirportCode
// return value: String
// parameters: none
// function: returns airportCode
// Part 4 student code starts here:
public String getAirportCode() {
return airportCode;
}
// Part 4 student code ends here.
// 5. ***** Write this method *****
// Accessor method for the gates instance variable
// method name: getGates
// return value: int
// parameters: none
// function: returns gates
// Part 5 student code starts here:
public int getGates() {
return gates;
}
// Part 5 student code ends here.
// 6. ***** Write this method *****
// Mutator method for the airportCode instance variable
// method name: setAirportCode
// return value: void
// parameters: String newAirportCode
// function: assigns airportCode the value of the
// newAirportCode parameter
// Part 6 student code starts here:
public void setAirportCode(String newAirportCode) {
this.airportCode = newAirportCode;
}
// Part 6 student code ends here.
// 7. ***** Write this method *****
// Mutator method for the gates instance variable
// method name: setGates
// return value: void
// parameters: int newGates
// function: validates the newGates parameter.
// if newGates is greater than or equal to 0,
// sets gates to newGates;
// otherwise, prints an error message to System.err
// and does not change value of gates
// Part 7 student code starts here:
public void setGates(int newGates) {
if(newGates >= 0){
this.gates = gates;
}
else{
System.err.println("Invalid no of gates");
}
}
// Part 7 student code ends here.
} // end of Airport class definition
Pause.java
/* Pause class
* Anderson, Franceschi
*/
public class Pause
{
/** wait method
* @param seconds number of seconds to pause
*/
public static void wait( double seconds )
{
try
{
Thread.sleep( (int)( seconds * 1000 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
}
}
AirportClient.java
/* A client program to display Airport object values
Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class AirportClient extends JFrame
{
String action1, action2;
boolean firstTime = true;
double animationPause = 5.0; // 5 seconds between animations
Airport airport1, airport2; // declare Airport object references
public void workWithAirports( )
{
animate( "Two airport object references declared:",
"Airport airport1, airport2;" );
/* Instantiate the airport using an empty argument list */
airport1 = new Airport( );
animate( "Instantiated airport1 using default constructor:",
"airport1 = new Airport( );" );
/* Set the airport code to IAD */
animate( "Set the airport1 code to IAD:",
"airport1.setAirportCode( "IAD" );" );
/* Set the number of gates to 30 */
airport1.setGates( 30 );
animate( "Set the number of gates to 30:" ,
"airport1.setGates( 30 );" );
/* Set the number of gates to -1, an invalid value */
airport1.setGates( -1 );
animate( "Attempted to set the number of gates to -1:",
"airport1.setGates( -1 );" );
/* Instantiate a second airport object using non-default constructor*/
airport2 = new Airport( "JFK", 69 );
animate( "Instantiated airport2 using overloaded constructor:",
"airport2 = new Airport( "JFK", 69 );" );
/* Finished */
animate( "Actions are complete, exiting", "" );
System.exit( 1 );
}
public AirportClient( )
{
super( "Using the Airport Class" );
setSize( 300, 350 );
setVisible( true );
}
public void paint( Graphics g )
{
super.paint( g );
if ( firstTime )
firstTime = false;
else
{
int boxL = 75, boxH = 20;
int sX = 50;
int sY = 75;
if ( airport1 != null )
{
// object reference box
g.setColor( Color.BLACK );
g.drawRect( sX, sY, boxL, boxH );
g.drawString( "airport1", sX, sY - 10 );
draw( g, sX, sY, airport1 ); // draw airport object
}
else
{
// indicate null reference
g.setColor( Color.BLACK );
g.drawRect( sX, sY, boxL, boxH );
g.drawString( "airport1", sX, sY - 10 );
g.setColor( Color.BLUE );
g.drawString( "null", sX + 15, sY + 15 );
}
sY = 200;
if ( airport2 != null )
{
// object reference box
g.setColor( Color.BLACK );
g.drawRect( sX, sY, boxL, boxH );
g.drawString( "airport2", sX, sY - 10 );
draw( g, sX, sY, airport2 ); // draw airport object
}
else
{
// indicate null reference
g.setColor( Color.BLACK );
g.drawRect( sX, sY, boxL, boxH );
g.drawString( "airport2", sX, sY - 10 );
g.setColor( Color.BLUE );
g.drawString( "null", sX + 15, sY + 15 );
}
// display action at bottom of screen
g.setColor( Color.BLUE );
g.drawString( action1, 20, 320 );
g.drawString( action2, 35, 335 );
}
}
private void draw( Graphics g, int sX, int sY, Airport a )
{
int boxL = 75, boxH = 20;
// arrow
g.setColor( Color.BLACK );
g.drawLine( sX + boxL, sY + boxH / 2,
sX + boxL + 25, sY + boxH / 2 );
g.drawLine( sX + boxL + 25, sY + boxH / 2,
sX + boxL + 25, sY + boxH * 2 );
g.drawLine( sX + boxL + 25 - 5, sY + boxH * 2 - 5,
sX + boxL + 25, sY + boxH * 2 );
g.drawLine( sX + boxL + 25 + 5, sY + boxH * 2 - 5,
sX + boxL + 25, sY + boxH * 2 );
// airportCode
g.setColor( Color.BLACK );
g.drawString( "airport code", sX + boxL - 75, sY + 2 * boxH + 15 );
g.drawRect( sX + boxL, sY + 2 * boxH, boxL, boxH );
g.setColor( Color.BLUE );
g.drawString( a.getAirportCode( ),
sX + boxL + 5, sY + 2 * boxH + 15 );
// gates
g.setColor( Color.BLACK );
g.drawString( "gates", sX + boxL - 75, sY + 3 * boxH + 15 );
g.drawRect( sX + boxL, sY + 3 * boxH, boxL, boxH );
g.setColor( Color.BLUE );
g.drawString( Integer.toString( a.getGates( ) ),
sX + boxL + 5, sY + 3 * boxH + 15 );
}
private void animate( String a1, String a2 )
{
action1 = a1;
action2 = a2;
repaint( );
Pause.wait( animationPause );
}
public static void main( String [] args )
{
AirportClient app = new AirportClient( );
app.workWithAirports( );
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.