├── 00_introduction.pdf ├── 01_java basic.pdf ├── 02-Problem Solving with Java.pdf ├── 03-arrays.pdf ├── 04-methods.pdf ├── 05-File IO and Exception Handling.pdf ├── 06-OO-Part1.pdf ├── 07-OO-Part2.pdf ├── 08-Interitance-Part 1.pdf ├── 09-Interitance-Part 2.pdf ├── 10-polymorphism.pdf ├── 11-Abstract Class and Interface and Lambda.pdf ├── Lab 01 Intorduction ├── 21237387_lab01.docx └── lab01.pdf ├── Lab 02 Decisions ├── 21237387_lab02.docx ├── 21237387_lab02 │ └── src │ │ ├── Age.java │ │ ├── DistanceOfTwoPoints.java │ │ ├── ParkingCharge.java │ │ ├── TestPositive.java │ │ └── WordGame.java └── lab02.pdf ├── Lab 03 Loops ├── 21237387_lab03.docx ├── 21237387_lab03 │ └── src │ │ ├── CalendarMonth.java │ │ ├── Diamond.java │ │ ├── FormattedOddNumbers.java │ │ ├── InvertedTriangle.java │ │ ├── IsoscelesTriangle.java │ │ ├── LineNumber.java │ │ ├── LineNumberToFile.java │ │ ├── OddNumbers.java │ │ ├── Rect.java │ │ ├── ReverseOddNumbers.java │ │ └── RightTriangle.java └── lab03.pdf ├── Lab 04 Arrays ├── 21237387_lab04.docx ├── 21237387_lab04 │ └── src │ │ ├── ArrayEx.java │ │ ├── AryRotation.java │ │ ├── CharFinder.java │ │ └── IntegerAdder.java └── lab04.pdf ├── Lab 05 More about Arrays ├── 21237387_lab05.docx ├── 21237387_lab05 │ ├── MapFlooding.java │ ├── Merging.java │ └── map.txt └── lab05.pdf ├── Lab 06 Methods ├── 21237387-lab06.docx ├── 21237387_lab06 │ └── 21237387_lab06 │ │ ├── Maze.java │ │ ├── Maze1.txt │ │ └── MyMethods.java └── lab06.pdf ├── Lab 07 Excepting Handling and Debugger ├── 21237387_lab07.docx ├── 21237387_lab07 │ └── src │ │ └── SaleAmount.java └── lab07.pdf ├── Lab 08 Introduction to OOP ├── 21237387_lab08 .docx ├── 21237387_lab08 │ └── src │ │ ├── Card.java │ │ └── Hand.java └── lab08.pdf ├── Lab 09 More about OOP ├── 21237387_lab09.docx ├── 21237387_lab09 │ └── src │ │ ├── Contact.java │ │ └── PhoneBook.java └── lab09.pdf ├── Lab 10 Inheritance & Polymorphism ├── 21237387_lab10.docx ├── 21237387_lab10 │ └── src │ │ ├── DailyEvent.java │ │ ├── Event.java │ │ ├── MonthlyEvent.java │ │ ├── OnetimeEvent.java │ │ ├── ScheduleBook.java │ │ └── ScheduleBookTester.java └── lab10.pdf ├── Lab 11 More about Inheritance & Polymorphism ├── 21237387_lab11.docx ├── 21237387_lab11 │ └── src │ │ ├── ABCEmployee.java │ │ ├── ABCEmployeeTester.java │ │ ├── Circle.java │ │ ├── MyShapeList.java │ │ ├── Shape.java │ │ └── Triangle.java └── lab11.pdf ├── Lab 12 Abstract Class ├── 21237387_lab12 │ └── src │ │ ├── Book.java │ │ ├── BookInfo.java │ │ ├── KidsMagazine.java │ │ ├── Magazine.java │ │ └── Publication.java └── lab12.pdf └── Lab 13 Interface and Lambda Expression ├── 21237387_lab13.docx ├── 21237387_lab13 ├── __MACOSX │ └── src │ │ ├── ._.DS_Store │ │ ├── ._Book.java │ │ ├── ._CheckoutableCollection.java │ │ ├── ._Circle.java │ │ ├── ._EvenChecker.java │ │ ├── ._KidsMegazine.java │ │ ├── ._Megazine.java │ │ ├── ._Movable.java │ │ ├── ._MovableCollection.java │ │ ├── ._MyProg1.java │ │ ├── ._MyProg2.java │ │ ├── ._MyProg3.java │ │ ├── ._NumChecker.java │ │ ├── ._Point.java │ │ ├── ._PositiveChecker.java │ │ ├── ._Publication.java │ │ ├── ._Rectangle.java │ │ └── ._Shape.java └── src │ ├── .DS_Store │ ├── Book.java │ ├── Checkoutable.java │ ├── CheckoutableCollection.java │ ├── Circle.java │ ├── EvenChecker.java │ ├── KidsMegazine.java │ ├── Main.java │ ├── Megazine.java │ ├── Movable.java │ ├── MovableCollection.java │ ├── MyProg1.java │ ├── MyProg2.java │ ├── MyProg3.java │ ├── NumChecker.java │ ├── Point.java │ ├── PositiveChecker.java │ ├── Publication.java │ ├── Rectangle.java │ ├── Resizable.java │ ├── ResizableCollection.java │ └── Shape.java └── lab13.pdf /00_introduction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/00_introduction.pdf -------------------------------------------------------------------------------- /01_java basic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/01_java basic.pdf -------------------------------------------------------------------------------- /02-Problem Solving with Java.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/02-Problem Solving with Java.pdf -------------------------------------------------------------------------------- /03-arrays.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/03-arrays.pdf -------------------------------------------------------------------------------- /04-methods.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/04-methods.pdf -------------------------------------------------------------------------------- /05-File IO and Exception Handling.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/05-File IO and Exception Handling.pdf -------------------------------------------------------------------------------- /06-OO-Part1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/06-OO-Part1.pdf -------------------------------------------------------------------------------- /07-OO-Part2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/07-OO-Part2.pdf -------------------------------------------------------------------------------- /08-Interitance-Part 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/08-Interitance-Part 1.pdf -------------------------------------------------------------------------------- /09-Interitance-Part 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/09-Interitance-Part 2.pdf -------------------------------------------------------------------------------- /10-polymorphism.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/10-polymorphism.pdf -------------------------------------------------------------------------------- /11-Abstract Class and Interface and Lambda.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/11-Abstract Class and Interface and Lambda.pdf -------------------------------------------------------------------------------- /Lab 01 Intorduction/21237387_lab01.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 01 Intorduction/21237387_lab01.docx -------------------------------------------------------------------------------- /Lab 01 Intorduction/lab01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 01 Intorduction/lab01.pdf -------------------------------------------------------------------------------- /Lab 02 Decisions/21237387_lab02.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 02 Decisions/21237387_lab02.docx -------------------------------------------------------------------------------- /Lab 02 Decisions/21237387_lab02/src/Age.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Age { 4 | public static void main(String[] args){ 5 | int age=0; 6 | int number = 0; 7 | Scanner in = new Scanner(System.in); 8 | System.out.print("Enter today's date:"); 9 | int year = in.nextInt(); 10 | int month = in.nextInt(); 11 | int day = in.nextInt(); 12 | System.out.print("Enter a persion's birth date:"); 13 | int year1 = in.nextInt(); 14 | int month1 = in.nextInt(); 15 | int day1 = in.nextInt(); 16 | 17 | if(month-month1 < 0){ 18 | number++; 19 | }else if(month-month1 >= 0){ 20 | if(day-day1 < 0){ 21 | number++; 22 | }else{ 23 | 24 | } 25 | } 26 | 27 | age = year - year1 - number; 28 | //2000 1 15 29 | System.out.print("The person's age is "+age); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Lab 02 Decisions/21237387_lab02/src/DistanceOfTwoPoints.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | import static jdk.nashorn.internal.objects.NativeMath.round; 4 | 5 | public class DistanceOfTwoPoints { 6 | public static void main(String[] args){ 7 | Scanner in = new Scanner(System.in); 8 | System.out.print("Enter x1 and y1:"); 9 | double x1 = in.nextDouble(); 10 | double y1 = in.nextDouble(); 11 | System.out.print("Enter x2 and y2:"); 12 | double x2 = in.nextDouble(); 13 | double y2 = in.nextDouble(); 14 | 15 | 16 | double ans = Math.sqrt( Math.pow((x2-x1),2) + Math.pow((y2-y1),2) ); 17 | 18 | System.out.printf("The distance of the two points is "+"%.2f",ans); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Lab 02 Decisions/21237387_lab02/src/ParkingCharge.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner;public class ParkingCharge { 2 | public static void main(String[] args){ 3 | int car = 38; 4 | int bus = 56; 5 | int truck = 63; 6 | int result = 0; 7 | Scanner in = new Scanner(System.in); 8 | System.out.print("Type of vehicle:"); 9 | String type = in.next(); 10 | System.out.print("Hours the vehicle spent:"); 11 | int hour = in.nextInt(); 12 | switch(type) 13 | { 14 | case "c" : 15 | result = car * hour; 16 | System.out.println("Parking Charge: " +result); 17 | break; 18 | case "b" : 19 | result = bus * hour; 20 | System.out.println("Parking Charge: " +result); 21 | break; 22 | case "t" : 23 | result = truck * hour; 24 | System.out.println("Parking Charge: " +result); 25 | break; 26 | 27 | 28 | 29 | 30 | } 31 | 32 | 33 | }} 34 | -------------------------------------------------------------------------------- /Lab 02 Decisions/21237387_lab02/src/TestPositive.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | 4 | public class TestPositive { 5 | public static void main(String[] args){ 6 | String message ="Good Bye !"; 7 | Scanner in = new Scanner(System.in); 8 | System.out.print("Enter an integer value:"); 9 | int value = in.nextInt(); 10 | 11 | if(value > 0){ 12 | System.out.println("The value is postitive. \n"+message); 13 | }else if (value < 0){ 14 | System.out.println("The value is negative. \n"+message); 15 | }else{ 16 | System.out.println("The value is zero. \n"+message); 17 | } 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lab 02 Decisions/21237387_lab02/src/WordGame.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class WordGame { 4 | public static void main (String[] agrs){ 5 | 6 | Scanner in = new Scanner(System.in); 7 | System.out.print("Enter your name: "); 8 | String NAME = in.next(); 9 | System.out.print("Enter your age: "); 10 | int AGE = in.nextInt(); 11 | System.out.print("Enter the name of a city: "); 12 | in.nextLine(); 13 | String CITY = in.nextLine(); 14 | System.out.print("Enter the name of a collage: "); 15 | String COLLEGE = in.next(); 16 | System.out.print("Enter a profession:"); 17 | String PROFESSION = in.next(); 18 | System.out.print("Enter a type of animal: "); 19 | String ANIMAL = in.next(); 20 | System.out.print("Enter a pet's name: "); 21 | String PETNAME = in.next(); 22 | 23 | System.out.println("There once was a person named "+NAME +" who lived in "+CITY+".\n" + 24 | "At the age of "+AGE+", "+NAME+" went to college at "+COLLEGE+".\n" + 25 | NAME+" graduated and went to work as a(n) "+PROFESSION+".\n" + 26 | "Then, "+NAME+" adopted a(n) "+ANIMAL+" named "+PETNAME+".\n" + 27 | "They both lived happily ever after!\n"); 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Lab 02 Decisions/lab02.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 02 Decisions/lab02.pdf -------------------------------------------------------------------------------- /Lab 03 Loops/21237387_lab03.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 03 Loops/21237387_lab03.docx -------------------------------------------------------------------------------- /Lab 03 Loops/21237387_lab03/src/CalendarMonth.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class CalendarMonth { 4 | public static void main(String[] args){ 5 | int y = 1; 6 | Scanner in = new Scanner(System.in); 7 | System.out.print("Enter the start day: "); 8 | int start = in.nextInt(); 9 | System.out.print("Enter the number of days: "); 10 | int number = in.nextInt(); 11 | 12 | 13 | System.out.println("Sun Mon Tue Wed Thu Fri Sat"); 14 | System.out.println("--- --- --- --- --- --- ---"); 15 | 16 | for(int x=0; x0){ 20 | for(int l=0;l<=n-2;l++ ){ 21 | System.out.print(" "); 22 | 23 | } 24 | System.out.println("*"); 25 | }else{ 26 | return; 27 | } 28 | 29 | 30 | for (int y=1; yj; k--){ 32 | System.out.print(" "); 33 | } 34 | 35 | System.out.print("*"); 36 | for (int x=0; x1){ 43 | 44 | for(int e=0; e0){ 14 | System.out.println(" *"); 15 | }else{ 16 | return; 17 | } 18 | 19 | 20 | for (int y=1; yj; k--){ 22 | System.out.print(" "); 23 | } 24 | 25 | System.out.print("*"); 26 | for (int x=0; x0; i--){ 7 | i--; 8 | System.out.printf("%2d ",i); 9 | q++; 10 | 11 | 12 | if (q==10){ 13 | System.out.println(""); 14 | q=0; 15 | } 16 | 17 | 18 | } 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Lab 03 Loops/21237387_lab03/src/RightTriangle.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | 4 | public class RightTriangle { 5 | public static void main(String[] args) { 6 | int n; 7 | 8 | 9 | Scanner in = new Scanner(System.in); 10 | 11 | System.out.print("n: "); 12 | n = in.nextInt(); 13 | 14 | 15 | 16 | for (int y=1; y<=n; y++) { 17 | 18 | for (int x=0; x= 0; h--){ 30 | 31 | System.out.print(input[h]+" "); 32 | } 33 | 34 | 35 | System.out.println(); 36 | System.out.print("Alternating sum: "); 37 | for(int g = 0; g < input.length; g++){ 38 | if (g%2==0){ 39 | sum += input[g]; 40 | } 41 | if (g%2!= 0 ){ 42 | sum -= input[g]; 43 | } 44 | } 45 | System.out.print(sum); 46 | } 47 | 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Lab 04 Arrays/21237387_lab04/src/AryRotation.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class AryRotation { 4 | 5 | public static void main(String[] args) { 6 | new AryRotation().runApp(); 7 | } 8 | 9 | 10 | void rotate(int[] a) { 11 | //TO DO: rotate the elements of array a by one position 12 | int save = a[0]; 13 | for(int k=0; k input ){ 34 | newmap[i][j] = (" "); 35 | }else{ 36 | newmap[i][j] = ("*"); 37 | } 38 | } 39 | } 40 | 41 | for(int i=0; i < row; i++){ 42 | for(int j=0; j < col; j++){ 43 | System.out.print(newmap[i][j]); 44 | } 45 | System.out.println(); 46 | } 47 | 48 | 49 | 50 | }} 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /Lab 05 More about Arrays/21237387_lab05/Merging.java: -------------------------------------------------------------------------------- 1 | public class Merging { 2 | 3 | public static void main(String[] args) { 4 | 5 | new Merging().runApp(); 6 | } 7 | 8 | 9 | void runApp() { 10 | 11 | int[] a = {1, 6, 11, 12}; 12 | int[] b = {3, 5, 7, 9, 10}; 13 | int[] c = new int[a.length + b.length]; 14 | int i = 0; 15 | int j = 0; 16 | int z = 0; 17 | 18 | 19 | while (i 3) { 50 | System.out.println("Error: Line " + line + ": Incorrect line format!"); 51 | } else 52 | System.out.println("Error: Line " + line + ": Incorrect service category!"); 53 | } 54 | } catch (Exception e) { 55 | System.out.println("Error: Line " + line + ": Incorrect line format!"); 56 | } 57 | 58 | 59 | } 60 | myReader.close(); 61 | 62 | 63 | System.out.println("Dinner Sale Amount: "+dinner); 64 | System.out.println("Conference Sale Amount: "+conference); 65 | System.out.println("Lodging Sale Amount: "+lodging); 66 | }catch(FileNotFoundException e){ 67 | System.out.println("Error: The file sale.txt is not found."); 68 | } 69 | 70 | }} 71 | -------------------------------------------------------------------------------- /Lab 07 Excepting Handling and Debugger/lab07.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 07 Excepting Handling and Debugger/lab07.pdf -------------------------------------------------------------------------------- /Lab 08 Introduction to OOP/21237387_lab08 .docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 08 Introduction to OOP/21237387_lab08 .docx -------------------------------------------------------------------------------- /Lab 08 Introduction to OOP/21237387_lab08/src/Card.java: -------------------------------------------------------------------------------- 1 | public class Card { 2 | private int suit; 3 | private int rank; 4 | 5 | public Card(int suit, int rank) { 6 | this.suit = suit; 7 | this.rank = rank; 8 | } 9 | 10 | 11 | public int getsuit() { 12 | return suit; 13 | } 14 | public void setsuit(int suit) { 15 | this.suit = suit; 16 | } 17 | public int getrank() { 18 | return rank; 19 | } 20 | public void setrank(int rank) { 21 | this.rank = rank; 22 | } 23 | 24 | public boolean equals(Card obj ){ 25 | if(this.suit == obj.suit && this.rank == obj.rank){ 26 | return true; 27 | } 28 | return false; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | 34 | String[] s = {"Diamonds" , "Clubs", "Hearts", "Spades"}; 35 | String[] r = {"","Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten", "Jack", "Queen", "King"}; 36 | 37 | return r[rank]+" of "+s[suit]; 38 | } 39 | 40 | public static void main(String[] args){ 41 | Card c1 = new Card(0, 1); //suit: Diamonds, rank: Ace 42 | Card c2 = new Card(0, 1); //suit: Diamonds, rank: Ace 43 | Card c3 = new Card(1, 1); //suit: Clubs, rank: Ace 44 | Card c4 = new Card(1, 2); //suit: Clubs, rank: 2 45 | System.out.println(c1); 46 | System.out.println(c2); 47 | 48 | if(c1.equals(c2)){ 49 | System.out.println("They are equal."); 50 | } 51 | else{ 52 | System.out.println("They are not equal."); 53 | } 54 | System.out.println(c1); 55 | System.out.println(c3); 56 | if(c1.equals(c3)){ 57 | System.out.println("They are equal."); 58 | } 59 | else{ 60 | System.out.println("They are not equal."); 61 | } 62 | System.out.println(c3); 63 | System.out.println(c4); 64 | if(c3.equals(c4)){ 65 | System.out.println("They are equal."); 66 | } 67 | else{ 68 | System.out.println("They are not equal."); 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /Lab 08 Introduction to OOP/21237387_lab08/src/Hand.java: -------------------------------------------------------------------------------- 1 | public class Hand { 2 | private Card[] cards; 3 | 4 | 5 | 6 | public Hand( ) { 7 | this.cards = new Card[0]; 8 | 9 | } 10 | 11 | public int getCount() { 12 | return cards.length; 13 | } 14 | 15 | public void addCard(Card c1) { 16 | Card[] newArray = new Card[cards.length + 1]; 17 | for (int i = 0; i < newArray.length-1; i++) { 18 | newArray[i] = cards[i]; 19 | } 20 | newArray[newArray.length-1] = c1; 21 | cards = newArray; 22 | } 23 | 24 | public Card getCard(int i){ 25 | return cards[i]; 26 | } 27 | 28 | public void removeCard( int num) { 29 | Card[] newHand = new Card[cards.length - 1]; 30 | for (int i = 0; i < num; i++) { 31 | newHand[i] = cards[i]; 32 | 33 | } 34 | for (int i = num; i < cards.length-1; i++) { 35 | newHand[i] = cards[++i]; 36 | cards = newHand; 37 | } 38 | } 39 | private void removeCard(Card card) { 40 | int num = 0; 41 | Card[] newHand = new Card[cards.length - 1]; 42 | for (int i = 0; i < cards.length; i++) { 43 | if (cards[i].equals(card)) { 44 | num = i; 45 | 46 | } 47 | } 48 | 49 | for (int j = num; j < cards.length-1; j++) { 50 | cards[j] = cards[++j]; 51 | } 52 | for (int k = 0; k < newHand.length; k++) { 53 | newHand[k] = cards[k]; 54 | } 55 | cards = newHand; 56 | } 57 | 58 | public String toString() { 59 | String str = ""; 60 | for (int i = 0; i < getCount(); i++) { 61 | try{ 62 | str +="["+i+"] " + getCard(i).toString() + "\n"; 63 | }catch(Exception e){ 64 | System.out.println("Debug message"+ i); } 65 | } 66 | return str; 67 | } 68 | 69 | 70 | public static void main(String[] args) { 71 | Hand myHand = new Hand(); 72 | Card c1 = new Card(0, 1); //suit: Diamonds rank: Ace 73 | Card c2 = new Card(1, 5); //suit: Clubs rank: 5 74 | Card c3 = new Card(2, 11); //suit: Hearts rank: Jack 75 | Card c4 = new Card(3, 13); //suit: Spades rank: King 76 | //getCount 77 | System.out.println("No. of Cards: " + myHand.getCount()); 78 | System.out.println(myHand); //empty 79 | //add cards 80 | myHand.addCard(c1); 81 | System.out.println("No. of Cards: " + myHand.getCount()); 82 | System.out.println(myHand); 83 | myHand.addCard(c2); 84 | System.out.println("No. of Cards: " + myHand.getCount()); 85 | System.out.println(myHand); 86 | myHand.addCard(c3); 87 | System.out.println("No. of Cards: " + myHand.getCount()); 88 | System.out.println(myHand); 89 | myHand.addCard(c4); 90 | System.out.println("No. of Cards: " + myHand.getCount()); 91 | System.out.println(myHand); 92 | //getCard 93 | System.out.println(myHand.getCard(2)); 94 | System.out.println("No. of Cards: " + myHand.getCount()); 95 | System.out.println(myHand); 96 | //remove cards 97 | myHand.removeCard(2); 98 | System.out.println("No. of Cards: " + myHand.getCount()); 99 | System.out.println(myHand); 100 | myHand.removeCard(new Card(1, 5)); 101 | System.out.println("No. of Cards: " + myHand.getCount()); 102 | System.out.println(myHand); 103 | } 104 | 105 | 106 | } -------------------------------------------------------------------------------- /Lab 08 Introduction to OOP/lab08.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 08 Introduction to OOP/lab08.pdf -------------------------------------------------------------------------------- /Lab 09 More about OOP/21237387_lab09.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 09 More about OOP/21237387_lab09.docx -------------------------------------------------------------------------------- /Lab 09 More about OOP/21237387_lab09/src/Contact.java: -------------------------------------------------------------------------------- 1 | public class Contact { 2 | private String name; 3 | private String[] phonenum; 4 | private String phone; 5 | 6 | 7 | public Contact(String name){ 8 | this.name = name; 9 | this.phonenum = new String[0]; 10 | } 11 | 12 | public Contact(String name, String phone){ 13 | this.name = name; 14 | this.phone = phone; 15 | this.phonenum = new String[]{phone}; 16 | 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | public String[] getPhoneNos() { 23 | return phonenum; 24 | } 25 | 26 | public void addPhoneNo(String c1){ 27 | String[] newArray = new String[this.phonenum.length+1]; 28 | for(int i = 0; i< this.phonenum.length; i++){ 29 | newArray[i] = this.phonenum[i]; 30 | } 31 | newArray[this.phonenum.length] = c1; 32 | this.phonenum = newArray; 33 | } 34 | 35 | public void deletePhoneNo(String c1) { 36 | int result = 0; 37 | String[] newArray = new String[this.phonenum.length-1]; 38 | for (int i =0; i < this.phonenum.length; i++){ 39 | if(phonenum[i] == c1){ 40 | result = i; 41 | } 42 | } 43 | for (int i =0; i < result; i++){ 44 | newArray[i]=this.phonenum[i]; 45 | } 46 | for (int i =result+1; i < this.phonenum.length; i++){ 47 | newArray[i-1]=this.phonenum[i]; 48 | } 49 | this.phonenum = newArray; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | String msg =""; 55 | for(int i=0; i aList; 11 | 12 | public ScheduleBook() { 13 | 14 | aList = new ArrayList<>(); 15 | } 16 | 17 | public static void main(String[] args) throws Exception { 18 | new ScheduleBook().runApp(); 19 | 20 | } 21 | 22 | public void runApp() throws Exception { 23 | Scanner in = new Scanner(System.in); 24 | outer: 25 | do{ 26 | System.out.println("1. Load events from file\n" + 27 | "2. Save event to file\n" + 28 | "3. Show events\n" + 29 | "4. Add event\n" + 30 | "5. Remove event\n" + 31 | "6. Quit\n"); 32 | System.out.print("Option: "); 33 | switch (in.nextInt()){ 34 | case 1: 35 | System.out.print("Enter input filename: "); 36 | System.out.println(loadEvents(in.next()) +" event(s) successfully read."); 37 | break outer; 38 | case 2: 39 | System.out.print("Enter output filename: "); 40 | saveEvents(in.next()); 41 | break outer; 42 | case 3: 43 | System.out.print("Enter the date(YYYY MM DD): "); 44 | printEventsOn(Integer.parseInt(in.next()), Integer.parseInt(in.next()), Integer.parseInt(in.next())); 45 | break outer; 46 | case 4: 47 | System.out.print("Event type (onetime, daily or monthly) : "); 48 | addEvent(in); 49 | break outer; 50 | case 5: 51 | System.out.print("Event event id: "); 52 | removeEvent(in.nextInt()); 53 | break outer; 54 | case 6: 55 | System.out.print("Bye~"); 56 | System.exit(0); 57 | } 58 | }while(true); 59 | 60 | 61 | } 62 | 63 | public void saveEvents(String filename) throws Exception { 64 | FileWriter myWriter = new FileWriter(filename); 65 | for (Event event: aList) { 66 | if (event instanceof OnetimeEvent){ 67 | myWriter.write("Onetime " +event.getYear() +" " +event.getMonth()+" "+ event.getDay() + " "+event.getDescription()+"\n"); 68 | }else if (event instanceof MonthlyEvent){ 69 | myWriter.write("Monthly " + event.getDay() + " "+event.getDescription()+"\n"); 70 | }else if (event instanceof DailyEvent){ 71 | myWriter.write("Daily " +event.getDescription()+"\n"); 72 | } 73 | } 74 | myWriter.close(); 75 | } 76 | 77 | public int loadEvents(String filename) throws Exception { 78 | Scanner scanner = new Scanner(new File(filename)); 79 | int looper = 0; 80 | while (scanner.hasNext()){ 81 | String [] s = scanner.nextLine().split(" "); 82 | String detail = ""; 83 | switch (s[0]){ 84 | case "Onetime": 85 | detail = ""; 86 | for (int i = 4; i < s.length; i++) { 87 | detail +=s[i] +" "; 88 | } 89 | aList.add(new OnetimeEvent(detail, Integer.parseInt(s[1]), Integer.parseInt(s[2]), Integer.parseInt(s[3]))); 90 | break; 91 | case "Monthly": 92 | detail = ""; 93 | for (int i = 2; i < s.length; i++) { 94 | detail +=s[i]+" "; 95 | } 96 | aList.add(new MonthlyEvent(detail, Integer.parseInt(s[1]))); 97 | break; 98 | case "Daily": 99 | detail = ""; 100 | for (int i = 1; i < s.length; i++) { 101 | detail +=s[i]+" "; 102 | } 103 | aList.add(new DailyEvent(detail)); 104 | break; 105 | } 106 | looper++; 107 | } 108 | return looper; 109 | } 110 | 111 | public void addEvent(Scanner in) { 112 | switch (in.next()){ 113 | case "onetime": 114 | System.out.print("Enter the date(YYYY MM DD): "); 115 | int yyyy = Integer.parseInt(in.next()); 116 | int mm = Integer.parseInt(in.next()); 117 | int dd = Integer.parseInt(in.next()); 118 | System.out.print("Enter description: "); 119 | String description = in.next(); 120 | aList.add(new OnetimeEvent(description, yyyy, mm , dd)); 121 | break; 122 | case "monthly": 123 | System.out.print("Enter the day of month: "); 124 | int day = Integer.parseInt(in.next()); 125 | System.out.print("Enter description: "); 126 | String description2 = in.next(); 127 | aList.add(new MonthlyEvent(description2, day)); 128 | break; 129 | case "daily": 130 | System.out.print("Enter description: "); 131 | String description3 = in.next(); 132 | aList.add(new DailyEvent(description3)); 133 | break; 134 | } 135 | } 136 | 137 | public void removeEvent(int id) { 138 | aList.removeIf(event -> event.getId() == id); 139 | } 140 | 141 | 142 | /** 143 | * You are not allowed to modify this method 144 | * Print all events occur on the given date 145 | * @param year - year of the event 146 | * @param month - month of the event 147 | * @param day - day of the event 148 | */ 149 | public void printEventsOn(int year, int month, int day) { 150 | for (Event a : aList) { 151 | if (a.occursOn(year, month, day)) { 152 | System.out.println(a); 153 | } 154 | } 155 | } 156 | 157 | } 158 | -------------------------------------------------------------------------------- /Lab 10 Inheritance & Polymorphism/21237387_lab10/src/ScheduleBookTester.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | import java.util.Scanner; 4 | 5 | public class ScheduleBookTester { 6 | 7 | List aList; 8 | 9 | public ScheduleBookTester() { 10 | aList = new ArrayList<>(); 11 | } 12 | 13 | public static void main(String[] args) throws Exception { 14 | new ScheduleBookTester().runApp(); 15 | 16 | } 17 | 18 | public void runApp() throws Exception { 19 | 20 | loadEvents(); 21 | 22 | System.out.print("Enter the date(YYYY MM DD): "); 23 | Scanner in = new Scanner(System.in); 24 | int year = in.nextInt(); 25 | int month = in.nextInt(); 26 | int day = in.nextInt(); 27 | printEventsOn(year, month, day); 28 | 29 | } 30 | 31 | 32 | public void loadEvents() throws Exception{ 33 | 34 | aList.add(new OnetimeEvent("See dentist", 2030, 5, 2)); 35 | aList.add(new OnetimeEvent("Mary's Birthday", 2030, 12, 23)); 36 | aList.add(new OnetimeEvent("Christmas Party", 2030, 12, 25)); 37 | aList.add(new OnetimeEvent("Visit Bob", 2030, 11, 12)); 38 | aList.add(new OnetimeEvent("Visit Ada", 2030, 11, 5)); 39 | aList.add(new MonthlyEvent("Pay Bills", 5)); 40 | aList.add(new MonthlyEvent("Visit Dad", 2)); 41 | aList.add(new MonthlyEvent("Visit Dad", 23)); 42 | aList.add(new DailyEvent("Yoga class")); 43 | 44 | } 45 | 46 | 47 | public void printEventsOn(int year, int month, int day) { 48 | for (Event a : aList) { 49 | if (a.occursOn(year, month, day)) { 50 | System.out.println(a); 51 | } 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Lab 10 Inheritance & Polymorphism/lab10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 10 Inheritance & Polymorphism/lab10.pdf -------------------------------------------------------------------------------- /Lab 11 More about Inheritance & Polymorphism/21237387_lab11.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 11 More about Inheritance & Polymorphism/21237387_lab11.docx -------------------------------------------------------------------------------- /Lab 11 More about Inheritance & Polymorphism/21237387_lab11/src/ABCEmployee.java: -------------------------------------------------------------------------------- 1 | //You are not allowed to modify this class 2 | 3 | /** 4 | * A class that represents an employee in ABC company 5 | */ 6 | public class ABCEmployee { 7 | 8 | private String name; 9 | private int id; 10 | private String dept; 11 | private String position; 12 | 13 | public ABCEmployee(String name, int id, String dept, String position) 14 | { 15 | this.name = name; 16 | this.id = id; 17 | this.dept = dept; 18 | this.position = position; 19 | 20 | } 21 | 22 | public ABCEmployee(String name, int id) 23 | { 24 | this(name, id, "", ""); 25 | 26 | } 27 | 28 | public ABCEmployee() 29 | { 30 | this("", 0, "",""); 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public int getId() { 42 | return id; 43 | } 44 | 45 | public void setId(int id) { 46 | this.id = id; 47 | } 48 | 49 | public String getDept() { 50 | return dept; 51 | } 52 | 53 | public void setDept(String dept) { 54 | this.dept = dept; 55 | } 56 | 57 | public String getPosition() { 58 | return position; 59 | } 60 | 61 | public void setPosition(String position) { 62 | this.position = position; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return "ABCEmployee{" + 68 | "name='" + name + '\'' + 69 | ", id=" + id + 70 | ", dept='" + dept + '\'' + 71 | ", position='" + position + '\'' + 72 | '}'; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Lab 11 More about Inheritance & Polymorphism/21237387_lab11/src/ABCEmployeeTester.java: -------------------------------------------------------------------------------- 1 | public class ABCEmployeeTester { 2 | 3 | public static void main(String[] args) { 4 | new ABCEmployeeTester().runApp(); 5 | } 6 | 7 | public void runApp(){ 8 | 9 | ABCEmployee employtee = new ABCEmployee("", 0, "", ""); 10 | 11 | employtee.setName("Susan Meyers"); 12 | employtee.setId(47988); 13 | employtee.setDept("Accounting"); 14 | employtee.setPosition("Vice President"); 15 | 16 | 17 | ABCEmployee employtee1 = new ABCEmployee("Mark Jones", 39119); 18 | employtee1.setDept("IT"); 19 | employtee1.setPosition("Programmer"); 20 | 21 | ABCEmployee employtee2 = new ABCEmployee("Joy Rogers",81774,"Manufacturing","Engineer"); 22 | 23 | 24 | System.out.println(employtee); 25 | System.out.println(employtee1); 26 | System.out.println(employtee2); 27 | 28 | 29 | } 30 | 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Lab 11 More about Inheritance & Polymorphism/21237387_lab11/src/Circle.java: -------------------------------------------------------------------------------- 1 | public class Circle extends Shape{ 2 | private double radius; 3 | 4 | public Circle(){ 5 | super() ; 6 | this.radius = 1; 7 | } 8 | 9 | public Circle(String name, double radius){ 10 | super(name) ; 11 | this.radius = radius; 12 | } 13 | 14 | @Override 15 | public double area(){ 16 | return Math.PI*(radius*radius); 17 | } 18 | 19 | @Override 20 | public double perimeter(){ 21 | return 2*Math.PI*radius; 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return "Circle: " + this.getName()+"\nRadius: "+this.radius; 27 | } 28 | 29 | 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Lab 11 More about Inheritance & Polymorphism/21237387_lab11/src/MyShapeList.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class MyShapeList { 5 | 6 | private List shapeList; 7 | 8 | public MyShapeList() { 9 | shapeList = new ArrayList<>(); 10 | } 11 | 12 | public void runApp() { 13 | addShapes(); 14 | System.out.println("=================================="); 15 | System.out.println("Print List: "); 16 | printList(); 17 | System.out.println("=================================="); 18 | System.out.printf("Total Area: %.2f\n", totalArea()); 19 | System.out.printf("Total Perimeter: %.2f\n", totalPerimeter()); 20 | System.out.println("No. of Circles: " + numOfCircle()); 21 | System.out.println("No. of Triangles: " + numOfTriangle()); 22 | 23 | } 24 | 25 | public void addShapes() { 26 | Circle deafultcircle = new Circle(); 27 | Circle circle = new Circle("MyCircle", 10); 28 | Triangle deafulttriangle = new Triangle(); 29 | Triangle triangle = new Triangle("EquilateralTriangle", 6); 30 | Triangle triangle1 = new Triangle("MyTriangle", 6,7,8); 31 | 32 | shapeList.add(deafultcircle); 33 | shapeList.add(circle); 34 | shapeList.add(deafulttriangle); 35 | shapeList.add(triangle); 36 | shapeList.add(triangle1); 37 | } 38 | 39 | public void printList() { 40 | for(int i = 0;i pList; 6 | 7 | public BookInfo() { 8 | this.pList = new ArrayList(); 9 | } 10 | 11 | public static void main(String[] args) { 12 | new BookInfo().runApp(); 13 | } 14 | 15 | public void runApp() { 16 | addPublications(); 17 | showAllPublications(); 18 | //test the methods here... 19 | 20 | } 21 | 22 | public void addPublications() { 23 | pList.add(new Book("Cindy and the Candy Factory", "AA Press", "Ben Don")); 24 | pList.add(new Book("Secret Code", "Ma House", "Dim Green")); 25 | pList.add(new Magazine("Living", "Person", 5,3)); 26 | pList.add(new Magazine("Cooking", "Person", 3,10)); 27 | pList.add(new KidsMagazine("Tinkering", "Teens World", 3,10, 6, 12)); 28 | pList.add(new KidsMagazine("Tinkering", "Teens World", 3,11, 6, 12)); 29 | pList.add(new KidsMagazine("Tinkering", "Teens World", 3,12, 6, 12)); 30 | pList.add(new KidsMagazine("My Dream", "Teens World", 8,5, 3, 6)); 31 | 32 | 33 | 34 | 35 | } 36 | 37 | public void showAllPublications() { 38 | for(int i =0;i ((KidsMagazine) pList.get(i)).getMinimumAge() && age <((KidsMagazine) pList.get(i)).getMaximumAge()){ 83 | pList.get(i).print(); 84 | System.out.println(); 85 | } 86 | } 87 | 88 | 89 | } 90 | 91 | public void showPublicationByTitle(String s) { 92 | for(int i =0;i pList; 6 | 7 | public CheckoutableCollection() { 8 | this.pList = new ArrayList(); 9 | } 10 | 11 | public static void main(String[] args) { 12 | new CheckoutableCollection().runApp(); 13 | } 14 | 15 | public void runApp() { 16 | addPublications(); 17 | 18 | //test the methods here... 19 | 20 | //showKidsMagazineByAge(6); 21 | 22 | 23 | } 24 | 25 | public void addPublications() { 26 | pList.add(new Book("Cindy and the Candy Factory", "AA Press", "Ben Don")); 27 | pList.add(new Book("Secret Code", "Ma House", "Dim Green")); 28 | 29 | pList.add(new Megazine("Living", "Person", 5, 3)); 30 | pList.add(new Megazine("Cooking", "Person", 3, 10)); 31 | 32 | pList.add(new KidsMegazine("Tinkering", "Teens world", 3, 10, 6, 12)); 33 | pList.add(new KidsMegazine("My Dream", "Teens world", 8, 5, 3, 6)); 34 | 35 | pList.forEach(checkoutable -> { 36 | checkoutable.checkout(); 37 | System.out.println("Return Date: "+checkoutable.returnDate()+"\n"); 38 | }); 39 | 40 | 41 | 42 | 43 | 44 | } 45 | 46 | 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/Circle.java: -------------------------------------------------------------------------------- 1 | public class Circle extends Shape implements Resizable{ 2 | 3 | private double radius; 4 | 5 | public Circle() { 6 | this(' ', 1); 7 | } 8 | 9 | public Circle(char name, double radius) { 10 | super(name); 11 | this.radius = radius; 12 | } 13 | 14 | public double area() { 15 | return radius * radius * Math.PI; 16 | } 17 | 18 | public double perimeter() { 19 | return 2 * Math.PI * radius; 20 | } 21 | 22 | public String toString() { 23 | return "Circle " + super.getName() + "\nRadius: " + radius + "\n"; 24 | } 25 | 26 | 27 | @Override 28 | public void resize() { 29 | radius *= DEFAULT_FACTOR; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/EvenChecker.java: -------------------------------------------------------------------------------- 1 | public class EvenChecker implements NumChecker{ 2 | @Override 3 | public boolean check(int n) { 4 | return n %2 == 0; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/KidsMegazine.java: -------------------------------------------------------------------------------- 1 | public class KidsMegazine extends Megazine{ 2 | private int minimumAge, maximumAge; 3 | 4 | public KidsMegazine(String title, String publisher, int volume, int issue, int minimumAge, int maximumAge) { 5 | super(title, publisher, volume, issue); 6 | this.minimumAge = minimumAge; 7 | this.maximumAge = maximumAge; 8 | } 9 | 10 | public void print(){ 11 | String str =String.format("Title: %s\nPublisher: %s\nVolume: %s\nIssue: %s\nAge Range: %d-%d\n", getTitle(), getPublisher(), getVolume(), getIssue(), minimumAge, maximumAge); 12 | System.out.print(str); 13 | } 14 | 15 | public int getMinimumAge() { 16 | return minimumAge; 17 | } 18 | 19 | public int getMaximumAge() { 20 | return maximumAge; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | // write your code here 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/Megazine.java: -------------------------------------------------------------------------------- 1 | import java.time.LocalDate; 2 | 3 | public class Megazine extends Publication implements Checkoutable{ 4 | 5 | private int volume; 6 | private int issue; 7 | private LocalDate checkoutDate; 8 | 9 | 10 | 11 | public Megazine(String title, String publisher, int volume, int issue) { 12 | super(title, publisher); 13 | this.volume = volume; 14 | this.issue = issue; 15 | } 16 | 17 | @Override 18 | public void print(){ 19 | String str =String.format("Title: %s\nPublisher: %s\nVolume: %s\nIssue: %s\n", getTitle(), getPublisher(), volume, issue); 20 | System.out.print(str); 21 | } 22 | 23 | public int getVolume() { 24 | return volume; 25 | } 26 | 27 | public int getIssue() { 28 | return issue; 29 | } 30 | 31 | @Override 32 | public void checkout() { 33 | this.checkoutDate = LocalDate.now(); 34 | print(); 35 | System.out.println("Checkout Date: "+checkoutDate.toString()); 36 | } 37 | 38 | @Override 39 | public LocalDate returnDate() { 40 | return checkoutDate.plusDays(7); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/Movable.java: -------------------------------------------------------------------------------- 1 | public interface Movable { 2 | 3 | int DX = 5; 4 | int DY = 5; 5 | 6 | int getX(); 7 | 8 | int getY(); 9 | 10 | void setX(int x); 11 | 12 | void setY(int y); 13 | 14 | void moveLeft(); 15 | 16 | void moveRight(); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/MovableCollection.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MovableCollection { 4 | private List movableList; 5 | 6 | public MovableCollection() { 7 | movableList = new ArrayList(); 8 | } 9 | 10 | public static void main(String args[]) { 11 | new MovableCollection().runApp(); 12 | } 13 | 14 | void runApp() { 15 | movableList.add(new Point('A', 1, 2)); 16 | movableList.add(new Point('B', 3, 4)); 17 | movableList.add(new Point('C', 5, 6)); 18 | 19 | //your code goes here... 20 | 21 | movableList.add(new Rectangle('D', 7, 8, 10, 20)); 22 | movableList.add(new Rectangle('E', 9, 10, 5, 10)); 23 | 24 | movableList.forEach(e -> System.out.println(e.toString())); 25 | 26 | 27 | movableList.forEach(Movable::moveRight); 28 | System.out.println("After moving right..."); 29 | movableList.forEach(e -> System.out.println(e.toString())); 30 | 31 | 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/MyProg1.java: -------------------------------------------------------------------------------- 1 | public class MyProg1 { 2 | public static void main(String[] args){ 3 | new MyProg1().runApp(); 4 | } 5 | 6 | /** 7 | * Print all the elements in the given integer array that pass the check 8 | * 9 | * @param a - an integer array 10 | * @param c - a NumberChecker 11 | */ 12 | public void printElements(int[] a, NumChecker c){ 13 | for (int i = 0; i < a.length; i++){ 14 | if(c.check(a[i])){ 15 | System.out.print(a[i] + " "); 16 | } 17 | } 18 | System.out.println(); 19 | } 20 | 21 | public void runApp(){ 22 | int[] intAry = {34, 6, 21, -1, -32, 24, -97, 76, 9}; 23 | 24 | System.out.print("Positive Elements: "); 25 | NumChecker pc = new PositiveChecker(); 26 | printElements(intAry, pc); 27 | 28 | System.out.print("Even Elements: "); 29 | NumChecker ec = new EvenChecker(); 30 | printElements(intAry, ec); 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/MyProg2.java: -------------------------------------------------------------------------------- 1 | public class MyProg2 { 2 | public static void main(String[] args){ 3 | new MyProg2().runApp(); 4 | } 5 | 6 | /** 7 | * Print all the elements in the given integer array that pass the check 8 | * 9 | * @param a - an integer array 10 | * @param c - a NumberChecker 11 | */ 12 | public void printElements(int[] a, NumChecker c){ 13 | for (int i = 0; i < a.length; i++){ 14 | if(c.check(a[i])){ 15 | System.out.print(a[i] + " "); 16 | } 17 | } 18 | System.out.println(); 19 | } 20 | 21 | public void runApp(){ 22 | int[] intAry = {34, 6, 21, -1, -32, 24, -97, 76, 9}; 23 | 24 | System.out.print("Positive Elements: "); 25 | printElements(intAry, new NumChecker() { //Anonymous class 26 | @Override 27 | public boolean check(int n) { 28 | return n > 0; 29 | } 30 | }); 31 | 32 | System.out.print("Even Elements: "); 33 | printElements(intAry, new NumChecker() { //Anonymous class 34 | @Override 35 | public boolean check(int n) { 36 | return n %2 == 0; 37 | } 38 | }); 39 | 40 | } 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/MyProg3.java: -------------------------------------------------------------------------------- 1 | public class MyProg3 { 2 | public static void main(String[] args){ 3 | new MyProg3().runApp(); 4 | } 5 | 6 | /** 7 | * Print all the elements in the given integer array that pass the check 8 | * 9 | * @param a - an integer array 10 | * @param c - a NumberChecker 11 | */ 12 | public void printElements(int[] a, NumChecker c){ 13 | for (int i = 0; i < a.length; i++){ 14 | if(c.check(a[i])){ 15 | System.out.print(a[i] + " "); 16 | } 17 | } 18 | System.out.println(); 19 | } 20 | 21 | public void runApp(){ 22 | int[] intAry = {34, 6, 21, -1, -32, 24, -97, 76, 9}; 23 | 24 | System.out.print("Positive Elements: "); 25 | printElements(intAry,(n)->{return n > 0;}); //Lambda expression 26 | 27 | System.out.print("Even Elements: "); 28 | printElements(intAry, (n)->{return n %2 == 0;}); //Lambda expression 29 | 30 | } 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/NumChecker.java: -------------------------------------------------------------------------------- 1 | public interface NumChecker { 2 | public boolean check(int n); 3 | } 4 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/Point.java: -------------------------------------------------------------------------------- 1 | public class Point implements Movable { 2 | private char name; 3 | private int x; 4 | private int y; 5 | 6 | public Point(char name, int x, int y) { 7 | this.name = name; 8 | this.x = x; 9 | this.y = y; 10 | } 11 | 12 | public int getX() { 13 | return this.x; 14 | } 15 | 16 | public int getY() { 17 | return this.y; 18 | } 19 | 20 | public void setX(int x) { 21 | this.x = x; 22 | } 23 | 24 | public void setY(int y) { 25 | this.y = y; 26 | } 27 | 28 | public void moveLeft() { 29 | this.x = this.x - DX; 30 | } 31 | 32 | public void moveRight() { 33 | this.x = this.x + DX; 34 | } 35 | 36 | public String toString() { 37 | return "Point " + name + " (" + x + ", " + y + ")\n"; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/PositiveChecker.java: -------------------------------------------------------------------------------- 1 | public class PositiveChecker implements NumChecker{ 2 | @Override 3 | public boolean check(int n) { 4 | return n > 0; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/Publication.java: -------------------------------------------------------------------------------- 1 | public abstract class Publication { 2 | private String title; 3 | private String publisher; 4 | 5 | public Publication(String title, String publisher) { 6 | this.title = title; 7 | this.publisher = publisher; 8 | } 9 | public void print(){ 10 | String str =String.format("Title: %s\nPublisher: %s\n", title, publisher); 11 | System.out.print(str); 12 | } 13 | 14 | 15 | public String getTitle() { 16 | return title; 17 | } 18 | 19 | public String getPublisher() { 20 | return publisher; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/Rectangle.java: -------------------------------------------------------------------------------- 1 | public class Rectangle extends Shape implements Movable , Resizable{ 2 | private double width; 3 | private double length; 4 | 5 | private int x; 6 | private int y; 7 | 8 | 9 | 10 | public Rectangle() { 11 | this(' ',1,1, 1, 1); 12 | } 13 | 14 | public Rectangle(char name,int x, int y, double width, double length) { 15 | super(name); 16 | this.x = x; 17 | this.y = y; 18 | this.width = width; 19 | this.length = length; 20 | } 21 | 22 | public double area() { 23 | return width * length; 24 | } 25 | 26 | public double perimeter() { 27 | return (width + length) * 2; 28 | } 29 | 30 | public String toString() { 31 | return "Rectangle " + super.getName() + " (" +x+", " +y+")" +"\nWidth: " + width + "\nLength: " + length + "\n"; 32 | } 33 | 34 | 35 | @Override 36 | public int getX() { 37 | return x; 38 | } 39 | 40 | @Override 41 | public int getY() { 42 | return y; 43 | } 44 | 45 | @Override 46 | public void setX(int x) { 47 | this.x=x; 48 | } 49 | 50 | @Override 51 | public void setY(int y) { 52 | this.y = y; 53 | } 54 | 55 | @Override 56 | public void moveLeft() { 57 | x = x-DX; 58 | } 59 | 60 | @Override 61 | public void moveRight() { 62 | x = x+DX; 63 | } 64 | 65 | @Override 66 | public void resize() { 67 | width *= DEFAULT_FACTOR; 68 | length *= DEFAULT_FACTOR; 69 | } 70 | } 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/Resizable.java: -------------------------------------------------------------------------------- 1 | public interface Resizable { 2 | double DEFAULT_FACTOR = 1.5; 3 | public abstract void resize(); 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/ResizableCollection.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class ResizableCollection { 4 | private ArrayList Resizable; 5 | 6 | public ResizableCollection() { 7 | Resizable = new ArrayList(); 8 | } 9 | 10 | public static void main(String args[]) { 11 | new MovableCollection().runApp(); 12 | } 13 | 14 | void runApp() { 15 | Resizable.add(new Rectangle('D', 7, 8, 10, 20)); 16 | Resizable.add(new Rectangle('E', 9, 10, 5, 10)); 17 | 18 | Resizable.add(new Circle('F', 5)); 19 | Resizable.add(new Circle('G', 15)); 20 | 21 | 22 | Resizable.forEach(e -> System.out.println(e.toString())); 23 | 24 | 25 | Resizable.forEach(resizable -> resizable.resize()); 26 | System.out.println("After resize..."); 27 | Resizable.forEach(e -> System.out.println(e.toString())); 28 | 29 | 30 | 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/21237387_lab13/src/Shape.java: -------------------------------------------------------------------------------- 1 | //You are not allowed to modify this program 2 | 3 | public class Shape { 4 | private char name; 5 | 6 | public Shape() { 7 | this.name = ' '; 8 | } 9 | 10 | public Shape(char name) { 11 | this.name = name; 12 | } 13 | 14 | public char getName() { 15 | return this.name; 16 | } 17 | 18 | public String toString() { 19 | return "Shape " + this.name; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Lab 13 Interface and Lambda Expression/lab13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/2246250622/COMP2026-Problem-Solving-using-Object-Oriented-Programming/63cc09523d501d52ed94d2a8049ab4ff0cd7146c/Lab 13 Interface and Lambda Expression/lab13.pdf --------------------------------------------------------------------------------