CS 2421 Object Oriented Programming in Java
  • CS 2421 Object Oriented Programming in Java
  • Module 1
    • Presentations
    • Lecture Videos
    • Source Code
    • Assignment
  • Module 2
    • Presentations
    • Lecture Videos
    • Source Code
    • Assignment
  • Module 3
    • Presentations
    • Lecture Videos
    • Source Code
    • Assignment
  • Module 4
    • Presentations
    • Lecture Videos
    • Source Code
    • Assignment
  • Module 5
    • Presentations
    • Videos
    • Source Code
    • Assignment
  • Module 6
    • Presentations
    • Videos
    • Source Code
    • Assignment
Powered by GitBook
On this page
  • M6Assignment1.java
  • M6Assignment2.java
  • M6Assignment3.java
  1. Module 6

Assignment

M6Assignment1.java

public class M6Assignment1 {
    public static void main(String[] args) {
        int[] numbers = {10, 5, 0}; // Array with a zero for division
        System.out.println("Attempting to divide...");

        // Task 1: Wrap this code with a try-catch block to handle arithmetic exceptions.
        int result = divideNumbers(numbers[0], numbers[2]);
        System.out.println("Result: " + result);

        // Task 2: Handle potential array index out of bounds exception.
        printElement(numbers, 5);

        // Task 3: Add a finally block to announce the end of division attempts.
    }

    public static int divideNumbers(int numerator, int denominator) throws InvalidDivisionException {
        if (denominator == 0) {
            throw new InvalidDivisionException("Cannot divide by zero.");
        }
        return numerator / denominator; // This can throw an ArithmeticException if denominator is zero.
    }

    public static void printElement(int[] array, int index) {
        System.out.println("Element at index " + index + " is: " + array[index]); // This can throw an ArrayIndexOutOfBoundsException.
    }
}

class InvalidDivisionException extends Exception {
    public InvalidDivisionException(String message) {
        super(message);
    }
}

M6Assignment2.java

public class M6Assignment2 {
    private double balance;

    // Constructor initializes the bank account with a starting balance
    public BankAccount(double initialBalance) {
        balance = initialBalance;
    }

    // Deposits money into the bank account
    public void deposit(double amount) {
        if (amount < 0) {
            throw new IllegalArgumentException("Deposit amount cannot be negative.");
        }
        balance += amount;
    }

    // Withdraws money from the bank account
    public void withdraw(double amount) throws InsufficientFundsException {
        if (amount > balance) {
            throw new InsufficientFundsException("Insufficient funds for this withdrawal.");
        }
        balance -= amount;
    }

    // Returns the current balance
    public double getBalance() {
        return balance;
    }

    // main method
    public static void main(String[] args) {
        BankAccount account = new BankAccount(1000);

        // Task 1: Handle exceptions for deposit and withdrawal operations.
        // Task 2: Implement the transaction history feature.
        
        
    }
}

class InsufficientFundsException extends Exception {
    public InsufficientFundsException(String message) {
        super(message);
    }
}

M6Assignment3.java

import java.util.*;

public class M6Assignment3 {
    public static void main(String[] args) {
        System.out.println("Module 6 Assignment 2");
        // Create new Scanner object (don't delete!)
        Scanner in = new Scanner(System.in);
        // menu variables
        boolean done = false;
        String choice;
        // do while menu loop
        do {
            System.out.println("P1 - Problem 1");
            System.out.println("P2 - Problem 2");
            System.out.println("P3 - Problem 3");
            System.out.println("P4 - Problem 4");
            System.out.println("P5 - Problem 5");
            System.out.println("Q - Quit");
            System.out.print("Choice: ");
            choice = in.nextLine();
            switch (choice) {
                case "P1":
                    System.out.println("Problem 1");
                    
                    break;
                case "P2":
                    System.out.println("Problem 2");
                    
                    break;
                case "P3":
                    System.out.println("Problem 3");

                    break;
                case "P4":
                    System.out.println("Problem 4");

                    break;
                case "P5":
                    System.out.println("Problem 5");

                    break;
                // quit and default cases
                case "Q":
                    System.out.println("Quit");
                    done = true;
                    break;
                default:
                    System.out.println("Invalid Choice");
                    break;
            }
        } while (!done);

        // close the Scanner (don't delete!)
        in.close();
    }
    
    
            
}
PreviousSource Code

Last updated 1 year ago