Week 19

Source Code

import java.util.*;

public class Week19 {
    public static void main(String[] args) {
        String myName = ""; // add your name
        System.out.println("Week 19" + " - " + 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 "E4":
                System.out.println("Example 4");
                example4();
                break;
            case "E5":
                System.out.println("Example 5");
                example5();
                break;
            case "E6":
                System.out.println("Example 6");
                example6();
                break;
            case "E7":
                System.out.println("Example 7");
                example7();
                break;
            case "Q":
                System.out.println("Quitting..!");
                break;
        }
    }

    // example1 method
    public static void example1() {
        // create a 2D array named ticketInfo
        

        // print the number of rows and columns
        

    }

    // example2 method
    public static void example2() {

        // create 2D array named seatingChart
        

        // assign the array elements
        

        // print the elements
        

    }

    // example3 method
    public static void example3() {
        // 2D array with an initializer list
        // Store A: Apple price, Banana price
        // Store B: Apple price, Banana price
        

        // Print the prices
        
    }

    // example4 method
    public static void example4() {
        // create a 3x4 matrix with values
        int[][] matrix = {
                { 1, 2, 3, 4 },
                { 5, 6, 7, 8 },
                { 9, 10, 11, 12 }
        };

        // nested for loop
        // Iterates through rows
        
            // Iterates through columns
            

        // enhanced for loop
        

    }

    // example5 method
    public static void example5() {
        // declare 2D array of numbers
        int[][] numbers = {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 }
        };

        // accumulate the elements
        
        

        // print the sum
        

        // calculate the average
        

    }

    // example6 method
    public static void example6() {
        // declare 2D array of numbers
        int[][] numbers = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        // initialize sum, rows and cols
        

        // nested for loops
        
                // Add the element if it is on the first/last row or first/last column
                

        // print the sum
        
    }

    // example7 method
    public static void example7() {
        // declare 2D array of numbers
        int[][] data = {
            { 12, 35, 7 },
            { 22, 18, 90 },
            { 42, 67, 55 }
        };

        // initialize max to the first element
        

        // print the max
        

    }
}



W19Problem1.java

import java.util.*;

public class W19Problem1
{
    public static void main(String[] args)
    {
        System.out.println("Problem 1");
        Scanner in = new Scanner(System.in);

        
        
    }
}

W19Problem2.java

import java.util.*;

public class W19Problem2
{
    public static void main(String[] args)
    {
        System.out.println("Problem 2");
        Scanner in = new Scanner(System.in);

        
        
    }
}

W19Problem3.java

import java.util.*;

public class W19Problem3
{
    public static void main(String[] args)
    {
        System.out.println("Problem 3");
        Scanner in = new Scanner(System.in);

        
        
    }
}

W19Problem4.java

import java.util.*;

public class W19Problem4
{
    // Method 1: Compute the sum of the primary diagonal
    public static int diagonalSum(int[][] matrix) {
        Scanner in = new Scanner(System.in);
        System.out.println("1: Right to Left");
        System.out.println("2: Left to Right");
        System.out.print("Choice: ");
        int direction = in.nextInt();
        in.nextLine();
        // TODO: Implement logic to sum the diagonal elements
        
        
        return 0;  // Placeholder return value
    }

    // Method 2: Compute the sum of all elements in a given row
    public static int rowSum(int[][] matrix, int rowIndex) {
        // TODO: Implement logic to sum the elements of the given row
        
        
        return 0;  // Placeholder return value
    }

    // Method 3: Compute the sum of all elements in a given column
    public static int columnSum(int[][] matrix, int colIndex) {
        // TODO: Implement logic to sum the elements of the given column
        
        
        return 0;  // Placeholder return value
    }

    public static void main(String[] args) {
        int[][] matrix = {
            {4, 7, 1, 8, 2},
            {9, 5, 6, 3, 7},
            {2, 6, 8, 4, 1},
            {1, 3, 9, 7, 5},
            {5, 2, 4, 6, 8}
        };

        // Test diagonal sum
        System.out.println("Diagonal Sum: " + diagonalSum(matrix));

        // Test row sum (for example, row index 2)
        System.out.println("Sum of row 2: " + rowSum(matrix, 2));

        // Test column sum (for example, column index 3)
        System.out.println("Sum of column 3: " + columnSum(matrix, 3));
    }
}

Candy.java

// Represents a single piece of candy
public class Candy {
    private String flavor;

    /** Constructor to initialize candy with a specific flavor */
    public Candy(String flavor) {
        this.flavor = flavor;
    }

