> 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-21.md).

# Session 21

## Source Code

```java
import java.util.*;

public class Week21 {
    public static void main(String[] args) {
        String myName = ""; // add your name
        System.out.println("Week 21" + " - " + 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();
                break;
            case "Q":
                System.out.println("Quitting..!");
                break;
        }
    }

    // example1 method
    public static void example1() {
        // Create a Greeter
        
        // Create a MeanGreeter
        

    }

    // example2 method
    public static void example2() {
        // Create a Greeter
        
        // Create a MeanGreeter
        
    }

    // example3 method
    public static void example3() {
        
    }
}

```

## Greeter.java

```java
public class Greeter {
    // greet method no parameters


    // greet method with one parameter

}
```

## MeanGeeter.java

```java
public class MeanGreeter extends Greeter {
    @Override // Compiler will verify this actually overrides a method
    // greet method with no parameters


    
    // greet method with one parameter


}
```

## SocialMediaDemo.java

```java
class SocialMediaUser {
    private String username;
    private String platform;

    // constructor


    // introduce method
    public void introduce() {

    }

    // Getter methods
    public String getUsername() {
        return username;
    }

    public String getPlatform() {
        return platform;
    }
}

class TeenInfluencer extends SocialMediaUser {
    private String contentNiche;
    private int followerCount;

    // constructor


    @Override
    public void introduce() {
       
       

    }

    // Helper method to format follower count (e.g., 1500 -> 1.5K)
    private String formatFollowerCount(int count) {
        if (count >= 1000000) {
            return String.format("%.1fM", count / 1000000.0);
        } else if (count >= 1000) {
            return String.format("%.1fK", count / 1000.0);
        } else {
            return String.valueOf(count);
        }
    }

    // Helper method to add a catchy tagline based on content niche
    private void addTagline() {
        switch (contentNiche.toLowerCase()) {
            case "gaming":
                System.out.println("Let's get this win! #GamerLife");
                break;
            case "fashion":
                System.out.println("Serving looks 24/7! #StyleIcon");
                break;
            case "music":
                System.out.println("Dropping beats and making waves! #MusicLife");
                break;
            case "fitness":
                System.out.println("No pain, no gain! #FitCheck");
                break;
            default:
                System.out.println("Don't forget to like and subscribe! #ContentCreator");
        }
    }
}

public class SocialMediaDemo {
    public static void main(String[] args) {
        // Create a regular social media user


        // Create some teen influencers with different niches


        // Test the introduce methods


    }
}
```

## Customer.java

```java
public class Customer
{
    private String name;
    private String address;

    public Customer(String n, String a)
    {
        name = n;
        address = a;
    }

    public String toString()
    {
        return "Name: " + name + "\nAddress: " + address;
    }

    public static void main(String[] args)
    {
        Customer c = new Customer("Fran Santiago", "123 Main St., Anytown, USA");
        System.out.println(c);

        // Uncomment these to test OnlineCustomer
        // OnlineCustomer c2 = new OnlineCustomer("Jasper Smith",
        //       "456 High St., Anytown, USA", "jsmith456@gmail.com");
        // System.out.println(c2);
    }
}

// Complete the OnlineCustomer class to inherit from Customer
// It should have an email attribute,
// a constructor with 3 arguments (name, address, email) that uses the super
// constructor,
// and an overridden toString() method that calls the super toString() method
//  and then prints "\nEmail:" and the email variable.

class OnlineCustomer
{

}
```

## MoreShapesDemo.java

```java
// Shape class
class Shape {
    // Method to describe the relationship between two shapes


    // Overriding toString() method


}

// Square class
class Square extends Shape {
    // Overriding toString() method


}

// Rectangle class
class Rectangle extends Shape {
    // Overriding toString() method

}

// Main class to demonstrate the relationship method
public class MoreShapesDemo {
    public static void main(String[] args) {
        // Creating Shapes


        // Array of Shapes


        // Demonstrating the relationships
        

    }
}
```

## ShoppingCartDemo.java

```java
import java.util.*;

/**
 * This code is based upon code in
 * https://practiceit.cs.washington.edu/problem/view/bjp4/chapter9/e10-DiscountBill
 */

// Class representing an item with a name and price
class Item {
    private String name; // Name of the item
    private double price; // Price of the item

    // Default constructor initializing item with empty name and price 0.0
    public Item() {
        this.name = "";
        this.price = 0.0;
    }

    // Constructor that initializes item with given name and price
    public Item(String name, double price) {
        this.name = name;
        this.price = price;
    }

    // Method to get the price of the item
    public double getPrice() {
        return price;
    }

    // Method to format a double value to a currency string
    public String valueToString(double value) {
        String result = "" + Math.abs(value);
        if (result.indexOf(".") == result.length() - 2) {
            result += "0";
        }
        result = "$" + result;
        return result;
    }

    // Overriding toString() to return the item name and price as a string
    public String toString() {
        return name + " " + valueToString(price);
    }
}

// DiscountedItem class inheriting from Item
class DiscountedItem extends Item {
    // Instance variable for the discount amount

    // Default constructor initializing discount to 0.0
    

    // Constructor that initializes name, price, and discount
    
    
        // Call the superclass constructor
        

    // Getter method for discount amount
    

    // Setter method for discount amount
    
    
    // Overriding toString() to include discount amount formatted as currency
    
}

// ShoppingCart class to manage a collection of items
class ShoppingCart {
    private ArrayList<Item> order; // List to store items
    private double total; // Total price of items
    private double internalDiscount; // Total discount amount

    // Constructor initializing the cart with an empty order list and zero totals
    public ShoppingCart() {
        order = new ArrayList<Item>();
        total = 0.0;
        internalDiscount = 0.0;
    }

    // Method to add an item to the cart and update totals
    public void add(Item i) {
        order.add(i);
        total += i.getPrice();
        // Check if the item is a discounted item and update discount
        // if (i instanceof DiscountedItem)
        //    internalDiscount += ((DiscountedItem) i).getDiscount();
    }

    // Method to print the order details
    public void printOrder() {
        System.out.println(this);
    }

    // Overriding toString() to return a formatted order summary
    public String toString() {
        return discountToString();
    }

    // Method to format the entire order, including subtotal, discount, and total
    public String discountToString() {
        return orderToString() + "\nSub-total: " + valueToString(total) + "\nDiscount: " + valueToString(internalDiscount) + "\nTotal: " + valueToString(total - internalDiscount);
    }

    // Method to format a double value to a currency string
    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;
    }

    // Method to build a string representation of all order items
    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;
    }
}

// Main class to demonstrate the ShoppingCart functionality
public class ShoppingCartDemo {
    public static void main(String[] args) {
        ShoppingCart cart = new ShoppingCart();
        // Adding regular items to the cart
        cart.add(new Item("bread", 3.25));
        cart.add(new Item("milk", 2.50));

        // Adding discounted items to the cart
        // cart.add(new DiscountedItem("ice cream", 4.50, 1.50));
        // cart.add(new DiscountedItem("apples", 1.35, 0.25));

        // Print the contents of the cart
        cart.printOrder();
    }
}

```


---

# 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-21.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.
