> For the complete documentation index, see [llms.txt](https://www.computersciencea.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.computersciencea.com/older/session-22.md).

# Session 22

## Source Code

```java
import java.util.*;

public class Week22 {
    public static void main(String[] args) {
        String myName = ""; // add your name
        System.out.println("Week 22" + " - " + myName);

        // Menu
        System.out.println("Menu");
        System.out.println("E1 - Example 1");
        System.out.println("Q  - Quit");

        // setup Scanner
        Scanner in = new Scanner(System.in);
        System.out.print("Choice: ");
        String choice = in.nextLine();

        // switch choices
        switch (choice) {
            case "E1":
                System.out.println("Example 1");
                example1();
                break;
            case "E2":
                System.out.println("Example 2");
                example2();
                break;
            case "E3":
                System.out.println("Example 3");
                example3();
            case "Q":
                System.out.println("Quitting..!");
                break;
        }
    }
    // example1 method
    public static void example1() {
        // This creates a GameCharacter object.
        

        // This creates a BossCharacter object,
        // but stores it in a GameCharacter reference.
        
        
        // This creates a BossCharacter object,
        // stored in a BossCharacter reference.
        


    }

    // example2 method
    public static void example2() {
        String s1 = "hello";
        String s2 = "hello";
        // .equals()
        

        // equality
        

        // new String
        

    }

    // example3 method
    public static void example3() {
        


        // "Fortnite" (Battle Royale) - Max Players: 100
        
        
        // "Minecraft" (Sandbox) - Max Players: 10
        



    }
}

```

## SeeNSayToy.java

```java
// Parent class
class FarmCreature {. 
    public void makeNoise() {
        System.out.println("This creature makes a sound.");
    }
}

// Subclasses
class Cow extends FarmCreature {
    public void makeNoise() {
        System.out.println("The cow says: Moo!");
    }
}

class Duck extends FarmCreature {
    public void makeNoise() {
        System.out.println("The duck says: Quack!");
    }
}

class Sheep extends FarmCreature {
    public void makeNoise() {
        System.out.println("The sheep says: Baa!");
    }
}

class Pig extends FarmCreature {
    public void makeNoise() {
        System.out.println("The pig says: Oink!");
    }
}

// SeeNSay Toy class
public class SeeNSayToy {
    public static void main(String[] args) {
        System.out.println("SeeNSayToy Example");
        // Create an array of FarmCreature references
        

        // Simulate the spinning arrow landing on a random creature


        
        // Polymorphism: behavior depends on actual object

    }
}

```

## ShoppingCartDemo2.java

```java
import java.util.*;

/**
 * The ShoppingCart class has an ArrayList of Items.
 * You will write a new class DiscountedItem that extends Item.
 * This code is adapted
 * https://practiceit.cs.washington.edu/problem/view/bjp4/chapter9/e10-DiscountBill
 */

public class ShoppingCartDemo2 {
    public static void main(String[] args) {
        ShoppingCart cart = new ShoppingCart();
        cart.add(new Item("bread", 3.25));
        cart.add(new Item("milk", 2.50));
        // cart.add(new DiscountedItem("ice cream", 4.50, 1.50));
        // cart.add(new DiscountedItem("apples", 1.35, 0.25));
        // add another DiscountedItem

        cart.printOrder();
        // call countDiscountedItems
        
    }
}

class DiscountedItem extends Item {
    // Copy your DiscountedItem code the last activity here!
    
    
}

class ShoppingCart {
    private ArrayList<Item> order;
    private double total;
    private double internalDiscount;

    public ShoppingCart() {
        order = new ArrayList<Item>();
        total = 0.0;
        internalDiscount = 0.0;
    }

    public void add(Item i) {
        order.add(i);
        total += i.getPrice();
        if (i instanceof DiscountedItem)
            internalDiscount += ((DiscountedItem) i).getDiscount();
    }  

    /** printOrder() will call toString() to print */
    public void printOrder() {
        System.out.println(this);
    }

    public String toString() {
        return discountToString();
    }

    public String discountToString() {
        return orderToString() + "\nSub-total: " + valueToString(total) + "\nDiscount: "
                + valueToString(internalDiscount) + "\nTotal: " + valueToString(total - internalDiscount);
    }

    private String valueToString(double value) {
        value = Math.rint(value * 100) / 100.0;
        String result = "" + Math.abs(value);
        if (result.indexOf(".") == result.length() - 2) {
            result += "0";
        }
        result = "$" + result;
        return result;
    }

    public String orderToString() {
        String build = "\nOrder Items:\n";
        for (int i = 0; i < order.size(); i++) {
            build += "   " + order.get(i);
            if (i != order.size() - 1) {
                build += "\n";
            }
        }
        return build;
    }
    // Add a method called countDiscountedItems()
    
    
    
}

class Item {
    private String name;
    private double price;

    public Item() {
        this.name = "";
        this.price = 0.0;
    }

    public Item(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public double getPrice() {
        return price;
    }

    public String valueToString(double value) {
        String result = "" + Math.abs(value);
        if (result.indexOf(".") == result.length() - 2) {
            result += "0";
        }
        result = "$" + result;
        return result;
    }

    public String toString() {
        return name + " " + valueToString(price);
    }
}

```

## Game.java

```java
// Superclass representing a generic video game
public class Game {
    // Instance variables to store the game's title and genre
    

    // Constructor to initialize the game's title and genre
    

    // Overrides the default toString() method to return a more readable string
    

    // Overrides the default equals() method to compare two Game objects logically
    
    
        // If both references point to the same object, they're equal
        

        // If obj is not an instance of Game, they can't be equal
        

        // Downcast obj to Game to compare their content
        

        // Check if both title and genre are equal
        

    
}

```

## OnlineGame.java

```java
// Subclass representing an online game that extends the base Game class
public class OnlineGame extends Game {
    // Instance variable to store the maximum number of players for the online game
    

    // Constructor
    

    // Overrides the toString method to include maxPlayers in the description
   

    // Overrides the equals method to compare OnlineGame objects logically
    
    
        // First, check if the superclass part of the object is equal (title and genre)
        

        // Ensure the object is actually an OnlineGame
        

        // Downcast obj to OnlineGame so we can compare maxPlayers
        

        // Return true if both objects have the same maxPlayers value
        
        
}

```

## MovieCollection.java

```java
import java.util.*;

public class MovieCollection {

    // Superclass
    public static class Movie {
        private String title;
        private String genre;

        public Movie(String title, String genre) {
            this.title = title;
            this.genre = genre;
        }

        // TODO: Override toString() to return a string like: "Inception" (Sci-Fi)
        @Override
        public String toString() {
            return ""; // placeholder
        }

        // TODO: Override equals to compare title and genre
        @Override
        public boolean equals(Object obj) {
            return false; // placeholder
        }

        public String getTitle() {
            return title;
        }
    }

    // Subclass
    public static class StreamingMovie extends Movie {
        private String platform;
        private String rating;

        public StreamingMovie(String title, String genre, String platform, String rating) {
            super(title, genre);
            this.platform = platform;
            this.rating = rating;
        }

        // TODO: Override toString() to add platform and rating
        @Override
        public String toString() {
            return ""; // placeholder
        }

        // TODO: Override equals to include platform and rating
        @Override
        public boolean equals(Object obj) {
            return false; // placeholder
        }

        public String getPlatform() {
            return platform;
        }
    }

    // Main method for testing
    public static void main(String[] args) {
        ArrayList<StreamingMovie> watchlist = new ArrayList<>();

        // TODO: Add at least 4 StreamingMovie objects to the watchlist

        // TODO: Print all movies in the list

        // TODO: Search for a movie by title and print its details

        // TODO: Count how many movies are on a specific platform (e.g., Netflix)
        
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.computersciencea.com/older/session-22.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