    /** Returns the flavor of the candy */
    public String getFlavor() {
        return flavor;
    }

    @Override
    public String toString() {
        return flavor; // Useful for debugging
    }
}

BoxOfCandy.java

// Represents a box of candy in a 2D array grid
public class BoxOfCandy {
    private Candy[][] box; // 2D array of Candy objects or null

    /** Constructor to initialize the box */
    public BoxOfCandy(Candy[][] box) {
        this.box = box;
    }

    /**
     * Moves one piece of candy in column col, if necessary and possible,
     * so that the box element in row 0 of column col contains a piece of candy.
     * Returns false if there is no piece of candy in column col and returns true
     * otherwise.
     */
    public boolean moveCandyToFirstRow(int col) {
        // Step 1: Check if there is already a candy in the first row of this column.
        

        // Step 2: Search the column from row 1 downwards to find the first candy.
        
                // Step 3: Move the found candy to the first row
                

                // Step 4: Remove the original reference to avoid duplication
                

                // Successfully moved the candy
                
            
        

        // Step 5: If no candy was found in the column, return false.
        return false;
    }

    /**
     * Removes from box and returns a piece of candy with the specified flavor,
     * or returns null if no such piece is found.
     */
    public Candy removeNextByFlavor(String flavor) {
        // Step 1: Traverse rows from the bottom-most row to the top-most row.
        
            // Step 2: Traverse each row from left to right.
            
                // Step 3: Check if this cell contains a Candy object and if the flavor matches.
                
                    // Step 4: Store the candy reference before removing it.
                    

                    // Step 5: Remove candy from the box by setting the reference to null.
                   

                    // Step 6: Return the removed candy.
                    
                
            
        

        // Step 7: If no matching candy was found, return null.
        return null;
    }

    /** Helper method to print the box for testing */
    public void printBox() {
        for (int i = 0; i < box.length; i++) {
            for (int j = 0; j < box[i].length; j++) {
                if (box[i][j] == null) {
                    System.out.print("[  ] ");
                } else {
                    System.out.print("[" + box[i][j].getFlavor().substring(0, 2) + "] ");
                }
            }
            System.out.println();
        }
        System.out.println();
    }

    /** Main method to test functionality */
    public static void main(String[] args) {
        Candy[][] testBox = {
                { null, new Candy("Caramel"), null, new Candy("Mint") },
                { null, null, new Candy("Strawberry"), null },
                { new Candy("Chocolate"), new Candy("Lime"), null, new Candy("Vanilla") }
        };

        BoxOfCandy box = new BoxOfCandy(testBox);

        System.out.println("Initial Box:");
        box.printBox();

        System.out.println("Moving candy to first row in column 2...");
        box.moveCandyToFirstRow(2);
        box.printBox();

        System.out.println("Removing candy with flavor 'Lime'...");
        Candy removed = box.removeNextByFlavor("Lime");
        System.out.println("Removed: " + (removed != null ? removed.getFlavor() : "None"));
        box.printBox();

        System.out.println("Removing candy with flavor 'Chocolate'...");
        removed = box.removeNextByFlavor("Chocolate");
        System.out.println("Removed: " + (removed != null ? removed.getFlavor() : "None"));
        box.printBox();
    }
}

Location.java

public class Location {
    private int theRow;
    private int theCol;

    public Location(int r, int c) {
        theRow = r;
        theCol = c;
    }

    public int getRow() {
        return theRow;
    }

    public int getCol() {
        return theCol;
    }
}

GridPath.java

public class GridPath {
    /** Initialized in the constructor with distinct values that never change */
    private int[][] grid;

    /** Constructor to initialize the grid */
    public GridPath(int[][] gridData) {
        grid = gridData;
    }

    /**
     * Returns the Location representing a neighbor of the grid element at row and col,
     * as described in part (a)
     */
    public Location getNextLoc(int row, int col) {
        // TODO: Implement this method
        return null;
    }

    /**
     * Computes and returns the sum of all values on a path through grid, as described in
     * part (b)
     */
    public int sumPath(int row, int col) {
        // TODO: Implement this method
        return 0;
    }
}

GridPathTester.java

