Homework 4, Part A: Airports

Learning Goals

Tasks

Create a class named Airport that keeps track of the flights that use a certain airport, either as their origin or destination. Consult the provided Airport javadoc-generated documentation.

For this exercise you are NOT allowed to use Java’s Array class. Just use arrays of Objects as we did in lectures.

0. Draw your class objects as you design and before you code them

You will need to submit a picture of your drawing, so keep it (reasonably) neat and clean. If you need a reminder on how to draw these class objects, make sure to revisit the slides and see the CD and CDCollection objects.

Your class should contain the following:

1. Instance variables

2. Constructor

It should take the name of the airport as an input. Think of what the purpose of a constructor is!

3. Instance methods

4. No main() is required in the Airport class but create a separate client class

Instead, create another client class called LoganAirport.java. This class contains only a main() method. It main purpose is to test your Airport class. Make sure you test your methods as you develop your code. Start by creating an instance of type Airport called logan, and some Flight instances. For the Flight class, use your implementation from the previous homework. If you think your Flight class might have bugs, you may ask the staff for their solution. We highly recommend testing your implementation using the unit tests we provided in the previous assignment.

5. Write good javadoc

Make sure you add nice javadoc to your code:

6. Testing

Save the printout of the LoganAirport class into a file named LoganTesting.txt. For inspiration look at our own test output:

Start testing. Creating BOS airport. Capacity = 5
Adding flights for BOS airport.
AA123 from BOS to LAX 
DL55 from LAX to BOS 
LX422 from FRA to BOS 
LX441 from BOS to FRA 
DL522 from BOS to ATH 
	addFlight():: BOS airport cannot accommodate any more flights

Listing BOS airport flights.
BOS airport can accommodate 5 flights.
It is currently serving the following 5 flights:
 	AA123 from BOS to LAX 
	DL55 from LAX to BOS 
	LX422 from FRA to BOS 
	LX441 from BOS to FRA 
	DL522 from BOS to ATH 

LX is using BOS for the following flights: (expecting 2 flights)
	LX422 from FRA to BOS 
	LX441 from BOS to FRA 

AG is using BOS for the following flights: (expecting no flight)

AA is using logan for the following flights: (expecting 1 flight)
	AA123 from BOS to LAX

Submitting your code

Remember that in Gradescope you have to upload all the files at once. Every submission you do overwrites the previous one, so if you upload one file at a time, only the last will be saved.

Check your submission to make sure the right files have been submitted.


Homework 4, Part B: Deck of Cards

Learning Goals

Specifications

Create a class named Deck that creates a Deck of Card objects. For this exercise you are NOT allowed to use Java’s Array class. Just use arrays of Objects as we did in lectures.

0. Study and use the provided Card class

The Card.java class is provided for you—do not edit it. Please read it carefully, paying close attention to the equals() method.

Card.java:

/**
 * Card
 *
 * @author Smaranda Sandu, Stella K
 * @version 2/21/2023
 */
public class Card
{
    //In a common deck of cards, expect the suit to be one of the follwoing:
    //"Diamonds", "Clubs", "Spades", "Hearts"
    //and the value to be
    //from 1 to 13 (11 is Jack, 12 is Queen, 13 is King)
    private String suit;
    private int value;

    /**
     * Constructor for objects of class Card
     *
     * @param s the suit of the card
     * @param v the value of the card
     */
    public Card(String s, int v)
    {
        suit = s;
        value = v;
    }

    /**
     * Getter for suit
     *
     * @return the suit of the card
     */
    public String getSuit() {
        return suit;
    }

    /**
     * Getter for value
     *
     * @return the value of the card
     */
    public int getValue() {
        return value;
    }

    /**
     * String representation of a card
     *
     * @return string representation of the card
     */
    public String toString() {
        return value + " of " + suit;
    }

    /**
     * Method that checks when two cards are equal.
     * Two cards are considered equal iff they ahve the same suit and the same value
     *
     * @param c the card this (the invoking) object is compared against
     * @return true if this card and the input one are equal, false otherwise
     */
    public boolean equals(Card c) {
        return (this.suit.equals(c.suit)) && this.value==c.value;
    }


    public static void main(String[] args) {
        Card c1 = new Card("Diamonds", 3);
        System.out.println(c1);

        Card c2 = new Card("Hearts", 10);
        System.out.println(c2);

        System.out.println(c1.equals(c2));
    }
}

Next, create a Deck class that contains,

1. Instance variables

2. Constructor

It should fill the array with all 52 unique possible Cards. Recall that a Card can have one of 4 possible suits and one of 13 possible values. You have complete freedom over how this can happen.

3. Instance methods

4. No main() is required in the Deck class

Instead, create another client class called Game.java. This class contains only a main() method. It main purpose is to test your Deck class. Make sure you test your methods as you develop your code.

5. Write good javadoc

Make sure you add nice javadoc to your code:

6. Testing

Save the printout of the Game class into a file named GameTesting.txt. You can optionally use the code provided below to additionally test your code.

DeckTest.java:

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class DeckTest {

    private Deck deck;

    @BeforeEach
    public void setUp() {
        deck = new Deck();
    }

    @Test
    public void testDeckInitialization() {
        String deckStr = deck.toString();
        assertTrue(deckStr.contains("Deck contains 52 cards:"));
        assertTrue(deckStr.contains("1 of Diamonds"));
        assertTrue(deckStr.contains("13 of Hearts"));
    }

    @Test
    public void testCut() {
        deck.cut();
        String deckStr = deck.toString();
        assertTrue(deckStr.contains("Deck contains 52 cards:"));
        // Since the cut location is random, we can't predict the exact order,
        // but we can still check if all cards are present.
        assertTrue(deckStr.contains("1 of Diamonds"));
        assertTrue(deckStr.contains("13 of Hearts"));
    }

    @Test
    public void testShuffle() {
        deck.shuffle();
        String deckStr = deck.toString();
        assertTrue(deckStr.contains("Deck contains 52 cards:"));
        // Similar to the cut method, shuffle is random, so we can't predict the exact order.
        // We can only check if all cards are present.
        assertTrue(deckStr.contains("1 of Diamonds"));
        assertTrue(deckStr.contains("13 of Hearts"));
    }

    // Additional tests can be added to check other functionalities or edge cases.
}

Submitting your code

Submit your commented Card.java, your Deck.java, your Game.java, and your GameTesting.txt, to Gradescope.

Remember that in Gradescope you have to upload all the files at once. Every submission you do overwrites the previous one, so if you upload one file at a time, only the last will be saved.

Check your submission to make sure the right files have been submitted.


Submission Checklist