package designingclasses2; import java.util.ArrayList; /** * * @author Mayelin */ public class DesigningClasses2 { public static void main(String[] args) { /****************************/ /* Static fields */ /****************************/ // create a bank account object BankAccount mikesAccount = new BankAccount(960.00, "Michael"); System.out.println("The interest rate is: " + BankAccount.getInterestRate()); mikesAccount.deposit(50); // Mike's balance should now be 1010.00 mikesAccount.withdraw(10); // Mike's balance should now be 1000.00 System.out.println("The balance in Mike's account before adding interest is: " + mikesAccount.getBalance()); mikesAccount.addMonthlyInterest(); // Mike's balance should now be 1050.00 System.out.println("The balance in Mike's account after adding interest is: " + mikesAccount.getBalance()); // create a second account BankAccount marysAccount = new BankAccount(1000.00, "Mary"); // changing the value of the interest rate BankAccount.setInterestRate(7.8); System.out.println("The balance in Mary's account before adding interest is: " + marysAccount.getBalance()); marysAccount.addMonthlyInterest(); // Mary's balance should now be 1078.00 System.out.println("The balance in Mary's account after adding interest is: " + marysAccount.getBalance()); /**********************************/ /* copy, equals, toString methods */ /**********************************/ // Create an Instructor object. Instructor instructor1 = new Instructor("Kramer", "Shawn", "RH3010"); Instructor instructor2 = instructor1.copy(); System.out.println("Are the two variables referencing the same object? " + (instructor1 == instructor2) ); System.out.println("Is the data in the two objects the same? " + instructor1.equals(instructor2) ); instructor2.setOfficeNumber("ECS231"); System.out.println(instructor1 + "\n"); System.out.println(instructor2 + "\n"); System.out.println("Is the data in the two objects the same? " + instructor1.equals(instructor2) ); /****************************/ /* Copy Constructors */ /****************************/ // Create a TextBook object. TextBook textBook1 = new TextBook("Starting Out with Java", "Gaddis", "Scott/Jones"); // Create another TextBook object using the copy constructor TextBook textBook2 = new TextBook(textBook1); System.out.println("Are the two variables referencing the same object? " + (textBook1 == textBook2) ); System.out.println("Is the data in the two objects the same? " + textBook1.equals(textBook2) ); /****************************/ /* Aggregation */ /****************************/ Student student_1 = new Student("John"); Student student_2 = new Student("Mary"); Student student_3 = new Student("Bob"); ArrayList students = new ArrayList<>(); students.add(student_1); students.add(student_2); students.add(student_3); // Create a Course object. Course myCourse = new Course("Intro to Java", instructor1, textBook1, students); // making changes to instructor1, textBook1, or students shouldn't affect the course object instructor1.setFirstName("Michael"); textBook1.setTitle("Blah blah."); students.remove(1); // Create a copy of the course object Course myOtherCourse = new Course(myCourse); // changing data for one Course object shouldn't affect the other myCourse.setInstructor(instructor2); myOtherCourse.setListOfStudents(students); System.out.println("\n\n" + myCourse); System.out.println("\n\n" + myOtherCourse); } }