public class GridPathTester {
    public static void main(String[] args) {
        // Part A
        int[][] grid1 = {
                { 12, 3, 4, 13, 5 },
                { 11, 21, 2, 14, 16 },
                { 7, 8, 9, 15, 0 },
                { 10, 17, 20, 19, 1 },
                { 18, 22, 30, 25, 6 },
        };
        // Part B
        int[][] grid2 = {
                { 12, 30, 40, 25, 5 },
                { 11, 3, 22, 15, 43 },
                { 7, 2, 9, 4, 0 },
                { 8, 33, 18, 6, 1 },
        };
        

        GridPath gp = new GridPath(grid1);

        // Test getNextLoc
        System.out.println("Testing getNextLoc:");
        Location loc1 = gp.getNextLoc(0, 0);
        if (loc1 != null) {
            System.out.println("Next location from (0,0): " + loc1.getRow() + ", " + loc1.getCol());
        } else {
            System.out.println("getNextLoc(0,0) not implemented yet.");
        }

        Location loc2 = gp.getNextLoc(1, 3);
        if (loc2 != null) {
            System.out.println("Next location from (1,3): " + loc2.getRow() + ", " + loc2.getCol());
        } else {
            System.out.println("getNextLoc(1,3) not implemented yet.");
        }

        Location loc3 = gp.getNextLoc(2, 4);
        if (loc3 != null) {
            System.out.println("Next location from (2,4): " + loc3.getRow() + ", " + loc3.getCol());
        } else {
            System.out.println("getNextLoc(2,4) not implemented yet.");
        }

        Location loc4 = gp.getNextLoc(4, 3);
        if (loc4 != null) {
            System.out.println("Next location from (4,3): " + loc4.getRow() + ", " + loc4.getCol());
        } else {
            System.out.println("getNextLoc(4,3) not implemented yet.");
        }

        // Test sumPath
        System.out.println("\nTesting sumPath:");
        int sum = gp.sumPath(1, 1);
        if (sum != 0) {
            System.out.println("Sum from (1,1): " + sum);
        } else {
            System.out.println("sumPath(1,1) not implemented yet.");
        }
    }
}

TheaterManagement.java

import java.util.*;

/**
 * Provides functionalities to manage a theater's seating arrangement, including displaying the seating grid,
 * checking seat pricing, purchasing seats, calculating revenue, and determining the percentage of sold seats.
 */
public class TheaterManagement {

    /**
     * Main method serving as the entry point of the application. It initializes the seating array
     * and processes user inputs through a console-based menu using a do-while loop.
     *
     * @param args command line arguments (not used)
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        // Create theater seats and prices
        int[][] seats = {

        };

        do {
            System.out.println("G - Show Theater Grid");
            System.out.println("S - Seat Pricing");
            System.out.println("P - Purchase Seat");
            System.out.println("R - Revenue");
            System.out.println("C - Calculate Sold Seats Percentage");
            System.out.println("Q - Quit");
            System.out.print("Choice: ");
            String choice = in.nextLine().toUpperCase();
            switch (choice) {
                case "G":
                    showTheaterGrid(seats);
                    break;
                case "S":
                    showSeatPricing(seats, in);
                    break;
                case "P":
                    purchaseSeat(seats, in);
                    break;
                case "R":
                    showRevenue(seats);
                    break;
                case "C":
                    showSoldSeatsPercentage(seats);
                    break;
                case "Q":
                    return; // Exit the program
                default:
                    System.out.println("Invalid choice. Please try again.");
                    break;
            }
        } while (true);
    }

    /**
     * Displays the theater seating grid, indicating seat prices and marking purchased seats with an 'X'.
     *
     * @param seats The 2D array representing the theater seats and their prices or purchase statuses.
     */
    private static void showTheaterGrid(int[][] seats) {

    }

    /**
     * Allows the user to check the pricing of a specific seat by entering its row and column numbers.
     *
     * @param seats The 2D array representing the theater seats and their prices.
     * @param in The Scanner object for user input.
     */
    private static void showSeatPricing(int[][] seats, Scanner in) {

    }

    /**
     * Processes the purchase of a seat, marking it as sold (0) in the seating array if available.
     *
     * @param seats The 2D array representing the theater seats and their prices or purchase statuses.
     * @param in The Scanner object for user input.
     */
    private static void purchaseSeat(int[][] seats, Scanner in) {

    }

    /**
     * Calculates and displays the total revenue generated from the sold seats.
     *
     * @param seats The 2D array representing the theater seats and their prices or purchase statuses.
     */
    private static void showRevenue(int[][] seats) {

    }

    /**
     * Calculates and displays the percentage of seats that have been sold.
     *
     * @param seats The 2D array representing the theater seats and their prices or purchase statuses.
     */
    private static void showSoldSeatsPercentage(int[][] seats) {

    }
}

Last updated

Was this helpful?