😎
Introduction to Programming with Java
  • Introduction to Programming with Java
  • Session 1
    • Assignment1.java
  • Session 2
    • Source Code
    • Activities
    • Assignment
  • Session 3
    • Source Code
    • Activities
    • Assignment
  • Session 4
    • Source Code
    • Activities
  • Session 5
    • Source Code
    • Project
  • Session 6
    • Source Code
  • Session 7
    • Source Code
  • Session 8
    • Source Code
  • Session 9
    • Source Code
  • Session 10
    • Source Code
Powered by GitBook
On this page
  • Session9.java
  • Feline.java
  • Cat.java
  • Pastry.java
  • Cake.java
  • Cookie.java
  • SmartDevice.java
  • SmartLight.java
  • Shopping.java
  1. Session 9

Source Code

Session9.java

import java.util.*;

public class Session9 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String myName = ""; // Add your name
        System.out.println("Session 9 - " + myName);

        // menu variables
        boolean done = false;
        String choice;

        do {
            // Menu
            System.out.println("Menu");
            System.out.println("E1 - Example 1");
            // optionally, add new menu items

            System.out.println("Q - Quit");
            System.out.print("Choice: ");
            choice = in.nextLine();

            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 "E4":
                    System.out.println("Example 4");
                    example4();
                    break;
                case "E5":
                    System.out.println("Example 5");
                    example5();
                    break;
                case "E6":
                    System.out.println("Example 6");
                    example5();
                    break;
                case "E7":
                    System.out.println("Example 7");
                    example7();
                    break;
                case "E8":
                    System.out.println("Example 8");
                    example8();
                    break;
                case "E9":
                    System.out.println("Example 9");
                    example9();
                    break;
                // quit and default cases
                case "Q":
                    System.out.println("Quit");
                    done = true;
                    break;
                default:
                    System.out.println("Invalid Choice");
                    break;
            }
        } while (!done);
    }

    // example1 method definition
    public static void example1() {
        // create Feline objects

        // call methods

   }

    // example2 method definition
    public static void example2() {
        // create Cat objects

        // call methods

    }

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

    }

    // example4 method definition
    public static void example4() {

    }

    // example5 method definition
    public static void example5() {

    }

    // example6 method definition
    public static void example6() {

    }

    // example7 method definition
    public static void example7() {

    }
    
    // example8 method definition
    public static void example8() {

    }

    // example9 method definition
    public static void example9() {

    }

}

Feline.java

// class header

    // private instance variable
    

    // constructors
    

    // methods
    

Cat.java

// class header


     // constructors




     // methods


Pastry.java

public class Pastry {
    // Constructor
    public Pastry() {
        System.out.println("Preparing a generic pastry.");
    }

    // Overloaded method #1
    public void order(int quantity) {
        System.out.println("Ordered " + quantity + " pastries.");
    }

    // Overloaded method #2
    public void order(int quantity, boolean pickup) {
        if (pickup) {
            System.out.println("Ordered " + quantity + " pastries for pickup.");
        } else {
            System.out.println("Ordered " + quantity + " pastries for delivery.");
        }
    }

    // Overloaded method #3
    public void order(int quantity, boolean pickup, String specialInstructions) {
        System.out.println("Ordered " + quantity + " pastries with instructions: " + specialInstructions + (pickup ? ", for pickup." : ", for delivery."));
    }
}

Cake.java

public class Cake extends Pastry {
    // Constructor
    public Cake() {
        System.out.println("A cake is being prepared.");
    }

    // Overriding the order method
    public void order(int quantity) {
        // Custom behavior for ordering cakes
        System.out.println("Ordered " + quantity + " cakes with beautiful decorations.");
    }

}

Cookie.java

class Cookie extends Pastry {
    // Method Overriding in the subclass
    @Override
    public void order(int quantity) {
        
    }

    @Override
    public void order(int quantity, boolean pickup) {

    }

    @Override
    public void order(int quantity, boolean pickup, String specialInstructions) {

    }
}

SmartDevice.java

public class SmartDevice {
    public void turnOn() {
        System.out.println("Device is powering on.");
    }

    public void turnOff() {
        System.out.println("Device is powering off.");
    }
}

SmartLight.java

public class SmartLight extends SmartDevice {
    @Override
    public void turnOn() {
        super.turnOn();  // Use the general turn-on method
        System.out.println("The light brightens gradually to the set level.");
    }

    @Override
    public void turnOff() {
        super.turnOff();  // Use the general turn-off method
        System.out.println("The light dims out slowly.");
    }
}

Shopping.java

import java.util.*;

/**
 * The ShoppingCart class has an ArrayList of Items.
 * You will write a new class DiscountedItem that extends Item.
 */

public class Shopping {
    public static void main(String[] args) {
        ShoppingCart cart = new ShoppingCart();
 
    }
}

// DiscountedItem inherits from Item
class DiscountedItem extends Item {
    // add an instance variable for the discount
    

    // Add constructors that call the super constructor
    

    // Override getPrice to return the price after discount
    

    // Override the toString() method to include the discount
    
    
}

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

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

    public void add(Item i) {
        order.add(i);
        total += i.getPrice();
    }

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

    public String toString() {
        return orderToString() + "\nTotal: " + valueToString(total);
    }

    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;
    }
}

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);
    }
}
PreviousSource CodeNextSource Code

Last updated 10 months ago