├── LICENSE ├── README.md ├── resources ├── COMP90041 Week1 - 6 Summary.pdf ├── Student Feedback.png └── readings.md ├── tutorial 1 ├── code │ ├── HmwSolution.java │ └── WspSolution.java └── comp90041_tute1Slides.pdf ├── tutorial 10 ├── code │ ├── ExceptionDemo.java │ ├── LoginFailException.java │ └── Student.java └── comp90041_tute10.pdf ├── tutorial 11 ├── code │ ├── Tute11.java │ ├── Tute11Solution2.java │ └── demo │ │ ├── GenericDemo.java │ │ ├── GenericPairDemo.java │ │ ├── GenericPairDemo2.java │ │ ├── Pair.java │ │ ├── TwoTypePair.java │ │ └── TwoTypePairDemo.java └── comp90041_tute11.pdf ├── tutorial 2 ├── code │ ├── Hmk2Solution.java │ └── Wsp2Solution.java └── comp90041_tute2.pdf ├── tutorial 3 ├── code │ ├── HwkSolution.java │ ├── Wsp3Solution1.java │ └── Wsp3Solution2.java └── comp90041_tute3.pdf ├── tutorial 4 ├── code │ ├── Hmw4Solution.java │ ├── Wks4Solution.java │ └── demo │ │ ├── Dog.java │ │ └── OverloadingExample.java └── comp90041_tute4.pdf ├── tutorial 5 ├── code │ ├── Character.java │ ├── Movie.java │ └── demo │ │ ├── ConstructorsDemo.java │ │ ├── Date.java │ │ ├── DateFifthTry.java │ │ ├── DateFourthTry.java │ │ ├── EqualsAndToStringDemo.java │ │ └── StringImmutability.java └── comp90041_tute5.pdf ├── tutorial 6 ├── code │ ├── Character.java │ ├── CinemaDemo.java │ ├── Movie.java │ └── demo │ │ ├── Date.java │ │ ├── Person.java │ │ ├── PersonDemo.java │ │ └── Player.java └── comp90041_tute6.pdf ├── tutorial 7 ├── code │ ├── Student.java │ ├── Subject.java │ └── demo │ │ └── Demo.java ├── comp90041_tute7.pdf └── readings │ └── java-arrays.pdf ├── tutorial 8 ├── code │ ├── Boot.java │ ├── Demo.java │ ├── DressShoe.java │ ├── HeelType.java │ ├── Shoe.java │ ├── TennisShoe.java │ ├── flyer │ │ ├── AirPlane.java │ │ ├── Airliner.java │ │ ├── Bird.java │ │ ├── Demo.java │ │ ├── Flyer.java │ │ ├── Hummingbird.java │ │ ├── Kiwi.java │ │ └── PrivatePlane.java │ └── hrSystem │ │ ├── Date.java │ │ ├── Employee.java │ │ ├── EnhancedStringTokenizer.java │ │ ├── HourlyEmployee.java │ │ ├── InheritanceDemo.java │ │ ├── IsADemo.java │ │ ├── SalariedEmployee.java │ │ └── improvedEquals │ │ ├── Date.java │ │ ├── Employee.java │ │ ├── HourlyEmployee.java │ │ └── SalariedEmployee.java └── comp90041_tute8.pdf └── tutorial 9 ├── code ├── flyerA │ ├── AirPlane.java │ ├── Bird.java │ ├── Flyer.java │ └── diagram.uml └── flyerI │ ├── Animal.java │ ├── Bird.java │ └── Flyer.java └── comp90041_tute9.pdf /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Chuang Wang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # COMP90041 Programming and Software Development 2 | 3 | ### **:point_right:Purpose** 4 | --- 5 | This repository contains tutorial slides and lab sample solutions of the subject [COMP90041](https://handbook.unimelb.edu.au/2019/subjects/comp90041) Semester2 2019 given by Dr Thomas Christy at [University of Melbourne](https://www.unimelb.edu.au/). The repo also contains a list of useful resources for Java beginners. 6 | 7 | ### **:running:Author** 8 | --- 9 | The slides and materials are developed by [Chuang Wang](https://www.linkedin.com/in/chuangw/), tutor for COMP90041 Semester2 2019. 10 | 11 | ### **:clipboard:Course Info** 12 | --- 13 | The aims for this subject is for students to develop an understanding of approaches to solving moderately complex problems with computers, and to be able to demonstrate proficiency in designing and writing programs. The programming language used is Java. Please check the [handbook](https://handbook.unimelb.edu.au/2019/subjects/comp90041) for more information about the subject. 14 | 15 | ### **:bookmark_tabs:Contents** 16 | --- 17 | 18 | | Tutorial | Topic | Date | 19 | | ------ | ------ |-----| 20 | | [Tutorial 1](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/tutorial%201/comp90041_tute1Slides.pdf) | `Setting up environment and Programming Practice` | 5 Aug 2019 - 9 Aug 2019 21 | | [Tutorial 2](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/tutorial%202/comp90041_tute2.pdf) | `Data Types & Input/Output` | 12 Aug 2019 - 16 Aug 2019 22 | | [Tutorial 3](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/tutorial%203/comp90041_tute3.pdf) | `Flow of Control` | 19 Aug 2019 - 23 Aug 2019 23 | | [Tutorial 4](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/tutorial%204/comp90041_tute4.pdf) | `The Anatomy of Classes & Methods(Introduction)` | 26 Aug 2019 - 30 Aug 2019 24 | | [Tutorial 5](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/tutorial%205/comp90041_tute5.pdf) | `The Anatomy of Classes & Methods(Immutable Objects)` | 2 Sep 2019 - 6 Sep 2019 25 | | [Tutorial 6](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/tutorial%206/comp90041_tute6.pdf) | `The Anatomy of Classes & Methods(Mutable Objects)` | 9 Sep 2019 - 13 Sep 2019 26 | | [Tutorial 7](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/tutorial%207/comp90041_tute7.pdf) | `Arrays` | 16 Sep 2019 - 20 Sep 2019 27 | | [Tutorial 8](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/tutorial%208/comp90041_tute8.pdf) | `Inheritance` | 23 Sep 2019 - 27 Sep 2019 28 | | [Tutorial 9](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/tutorial%209/comp90041_tute9.pdf) | `Abstract class & Interfaces` | 7 Oct 2019 - 11 Oct 2019 29 | | [Tutorial 10](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/tutorial%2010/comp90041_tute10.pdf) | `Exception handling and Errors` | 14 Oct 2019 - 18 Oct 2019 30 | | [Tutorial 11](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/tutorial%2011/comp90041_tute11.pdf) | `Generics and ArrayList` | 21 Oct 2019 - 25 Oct 2019 31 | 32 | ### **:file_folder:Resources** 33 | --- 34 | - [A list of articles, blogs and books you might find useful! :thumbsup:](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/resources/readings.md) 35 | - [Week 1 - 6 Checklist](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/resources/COMP90041%20Week1%20-%206%20Summary.pdf) 36 | 37 | ### **:page_facing_up:Notes** 38 | --- 39 | >Feedbacks and comments are more than welcome!\ 40 | >Please contact the author. Thanks a lot! 41 | 42 | *The following comments on my teaching are from my students* 43 | ![](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/resources/Student%20Feedback.png) 44 | 45 | ### **:email: Contact Info** 46 | --- 47 | - :e-mail:: _chuangw1@unimelb.edu.au_ 48 | - [_LinkedIn_](https://www.linkedin.com/in/chuangw) 49 | 50 | ### **License** 51 | --- 52 | [![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/chuangw46/COMP90041_Tutorial/blob/master/LICENSE) 53 | -------------------------------------------------------------------------------- /resources/COMP90041 Week1 - 6 Summary.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/resources/COMP90041 Week1 - 6 Summary.pdf -------------------------------------------------------------------------------- /resources/Student Feedback.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/resources/Student Feedback.png -------------------------------------------------------------------------------- /resources/readings.md: -------------------------------------------------------------------------------- 1 | 2 | # A list of articles, blogs and books you might find useful :thumbsup: 3 | # **Table of Contents** 4 | 5 | - [**1. Always override `hashCode` when you override `equals`**](#1-always-override-hashcode-when-you-override-equals) 6 | - [**2. Java Arrays**](#2-java-arrays) 7 | - [**3. Java String**](#3-java-string) 8 | - [**4. Common data structure**](#4-common-data-structure) 9 | - [**5. UML**](#5-uml) 10 | - [**6. Primitive type & Reference type**](#6-primitive-type--reference-type) 11 | - [**7. Terminate a function VS Terminate the program**](#7-terminate-a-function-vs-terminate-the-program) 12 | 13 | 14 | 15 | 16 | # **Start here** 17 | ### **1. Always override `hashCode` when you override `equals`** 18 | --- 19 | 20 | - [Why do you have to override the equals and hashCode methods in Java?](https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java) 21 | - Further details available at Page 50 of the book [_Effective Java Third Edition_](https://www.oreilly.com/library/view/effective-java-3rd/9780134686097/), by Joshua Bloch 22 | ### **2. Java Arrays** 23 | --- 24 | - [Visualization of arrays](https://marcus-biel.com/java-arrays-enums/?cn-reloaded=1) 25 | - [Difference between array and enum](https://www.quora.com/What-is-the-difference-between-array-and-enum-in-Java) 26 | - Array, ArrayList, LinkedList and Vector [\[1\]](https://dzone.com/articles/arraylist-vs-linkedlist-vs)[\[2\]](https://stackoverflow.com/questions/40484657/arraylist-linkedlist-and-vector-which-one-is-the-best-for-adding-or-removing-th)[\[3\]](https://towardsdatascience.com/linked-lists-vs-arrays-78746f983267)[\[4\]](http://www.codenuclear.com/difference-between-arraylist-and-linkedlist-arraylist-vs-linkedlist/) 27 | 28 | ### **3. Java String** 29 | --- 30 | - [Difference](https://stackoverflow.com/questions/14757978/new-string-vs-literal-string-performance/14758013) between `String s = "hello";` VS `String s = new String("hello");` 31 | - [Immutability of Strings in Java](https://stackoverflow.com/questions/8798403/string-is-immutable-what-exactly-is-the-meaning) 32 | - [String, StringBuffer, and StringBuilder](https://stackoverflow.com/questions/2971315/string-stringbuffer-and-stringbuilder) 33 | - [Benefits of Immutable Data Structure](https://www.jworks.io/immutable-data-structures-in-java/) 34 | 35 | ### **4. Common data structure** 36 | --- 37 | - Common Data Structure [Operations and Complexity](https://www.bigocheatsheet.com/) 38 | - Java Collections Cheat Sheet [\[1\]](https://medium.com/@tushar0618/java-collections-cheat-sheet-3ee5c4fc11d7) [\[2\]](https://en.proft.me/2013/11/3/java-collection-framework-cheat-sheet/) 39 | 40 | ### **5. UML** 41 | --- 42 | - [An introduction to the Unified Modeling Language](https://developer.ibm.com/articles/an-introduction-to-uml/) 43 | - [The class diagram](https://developer.ibm.com/articles/the-class-diagram/) 44 | - [The component diagram](https://developer.ibm.com/articles/the-component-diagram/) 45 | - [The sequence diagram](https://developer.ibm.com/articles/the-sequence-diagram/) 46 | 47 | ### **6. Primitive type & Reference type** 48 | --- 49 | - [Difference](https://stackoverflow.com/questions/8790809/whats-the-difference-between-primitive-and-reference-types) between primitive and reference 50 | - [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) 51 | 52 | ### **7. Terminate a function VS Terminate the program** 53 | --- 54 | - `return;` statement is used inside a method to come out of it. It returns from the function to its caller and the program continues running. 55 | - `System.exit();` terminates the program; control does not return to the caller. 56 | - System.exit(0) is used in any method to come out of program. System.exit(0) terminates the program **normally**. 57 | - Whereas System.exit(1) terminates the program because of some **error** encountered in the program. 58 | 59 | 60 | ### **Author** 61 | --- 62 | The reading list is collected by Chuang Wang, tutor for COMP90041 Semester2 2019\ 63 | Chuang Wang LinkedIn:https://www.linkedin.com/in/chuangw 64 | 65 | 66 | ### **Notes** 67 | --- 68 | >Feedbacks and comments are more than welcome!\ 69 | >Please contact the author: chuangw1@unimelb.edu.au\ 70 | >Thanks a lot! 71 | 72 | 73 | ### **License** 74 | --- 75 | MIT 76 | -------------------------------------------------------------------------------- /tutorial 1/code/HmwSolution.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class HmwSolution { 8 | public static void main(String[] args) { 9 | String str = "Hello World!"; 10 | System.out.println(str); 11 | System.out.println(str.toUpperCase()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tutorial 1/code/WspSolution.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class WspSolution { 8 | public static void main(String[] args) { 9 | System.out.println("Chuang Wang"); 10 | System.out.println("123 Elizabeth St"); 11 | System.out.println("0123456789"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tutorial 1/comp90041_tute1Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 1/comp90041_tute1Slides.pdf -------------------------------------------------------------------------------- /tutorial 10/code/ExceptionDemo.java: -------------------------------------------------------------------------------- 1 | package tute10; 2 | 3 | import java.util.Scanner; 4 | 5 | /** 6 | * @author Chuang Wang 7 | * @institution University of Melbourne 8 | */ 9 | public class ExceptionDemo { 10 | public static void main(String[] args) { 11 | // TODO 1: try to call any of following methods you want and see what happens 12 | } 13 | 14 | /** 15 | * ArithmeticException example 16 | */ 17 | public static void divideByZero() { 18 | int numerator = 10; 19 | int denominator = 0; 20 | System.out.println(numerator / denominator); 21 | } 22 | 23 | /** 24 | * NullPointerException example 25 | */ 26 | public static void nullpointerException() { 27 | Student s = null; 28 | System.out.println(s.getId()); 29 | } 30 | 31 | /** 32 | * ArrayIndexOutOfBoundsException example 33 | */ 34 | public static void arrayIndexOutOfBounds() { 35 | String fruits[] = {"apple", "banana", "watermelon"}; 36 | 37 | System.out.println(fruits[3]); 38 | } 39 | 40 | /** 41 | * StackOverflowError example 42 | */ 43 | public static void badRecursive() { 44 | badRecursive(); 45 | } 46 | 47 | /** 48 | * InputMismatchException example 49 | */ 50 | public static void scannerDemo() { 51 | Scanner keyboard = new Scanner(System.in); 52 | System.out.println("Enter the number of laptops you want to order"); 53 | int numOfLaptops = keyboard.nextInt(); 54 | System.out.println("You have ordered " + numOfLaptops + " laptops"); 55 | } 56 | 57 | /** 58 | * this example is to illustrate how to use your custom exception class 59 | * TODO 2: try to modify it by deleting try-catch and throw the exception to a calling method and see what happens 60 | * @param inputPwd 61 | */ 62 | public static void logInExample(String inputPwd) { 63 | 64 | String username = "uni"; 65 | String correctPwd = "unimelb123"; 66 | try { 67 | if (inputPwd.equals(correctPwd)) { 68 | System.out.println(" yeah, correct pwd"); 69 | } else { 70 | throw new LoginFailException("sorry wrong password"); 71 | } 72 | } catch (LoginFailException e) { 73 | System.out.println("wrong pwd"); 74 | // the alternative way is 75 | // System.out.println(e.getMessage()); 76 | } 77 | 78 | 79 | System.out.println("test"); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /tutorial 10/code/LoginFailException.java: -------------------------------------------------------------------------------- 1 | package tute10; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class LoginFailException extends Exception { 8 | public LoginFailException(String msg) { 9 | super(msg); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tutorial 10/code/Student.java: -------------------------------------------------------------------------------- 1 | package tute10; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * @author Chuang Wang 7 | * @institution University of Melbourne 8 | */ 9 | public class Student { 10 | private int id; 11 | private String name; 12 | 13 | public Student(int id, String name) { 14 | this.id = id; 15 | this.name = name; 16 | } 17 | 18 | public int getId() { 19 | return id; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | @Override 27 | public boolean equals(Object o) { 28 | if (this == o) 29 | return true; 30 | if (o == null || getClass() != o.getClass()) 31 | return false; 32 | Student student = (Student) o; 33 | return id == student.id; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return Objects.hash(id); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tutorial 10/comp90041_tute10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 10/comp90041_tute10.pdf -------------------------------------------------------------------------------- /tutorial 11/code/Tute11.java: -------------------------------------------------------------------------------- 1 | package tute11; 2 | 3 | import java.util.ArrayList; 4 | import java.util.InputMismatchException; 5 | import java.util.List; 6 | import java.util.Scanner; 7 | 8 | /** 9 | * @author Chuang Wang 10 | * @institution University of Melbourne 11 | */ 12 | public class Tute11 { 13 | public static void main(String[] args) { 14 | List friends = new ArrayList<>(); 15 | 16 | Scanner keyboard = new Scanner(System.in); 17 | System.out.println("Which action would you like to perform?"); 18 | System.out.println("Type E for enter names"); 19 | System.out.println("Type D for delete a name"); 20 | System.out.println("Type Q for quit"); 21 | 22 | getOption(keyboard, friends); 23 | 24 | } 25 | 26 | private static void getOption(Scanner keyboard, List friends) { 27 | 28 | while (true){ 29 | String s = keyboard.nextLine(); 30 | if (s.equalsIgnoreCase("e")) { 31 | addNames(keyboard, friends); 32 | } else if (s.equalsIgnoreCase("d")) { 33 | deleteName(keyboard, friends); 34 | } else if (s.equalsIgnoreCase("q")) { 35 | System.exit(0); 36 | } else { 37 | System.out.println("Incorrect command, please try again"); 38 | } 39 | } 40 | } 41 | 42 | private static void addNames(Scanner keyboard, List friends) { 43 | System.out.println("please type names and type a blank line to terminate"); 44 | String name = keyboard.nextLine(); 45 | while (!name.isEmpty()){ 46 | friends.add(name); 47 | name = keyboard.nextLine(); 48 | } 49 | 50 | System.out.println("you have entered "); 51 | for (String s : friends){ 52 | System.out.println(friends.indexOf(s) + " " + s); 53 | } 54 | 55 | System.out.println("next action to perform"); 56 | } 57 | 58 | private static void deleteName(Scanner keyboard, List friends) { 59 | if (friends.size() == 0) { 60 | System.out.println("nothing to delete. please try to take another action"); 61 | } else { 62 | System.out.println("Please enter one of following index of the name to delete"); 63 | for (String s : friends) { 64 | System.out.println(friends.indexOf(s) + " " + s); 65 | } 66 | 67 | try { 68 | int i = keyboard.nextInt(); 69 | friends.remove(i); 70 | System.out.println("Removed successfully"); 71 | } catch (InputMismatchException e) { 72 | System.out.println("Sorry, please type an integer"); 73 | } catch (IndexOutOfBoundsException e) { 74 | System.out.println("Sorry, this index is out of bounds"); 75 | } 76 | } 77 | // remove the rest of the line; otherwise, "" will remain in the keyboard 78 | keyboard.nextLine(); 79 | 80 | System.out.println("next action to perform"); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /tutorial 11/code/Tute11Solution2.java: -------------------------------------------------------------------------------- 1 | package tute11; 2 | 3 | import java.util.ArrayList; 4 | import java.util.InputMismatchException; 5 | import java.util.List; 6 | import java.util.Scanner; 7 | 8 | /** 9 | * @author Chuang Wang 10 | * @institution University of Melbourne 11 | */ 12 | public class Tute11Solution2 { 13 | private static List friends = new ArrayList<>(); 14 | private static Scanner keyboard = new Scanner(System.in); 15 | 16 | public static void main(String[] args) { 17 | 18 | System.out.println("Which action would you like to perform?"); 19 | System.out.println("Type E for enter names"); 20 | System.out.println("Type D for delete a name"); 21 | System.out.println("Type Q for quit"); 22 | 23 | getOption(); 24 | 25 | } 26 | 27 | private static void getOption() { 28 | while (true){ 29 | String s = keyboard.nextLine(); 30 | if (s.equalsIgnoreCase("e")) { 31 | addNames(); 32 | } else if (s.equalsIgnoreCase("d")) { 33 | deleteName(); 34 | } else if (s.equalsIgnoreCase("q")) { 35 | System.exit(0); 36 | } else { 37 | System.out.println("Incorrect command, please try again"); 38 | } 39 | } 40 | } 41 | 42 | private static void addNames() { 43 | System.out.println("please type names and type a blank line to terminate"); 44 | String name = keyboard.nextLine(); 45 | while (!name.isEmpty()){ 46 | friends.add(name); 47 | name = keyboard.nextLine(); 48 | } 49 | 50 | System.out.println("you have entered "); 51 | for (String s : friends){ 52 | System.out.println(friends.indexOf(s) + " " + s); 53 | } 54 | 55 | System.out.println("next action to perform"); 56 | } 57 | 58 | private static void deleteName() { 59 | if (friends.size() == 0) { 60 | System.out.println("nothing to delete. please try to take another action"); 61 | } else { 62 | System.out.println("Please enter one of following index of the name to delete"); 63 | for (String s : friends) { 64 | System.out.println(friends.indexOf(s) + " " + s); 65 | } 66 | 67 | try { 68 | int i = keyboard.nextInt(); 69 | friends.remove(i); 70 | System.out.println("Removed successfully"); 71 | } catch (InputMismatchException e) { 72 | System.out.println("Sorry, please type an integer"); 73 | } catch (IndexOutOfBoundsException e) { 74 | System.out.println("Sorry, this index is out of bounds"); 75 | } 76 | } 77 | // remove the rest of the line; otherwise, "" will remain in the keyboard 78 | keyboard.nextLine(); 79 | 80 | System.out.println("next action to perform"); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tutorial 11/code/demo/GenericDemo.java: -------------------------------------------------------------------------------- 1 | package tute11.demo; 2 | 3 | import tute10.Student; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | /** 9 | * @author Chuang Wang 10 | * @institution University of Melbourne 11 | */ 12 | public class GenericDemo { 13 | public static void main(String[] args) { 14 | Student s1 = new Student(1, "frank"); 15 | Student s2 = new Student(2, "mark"); 16 | 17 | List list = new ArrayList<>(); 18 | 19 | list.add(s1); 20 | list.add(s2); 21 | System.out.println(list.size()); 22 | 23 | System.out.println(list.contains(s1)); 24 | 25 | list.remove(s2); 26 | System.out.println(list.size()); 27 | 28 | } 29 | 30 | public static void printOutArray(T[] array){ 31 | for (int i = 0; i < array.length; i++) { 32 | System.out.println(array[i]); 33 | } 34 | } 35 | 36 | public static void printOutArray(String[] array){ 37 | for (int i = 0; i < 2; i++) { 38 | System.out.println(array[i]); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /tutorial 11/code/demo/GenericPairDemo.java: -------------------------------------------------------------------------------- 1 | package tute11.demo; 2 | 3 | import java.util.Scanner; 4 | 5 | public class GenericPairDemo { 6 | public static void main(String[] args) { 7 | 8 | Pair secretPair = new Pair<>("Happy", "Day"); 9 | 10 | Scanner keyboard = new Scanner(System.in); 11 | System.out.println("Enter two words:"); 12 | 13 | String word1 = keyboard.next(); 14 | String word2 = keyboard.next(); 15 | 16 | Pair inputPair = new Pair<>(word1, word2); 17 | 18 | 19 | if (inputPair.equals(secretPair)) { 20 | System.out.println("You guessed the secret words"); 21 | System.out.println("in the correct order!"); 22 | } else { 23 | System.out.println("You guessed incorrectly."); 24 | System.out.println("You guessed"); 25 | System.out.println(inputPair); 26 | System.out.println("The secret words are"); 27 | System.out.println(secretPair); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tutorial 11/code/demo/GenericPairDemo2.java: -------------------------------------------------------------------------------- 1 | package tute11.demo; 2 | 3 | import java.util.Scanner; 4 | 5 | public class GenericPairDemo2 6 | { 7 | public static void main(String[] args) 8 | { 9 | Pair secretPair = 10 | new Pair(42, 24); 11 | 12 | Scanner keyboard = new Scanner(System.in); 13 | System.out.println("Enter two numbers:"); 14 | int n1 = keyboard.nextInt(); 15 | int n2 = keyboard.nextInt(); 16 | Pair inputPair = 17 | new Pair(n1, n2); 18 | if (inputPair.equals(secretPair)) 19 | { 20 | System.out.println("You guessed the secret numbers"); 21 | System.out.println("in the correct order!"); 22 | } 23 | else 24 | { 25 | System.out.println("You guessed incorrectly."); 26 | System.out.println("You guessed"); 27 | System.out.println(inputPair); 28 | System.out.println("The secret numbers are"); 29 | System.out.println(secretPair); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tutorial 11/code/demo/Pair.java: -------------------------------------------------------------------------------- 1 | package tute11.demo; 2 | 3 | public class Pair { 4 | private T first; 5 | private T second; 6 | 7 | public Pair(T firstItem, T secondItem) { 8 | first = firstItem; 9 | second = secondItem; 10 | } 11 | 12 | public void setFirst(T newFirst) { 13 | first = newFirst; 14 | } 15 | 16 | public void setSecond(T newSecond) { 17 | second = newSecond; 18 | } 19 | 20 | public T getFirst() { 21 | return first; 22 | } 23 | 24 | public T getSecond() { 25 | return second; 26 | } 27 | 28 | public String toString() { 29 | return ("first: " + first.toString() + "\n" 30 | + "second: " + second.toString()); 31 | } 32 | 33 | public boolean equals(Object otherObject) { 34 | if (otherObject == null) 35 | return false; 36 | else if (getClass() != otherObject.getClass()) 37 | return false; 38 | else { 39 | Pair otherPair = (Pair) otherObject; 40 | return (first.equals(otherPair.first) 41 | && second.equals(otherPair.second)); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tutorial 11/code/demo/TwoTypePair.java: -------------------------------------------------------------------------------- 1 | package tute11.demo; 2 | 3 | public class TwoTypePair { 4 | private T1 first; 5 | private T2 second; 6 | 7 | public TwoTypePair() 8 | { 9 | first = null; 10 | second = null; 11 | } 12 | 13 | public TwoTypePair(T1 firstItem, T2 secondItem) 14 | { 15 | first = firstItem; 16 | second = secondItem; 17 | } 18 | 19 | public void setFirst(T1 newFirst) 20 | { 21 | first = newFirst; 22 | } 23 | 24 | public void setSecond(T2 newSecond) 25 | { 26 | second = newSecond; 27 | } 28 | 29 | public T1 getFirst() 30 | { 31 | return first; 32 | } 33 | 34 | public T2 getSecond() 35 | { 36 | return second; 37 | } 38 | 39 | public String toString() 40 | { 41 | return ( "first: " + first.toString() + "\n" 42 | + "second: " + second.toString() ); 43 | } 44 | 45 | public boolean equals(Object otherObject) 46 | { 47 | if (otherObject == null) 48 | return false; 49 | else if (getClass( ) != otherObject.getClass( )) 50 | return false; 51 | else 52 | { 53 | TwoTypePair otherPair = (TwoTypePair)otherObject; 54 | return (first.equals(otherPair.first) 55 | && second.equals(otherPair.second)); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tutorial 11/code/demo/TwoTypePairDemo.java: -------------------------------------------------------------------------------- 1 | package tute11.demo; 2 | 3 | import java.util.Scanner; 4 | 5 | public class TwoTypePairDemo { 6 | public static void main(String[] args) { 7 | TwoTypePair rating = new TwoTypePair<>("avengers", 8); 8 | 9 | Scanner keyboard = new Scanner(System.in); 10 | System.out.println("Our current rating for " + rating.getFirst()); 11 | System.out.println(" is " + rating.getSecond()); 12 | 13 | System.out.println("How would you rate them?"); 14 | int score = keyboard.nextInt(); 15 | rating.setSecond(score); 16 | 17 | System.out.println("Our new rating for " + rating.getFirst()); 18 | System.out.println(" is " + rating.getSecond()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tutorial 11/comp90041_tute11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 11/comp90041_tute11.pdf -------------------------------------------------------------------------------- /tutorial 2/code/Hmk2Solution.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @author Chuang Wang 4 | * @institution University of Melbourne 5 | */ 6 | public class Hmk2Solution { 7 | public static void main(String[] args) { 8 | int commandLength = args.length; 9 | int i; 10 | for (i = 0; i < commandLength; i++){ 11 | if (args[i].startsWith("\"") && args[i].endsWith("\"")){ 12 | System.out.println("the result is "); 13 | System.out.println("word " + args[i] + " at index " + i); 14 | break; 15 | } 16 | } 17 | 18 | if (i >= commandLength){ 19 | System.out.println("No such word"); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tutorial 2/code/Wsp2Solution.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class Wsp2Solution { 8 | public static void main(String[] args) { 9 | // create a Scanner object called keyboard 10 | Scanner keyboard = new Scanner(System.in); 11 | /** 12 | * Reads the rest of the current keyboard input line 13 | * and returns the characters read as a value of type String. 14 | */ 15 | String userInput = keyboard.nextLine(); 16 | 17 | // print out the input length 18 | System.out.println(userInput.length()); 19 | 20 | // produce the array of string by splitting the string with blank delimiter " " 21 | String[] inputs = userInput.split(" "); 22 | 23 | // prints out just the 1st word of the user’s text 24 | System.out.println(inputs[0]); 25 | 26 | // prints all of the user’s input but the 1st word. 27 | for (int i = 1; i < inputs.length; i++){ 28 | System.out.print(inputs[i] + " "); 29 | } 30 | 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tutorial 2/comp90041_tute2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 2/comp90041_tute2.pdf -------------------------------------------------------------------------------- /tutorial 3/code/HwkSolution.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @author Chuang Wang 4 | * @institution University of Melbourne 5 | */ 6 | public class HwkSolution { 7 | public static void main(String[] args) { 8 | String print_line = ""; 9 | for (int i = 0; i <= 10; i++) { 10 | if (i == 0) { 11 | print_line = ""; 12 | for (int j = 0; j <= 10; j++) { 13 | if (j == 0) { 14 | print_line += " * "; 15 | } else { 16 | print_line += "|" + numWithSpace(j); 17 | } 18 | } 19 | System.out.println(print_line); 20 | } else { 21 | print_line = numWithSpace(i); 22 | for (int j = 1; j <= 10; j++) { 23 | print_line += "|" + numWithSpace(i * j); 24 | } 25 | System.out.println(print_line); 26 | } 27 | print_line = "-----"; 28 | for (int j = 0; j < 10; j++) 29 | print_line += "+-----"; 30 | System.out.println(print_line); 31 | } 32 | } 33 | 34 | private static String numWithSpace(int num) { 35 | if (num > 0 && num < 10) { 36 | return " " + num + " "; 37 | } else if (num >= 10 && num < 100) { 38 | return " " + num + " "; 39 | } else if (num == 100) { 40 | return " 100 "; 41 | } else 42 | return ""; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tutorial 3/code/Wsp3Solution1.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @author Chuang Wang 4 | * @institution University of Melbourne 5 | */ 6 | 7 | public class Wsp3Solution1 { 8 | public static void main(String[] args) { 9 | if (args.length == 1) { 10 | if (args[0].equals("N")) 11 | System.out.println("0"); 12 | else if (args[0].equals("S")) 13 | System.out.println("90"); 14 | else if (args[0].equals("E")) 15 | System.out.println("180"); 16 | else if (args[0].equals("W")) 17 | System.out.println("270"); 18 | else 19 | System.out.println("Invalid input"); 20 | } else { 21 | System.out.println("Invalid input"); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tutorial 3/code/Wsp3Solution2.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @author Chuang Wang 4 | * @institution University of Melbourne 5 | */ 6 | public class Wsp3Solution2 { 7 | public static void main(String[] args) { 8 | if (args.length == 1){ 9 | switch (args[0]){ 10 | case "N": 11 | System.out.println("0"); 12 | break; 13 | case "S": 14 | System.out.println("90"); 15 | break; 16 | case "E": 17 | System.out.println("180"); 18 | break; 19 | case "W": 20 | System.out.println("270"); 21 | break; 22 | default: 23 | System.out.println("Invalid input"); 24 | break; 25 | } 26 | } else { 27 | System.out.println("Invalid input"); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tutorial 3/comp90041_tute3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 3/comp90041_tute3.pdf -------------------------------------------------------------------------------- /tutorial 4/code/Hmw4Solution.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Arrays; 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class Hmw4Solution { 8 | //-------------- All instance variables ------------------- 9 | private static String[] names = {"AAA", "BBB", "CCC", "DDD"}; 10 | 11 | //-------------- All method definitions ------------------- 12 | public static void main(String[] args) { 13 | System.out.println(isAFavourite("BB")); 14 | printMovie("The Avengers,Iron Man,Thor"); 15 | } 16 | 17 | public static void printMovie() { 18 | for (int i = 0; i < names.length; i++) { 19 | System.out.println((i + 1) + ". " + names[i]); 20 | } 21 | } 22 | 23 | public static void printMovie(int rank, String name) { 24 | System.out.println(rank + ". " + name); 25 | } 26 | 27 | 28 | public static boolean isAFavourite(String name){ 29 | String list = Arrays.toString(names); 30 | return list.contains(name); 31 | } 32 | 33 | public static void printMovie(String movies) { 34 | String[] list = movies.split(","); 35 | for (String s : list){ 36 | System.out.println(s); 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /tutorial 4/code/Wks4Solution.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @author Chuang Wang 4 | * @institution University of Melbourne 5 | */ 6 | public class Wks4Solution { 7 | //-------------- All instance variables ------------------- 8 | private static String[] names = {"AAA", "BBB", "CCC", "DDD"}; 9 | 10 | //-------------- All method definitions ------------------- 11 | public static void main(String[] args) { 12 | printMovie(); 13 | System.out.println(); 14 | printMovie(1, "EEE"); 15 | } 16 | 17 | public static void printMovie() { 18 | for (int i = 0; i < names.length; i++) { 19 | System.out.println((i + 1) + ". " + names[i]); 20 | } 21 | } 22 | 23 | public static void printMovie(int rank, String name) { 24 | System.out.println(rank + ". " + name); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tutorial 4/code/demo/Dog.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @author Chuang Wang 4 | * @institution University of Melbourne 5 | */ 6 | public class Dog { 7 | //-------------- All instance variables ------------------- 8 | private String name; 9 | private int age; 10 | private String breed; 11 | 12 | //--------------------- Constructor ------------------------ 13 | public Dog(String name) { 14 | this.name = name; 15 | } 16 | 17 | //-------------- All method definitions ------------------- 18 | public void bark(){ 19 | System.out.println("The dog " + name + " is barking"); 20 | } 21 | 22 | public void wagTail(){ 23 | System.out.println("The dog is waging tail"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tutorial 4/code/demo/OverloadingExample.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class OverloadingExample { 8 | private static int sumOf(int a, int b) { 9 | return a + b; 10 | } 11 | 12 | static int sumOf(int a, int b, int c) { 13 | return a + b + c; 14 | } 15 | 16 | static double sumOf(double a, double b) { 17 | return a + b; 18 | } 19 | 20 | private static double sumOf(double a, double b, double c) { 21 | return a + b + c; 22 | } 23 | 24 | public static void main(String[] args) { 25 | System.out.println(sumOf(1, 2)); 26 | System.out.println(sumOf(10.5, 20.6, 30.7)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tutorial 4/comp90041_tute4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 4/comp90041_tute4.pdf -------------------------------------------------------------------------------- /tutorial 5/code/Character.java: -------------------------------------------------------------------------------- 1 | package tute5; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class Character { 8 | private final String characterName; 9 | private final String actorName; 10 | private final int rating; 11 | private final String movieName; 12 | 13 | public Character(String characterName, String actorName, int rating, String movieName) { 14 | this.characterName = characterName; 15 | this.actorName = actorName; 16 | this.rating = rating; 17 | this.movieName = movieName; 18 | } 19 | 20 | public String getCharacterName() { 21 | return characterName; 22 | } 23 | 24 | public String getActorName() { 25 | return actorName; 26 | } 27 | 28 | public int getRating() { 29 | return rating; 30 | } 31 | 32 | public String getMovieName() { 33 | return movieName; 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /tutorial 5/code/Movie.java: -------------------------------------------------------------------------------- 1 | package tute5; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class Movie { 8 | private final String title; 9 | private final int rank; 10 | private final int runTime; 11 | private final Character mainChar; 12 | 13 | public Movie(String title, int rank, int runTime, Character mainChar) { 14 | this.title = title; 15 | this.rank = rank; 16 | this.runTime = runTime; 17 | this.mainChar = mainChar; 18 | } 19 | 20 | public String getTitle() { 21 | return title; 22 | } 23 | 24 | public int getRank() { 25 | return rank; 26 | } 27 | 28 | public int getRunTime() { 29 | return runTime; 30 | } 31 | 32 | public Character getMainChar() { 33 | return mainChar; 34 | } 35 | 36 | public boolean equals(Movie movie) { 37 | return this.title.equals(movie.title) && 38 | this.rank == movie.rank && 39 | this.runTime == movie.runTime; 40 | } 41 | 42 | public String toString() { 43 | return rank + ". " + title; 44 | } 45 | 46 | // @Override 47 | // public boolean equals(Object o) { 48 | // if (this == o) 49 | // return true; 50 | // if (o == null || getClass() != o.getClass()) 51 | // return false; 52 | // Movie movie = (Movie) o; 53 | // return rank == movie.rank && 54 | // runTime == movie.runTime && 55 | // Objects.equals(title, movie.title); 56 | // } 57 | // 58 | // @Override 59 | // public int hashCode() { 60 | // return Objects.hash(title, rank, runTime); 61 | // } 62 | } 63 | -------------------------------------------------------------------------------- /tutorial 5/code/demo/ConstructorsDemo.java: -------------------------------------------------------------------------------- 1 | package tute5.demo; 2 | 3 | public class ConstructorsDemo 4 | { 5 | public static void main(String[] args) 6 | { 7 | Date date1 = new Date("December", 16, 1770), 8 | date2 = new Date(1, 27, 1756), 9 | date3 = new Date(1882), 10 | date4 = new Date( ); 11 | 12 | 13 | System.out.println("Whose birthday is " + date1 + "?"); 14 | System.out.println("Whose birthday is " + date2 + "?"); 15 | System.out.println("Whose birthday is " + date3 + "?"); 16 | System.out.println("The default date is " + date4 + "."); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tutorial 5/code/demo/Date.java: -------------------------------------------------------------------------------- 1 | package tute5.demo; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Date { 6 | private String month; 7 | private int day; 8 | private int year; //a four digit number. 9 | 10 | public Date() { 11 | month = "January"; 12 | day = 1; 13 | year = 1000; 14 | } 15 | 16 | public Date(int monthInt, int day, int year) { 17 | setDate(monthInt, day, year); 18 | } 19 | 20 | public Date(String monthString, int day, int year) { 21 | setDate(monthString, day, year); 22 | } 23 | 24 | public Date(int year) { 25 | setDate(1, 1, year); 26 | } 27 | 28 | public Date(Date aDate) { 29 | if (aDate == null)//Not a real date. 30 | { 31 | System.out.println("Fatal Error."); 32 | System.exit(0); 33 | } 34 | 35 | month = aDate.month; 36 | day = aDate.day; 37 | year = aDate.year; 38 | } 39 | 40 | public void setDate(int monthInt, int day, int year) { 41 | if (dateOK(monthInt, day, year)) { 42 | this.month = monthString(monthInt); 43 | this.day = day; 44 | this.year = year; 45 | } else { 46 | System.out.println("Fatal Error"); 47 | System.exit(0); 48 | } 49 | } 50 | 51 | public void setDate(String monthString, int day, int year) { 52 | if (dateOK(monthString, day, year)) { 53 | this.month = monthString; 54 | this.day = day; 55 | this.year = year; 56 | } else { 57 | System.out.println("Fatal Error"); 58 | System.exit(0); 59 | } 60 | } 61 | 62 | public void setDate(int year) { 63 | setDate(1, 1, year); 64 | } 65 | 66 | public void setYear(int year) { 67 | if ((year < 1000) || (year > 9999)) { 68 | System.out.println("Fatal Error"); 69 | System.exit(0); 70 | } else 71 | this.year = year; 72 | } 73 | 74 | public void setMonth(int monthNumber) { 75 | if ((monthNumber <= 0) || (monthNumber > 12)) { 76 | System.out.println("Fatal Error"); 77 | System.exit(0); 78 | } else 79 | month = monthString(monthNumber); 80 | } 81 | 82 | public void setDay(int day) { 83 | if ((day <= 0) || (day > 31)) { 84 | System.out.println("Fatal Error"); 85 | System.exit(0); 86 | } else 87 | this.day = day; 88 | } 89 | 90 | public int getMonth() { 91 | if (month.equals("January")) 92 | return 1; 93 | else if (month.equals("February")) 94 | return 2; 95 | else if (month.equalsIgnoreCase("March")) 96 | return 3; 97 | else if (month.equalsIgnoreCase("April")) 98 | return 4; 99 | else if (month.equalsIgnoreCase("May")) 100 | return 5; 101 | else if (month.equals("June")) 102 | return 6; 103 | else if (month.equalsIgnoreCase("July")) 104 | return 7; 105 | else if (month.equalsIgnoreCase("August")) 106 | return 8; 107 | else if (month.equalsIgnoreCase("September")) 108 | return 9; 109 | else if (month.equalsIgnoreCase("October")) 110 | return 10; 111 | else if (month.equals("November")) 112 | return 11; 113 | else if (month.equals("December")) 114 | return 12; 115 | else { 116 | System.out.println("Fatal Error"); 117 | System.exit(0); 118 | return 0; //Needed to keep the compiler happy 119 | } 120 | } 121 | 122 | public int getDay() { 123 | return day; 124 | } 125 | 126 | public int getYear() { 127 | return year; 128 | } 129 | 130 | public String toString() { 131 | return (month + " " + day + ", " + year); 132 | } 133 | 134 | public boolean equals(Date otherDate) { 135 | return ((month.equals(otherDate.month)) 136 | && (day == otherDate.day) && (year == otherDate.year)); 137 | } 138 | 139 | public boolean precedes(Date otherDate) { 140 | return ((year < otherDate.year) || 141 | (year == otherDate.year && getMonth() < otherDate.getMonth()) || 142 | (year == otherDate.year && month.equals(otherDate.month) 143 | && day < otherDate.day)); 144 | } 145 | 146 | public void readInput() { 147 | boolean tryAgain = true; 148 | Scanner keyboard = new Scanner(System.in); 149 | while (tryAgain) { 150 | System.out.println("Enter month, day, and year."); 151 | System.out.println("Do not use a comma."); 152 | String monthInput = keyboard.next(); 153 | int dayInput = keyboard.nextInt(); 154 | int yearInput = keyboard.nextInt(); 155 | if (dateOK(monthInput, dayInput, yearInput)) { 156 | setDate(monthInput, dayInput, yearInput); 157 | tryAgain = false; 158 | } else 159 | System.out.println("Illegal date. Reenter input."); 160 | } 161 | } 162 | 163 | private boolean dateOK(int monthInt, int dayInt, int yearInt) { 164 | return ((monthInt >= 1) && (monthInt <= 12) && 165 | (dayInt >= 1) && (dayInt <= 31) && 166 | (yearInt >= 1000) && (yearInt <= 9999)); 167 | } 168 | 169 | private boolean dateOK(String monthString, int dayInt, int yearInt) { 170 | return (monthOK(monthString) && 171 | (dayInt >= 1) && (dayInt <= 31) && 172 | (yearInt >= 1000) && (yearInt <= 9999)); 173 | } 174 | 175 | private boolean monthOK(String month) { 176 | return (month.equals("January") || month.equals("February") || 177 | month.equals("March") || month.equals("April") || 178 | month.equals("May") || month.equals("June") || 179 | month.equals("July") || month.equals("August") || 180 | month.equals("September") || month.equals("October") || 181 | month.equals("November") || month.equals("December")); 182 | } 183 | 184 | private String monthString(int monthNumber) { 185 | switch (monthNumber) { 186 | case 1: 187 | return "January"; 188 | case 2: 189 | return "February"; 190 | case 3: 191 | return "March"; 192 | case 4: 193 | return "April"; 194 | case 5: 195 | return "May"; 196 | case 6: 197 | return "June"; 198 | case 7: 199 | return "July"; 200 | case 8: 201 | return "August"; 202 | case 9: 203 | return "September"; 204 | case 10: 205 | return "October"; 206 | case 11: 207 | return "November"; 208 | case 12: 209 | return "December"; 210 | default: 211 | System.out.println("Fatal Error"); 212 | System.exit(0); 213 | return "Error"; //to keep the compiler happy 214 | } 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /tutorial 5/code/demo/DateFifthTry.java: -------------------------------------------------------------------------------- 1 | package tute5.demo; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DateFifthTry 6 | { 7 | private String month; 8 | private int day; 9 | private int year; //a four digit number. 10 | 11 | 12 | public void writeOutput( ) 13 | { 14 | System.out.println(month + " " + day + ", " + year); 15 | } 16 | 17 | public void readInput( ) 18 | { 19 | boolean tryAgain = true; 20 | Scanner keyboard = new Scanner(System.in); 21 | while (tryAgain) 22 | { 23 | System.out.println("Enter month, day, and year"); 24 | System.out.println("as three integers:"); 25 | System.out.println("do not use commas or other punctuations."); 26 | int monthInput = keyboard.nextInt( ); 27 | int dayInput = keyboard.nextInt( ); 28 | int yearInput = keyboard.nextInt( ); 29 | if (dateOK(monthInput, dayInput, yearInput) ) 30 | { 31 | setDate(monthInput, dayInput, yearInput); 32 | tryAgain = false; 33 | } 34 | else 35 | System.out.println("Illegal date. Reenter input."); 36 | } 37 | } 38 | 39 | public void setDate(int month, int day, int year) 40 | { 41 | if (dateOK(month, day, year)) 42 | { 43 | this.month = monthString(month); 44 | this.day = day; 45 | this.year = year; 46 | } 47 | else 48 | { 49 | System.out.println("Fatal Error"); 50 | System.exit(0); 51 | } 52 | } 53 | 54 | public void setMonth(int monthNumber) 55 | { 56 | if ((monthNumber <= 0) || (monthNumber > 12)) 57 | { 58 | System.out.println("Fatal Error"); 59 | System.exit(0); 60 | } 61 | else 62 | month = monthString(monthNumber); 63 | } 64 | 65 | public void setDay(int day) 66 | { 67 | if ((day <= 0) || (day > 31)) 68 | { 69 | System.out.println("Fatal Error"); 70 | System.exit(0); 71 | } 72 | else 73 | this.day = day; 74 | } 75 | 76 | public void setYear(int year) 77 | { 78 | if ( (year < 1000) || (year > 9999) ) 79 | { 80 | System.out.println("Fatal Error"); 81 | System.exit(0); 82 | } 83 | else 84 | this.year = year; 85 | } 86 | 87 | public boolean equals(DateFifthTry otherDate) 88 | { 89 | return ( (month.equalsIgnoreCase(otherDate.month)) 90 | && (day == otherDate.day) && (year == otherDate.year) ); 91 | } 92 | public boolean precedes(DateFifthTry otherDate) 93 | { 94 | return ( (year < otherDate.year) || 95 | (year == otherDate.year && getMonth( ) < otherDate.getMonth( )) || 96 | (year == otherDate.year && month.equals(otherDate.month) 97 | && day < otherDate.day) ); 98 | } 99 | 100 | public String toString( ) 101 | { 102 | return (month + " " + day + ", " + year); 103 | } 104 | 105 | public int getDay( ) 106 | { 107 | return day; 108 | } 109 | 110 | public int getYear( ) 111 | { 112 | return year; 113 | } 114 | 115 | public int getMonth( ) 116 | { 117 | if (month.equalsIgnoreCase("January")) 118 | return 1; 119 | else if (month.equalsIgnoreCase("February")) 120 | return 2; 121 | else if (month.equalsIgnoreCase("March")) 122 | return 3; 123 | else if (month.equalsIgnoreCase("April")) 124 | return 4; 125 | else if (month.equalsIgnoreCase("May")) 126 | return 5; 127 | else if (month.equals("June")) 128 | return 6; 129 | else if (month.equalsIgnoreCase("July")) 130 | return 7; 131 | else if (month.equalsIgnoreCase("August")) 132 | return 8; 133 | else if (month.equalsIgnoreCase("September")) 134 | return 9; 135 | else if (month.equalsIgnoreCase("October")) 136 | return 10; 137 | else if (month.equalsIgnoreCase("November")) 138 | return 11; 139 | else if (month.equalsIgnoreCase("December")) 140 | return 12; 141 | else 142 | { 143 | System.out.println("Fatal Error"); 144 | System.exit(0); 145 | return 0; //Needed to keep the compiler happy 146 | } 147 | } 148 | 149 | 150 | private boolean dateOK(int monthInt, int dayInt, int yearInt) 151 | { 152 | return ( (monthInt >= 1) && (monthInt <= 12) && 153 | (dayInt >= 1) && (dayInt <= 31) && 154 | (yearInt >= 1000) && (yearInt <= 9999) ); 155 | } 156 | 157 | private String monthString(int monthNumber) 158 | { 159 | switch (monthNumber) 160 | { 161 | case 1: 162 | return "January"; 163 | case 2: 164 | return "February"; 165 | case 3: 166 | return "March"; 167 | case 4: 168 | return "April"; 169 | case 5: 170 | return "May"; 171 | case 6: 172 | return "June"; 173 | case 7: 174 | return "July"; 175 | case 8: 176 | return "August"; 177 | case 9: 178 | return "September"; 179 | case 10: 180 | return "October"; 181 | case 11: 182 | return "November"; 183 | 184 | case 12: 185 | return "December"; 186 | default: 187 | System.out.println("Fatal Error"); 188 | System.exit(0); 189 | return "Error"; //to keep the compiler happy 190 | } 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /tutorial 5/code/demo/DateFourthTry.java: -------------------------------------------------------------------------------- 1 | package tute5.demo; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DateFourthTry 6 | { 7 | private String month; 8 | private int day; 9 | private int year; //a four digit number. 10 | 11 | public String toString( ) 12 | { 13 | return (month + " " + day + ", " + year); 14 | } 15 | 16 | public void writeOutput( ) 17 | { 18 | System.out.println(month + " " + day + ", " + year); 19 | } 20 | 21 | 22 | 23 | public boolean equals(DateFourthTry otherDate) 24 | { 25 | return ( (month.equals(otherDate.month)) 26 | && (day == otherDate.day) && (year == otherDate.year) ); 27 | } 28 | 29 | public boolean precedes(DateFourthTry otherDate) 30 | { 31 | return ( (year < otherDate.year) || 32 | (year == otherDate.year && getMonth( ) < otherDate.getMonth( )) || 33 | (year == otherDate.year && month.equals(otherDate.month) 34 | && day < otherDate.day) ); 35 | } 36 | 37 | public void setDate(int newMonth, int newDay, int newYear) 38 | { 39 | month = monthString(newMonth); 40 | day = newDay; 41 | year = newYear; 42 | } 43 | 44 | public String monthString(int monthNumber) 45 | { 46 | switch (monthNumber) 47 | { 48 | case 1: 49 | return "January"; 50 | case 2: 51 | return "February"; 52 | case 3: 53 | return "March"; 54 | case 4: 55 | return "April"; 56 | case 5: 57 | return "May"; 58 | case 6: 59 | return "June"; 60 | case 7: 61 | return "July"; 62 | case 8: 63 | return "August"; 64 | case 9: 65 | return "September"; 66 | case 10: 67 | return "October"; 68 | case 11: 69 | return "November"; 70 | case 12: 71 | return "December"; 72 | default: 73 | System.out.println("Fatal Error"); 74 | System.exit(0); 75 | return "Error"; //to keep the compiler happy 76 | } 77 | } 78 | 79 | public void readInput( ) 80 | { 81 | Scanner keyboard = new Scanner(System.in); 82 | System.out.println("Enter month, day, and year."); 83 | System.out.println("Do not use a comma."); 84 | month = keyboard.next( ); 85 | day = keyboard.nextInt( ); 86 | year = keyboard.nextInt( ); 87 | } 88 | 89 | public int getDay( ) 90 | { 91 | return day; 92 | } 93 | 94 | public int getYear( ) 95 | { 96 | return year; 97 | } 98 | 99 | public int getMonth( ) 100 | { 101 | if (month.equalsIgnoreCase("January")) 102 | return 1; 103 | else if (month.equalsIgnoreCase("February")) 104 | return 2; 105 | else if (month.equalsIgnoreCase("March")) 106 | return 3; 107 | else if (month.equalsIgnoreCase("April")) 108 | return 4; 109 | else if (month.equalsIgnoreCase("May")) 110 | return 5; 111 | else if (month.equals("June")) 112 | return 6; 113 | else if (month.equalsIgnoreCase("July")) 114 | return 7; 115 | else if (month.equalsIgnoreCase("August")) 116 | return 8; 117 | else if (month.equalsIgnoreCase("September")) 118 | return 9; 119 | else if (month.equalsIgnoreCase("October")) 120 | return 10; 121 | else if (month.equalsIgnoreCase("November")) 122 | return 11; 123 | else if (month.equalsIgnoreCase("December")) 124 | return 12; 125 | else 126 | { 127 | System.out.println("Fatal Error"); 128 | System.exit(0); 129 | return 0; //Needed to keep the compiler happy 130 | } 131 | } 132 | 133 | } 134 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /tutorial 5/code/demo/EqualsAndToStringDemo.java: -------------------------------------------------------------------------------- 1 | package tute5.demo; 2 | 3 | public class EqualsAndToStringDemo 4 | { 5 | public static void main(String[] args) 6 | { 7 | DateFourthTry date1 = new DateFourthTry( ), 8 | date2 = new DateFourthTry( ); 9 | date1.setDate(6, 17, 1882); 10 | date2.setDate(6, 17, 1882); 11 | 12 | if (date1.equals(date2)) 13 | System.out.println(date1 + " equals " + date2); 14 | else 15 | System.out.println(date1 + " does not equal " + date2); 16 | 17 | date1.setDate(7, 28, 1750); 18 | 19 | if (date1.precedes(date2)) 20 | System.out.println(date1 + " comes before " + date2); 21 | else 22 | System.out.println(date2 + " comes before or is equal to " 23 | + date1); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tutorial 5/code/demo/StringImmutability.java: -------------------------------------------------------------------------------- 1 | package tute5.demo; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class StringImmutability { 8 | public static void main(String[] args) { 9 | String s1 = "cat"; 10 | String s2 = "cat"; 11 | String s3 = new String("cat"); 12 | String s4 = new String("cat"); 13 | System.out.println(s1.equals(s2)); // true 14 | System.out.println(s1 == s2); // true 15 | 16 | System.out.println(s1.equals(s3)); // true 17 | System.out.println(s1 == s3); // false 18 | 19 | System.out.println(s3.equals(s4)); // true 20 | System.out.println(s3 == s4); // false 21 | 22 | String b1 = "hello"; 23 | System.out.println(b1.concat(" frank")); // hello frank 24 | System.out.println(b1); // hello 25 | System.out.println(b1 + " frank"); // hello frank 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tutorial 5/comp90041_tute5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 5/comp90041_tute5.pdf -------------------------------------------------------------------------------- /tutorial 6/code/Character.java: -------------------------------------------------------------------------------- 1 | package tute6; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class Character { 8 | private String characterName; 9 | private String actorName; 10 | private int rating; 11 | private String movieName; 12 | 13 | public Character(String characterName, String actorName, int rating, String movieName) { 14 | this.characterName = characterName; 15 | this.actorName = actorName; 16 | this.rating = rating; 17 | this.movieName = movieName; 18 | } 19 | 20 | public Character(Character original) { 21 | if (original == null) { 22 | System.out.println("Fatal error."); 23 | System.exit(0); 24 | } 25 | this.characterName = original.characterName; 26 | this.actorName = original.actorName; 27 | this.rating = original.rating; 28 | this.movieName = original.movieName; 29 | 30 | } 31 | 32 | 33 | public String getCharacterName() { 34 | return characterName; 35 | } 36 | 37 | public String getActorName() { 38 | return actorName; 39 | } 40 | 41 | public int getRating() { 42 | return rating; 43 | } 44 | 45 | public String getMovieName() { 46 | return movieName; 47 | } 48 | 49 | public void setCharacterName(String characterName) { 50 | this.characterName = characterName; 51 | } 52 | 53 | public void setActorName(String actorName) { 54 | this.actorName = actorName; 55 | } 56 | 57 | public void setRating(int rating) { 58 | this.rating = rating; 59 | } 60 | 61 | public void setMovieName(String movieName) { 62 | this.movieName = movieName; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tutorial 6/code/CinemaDemo.java: -------------------------------------------------------------------------------- 1 | package tute6; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class CinemaDemo { 8 | public static void main(String[] args) { 9 | Character mainChar = new Character("character", "Jone", 1, "Sample"); 10 | 11 | Movie movie1 = new Movie("Sample", 1, 120, mainChar); 12 | 13 | movie1.getMainChar().setActorName("Frank"); // potential privacy leak 14 | 15 | System.out.println(movie1.getMainChar().getActorName()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tutorial 6/code/Movie.java: -------------------------------------------------------------------------------- 1 | package tute6; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class Movie { 8 | private String title; 9 | private int rank; 10 | private int runTime; 11 | private Character mainChar; 12 | 13 | public Movie(String title, int rank, int runTime, Character mainChar) { 14 | this.title = title; 15 | this.rank = rank; 16 | this.runTime = runTime; 17 | this.mainChar = mainChar; 18 | } 19 | 20 | public String getTitle() { 21 | return title; 22 | } 23 | 24 | public int getRank() { 25 | return rank; 26 | } 27 | 28 | public int getRunTime() { 29 | return runTime; 30 | } 31 | 32 | public Character getMainChar() { 33 | return new Character(mainChar); 34 | // return mainChar; // privacy leak 35 | } 36 | 37 | public boolean equals(Movie movie) { 38 | return this.title.equals(movie.title) && 39 | this.rank == movie.rank && 40 | this.runTime == movie.runTime; 41 | } 42 | 43 | public String toString() { 44 | return rank + ". " + title; 45 | } 46 | 47 | // @Override 48 | // public boolean equals(Object o) { 49 | // if (this == o) 50 | // return true; 51 | // if (o == null || getClass() != o.getClass()) 52 | // return false; 53 | // Movie movie = (Movie) o; 54 | // return rank == movie.rank && 55 | // runTime == movie.runTime && 56 | // Objects.equals(title, movie.title); 57 | // } 58 | // 59 | // @Override 60 | // public int hashCode() { 61 | // return Objects.hash(title, rank, runTime); 62 | // } 63 | } 64 | -------------------------------------------------------------------------------- /tutorial 6/code/demo/Date.java: -------------------------------------------------------------------------------- 1 | package tute6.demo; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Date { 6 | private String month; 7 | private int day; 8 | private int year; //a four digit number. 9 | 10 | public Date() { 11 | month = "January"; 12 | day = 1; 13 | year = 1000; 14 | } 15 | 16 | public Date(int monthInt, int day, int year) { 17 | setDate(monthInt, day, year); 18 | } 19 | 20 | public Date(String monthString, int day, int year) { 21 | setDate(monthString, day, year); 22 | } 23 | 24 | public Date(int year) { 25 | setDate(1, 1, year); 26 | } 27 | 28 | public Date(Date aDate) { 29 | if (aDate == null)//Not a real date. 30 | { 31 | System.out.println("Fatal Error."); 32 | System.exit(0); 33 | } 34 | 35 | month = aDate.month; 36 | day = aDate.day; 37 | year = aDate.year; 38 | } 39 | 40 | public void setDate(int monthInt, int day, int year) { 41 | if (dateOK(monthInt, day, year)) { 42 | this.month = monthString(monthInt); 43 | this.day = day; 44 | this.year = year; 45 | } else { 46 | System.out.println("Fatal Error"); 47 | System.exit(0); 48 | } 49 | } 50 | 51 | public void setDate(String monthString, int day, int year) { 52 | if (dateOK(monthString, day, year)) { 53 | this.month = monthString; 54 | this.day = day; 55 | this.year = year; 56 | } else { 57 | System.out.println("Fatal Error"); 58 | System.exit(0); 59 | } 60 | } 61 | 62 | public void setDate(int year) { 63 | setDate(1, 1, year); 64 | } 65 | 66 | public void setYear(int year) { 67 | if ((year < 1000) || (year > 9999)) { 68 | System.out.println("Fatal Error"); 69 | System.exit(0); 70 | } else 71 | this.year = year; 72 | } 73 | 74 | public void setMonth(int monthNumber) { 75 | if ((monthNumber <= 0) || (monthNumber > 12)) { 76 | System.out.println("Fatal Error"); 77 | System.exit(0); 78 | } else 79 | month = monthString(monthNumber); 80 | } 81 | 82 | public void setDay(int day) { 83 | if ((day <= 0) || (day > 31)) { 84 | System.out.println("Fatal Error"); 85 | System.exit(0); 86 | } else 87 | this.day = day; 88 | } 89 | 90 | public int getMonth() { 91 | if (month.equals("January")) 92 | return 1; 93 | else if (month.equals("February")) 94 | return 2; 95 | else if (month.equalsIgnoreCase("March")) 96 | return 3; 97 | else if (month.equalsIgnoreCase("April")) 98 | return 4; 99 | else if (month.equalsIgnoreCase("May")) 100 | return 5; 101 | else if (month.equals("June")) 102 | return 6; 103 | else if (month.equalsIgnoreCase("July")) 104 | return 7; 105 | else if (month.equalsIgnoreCase("August")) 106 | return 8; 107 | else if (month.equalsIgnoreCase("September")) 108 | return 9; 109 | else if (month.equalsIgnoreCase("October")) 110 | return 10; 111 | else if (month.equals("November")) 112 | return 11; 113 | else if (month.equals("December")) 114 | return 12; 115 | else { 116 | System.out.println("Fatal Error"); 117 | System.exit(0); 118 | return 0; //Needed to keep the compiler happy 119 | } 120 | } 121 | 122 | public int getDay() { 123 | return day; 124 | } 125 | 126 | public int getYear() { 127 | return year; 128 | } 129 | 130 | public String toString() { 131 | return (month + " " + day + ", " + year); 132 | } 133 | 134 | public boolean equals(Date otherDate) { 135 | return ((month.equals(otherDate.month)) 136 | && (day == otherDate.day) && (year == otherDate.year)); 137 | } 138 | 139 | public boolean precedes(Date otherDate) { 140 | return ((year < otherDate.year) || 141 | (year == otherDate.year && getMonth() < otherDate.getMonth()) || 142 | (year == otherDate.year && month.equals(otherDate.month) 143 | && day < otherDate.day)); 144 | } 145 | 146 | public void readInput() { 147 | boolean tryAgain = true; 148 | Scanner keyboard = new Scanner(System.in); 149 | while (tryAgain) { 150 | System.out.println("Enter month, day, and year."); 151 | System.out.println("Do not use a comma."); 152 | String monthInput = keyboard.next(); 153 | int dayInput = keyboard.nextInt(); 154 | int yearInput = keyboard.nextInt(); 155 | if (dateOK(monthInput, dayInput, yearInput)) { 156 | setDate(monthInput, dayInput, yearInput); 157 | tryAgain = false; 158 | } else 159 | System.out.println("Illegal date. Reenter input."); 160 | } 161 | } 162 | 163 | private boolean dateOK(int monthInt, int dayInt, int yearInt) { 164 | return ((monthInt >= 1) && (monthInt <= 12) && 165 | (dayInt >= 1) && (dayInt <= 31) && 166 | (yearInt >= 1000) && (yearInt <= 9999)); 167 | } 168 | 169 | private boolean dateOK(String monthString, int dayInt, int yearInt) { 170 | return (monthOK(monthString) && 171 | (dayInt >= 1) && (dayInt <= 31) && 172 | (yearInt >= 1000) && (yearInt <= 9999)); 173 | } 174 | 175 | private boolean monthOK(String month) { 176 | return (month.equals("January") || month.equals("February") || 177 | month.equals("March") || month.equals("April") || 178 | month.equals("May") || month.equals("June") || 179 | month.equals("July") || month.equals("August") || 180 | month.equals("September") || month.equals("October") || 181 | month.equals("November") || month.equals("December")); 182 | } 183 | 184 | private String monthString(int monthNumber) { 185 | switch (monthNumber) { 186 | case 1: 187 | return "January"; 188 | case 2: 189 | return "February"; 190 | case 3: 191 | return "March"; 192 | case 4: 193 | return "April"; 194 | case 5: 195 | return "May"; 196 | case 6: 197 | return "June"; 198 | case 7: 199 | return "July"; 200 | case 8: 201 | return "August"; 202 | case 9: 203 | return "September"; 204 | case 10: 205 | return "October"; 206 | case 11: 207 | return "November"; 208 | case 12: 209 | return "December"; 210 | default: 211 | System.out.println("Fatal Error"); 212 | System.exit(0); 213 | return "Error"; //to keep the compiler happy 214 | } 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /tutorial 6/code/demo/Person.java: -------------------------------------------------------------------------------- 1 | package tute6.demo; 2 | 3 | /** 4 | Class for a person with a name and dates for birth and death. 5 | Class invariant: A Person always has a date of birth, and if the Person 6 | has a date of death, then the date of death is equal to or later than the 7 | date of birth. 8 | */ 9 | public class Person 10 | { 11 | private String name; 12 | private Date born; 13 | private Date died;//null indicates still alive. 14 | 15 | public Person(String initialName, Date birthDate, Date deathDate) 16 | { 17 | if (consistent(birthDate, deathDate)) 18 | { 19 | name = initialName; 20 | born = new Date(birthDate); 21 | if (deathDate == null) 22 | died = null; 23 | else 24 | died = new Date(deathDate); 25 | } 26 | else 27 | { 28 | System.out.println("Inconsistent dates. Aborting."); 29 | System.exit(0); 30 | } 31 | } 32 | 33 | public Person(Person original)//copy constructor 34 | { 35 | if (original == null) 36 | { 37 | System.out.println("Fatal error."); 38 | System.exit(0); 39 | } 40 | 41 | name = original.name; 42 | born = new Date(original.born); 43 | 44 | if (original.died == null) 45 | died = null; 46 | else 47 | died = new Date(original.died); 48 | } 49 | 50 | public void set(String newName, Date birthDate, Date deathDate)//set instance variables 51 | { 52 | if (consistent(birthDate, deathDate)) 53 | { 54 | name = newName; 55 | born = new Date(birthDate); 56 | if (deathDate == null) 57 | died = null; 58 | else 59 | died = new Date(deathDate); 60 | } 61 | else 62 | { 63 | System.out.println("Inconsistent dates. Aborting."); 64 | System.exit(0); 65 | } 66 | } 67 | 68 | public String toString( ) 69 | { 70 | String diedString; 71 | if (died == null) 72 | diedString = ""; //Empty string 73 | else 74 | diedString = died.toString( ); 75 | 76 | return (name + ", " + born + " - " + diedString); 77 | } 78 | 79 | public boolean equals(Person otherPerson) 80 | { 81 | if (otherPerson == null) 82 | return false; 83 | else 84 | return (name.equals(otherPerson.name) 85 | && born.equals(otherPerson.born) 86 | && datesMatch(died, otherPerson.died) ); 87 | } 88 | 89 | /** 90 | To match date1 and date2 must either be the same date or both be null. 91 | */ 92 | private static boolean datesMatch(Date date1, Date date2) 93 | { 94 | if (date1 == null) 95 | return (date2 == null); 96 | else if (date2 == null) //&& date1 != null 97 | return false; 98 | else // both dates are not null. 99 | return(date1.equals(date2)); 100 | } 101 | 102 | /** 103 | Precondition: newDate is a consistent date of birth. 104 | Postcondition: Date of birth of the calling object is newDate. 105 | */ 106 | public void setBirthDate(Date newDate)//mutator 107 | { 108 | if (consistent(newDate, died)) 109 | born = new Date(newDate); 110 | else 111 | { 112 | System.out.println("Inconsistent dates. Aborting."); 113 | System.exit(0); 114 | } 115 | } 116 | 117 | /** 118 | Precondition: newDate is a consistent date of death. 119 | Postcondition: Date of death of the calling object is newDate. 120 | */ 121 | public void setDeathDate(Date newDate) 122 | { 123 | 124 | if (!consistent(born, newDate)) 125 | { 126 | System.out.println("Inconsistent dates. Aborting."); 127 | System.exit(0); 128 | } 129 | 130 | if (newDate == null) 131 | died = null; 132 | else 133 | died = new Date(newDate); 134 | } 135 | 136 | public void setName(String newName) 137 | { 138 | name = newName; 139 | } 140 | 141 | /** 142 | Precondition: The date of birth has been set, and changing the year 143 | part of the date of birth will give a consistent date of birth. 144 | Postcondition: The year of birth is (changed to) newYear. 145 | */ 146 | public void setBirthYear(int newYear) 147 | { 148 | if (born == null) //Precondition is violated 149 | { 150 | System.out.println("Fata; Error. Aborting."); 151 | System.exit(0); 152 | } 153 | born.setYear(newYear); 154 | if (!consistent(born, died)) 155 | { 156 | System.out.println("Inconsistent dates. Aborting."); 157 | System.exit(0); 158 | } 159 | } 160 | 161 | /** 162 | Precondition: The date of death has been set, and changing the year 163 | part of the date of death will give a consistent date of death. 164 | Postcondition: The year of death is (changed to) newYear. 165 | */ 166 | public void setDeathYear(int newYear) 167 | { 168 | if (died == null) //Precondition is violated 169 | { 170 | System.out.println("Fata; Error. Aborting."); 171 | System.exit(0); 172 | } 173 | died.setYear(newYear); 174 | if (!consistent(born, died)) 175 | { 176 | System.out.println("Inconsistent dates. Aborting."); 177 | System.exit(0); 178 | } 179 | } 180 | 181 | public String getName( ) 182 | { 183 | return name; 184 | } 185 | 186 | public Date getBirthDate( ) 187 | { 188 | // deep copy 189 | // by using copy constructor 190 | return new Date(born); 191 | } 192 | 193 | public Date getDeathDate( ) 194 | { 195 | if (died == null) 196 | return null; 197 | else 198 | return new Date(died); 199 | } 200 | 201 | /** 202 | To be consistent, birthDate must not be null. If there is no date of 203 | death (deathDate == null), that is consistent with any birthDate. 204 | Otherwise, the birthDate must come before or be equal to the deathDate. 205 | */ 206 | 207 | private static boolean consistent(Date birthDate, Date deathDate) 208 | { 209 | if (birthDate == null) 210 | return false; 211 | else if (deathDate == null) 212 | return true; 213 | else 214 | return (birthDate.precedes(deathDate ) 215 | || birthDate.equals(deathDate ));//check the ch4 date class 216 | } 217 | } -------------------------------------------------------------------------------- /tutorial 6/code/demo/PersonDemo.java: -------------------------------------------------------------------------------- 1 | package tute6.demo; 2 | 3 | public class PersonDemo 4 | { 5 | public static void main(String[] args) 6 | { 7 | Date birthDate = new Date("March", 21, 1685); 8 | Date deathDate = new Date("July", 28, 1750); 9 | Person bach = new Person("Jone Bach", birthDate, deathDate); 10 | 11 | // privacy leak if getBirthDate returns original reference only 12 | bach.getBirthDate().setDate("march", 21, 2000); 13 | 14 | 15 | Person bachTwin = new Person(bach); 16 | 17 | System.out.println("Comparing bach and bachTwin:"); 18 | if (bachTwin == bach) 19 | System.out.println("Same reference for both."); 20 | else 21 | System.out.println("Distinct copies."); 22 | 23 | if (bachTwin.equals(bach)) 24 | System.out.println("Same data."); 25 | else 26 | System.out.println("Not same data."); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tutorial 6/code/demo/Player.java: -------------------------------------------------------------------------------- 1 | package tute6.demo; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | 8 | 9 | public class Player { 10 | private final String firstName; 11 | private final String lastName; 12 | private final int score; 13 | 14 | public Player(String firstName, String lastName) { 15 | this.firstName = firstName; 16 | this.lastName = lastName; 17 | this.score = 0; 18 | } 19 | 20 | public String getFirstName() { 21 | return firstName; 22 | } 23 | 24 | public String getLastName() { 25 | return lastName; 26 | } 27 | 28 | public int getScore() { 29 | return score; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tutorial 6/comp90041_tute6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 6/comp90041_tute6.pdf -------------------------------------------------------------------------------- /tutorial 7/code/Student.java: -------------------------------------------------------------------------------- 1 | package tute7; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @studentID 791793 6 | * @institution University of Melbourne 7 | */ 8 | public class Student { 9 | private String name; 10 | private int studentID; 11 | private String username; 12 | private Subject[] enrolledSubjects; 13 | 14 | public Student(String name, int studentID, String username) { 15 | this.name = name; 16 | this.studentID = studentID; 17 | this.username = username; 18 | enrolledSubjects = new Subject[4]; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public int getStudentID() { 26 | return studentID; 27 | } 28 | 29 | 30 | public String getUsername() { 31 | return username; 32 | } 33 | 34 | public void setUsername(String username) { 35 | this.username = username; 36 | } 37 | 38 | public Subject[] getEnrolledSubjects() { 39 | return enrolledSubjects.clone(); 40 | } 41 | 42 | public void changeEnrolledSubjectInfo(Subject updatedSubject) { 43 | for (int i = 0; i < enrolledSubjects.length; i++) { 44 | if (enrolledSubjects[i].equals(updatedSubject)){ 45 | enrolledSubjects[i] = updatedSubject; 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tutorial 7/code/Subject.java: -------------------------------------------------------------------------------- 1 | package tute7; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @studentID 791793 6 | * @institution University of Melbourne 7 | */ 8 | public class Subject { 9 | private String code; 10 | private String title; 11 | private String coordinator; 12 | 13 | public Subject(String code) { 14 | this.code = code; 15 | } 16 | 17 | public String getCode() { 18 | return code; 19 | } 20 | 21 | public void setCode(String code) { 22 | // also change the subject recorded by that Student object - TBD 23 | this.code = code; 24 | } 25 | 26 | public String getTitle() { 27 | return title; 28 | } 29 | 30 | public void setTitle(String title) { 31 | // also change the subject recorded by that Student object - TBD 32 | this.title = title; 33 | } 34 | 35 | public String getCoordinator() { 36 | return coordinator; 37 | } 38 | 39 | public void setCoordinator(String coordinator) { 40 | // also change the subject recorded by that Student object - TBD 41 | this.coordinator = coordinator; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tutorial 7/code/demo/Demo.java: -------------------------------------------------------------------------------- 1 | package tute7.demo; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @studentID 791793 6 | * @institution University of Melbourne 7 | */ 8 | public class Demo { 9 | public static void main(String[] args) { 10 | 11 | // String[] cars = new String[4]; 12 | // cars[0] = "0"; 13 | // cars[1] = "1"; 14 | // System.out.println(cars[4]); 15 | 16 | String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; 17 | System.out.println("---------------loop over indices---------------"); 18 | for (int i = 0; i < cars.length; i++) { 19 | System.out.println(cars[i]); 20 | } 21 | System.out.println("---------------for each loop---------------"); 22 | for (String car : cars) { 23 | System.out.println(car); 24 | } 25 | 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tutorial 7/comp90041_tute7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 7/comp90041_tute7.pdf -------------------------------------------------------------------------------- /tutorial 7/readings/java-arrays.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 7/readings/java-arrays.pdf -------------------------------------------------------------------------------- /tutorial 8/code/Boot.java: -------------------------------------------------------------------------------- 1 | package tute8; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * @author Chuang Wang 7 | * @institution University of Melbourne 8 | */ 9 | public class Boot extends Shoe{ 10 | private HeelType heelType; 11 | 12 | public Boot(String color, String designer, float size) { 13 | super(color, designer, size); 14 | } 15 | 16 | public HeelType getHeelType() { 17 | return heelType; 18 | } 19 | 20 | public void setHeelType(HeelType heelType) { 21 | this.heelType = heelType; 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return super.toString() + "\n" + 27 | "Boot{" + 28 | "heelType=" + heelType + 29 | '}'; 30 | } 31 | 32 | @Override 33 | public boolean equals(Object o) { 34 | if (this == o) 35 | return true; 36 | if (o == null || getClass() != o.getClass()) 37 | return false; 38 | if (!super.equals(o)) 39 | return false; 40 | Boot boot = (Boot) o; 41 | return heelType == boot.heelType; 42 | } 43 | 44 | @Override 45 | public int hashCode() { 46 | return Objects.hash(super.hashCode(), heelType); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /tutorial 8/code/Demo.java: -------------------------------------------------------------------------------- 1 | package tute8; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @studentID 791793 6 | * @institution University of Melbourne 7 | */ 8 | public class Demo { 9 | 10 | public static void main(String[] args) { 11 | Boot b = new Boot("color", "design", 42.3f); 12 | b.setHeelType(HeelType.FLAT); 13 | System.out.println(b.getHeelType()); 14 | b.setHeelType(HeelType.PUMP); 15 | System.out.println(b.getHeelType()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tutorial 8/code/DressShoe.java: -------------------------------------------------------------------------------- 1 | package tute8; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * @author Chuang Wang 7 | * @institution University of Melbourne 8 | */ 9 | public class DressShoe extends Shoe{ 10 | HeelType heelType; 11 | 12 | public DressShoe(String color, String designer, float size) { 13 | super(color, designer, size); 14 | } 15 | 16 | public HeelType getHeelType() { 17 | return heelType; 18 | } 19 | 20 | public void setHeelType(HeelType heelType) { 21 | this.heelType = heelType; 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return super.toString() + "\n" + 27 | "DressShoe{" + 28 | "heelType=" + heelType + 29 | '}'; 30 | } 31 | 32 | @Override 33 | public boolean equals(Object o) { 34 | if (this == o) 35 | return true; 36 | if (o == null || getClass() != o.getClass()) 37 | return false; 38 | if (!super.equals(o)) 39 | return false; 40 | DressShoe dressShoe = (DressShoe) o; 41 | return heelType == dressShoe.heelType; 42 | } 43 | 44 | @Override 45 | public int hashCode() { 46 | return Objects.hash(super.hashCode(), heelType); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /tutorial 8/code/HeelType.java: -------------------------------------------------------------------------------- 1 | package tute8; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public enum HeelType { 8 | PUMP, 9 | HEEL, 10 | FLAT; 11 | } 12 | -------------------------------------------------------------------------------- /tutorial 8/code/Shoe.java: -------------------------------------------------------------------------------- 1 | package tute8; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * @author Chuang Wang 7 | * @institution University of Melbourne 8 | */ 9 | public class Shoe { 10 | private String color; 11 | private String designer; 12 | private float size; 13 | 14 | public Shoe() { 15 | color = "blue"; 16 | designer = "david"; 17 | size = 45.3f; 18 | } 19 | 20 | public Shoe(String color, String designer, float size) { 21 | this.color = color; 22 | this.designer = designer; 23 | this.size = size; 24 | } 25 | 26 | public String getColor() { 27 | return color; 28 | } 29 | 30 | public void setColor(String color) { 31 | this.color = color; 32 | } 33 | 34 | public String getDesigner() { 35 | return designer; 36 | } 37 | 38 | public void setDesigner(String designer) { 39 | this.designer = designer; 40 | } 41 | 42 | public float getSize() { 43 | return size; 44 | } 45 | 46 | public void setSize(float size) { 47 | this.size = size; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return "Shoe{" + 53 | "color='" + color + '\'' + 54 | ", designer='" + designer + '\'' + 55 | ", size=" + size + 56 | '}'; 57 | } 58 | 59 | @Override 60 | public boolean equals(Object o) { 61 | if (this == o) 62 | return true; 63 | if (o == null || getClass() != o.getClass()) 64 | return false; 65 | Shoe shoe = (Shoe) o; 66 | return Float.compare(shoe.size, size) == 0 && 67 | color.equals(shoe.color) && 68 | designer.equals(shoe.designer); 69 | } 70 | 71 | @Override 72 | public int hashCode() { 73 | return Objects.hash(color, designer, size); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /tutorial 8/code/TennisShoe.java: -------------------------------------------------------------------------------- 1 | package tute8; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * @author Chuang Wang 7 | * @institution University of Melbourne 8 | */ 9 | public class TennisShoe extends Shoe{ 10 | private String soleType; 11 | private String canvasType; 12 | 13 | public TennisShoe(String color, String designer, float size) { 14 | super(color, designer, size); 15 | } 16 | 17 | public String getSoleType() { 18 | return soleType; 19 | } 20 | 21 | public void setSoleType(String soleType) { 22 | this.soleType = soleType; 23 | } 24 | 25 | public String getCanvasType() { 26 | return canvasType; 27 | } 28 | 29 | public void setCanvasType(String canvasType) { 30 | this.canvasType = canvasType; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return super.toString() + "\n" + 36 | "TennisShoe{" + 37 | "soleType='" + soleType + '\'' + 38 | ", canvasType='" + canvasType + '\'' + 39 | '}'; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) 45 | return true; 46 | if (o == null || getClass() != o.getClass()) 47 | return false; 48 | if (!super.equals(o)) 49 | return false; 50 | TennisShoe that = (TennisShoe) o; 51 | return soleType.equals(that.soleType) && 52 | canvasType.equals(that.canvasType); 53 | } 54 | 55 | @Override 56 | public int hashCode() { 57 | return Objects.hash(super.hashCode(), soleType, canvasType); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /tutorial 8/code/flyer/AirPlane.java: -------------------------------------------------------------------------------- 1 | package tute8.flyer; 2 | 3 | /** 4 | * @author Chuang Wang (inspired by CPSC210 Gail Murphy, Elisa Baniassad) 5 | * @institution University of Melbourne 6 | */ 7 | public class AirPlane extends Flyer{ 8 | 9 | // REQUIRES: being on land 10 | // MODIFIES: this 11 | // EFFECTS: moves this into the air 12 | public void takeOff(){ 13 | System.out.println("takeoff from AirPlane class"); 14 | } 15 | 16 | 17 | // REQUIRES: being in the air 18 | // MODIFIES: this 19 | // EFFECTS: moves this horizontally in the air 20 | public void fly(){ 21 | System.out.println("fly from AirPlane class"); 22 | } 23 | 24 | // REQUIRES: being in the air 25 | // MODIFIES: this 26 | // EFFECTS: moves this onto land 27 | public void land(){ 28 | System.out.println("land from AirPlane class"); 29 | } 30 | 31 | public void serveDrinks() { 32 | System.out.println("Serving airplane drinks"); 33 | } 34 | 35 | public void serveSnacks() { 36 | System.out.println("Serving airplane snacks"); 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /tutorial 8/code/flyer/Airliner.java: -------------------------------------------------------------------------------- 1 | package tute8.flyer; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class Airliner extends AirPlane{ 8 | public void firstClass(){ 9 | System.out.println("serving 1st class passengers"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tutorial 8/code/flyer/Bird.java: -------------------------------------------------------------------------------- 1 | package tute8.flyer; 2 | 3 | /** 4 | * @author Chuang Wang (inspired by CPSC210 Gail Murphy, Elisa Baniassad) 5 | * @institution University of Melbourne 6 | */ 7 | public class Bird extends Flyer{ 8 | // REQUIRES: being on land 9 | // MODIFIES: this 10 | // EFFECTS: moves this into the air 11 | public void takeOff(){ 12 | System.out.println("takeoff from Bird class"); 13 | } 14 | 15 | 16 | // REQUIRES: being in the air 17 | // MODIFIES: this 18 | // EFFECTS: moves this horizontally in the air 19 | public void fly(){ 20 | System.out.println("fly from Bird class"); 21 | } 22 | 23 | // REQUIRES: being in the air 24 | // MODIFIES: this 25 | // EFFECTS: moves this onto land 26 | public void land(){ 27 | System.out.println("land from Bird class"); 28 | } 29 | 30 | public void tweet(){ 31 | System.out.println("birds tweet"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tutorial 8/code/flyer/Demo.java: -------------------------------------------------------------------------------- 1 | package tute8.flyer; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class Demo { 8 | public static void main(String[] args) { 9 | Flyer f1 = new Flyer(); 10 | Flyer f2 = new Bird(); 11 | Bird b1 = new Bird(); 12 | Bird b2 = new Kiwi(); 13 | PrivatePlane p = new PrivatePlane(); 14 | f1.fly(); 15 | f2.land(); 16 | b1.takeOff(); 17 | b2.tweet(); 18 | p.serveDrinks(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tutorial 8/code/flyer/Flyer.java: -------------------------------------------------------------------------------- 1 | package tute8.flyer; 2 | 3 | /** 4 | * @author Chuang Wang (inspired by CPSC210 Gail Murphy, Elisa Baniassad) 5 | * @institution University of Melbourne 6 | */ 7 | public class Flyer { 8 | // REQUIRES: being on land 9 | // MODIFIES: this 10 | // EFFECTS: moves this into the air 11 | public void takeOff(){ 12 | System.out.println("takeoff from flyer class"); 13 | } 14 | 15 | 16 | // REQUIRES: being in the air 17 | // MODIFIES: this 18 | // EFFECTS: moves this horizontally in the air 19 | public void fly(){ 20 | System.out.println("fly from flyer class"); 21 | } 22 | 23 | // REQUIRES: being in the air 24 | // MODIFIES: this 25 | // EFFECTS: moves this onto land 26 | public void land(){ 27 | System.out.println("land from flyer class"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tutorial 8/code/flyer/Hummingbird.java: -------------------------------------------------------------------------------- 1 | package tute8.flyer; 2 | 3 | /** 4 | * @author Chuang Wang (inspired by CPSC210 Gail Murphy, Elisa Baniassad) 5 | * @institution University of Melbourne 6 | */ 7 | public class Hummingbird extends Bird{ 8 | @Override 9 | public void fly() { 10 | System.out.println("fly from Hummingbird class"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tutorial 8/code/flyer/Kiwi.java: -------------------------------------------------------------------------------- 1 | package tute8.flyer; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @institution University of Melbourne 6 | */ 7 | public class Kiwi extends Bird{ 8 | public void jump(){ 9 | System.out.println("kiwi jumps"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tutorial 8/code/flyer/PrivatePlane.java: -------------------------------------------------------------------------------- 1 | package tute8.flyer; 2 | 3 | /** 4 | * @author Chuang Wang(inspired by CPSC210 Gail Murphy, Elisa Baniassad) 5 | * @institution University of Melbourne 6 | */ 7 | public class PrivatePlane extends AirPlane{ 8 | // EFFECTS: prints "Here is your warm towel!" to the console 9 | public void bringWarmTowels(){ 10 | System.out.println("Here is your warm towel!!"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tutorial 8/code/hrSystem/Date.java: -------------------------------------------------------------------------------- 1 | package tute8.hrSystem; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Date 6 | { 7 | private String month; 8 | private int day; 9 | private int year; //a four digit number. 10 | 11 | public Date( ) 12 | { 13 | month = "January"; 14 | day = 1; 15 | year = 1000; 16 | } 17 | 18 | public Date(int monthInt, int day, int year) 19 | { 20 | setDate(monthInt, day, year); 21 | } 22 | 23 | public Date(String monthString, int day, int year) 24 | { 25 | setDate(monthString, day, year); 26 | } 27 | 28 | public Date(int year) 29 | { 30 | setDate(1, 1, year); 31 | } 32 | 33 | public Date(Date aDate) 34 | { 35 | if (aDate == null)//Not a real date. 36 | { 37 | System.out.println("Fatal Error."); 38 | System.exit(0); 39 | } 40 | 41 | month = aDate.month; 42 | day = aDate.day; 43 | year = aDate.year; 44 | } 45 | 46 | public void setDate(int monthInt, int day, int year) 47 | { 48 | if (dateOK(monthInt, day, year)) 49 | { 50 | this.month = monthString(monthInt); 51 | this.day = day; 52 | this.year = year; 53 | } 54 | else 55 | { 56 | System.out.println("Fatal Error"); 57 | System.exit(0); 58 | } 59 | } 60 | 61 | public void setDate(String monthString, int day, int year) 62 | { 63 | if (dateOK(monthString, day, year)) 64 | { 65 | this.month = monthString; 66 | this.day = day; 67 | this.year = year; 68 | } 69 | else 70 | { 71 | System.out.println("Fatal Error"); 72 | System.exit(0); 73 | } 74 | } 75 | 76 | public void setDate(int year) 77 | { 78 | setDate(1, 1, year); 79 | } 80 | 81 | public void setYear(int year) 82 | { 83 | if ( (year < 1000) || (year > 9999) ) 84 | { 85 | System.out.println("Fatal Error"); 86 | System.exit(0); 87 | } 88 | else 89 | this.year = year; 90 | } 91 | public void setMonth(int monthNumber) 92 | { 93 | if ((monthNumber <= 0) || (monthNumber > 12)) 94 | { 95 | System.out.println("Fatal Error"); 96 | System.exit(0); 97 | } 98 | else 99 | month = monthString(monthNumber); 100 | } 101 | 102 | public void setDay(int day) 103 | { 104 | if ((day <= 0) || (day > 31)) 105 | { 106 | System.out.println("Fatal Error"); 107 | System.exit(0); 108 | } 109 | else 110 | this.day = day; 111 | } 112 | 113 | public int getMonth( ) 114 | { 115 | if (month.equals("January")) 116 | return 1; 117 | else if (month.equals("February")) 118 | return 2; 119 | else if (month.equalsIgnoreCase("March")) 120 | return 3; 121 | else if (month.equalsIgnoreCase("April")) 122 | return 4; 123 | else if (month.equalsIgnoreCase("May")) 124 | return 5; 125 | else if (month.equals("June")) 126 | return 6; 127 | else if (month.equalsIgnoreCase("July")) 128 | return 7; 129 | else if (month.equalsIgnoreCase("August")) 130 | return 8; 131 | else if (month.equalsIgnoreCase("September")) 132 | return 9; 133 | else if (month.equalsIgnoreCase("October")) 134 | return 10; 135 | else if (month.equals("November")) 136 | return 11; 137 | else if (month.equals("December")) 138 | return 12; 139 | else 140 | { 141 | System.out.println("Fatal Error"); 142 | System.exit(0); 143 | return 0; //Needed to keep the compiler happy 144 | } 145 | } 146 | 147 | public int getDay( ) 148 | { 149 | return day; 150 | } 151 | 152 | public int getYear( ) 153 | { 154 | return year; 155 | } 156 | 157 | public String toString( ) 158 | { 159 | return (month + " " + day + ", " + year); 160 | } 161 | 162 | public boolean equals(Date otherDate) 163 | { 164 | if (otherDate == null) 165 | return false; 166 | else 167 | return ( (month.equals(otherDate.month)) && 168 | (day == otherDate.day) && (year == otherDate.year) ); 169 | } 170 | 171 | public boolean precedes(Date otherDate) 172 | { 173 | return ( (year < otherDate.year) || 174 | (year == otherDate.year && getMonth( ) < otherDate.getMonth( )) || 175 | (year == otherDate.year && month.equals(otherDate.month) 176 | && day < otherDate.day) ); 177 | } 178 | 179 | public void readInput( ) 180 | { 181 | boolean tryAgain = true; 182 | Scanner keyboard = new Scanner(System.in); 183 | while (tryAgain) 184 | { 185 | System.out.println("Enter month, day, and year."); 186 | System.out.println("Do not use a comma."); 187 | String monthInput = keyboard.next( ); 188 | int dayInput = keyboard.nextInt( ); 189 | int yearInput = keyboard.nextInt( ); 190 | if (dateOK(monthInput, dayInput, yearInput) ) 191 | { 192 | setDate(monthInput, dayInput, yearInput); 193 | tryAgain = false; 194 | } 195 | else 196 | System.out.println("Illegal date. Reenter input."); 197 | } 198 | } 199 | 200 | private boolean dateOK(int monthInt, int dayInt, int yearInt) 201 | { 202 | return ( (monthInt >= 1) && (monthInt <= 12) && 203 | (dayInt >= 1) && (dayInt <= 31) && 204 | (yearInt >= 1000) && (yearInt <= 9999) ); 205 | } 206 | 207 | private boolean dateOK(String monthString, int dayInt, int yearInt) 208 | { 209 | return ( monthOK(monthString) && 210 | (dayInt >= 1) && (dayInt <= 31) && 211 | (yearInt >= 1000) && (yearInt <= 9999) ); 212 | } 213 | 214 | private boolean monthOK(String month) 215 | { 216 | return (month.equals("January") || month.equals("February") || 217 | month.equals("March") || month.equals("April") || 218 | month.equals("May") || month.equals("June") || 219 | month.equals("July") || month.equals("August") || 220 | month.equals("September") || month.equals("October") || 221 | month.equals("November") || month.equals("December") ); 222 | } 223 | 224 | private String monthString(int monthNumber) 225 | { 226 | switch (monthNumber) 227 | { 228 | case 1: 229 | return "January"; 230 | case 2: 231 | return "February"; 232 | case 3: 233 | return "March"; 234 | case 4: 235 | return "April"; 236 | case 5: 237 | return "May"; 238 | case 6: 239 | return "June"; 240 | case 7: 241 | return "July"; 242 | case 8: 243 | return "August"; 244 | case 9: 245 | return "September"; 246 | case 10: 247 | return "October"; 248 | case 11: 249 | return "November"; 250 | case 12: 251 | return "December"; 252 | default: 253 | System.out.println("Fatal Error"); 254 | System.exit(0); 255 | return "Error"; //to keep the compiler happy 256 | } 257 | } 258 | } 259 | -------------------------------------------------------------------------------- /tutorial 8/code/hrSystem/Employee.java: -------------------------------------------------------------------------------- 1 | package tute8.hrSystem; 2 | 3 | /** 4 | Class Invariant: All objects have a name string and hire date. 5 | A name string of "No name" indicates no real name specified yet. 6 | A hire date of Jan 1, 1000 indicates no real hire date specified yet. 7 | */ 8 | public class Employee 9 | { 10 | private String name; 11 | private Date hireDate; 12 | 13 | public Employee( ) 14 | { 15 | name = "No name"; 16 | hireDate = new Date("Jan", 1, 1000); //Just a placeholder. 17 | } 18 | 19 | /** 20 | Precondition: Neither theName nor theDate is null. 21 | */ 22 | public Employee(String theName, Date theDate) 23 | { 24 | if (theName == null || theDate == null) 25 | { 26 | System.out.println("Fatal Error creating employee."); 27 | System.exit(0); 28 | } 29 | name = theName; 30 | hireDate = new Date(theDate); 31 | } 32 | 33 | public Employee(Employee originalObject) 34 | { 35 | name = originalObject.name; 36 | hireDate = new Date(originalObject.hireDate); 37 | } 38 | 39 | public String getName( ) 40 | { 41 | return name; 42 | } 43 | 44 | public Date getHireDate( ) 45 | { 46 | return new Date(hireDate); 47 | } 48 | 49 | /** 50 | Precondition newName is not null. 51 | */ 52 | public void setName(String newName) 53 | { 54 | if (newName == null) 55 | { 56 | System.out.println("Fatal Error setting employee name."); 57 | System.exit(0); 58 | } 59 | else 60 | name = newName; 61 | } 62 | 63 | /** 64 | Precondition newDate is not null. 65 | */ 66 | public void setHireDate(Date newDate) 67 | { 68 | if (newDate == null) 69 | { 70 | System.out.println("Fatal Error setting employee hire date."); 71 | System.exit(0); 72 | } 73 | else 74 | hireDate = new Date(newDate); 75 | } 76 | 77 | 78 | public String toString( ) 79 | { 80 | return (name + " " + hireDate.toString( )); 81 | } 82 | 83 | 84 | public boolean equals(Employee otherEmployee) 85 | { 86 | return (name.equals(otherEmployee.name) 87 | && hireDate.equals(otherEmployee.hireDate)); 88 | } 89 | } 90 | 91 | -------------------------------------------------------------------------------- /tutorial 8/code/hrSystem/EnhancedStringTokenizer.java: -------------------------------------------------------------------------------- 1 | package tute8.hrSystem; 2 | 3 | import java.util.StringTokenizer; 4 | 5 | public class EnhancedStringTokenizer extends StringTokenizer 6 | { 7 | private String[] a; 8 | private int count; 9 | 10 | public EnhancedStringTokenizer(String theString ) 11 | { 12 | super(theString); 13 | a = new String[countTokens( )];//the method is inherited and is not overridden 14 | count = 0; 15 | } 16 | 17 | public EnhancedStringTokenizer(String theString, String delimiters) 18 | { 19 | super(theString, delimiters); 20 | a = new String[countTokens( )]; 21 | count = 0; 22 | } 23 | 24 | /** 25 | Returns the same value as the same method in the StringTokenizer class, 26 | but it also stores data for the method tokensSoFar to use. 27 | */ 28 | // this method netToken has its definition overridden 29 | public String nextToken( ) 30 | { 31 | String token = super.nextToken( );//super.nextToken is the version of nextToken defined in the base class StringTokenizer 32 | a[count] = token; 33 | count++; 34 | return token; 35 | } 36 | 37 | /** 38 | Returns the same value as the same method in the StringTokenizer class, 39 | changes the delimiter set in the same way as does the same method in the 40 | StringTokenizer class, but it also stores data for the method tokensSoFar to use. 41 | */ 42 | // this method nextToken also has its definition overridden 43 | public String nextToken(String delimiters) 44 | { 45 | String token = super.nextToken(delimiters); 46 | a[count] = token; 47 | count++; 48 | return token; 49 | } 50 | 51 | /** 52 | Returns an array of all tokens produced so far. 53 | Array returned has length equal to the number of tokens produced so far. 54 | */ 55 | //tokenSoFar is a new method 56 | public String[] tokensSoFar( ) 57 | { 58 | String[] arrayToReturn = new String[count]; 59 | for (int i = 0; i < count; i++) 60 | arrayToReturn[i] = a[i]; 61 | return arrayToReturn; 62 | } 63 | 64 | public Object nextElement( ) 65 | { 66 | String token = super.nextToken( ); 67 | a[count] = token; 68 | count++; 69 | return (Object)token; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /tutorial 8/code/hrSystem/HourlyEmployee.java: -------------------------------------------------------------------------------- 1 | package tute8.hrSystem; 2 | 3 | /** 4 | Class Invariant: All objects have a name string, hire date, nonnegative 5 | wage rate, and nonnegative number of hours worked. A name string of 6 | "No name" indicates no real name specified yet. A hire date of Jan 1, 1000 7 | indicates no real hire date specified yet. 8 | */ 9 | public class HourlyEmployee extends Employee 10 | { 11 | private double wageRate; 12 | private double hours; //for the month 13 | 14 | public HourlyEmployee( ) 15 | { 16 | super( );//if the line is omitted, Java will still invoke the non-argument constructor for the base class 17 | wageRate = 0; 18 | hours = 0; 19 | } 20 | 21 | /** 22 | Precondition: Neither theName nor theDate is null; 23 | theWageRate and theHours are nonnegative. 24 | */ 25 | public HourlyEmployee(String theName, Date theDate, 26 | double theWageRate, double theHours) 27 | { 28 | super(theName, theDate);//call a constructor for the base class 29 | if ((theWageRate >= 0) && (theHours >= 0)) 30 | { 31 | wageRate = theWageRate; 32 | hours = theHours; 33 | } 34 | else 35 | { 36 | System.out.println( 37 | "Fatal Error: creating an illegal hourly employee."); 38 | System.exit(0); 39 | } 40 | } 41 | 42 | public HourlyEmployee(HourlyEmployee originalObject) 43 | { 44 | super(originalObject); 45 | wageRate = originalObject.wageRate; 46 | hours = originalObject.hours; 47 | } 48 | 49 | public double getRate( ) 50 | { 51 | return wageRate; 52 | } 53 | 54 | public double getHours( ) 55 | { 56 | return hours; 57 | } 58 | 59 | /** 60 | Returns the pay for the month. 61 | */ 62 | public double getPay( ) 63 | { 64 | return wageRate*hours; 65 | } 66 | 67 | /** 68 | Precondition: hoursWorked is nonnegative. 69 | */ 70 | public void setHours(double hoursWorked) 71 | { 72 | if (hoursWorked >= 0) 73 | hours = hoursWorked; 74 | else 75 | { 76 | System.out.println("Fatal Error: Negative hours worked."); 77 | System.exit(0); 78 | } 79 | } 80 | 81 | /** 82 | Precondition: newWageRate is nonnegative. 83 | */ 84 | public void setRate(double newWageRate) 85 | { 86 | if (newWageRate >= 0) 87 | wageRate = newWageRate; 88 | else 89 | { 90 | System.out.println("Fatal Error: Negative wage rate."); 91 | System.exit(0); 92 | } 93 | } 94 | 95 | //the method toString is "overridden" so it is different in the derived class HourlyEmployee than it is in the base class Employee 96 | public String toString( ) 97 | { 98 | return (getName( ) + " " + getHireDate( ).toString( ) 99 | + "\n$" + wageRate + " per hour for " + hours + " hours"); 100 | /* 101 | return (super.toString() 102 | + "\n$" + wageRate + " per hour for " + hours + " hours"); 103 | */ 104 | 105 | } 106 | 107 | // public boolean equals(HourlyEmployee other) 108 | // { 109 | // return (super.equals(other) && wageRate == other.wageRate 110 | // && hours == other.hours); 111 | 112 | 113 | // return (getName( ).equals(other.getName( )) 114 | // && getHireDate( ).equals(other.HireDate( )) 115 | // && wageRate == other.wageRate 116 | // && hours == other.hours); 117 | 118 | // } 119 | 120 | 121 | public boolean equals(Object otherObject) 122 | { 123 | if(otherObject == null) 124 | return false; 125 | else if (getClass() != otherObject.getClass() ) 126 | return false; 127 | else 128 | { 129 | HourlyEmployee otherHourlyEmployee = (HourlyEmployee)otherObject; 130 | return (super.equals(otherHourlyEmployee) && wageRate == otherHourlyEmployee.wageRate 131 | && hours == otherHourlyEmployee.hours); 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /tutorial 8/code/hrSystem/InheritanceDemo.java: -------------------------------------------------------------------------------- 1 | package tute8.hrSystem; 2 | 3 | public class InheritanceDemo 4 | { 5 | public static void main(String[] args) 6 | { 7 | HourlyEmployee joe = new HourlyEmployee("Joe Worker", 8 | new Date("January", 1, 2004), 50.50, 160); 9 | 10 | System.out.println("joe's longer name is " + joe.getName( )); 11 | 12 | System.out.println("Changing joe's name to Josephine."); 13 | joe.setName("Josephine"); 14 | 15 | System.out.println("joe's record is as follows:"); 16 | System.out.println(joe); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tutorial 8/code/hrSystem/IsADemo.java: -------------------------------------------------------------------------------- 1 | package tute8.hrSystem; 2 | 3 | public class IsADemo 4 | { 5 | public static void main(String[] args) 6 | { 7 | SalariedEmployee joe = new SalariedEmployee("Josephine", 8 | new Date("January", 1, 2004), 100000); 9 | HourlyEmployee sam = new HourlyEmployee("Sam", 10 | new Date("February", 1, 2003), 50.50, 40); 11 | 12 | System.out.println("joe's longer name is " + joe.getName( )); 13 | 14 | System.out.println("showEmployee(joe) invoked:"); 15 | showEmployee(joe); 16 | 17 | System.out.println("showEmployee(sam) invoked:"); 18 | showEmployee(sam); 19 | 20 | } 21 | 22 | public static void showEmployee(Employee employeeObject) 23 | { 24 | System.out.println(employeeObject.getName( )); 25 | System.out.println(employeeObject.getHireDate( )); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tutorial 8/code/hrSystem/SalariedEmployee.java: -------------------------------------------------------------------------------- 1 | package tute8.hrSystem; 2 | 3 | /** 4 | Class Invariant: All objects have a name string, hire date, 5 | and nonnegative salary. A name string of "No name" indicates 6 | no real name specified yet. A hire date of Jan 1, 1000 indicates 7 | no real hire date specified yet. 8 | */ 9 | public class SalariedEmployee extends Employee 10 | { 11 | private double salary; //annual 12 | 13 | public SalariedEmployee( ) 14 | { 15 | super( ); 16 | salary = 0; 17 | } 18 | 19 | /** 20 | Precondition: Neither theName nor theDate are null; 21 | theSalary is nonnegative. 22 | */ 23 | public SalariedEmployee(String theName, Date theDate, double theSalary) 24 | { 25 | super(theName, theDate); 26 | if (theSalary >= 0) 27 | salary = theSalary; 28 | else 29 | { 30 | System.out.println("Fatal Error: Negative salary."); 31 | System.exit(0); 32 | } 33 | } 34 | 35 | public SalariedEmployee(SalariedEmployee originalObject ) 36 | { 37 | super(originalObject); 38 | salary = originalObject.salary; 39 | } 40 | 41 | public double getSalary( ) 42 | { 43 | return salary; 44 | } 45 | 46 | /** 47 | Returns the pay for the month. 48 | */ 49 | public double getPay( ) 50 | { 51 | return salary/12; 52 | } 53 | 54 | /** 55 | Precondition: newSalary is nonnegative. 56 | */ 57 | public void setSalary(double newSalary) 58 | { 59 | if (newSalary >= 0) 60 | salary = newSalary; 61 | else 62 | { 63 | System.out.println("Fatal Error: Negative salary."); 64 | System.exit(0); 65 | } 66 | } 67 | 68 | public String toString( ) 69 | { 70 | return (getName( ) + " " + getHireDate( ).toString( ) 71 | + "\n$" + salary + " per year"); 72 | } 73 | 74 | // public boolean equals(SalariedEmployee other) 75 | // { 76 | // return (getName( ).equals(other.getName( )) 77 | // && getHireDate( ).equals(other.getHireDate( )) 78 | // && salary == other.salary); 79 | // } 80 | 81 | public boolean equals(Object otherObject) 82 | { 83 | if(otherObject == null) 84 | return false; 85 | else if (getClass() != otherObject.getClass()) 86 | return false; 87 | else{ 88 | SalariedEmployee otherSalariedEmployee = (SalariedEmployee)otherObject; 89 | return (super.equals(otherSalariedEmployee)&& salary == otherSalariedEmployee.salary); 90 | 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /tutorial 8/code/hrSystem/improvedEquals/Date.java: -------------------------------------------------------------------------------- 1 | package tute8.hrSystem.improvedEquals; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Date 6 | { 7 | private String month; 8 | private int day; 9 | private int year; //a four digit number. 10 | 11 | public Date( ) 12 | { 13 | month = "January"; 14 | day = 1; 15 | year = 1000; 16 | } 17 | 18 | public Date(int monthInt, int day, int year) 19 | { 20 | setDate(monthInt, day, year); 21 | } 22 | 23 | public Date(String monthString, int day, int year) 24 | { 25 | setDate(monthString, day, year); 26 | } 27 | 28 | public Date(int year) 29 | { 30 | setDate(1, 1, year); 31 | } 32 | 33 | public Date(Date aDate) 34 | { 35 | if (aDate == null)//Not a real date. 36 | { 37 | System.out.println("Fatal Error."); 38 | System.exit(0); 39 | } 40 | 41 | month = aDate.month; 42 | day = aDate.day; 43 | year = aDate.year; 44 | } 45 | 46 | public void setDate(int monthInt, int day, int year) 47 | { 48 | if (dateOK(monthInt, day, year)) 49 | { 50 | this.month = monthString(monthInt); 51 | this.day = day; 52 | this.year = year; 53 | } 54 | else 55 | { 56 | System.out.println("Fatal Error"); 57 | System.exit(0); 58 | } 59 | } 60 | 61 | public void setDate(String monthString, int day, int year) 62 | { 63 | if (dateOK(monthString, day, year)) 64 | { 65 | this.month = monthString; 66 | this.day = day; 67 | this.year = year; 68 | } 69 | else 70 | { 71 | System.out.println("Fatal Error"); 72 | System.exit(0); 73 | } 74 | } 75 | 76 | public void setDate(int year) 77 | { 78 | setDate(1, 1, year); 79 | } 80 | 81 | public void setYear(int year) 82 | { 83 | if ( (year < 1000) || (year > 9999) ) 84 | { 85 | System.out.println("Fatal Error"); 86 | System.exit(0); 87 | } 88 | else 89 | this.year = year; 90 | } 91 | public void setMonth(int monthNumber) 92 | { 93 | if ((monthNumber <= 0) || (monthNumber > 12)) 94 | { 95 | System.out.println("Fatal Error"); 96 | System.exit(0); 97 | } 98 | else 99 | month = monthString(monthNumber); 100 | } 101 | 102 | public void setDay(int day) 103 | { 104 | if ((day <= 0) || (day > 31)) 105 | { 106 | System.out.println("Fatal Error"); 107 | System.exit(0); 108 | } 109 | else 110 | this.day = day; 111 | } 112 | 113 | public int getMonth( ) 114 | { 115 | if (month.equals("January")) 116 | return 1; 117 | else if (month.equals("February")) 118 | return 2; 119 | else if (month.equalsIgnoreCase("March")) 120 | return 3; 121 | else if (month.equalsIgnoreCase("April")) 122 | return 4; 123 | else if (month.equalsIgnoreCase("May")) 124 | return 5; 125 | else if (month.equals("June")) 126 | return 6; 127 | else if (month.equalsIgnoreCase("July")) 128 | return 7; 129 | else if (month.equalsIgnoreCase("August")) 130 | return 8; 131 | else if (month.equalsIgnoreCase("September")) 132 | return 9; 133 | else if (month.equalsIgnoreCase("October")) 134 | return 10; 135 | else if (month.equals("November")) 136 | return 11; 137 | else if (month.equals("December")) 138 | return 12; 139 | else 140 | { 141 | System.out.println("Fatal Error"); 142 | System.exit(0); 143 | return 0; //Needed to keep the compiler happy 144 | } 145 | } 146 | 147 | public int getDay( ) 148 | { 149 | return day; 150 | } 151 | 152 | public int getYear( ) 153 | { 154 | return year; 155 | } 156 | 157 | public String toString( ) 158 | { 159 | return (month + " " + day + ", " + year); 160 | } 161 | 162 | public boolean equals(Object otherObject) 163 | { 164 | if (otherObject == null) 165 | return false; 166 | else if (getClass( ) != otherObject.getClass( )) 167 | return false; 168 | else 169 | { 170 | Date otherDate = 171 | (Date)otherObject; 172 | return ( month.equals(otherDate.month) 173 | && (day == otherDate.day) 174 | && (year == otherDate.year) ); 175 | } 176 | } 177 | 178 | public boolean precedes(Date otherDate) 179 | { 180 | return ( (year < otherDate.year) || 181 | (year == otherDate.year && getMonth( ) < otherDate.getMonth( )) || 182 | (year == otherDate.year && month.equals(otherDate.month) 183 | && day < otherDate.day) ); 184 | } 185 | 186 | public void readInput( ) 187 | { 188 | boolean tryAgain = true; 189 | Scanner keyboard = new Scanner(System.in); 190 | while (tryAgain) 191 | { 192 | System.out.println("Enter month, day, and year."); 193 | System.out.println("Do not use a comma."); 194 | String monthInput = keyboard.next( ); 195 | int dayInput = keyboard.nextInt( ); 196 | int yearInput = keyboard.nextInt( ); 197 | if (dateOK(monthInput, dayInput, yearInput) ) 198 | { 199 | setDate(monthInput, dayInput, yearInput); 200 | tryAgain = false; 201 | } 202 | else 203 | System.out.println("Illegal date. Reenter input."); 204 | } 205 | } 206 | 207 | private boolean dateOK(int monthInt, int dayInt, int yearInt) 208 | { 209 | return ( (monthInt >= 1) && (monthInt <= 12) && 210 | (dayInt >= 1) && (dayInt <= 31) && 211 | (yearInt >= 1000) && (yearInt <= 9999) ); 212 | } 213 | 214 | private boolean dateOK(String monthString, int dayInt, int yearInt) 215 | { 216 | return ( monthOK(monthString) && 217 | (dayInt >= 1) && (dayInt <= 31) && 218 | (yearInt >= 1000) && (yearInt <= 9999) ); 219 | } 220 | 221 | private boolean monthOK(String month) 222 | { 223 | return (month.equals("January") || month.equals("February") || 224 | month.equals("March") || month.equals("April") || 225 | month.equals("May") || month.equals("June") || 226 | month.equals("July") || month.equals("August") || 227 | month.equals("September") || month.equals("October") || 228 | month.equals("November") || month.equals("December") ); 229 | } 230 | 231 | private String monthString(int monthNumber) 232 | { 233 | switch (monthNumber) 234 | { 235 | case 1: 236 | return "January"; 237 | case 2: 238 | return "February"; 239 | case 3: 240 | return "March"; 241 | case 4: 242 | return "April"; 243 | case 5: 244 | return "May"; 245 | case 6: 246 | return "June"; 247 | case 7: 248 | return "July"; 249 | case 8: 250 | return "August"; 251 | case 9: 252 | return "September"; 253 | case 10: 254 | return "October"; 255 | case 11: 256 | return "November"; 257 | case 12: 258 | return "December"; 259 | default: 260 | System.out.println("Fatal Error"); 261 | System.exit(0); 262 | return "Error"; //to keep the compiler happy 263 | } 264 | } 265 | } 266 | -------------------------------------------------------------------------------- /tutorial 8/code/hrSystem/improvedEquals/Employee.java: -------------------------------------------------------------------------------- 1 | package tute8.hrSystem.improvedEquals; 2 | 3 | /** 4 | Class Invariant: All objects have a name string and hire date. 5 | A name string of "No name" indicates no real name specified yet. 6 | A hire date of Jan 1, 1000 indicates no real hire date specified yet. 7 | */ 8 | public class Employee 9 | { 10 | private String name; 11 | private Date hireDate; 12 | 13 | public Employee( ) 14 | { 15 | name = "No name"; 16 | hireDate = new Date("Jan", 1, 1000); //Just a placeholder. 17 | } 18 | 19 | /** 20 | Precondition: Neither theName nor theDate is null. 21 | */ 22 | public Employee(String theName, Date theDate) 23 | { 24 | if (theName == null || theDate == null) 25 | { 26 | System.out.println("Fatal Error creating employee."); 27 | System.exit(0); 28 | } 29 | name = theName; 30 | hireDate = new Date(theDate); 31 | } 32 | 33 | public Employee(Employee originalObject) 34 | { 35 | name = originalObject.name; 36 | hireDate = new Date(originalObject.hireDate); 37 | } 38 | 39 | public String getName( ) 40 | { 41 | return name; 42 | } 43 | 44 | public Date getHireDate( ) 45 | { 46 | return new Date(hireDate); 47 | } 48 | 49 | /** 50 | Precondition newName is not null. 51 | */ 52 | public void setName(String newName) 53 | { 54 | if (newName == null) 55 | { 56 | System.out.println("Fatal Error setting employee name."); 57 | System.exit(0); 58 | } 59 | else 60 | name = newName; 61 | } 62 | 63 | /** 64 | Precondition newDate is not null. 65 | */ 66 | public void setHireDate(Date newDate) 67 | { 68 | if (newDate == null) 69 | { 70 | System.out.println("Fatal Error setting employee hire date."); 71 | System.exit(0); 72 | } 73 | else 74 | hireDate = new Date(newDate); 75 | } 76 | 77 | 78 | public String toString( ) 79 | { 80 | return (name + " " + hireDate.toString( )); 81 | } 82 | 83 | 84 | public boolean equals(Object otherObject) 85 | { 86 | if (otherObject == null) 87 | return false; 88 | else if (getClass( ) != otherObject.getClass( )) 89 | return false; 90 | else 91 | { 92 | Employee otherEmployee = (Employee)otherObject; 93 | return (name.equals(otherEmployee.name) 94 | && hireDate.equals(otherEmployee.hireDate)); 95 | } 96 | } 97 | 98 | 99 | } 100 | -------------------------------------------------------------------------------- /tutorial 8/code/hrSystem/improvedEquals/HourlyEmployee.java: -------------------------------------------------------------------------------- 1 | package tute8.hrSystem.improvedEquals; 2 | 3 | /** 4 | Class Invariant: All objects have a name string, hire date, nonnegative 5 | wage rate, and nonnegative number of hours worked. A name string of 6 | "No name" indicates no real name specified yet. A hire date of Jan 1, 1000 7 | indicates no real hire date specified yet. 8 | */ 9 | public class HourlyEmployee extends Employee 10 | { 11 | private double wageRate; 12 | private double hours; //for the month 13 | 14 | public HourlyEmployee( ) 15 | { 16 | super( ); 17 | wageRate = 0; 18 | hours = 0; 19 | } 20 | 21 | /** 22 | Precondition: Neither theName nor theDate is null; 23 | theWageRate and theHours are nonnegative. 24 | */ 25 | public HourlyEmployee(String theName, Date theDate, 26 | double theWageRate, double theHours) 27 | { 28 | super(theName, theDate); 29 | if ((theWageRate >= 0) && (theHours >= 0)) 30 | { 31 | wageRate = theWageRate; 32 | hours = theHours; 33 | } 34 | else 35 | { 36 | System.out.println( 37 | "Fatal Error: creating an illegal hourly employee."); 38 | System.exit(0); 39 | } 40 | } 41 | 42 | public HourlyEmployee(HourlyEmployee originalObject) 43 | { 44 | super(originalObject); 45 | wageRate = originalObject.wageRate; 46 | hours = originalObject.hours; 47 | } 48 | 49 | public double getRate( ) 50 | { 51 | return wageRate; 52 | } 53 | 54 | public double getHours( ) 55 | { 56 | return hours; 57 | } 58 | 59 | /** 60 | Returns the pay for the month. 61 | */ 62 | public double getPay( ) 63 | { 64 | return wageRate*hours; 65 | } 66 | 67 | /** 68 | Precondition: hoursWorked is nonnegative. 69 | */ 70 | public void setHours(double hoursWorked) 71 | { 72 | if (hoursWorked >= 0) 73 | hours = hoursWorked; 74 | else 75 | { 76 | System.out.println("Fatal Error: Negative hours worked."); 77 | System.exit(0); 78 | } 79 | } 80 | 81 | /** 82 | Precondition: newWageRate is nonnegative. 83 | */ 84 | public void setRate(double newWageRate) 85 | { 86 | if (newWageRate >= 0) 87 | wageRate = newWageRate; 88 | else 89 | { 90 | System.out.println("Fatal Error: Negative wage rate."); 91 | System.exit(0); 92 | } 93 | } 94 | 95 | public String toString( ) 96 | { 97 | return (getName( ) + " " + getHireDate( ).toString( ) 98 | + "\n$" + wageRate + " per hour for " + hours + " hours"); 99 | } 100 | 101 | public boolean equals(Object otherObject) 102 | { 103 | if (otherObject == null) 104 | return false; 105 | else if (getClass( ) != otherObject.getClass( )) 106 | return false; 107 | else 108 | { 109 | HourlyEmployee otherHourlyEmployee = 110 | (HourlyEmployee)otherObject; 111 | return (super.equals(otherHourlyEmployee) 112 | && (wageRate == otherHourlyEmployee.wageRate) 113 | && (hours == otherHourlyEmployee.hours)); 114 | } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /tutorial 8/code/hrSystem/improvedEquals/SalariedEmployee.java: -------------------------------------------------------------------------------- 1 | package tute8.hrSystem.improvedEquals; 2 | 3 | 4 | /** 5 | Class Invariant: All objects have a name string, hire date, 6 | and nonnegative salary. A name string of "No name" indicates 7 | no real name specified yet. A hire date of Jan 1, 1000 indicates 8 | no real hire date specified yet. 9 | */ 10 | public class SalariedEmployee extends Employee { 11 | private double salary; //annual 12 | 13 | public SalariedEmployee( ) 14 | { 15 | super( ); 16 | salary = 0; 17 | } 18 | 19 | /** 20 | Precondition: Neither theName nor theDate are null; 21 | theSalary is nonnegative. 22 | */ 23 | public SalariedEmployee(String theName, Date theDate, double theSalary) 24 | { 25 | super(theName, theDate); 26 | if (theSalary >= 0) 27 | salary = theSalary; 28 | else 29 | { 30 | System.out.println("Fatal Error: Negative salary."); 31 | System.exit(0); 32 | } 33 | } 34 | 35 | public SalariedEmployee(SalariedEmployee originalObject ) 36 | { 37 | super(originalObject); 38 | salary = originalObject.salary; 39 | } 40 | 41 | public double getSalary( ) 42 | { 43 | return salary; 44 | } 45 | 46 | /** 47 | Returns the pay for the month. 48 | */ 49 | public double getPay( ) 50 | { 51 | return salary/12; 52 | } 53 | 54 | /** 55 | Precondition: newSalary is nonnegative. 56 | */ 57 | public void setSalary(double newSalary) 58 | { 59 | if (newSalary >= 0) 60 | salary = newSalary; 61 | else 62 | { 63 | System.out.println("Fatal Error: Negative salary."); 64 | System.exit(0); 65 | } 66 | } 67 | 68 | public String toString( ) 69 | { 70 | return (getName( ) + " " + getHireDate( ).toString( ) 71 | + "\n$" + salary + " per year"); 72 | } 73 | 74 | public boolean equals(Object otherObject) 75 | { 76 | if (otherObject == null) 77 | return false; 78 | else if (getClass( ) != otherObject.getClass( )) 79 | return false; 80 | else 81 | { 82 | SalariedEmployee otherSalariedEmployee = 83 | (SalariedEmployee)otherObject; 84 | return (super.equals(otherSalariedEmployee) 85 | && (salary == otherSalariedEmployee.salary)); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /tutorial 8/comp90041_tute8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 8/comp90041_tute8.pdf -------------------------------------------------------------------------------- /tutorial 9/code/flyerA/AirPlane.java: -------------------------------------------------------------------------------- 1 | package tute9.flyerA; 2 | 3 | /** 4 | * @author Chuang Wang (inspired by CPSC210 Gail Murphy, Elisa Baniassad) 5 | * @institution University of Melbourne 6 | */ 7 | public class AirPlane extends Flyer { 8 | 9 | public void takeOff() { 10 | System.out.println("taxiing -> flying"); 11 | } 12 | 13 | public void land(){ 14 | System.out.println("land from AirPlane class"); 15 | } 16 | 17 | public void serveDrinks() { 18 | System.out.println("Serving airplane drinks"); 19 | } 20 | 21 | public void serveFood() { 22 | System.out.println("Serving chips & cookies"); 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /tutorial 9/code/flyerA/Bird.java: -------------------------------------------------------------------------------- 1 | package tute9.flyerA; 2 | 3 | /** 4 | * @author Chuang Wang (inspired by CPSC210 Gail Murphy, Elisa Baniassad) 5 | * @institution University of Melbourne 6 | */ 7 | public class Bird extends Flyer { 8 | private int size; 9 | // public Bird(String name){ 10 | // super(name); 11 | // } 12 | 13 | @Override 14 | public void takeOff() { 15 | System.out.println("takeOff from bird class"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tutorial 9/code/flyerA/Flyer.java: -------------------------------------------------------------------------------- 1 | package tute9.flyerA; 2 | 3 | /** 4 | * @author Chuang Wang (inspired by CPSC210 Gail Murphy, Elisa Baniassad) 5 | * @institution University of Melbourne 6 | */ 7 | public abstract class Flyer { 8 | private String name; 9 | 10 | 11 | // REQUIRES: being on land 12 | // MODIFIES: this 13 | // EFFECTS: moves this into the air 14 | public abstract void takeOff(); 15 | 16 | 17 | 18 | // REQUIRES: being in the air 19 | // MODIFIES: this 20 | // EFFECTS: moves this horizontally in the air 21 | public void fly(){ 22 | System.out.println("fly from flyer class"); 23 | } 24 | 25 | // REQUIRES: being in the air 26 | // MODIFIES: this 27 | // EFFECTS: moves this onto land 28 | public void land(){ 29 | System.out.println("land from flyer class"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tutorial 9/code/flyerA/diagram.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | JAVA 4 | 5 | 6 | tute9.flyerI.Flyer 7 | tute9.flyerI.Bird 8 | tute9.flyerI.Kiwi 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | Fields 25 | Constructors 26 | Methods 27 | Properties 28 | 29 | All 30 | private 31 | 32 | 33 | -------------------------------------------------------------------------------- /tutorial 9/code/flyerI/Animal.java: -------------------------------------------------------------------------------- 1 | package tute9.flyerI; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @studentID 791793 6 | * @institution University of Melbourne 7 | */ 8 | public interface Animal{ 9 | public void tweet(); 10 | } 11 | -------------------------------------------------------------------------------- /tutorial 9/code/flyerI/Bird.java: -------------------------------------------------------------------------------- 1 | package tute9.flyerI; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @studentID 791793 6 | * @institution University of Melbourne 7 | */ 8 | public class Bird extends Flyer implements Animal { 9 | 10 | 11 | @Override 12 | public void tweet() { 13 | System.out.println("tweet from bird class"); 14 | } 15 | 16 | @Override 17 | public void takeOff() { 18 | System.out.println("takeOff from bird class"); 19 | 20 | } 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /tutorial 9/code/flyerI/Flyer.java: -------------------------------------------------------------------------------- 1 | package tute9.flyerI; 2 | 3 | /** 4 | * @author Chuang Wang 5 | * @studentID 791793 6 | * @institution University of Melbourne 7 | */ 8 | public abstract class Flyer { 9 | 10 | public abstract void takeOff(); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /tutorial 9/comp90041_tute9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuangw6/COMP90041_Tutorial/fbe3f3658c0c9c54f6e8ea54be6bef0ced2c23cc/tutorial 9/comp90041_tute9.pdf --------------------------------------------------------------------------------