├── .github └── workflows │ └── main.yml ├── .gitignore ├── .vscode └── settings.json ├── Basics ├── Classes │ ├── MyMain.java │ └── Recepitionist.java ├── Constructors │ ├── ConstructorsMain.java │ └── Recepitionist.java ├── ControlStatment │ ├── Continue.java │ ├── DoWhile.java │ ├── ForLoop.java │ ├── If-else.java │ └── WhileLoop.java ├── Data Structure │ ├── Queue-datastructure │ │ └── QueueX.java │ ├── Queue-ticketingProcess │ │ ├── Queue.java │ │ └── main.java │ ├── Stack-Paranthesizer │ │ ├── Paranthesizer.java │ │ └── StackX.java │ ├── Stack-Reverser │ │ ├── Reverser.java │ │ └── StackX.java │ ├── binaryTree │ │ └── BinarySearchTree.java │ ├── linkedList_datastructure │ │ └── SinglyLinkedList.java │ ├── queue_datastructure │ │ └── Queue.java │ └── stack_datastructure │ │ └── Stack.java ├── Design Pattern │ ├── Bridge Pattern │ │ ├── .classpath │ │ ├── .project │ │ └── src │ │ │ ├── Assemble.java │ │ │ ├── AutoMobile.java │ │ │ ├── Bus.java │ │ │ ├── Produce.java │ │ │ ├── User.java │ │ │ └── WorkShop.java │ ├── Command Pattern │ │ ├── .classpath │ │ ├── .project │ │ └── src │ │ │ ├── Command.java │ │ │ ├── KitchedLight.java │ │ │ ├── Light.java │ │ │ ├── LightOffCommand.java │ │ │ ├── LightOnCommand.java │ │ │ ├── LivingRoomLight.java │ │ │ └── Test.java │ ├── Template Pattern │ │ ├── .classpath │ │ ├── .project │ │ └── src │ │ │ ├── Beverages.java │ │ │ ├── Coffee.java │ │ │ ├── Tea.java │ │ │ └── Test.java │ ├── abstractFactory │ │ ├── AbstractFactory.java │ │ ├── AbstractFactoryPatternDemo.java │ │ ├── Blue.java │ │ ├── Circle.java │ │ ├── Color.java │ │ ├── ColorFactory.java │ │ ├── FactoryProducer.java │ │ ├── Green.java │ │ ├── Rectangle.java │ │ ├── Red.java │ │ ├── Shape.java │ │ ├── ShapeFactory.java │ │ └── Square.java │ ├── decorator │ │ ├── Consultant.java │ │ ├── Employee.java │ │ ├── EmployeeCertification.java │ │ ├── EmployeeQualification.java │ │ ├── Engineer.java │ │ ├── Manager.java │ │ ├── TestPattern.java │ │ └── WorkExperience.java │ ├── singleton │ │ ├── Singleton.java │ │ ├── Test.java │ │ ├── TestSingleton.java │ │ ├── TestThreadSingleton.java │ │ └── ThreadSafeSingleton.java │ └── strategy │ │ ├── Duck.java │ │ ├── FlyNoWay.java │ │ ├── FlyRocketPower.java │ │ ├── FlyWithWings.java │ │ ├── IFlyBehaviour.java │ │ ├── IQuackBehaviour.java │ │ ├── ModelDuck.java │ │ ├── ModelQuack.java │ │ ├── MollardDuck.java │ │ ├── Quack.java │ │ └── TestDuck.java ├── GettersAndSetters │ ├── GettersAndSettersMain.java │ └── Recepitionist.java ├── Inheritance │ ├── InheritanceMain.java │ ├── Rectangle.java │ └── Shape.java ├── Input.java ├── Polymorphism │ └── PolymorphismMain.java ├── Printing.java ├── Thread │ ├── ThreadJoin.java │ ├── ThreadNotifyTest.java │ └── ThreadYield.java └── Variables.java ├── Exercises └── L01 │ ├── Exercise1.java │ ├── Exercise2.java │ ├── Exercise3.java │ └── Exercise4.java ├── HelloWorld.java ├── LICENSE ├── Lab Sheets ├── Lab Sheet 01 │ ├── Lab Sheet 01.pdf │ ├── Lab1Ex1.java │ ├── Lab1Ex2.java │ ├── Lab1Ex3.java │ ├── Lab1Ex4.java │ ├── Lab1Ex5.java │ ├── Lab1Ex6.java │ └── Lab1Ex7.java ├── Lab Sheet 02 │ ├── Lab Sheet 02.pdf │ ├── Lakshan │ │ ├── Lab2Ex1.java │ │ ├── Lab2Ex2.java │ │ ├── Lab2Ex3.java │ │ ├── Lab2Ex4.java │ │ └── Lab2Ex5 │ │ │ ├── Demo.java │ │ │ └── EvenOddNumber.java │ └── Sahan │ │ ├── Exercise1.java │ │ ├── Exercise2.java │ │ ├── Exercise3.java │ │ └── Exercise4.java ├── Lab Sheet 03 │ ├── Lab Sheet 03.pdf │ ├── Lakshan │ │ ├── Exercise 1 │ │ │ ├── Student.Java │ │ │ └── Test.java │ │ ├── Exercise 10 │ │ │ ├── Cat.java │ │ │ ├── Dog.java │ │ │ ├── Main.java │ │ │ └── Pet.java │ │ ├── Exercise 11 │ │ │ ├── Cat.java │ │ │ ├── Dog.java │ │ │ ├── Main.java │ │ │ └── Pet.java │ │ ├── Exercise 2 │ │ │ ├── Student.Java │ │ │ └── Test.java │ │ ├── Exercise 3 │ │ │ ├── Student.Java │ │ │ └── Test.java │ │ ├── Exercise 4 │ │ │ ├── Student.Java │ │ │ └── Test.java │ │ ├── Exercise 5 │ │ │ ├── InheritanceDemo.java │ │ │ └── Person.java │ │ ├── Exercise 6 │ │ │ ├── InheritanceDemo.java │ │ │ ├── Person.java │ │ │ └── Student.java │ │ ├── Exercise 7 │ │ │ ├── InheritanceDemo.java │ │ │ ├── PartTimeStudent.java │ │ │ ├── Person.java │ │ │ └── Student.java │ │ ├── Exercise 8 │ │ │ ├── Cat.java │ │ │ ├── Main.java │ │ │ └── Pet.java │ │ └── Exercise 9 │ │ │ ├── Cat.java │ │ │ ├── Main.java │ │ │ └── Pet.java │ ├── Sahan │ │ ├── Exercise 1 │ │ │ ├── Student.java │ │ │ └── Test.java │ │ ├── Exercise 2 │ │ │ ├── Student.java │ │ │ └── Test.java │ │ ├── Exercise 3 │ │ │ ├── Student.java │ │ │ └── Test.java │ │ ├── Exercise 4 │ │ │ ├── Student.java │ │ │ └── Test.java │ │ ├── Exercise 5 │ │ │ ├── InheritanceDemo.java │ │ │ └── Person.java │ │ ├── Exercise 6 │ │ │ ├── InheritanceDemo.java │ │ │ ├── Person.java │ │ │ └── Student.java │ │ └── Exercise 7 │ │ │ ├── InheritanceDemo.java │ │ │ ├── PartTimeStudent.java │ │ │ ├── Person.java │ │ │ └── Student.java │ └── Thushara │ │ ├── Lab03Ex01 │ │ ├── Student.java │ │ └── Test.java │ │ ├── Lab03Ex02 │ │ ├── Student.java │ │ └── Test.java │ │ ├── Lab03Ex03 │ │ ├── Student.java │ │ └── Test.java │ │ ├── Lab03Ex04 │ │ ├── Student.java │ │ └── Test.java │ │ ├── Lab03Ex05-06-07 │ │ ├── InheritanceDemo.java │ │ ├── PartTimeStudent.java │ │ ├── Person.java │ │ └── Student.java │ │ └── Lab03Ex08-09-10-11 │ │ ├── Cat.java │ │ ├── Dog.java │ │ ├── Main.java │ │ └── Pet.java ├── Lab Sheet 04 │ ├── Lab Sheet 04.pdf │ └── Lab04Ex01 │ │ ├── Student.java │ │ └── StudentApp.java └── Lab Sheet 05 │ └── Lab Sheet 05.pdf ├── MID Exam └── Revision │ ├── Answers.txt │ └── MID revision.docx ├── Online Assignement 2021 June ├── Online Assignement 2021 - IT2030.pdf ├── Randula[Answer] │ ├── BlueSoldier.java │ ├── Board.java │ ├── GreenSoldier.java │ ├── Hunter.java │ ├── MainApp.java │ ├── RedSoldier.java │ ├── Soldier.java │ └── SoundException.java ├── Sahan [Answer] │ ├── BlueSoldier.java │ ├── Board.java │ ├── GreenSoldier.java │ ├── Hunt.java │ ├── Hunter.java │ ├── MainApp.java │ ├── RedSoldier.java │ ├── Soldier.java │ └── SoundException.java └── Thushara [Answer] │ ├── Board.java │ ├── Hunter.java │ ├── Main.java │ ├── Soldier.java │ └── SoundException.java ├── Other Questions ├── ATM │ └── Atm.java ├── Gender │ └── Gender.java └── Sahan │ ├── Question 11 │ ├── Question 1 │ │ ├── A.java │ │ └── MyClass.java │ ├── Question 2 │ │ ├── InheritanceDemo.java │ │ ├── PartTimeStudent.java │ │ ├── Person.java │ │ └── Student.java │ └── Question 3 │ │ ├── Arithmetic.java │ │ ├── Test.java │ │ └── TestInt.java │ ├── Question 12 │ ├── Question 1 │ │ └── Main.java │ ├── Question 2 │ │ └── MutipleCatch.java │ ├── Question 3 │ │ └── Main.java │ ├── Question 4 │ │ └── TestFinallyBlock.java │ ├── Question 5 │ │ └── Demo.java │ └── Question 6 │ │ └── SoundException.java │ ├── Question 2 │ └── Main.java │ ├── Question 3 │ └── Main.java │ ├── Question 4 │ └── Main.java │ ├── Question 6 │ ├── Date.java │ └── DateTest.java │ ├── Question 9 │ ├── Car.java │ ├── Ford.java │ ├── Sedan.java │ └── Truck.java │ └── Questions │ ├── Page 1.jpg │ ├── Page 2.jpg │ ├── Page 3.jpg │ ├── Page 4.jpg │ ├── Page 5.jpg │ ├── Page 6.jpg │ └── Page 7.jpg ├── Past Papers ├── 2021 OOP Final.pdf ├── IT2030 - Mock paper.pdf ├── Mock Exam.pdf └── Mock paper Answers-20211018 │ ├── Mock Esay Answers │ ├── Designpattern3 │ │ ├── .classpath │ │ ├── .project │ │ ├── .settings │ │ │ └── org.eclipse.jdt.core.prefs │ │ └── src │ │ │ ├── Action.java │ │ │ ├── Developer.java │ │ │ ├── IDE.java │ │ │ ├── Receiver.java │ │ │ ├── SaveAction.java │ │ │ └── SaveAllAction.java │ └── Threads │ │ └── WrappingPaperArt.java │ └── mock MCQ - answers.txt ├── README.md ├── Resources ├── Code Conventions.pdf ├── Exceptions.jpg ├── Hierarchy of Java Exception Classes.jpg ├── Java Exception - CheatSheet.pdf ├── Java Generics - CheatSheet.pdf ├── Java OOP - CheatSheet.pdf ├── Javabook.pdf └── Useful Links.txt └── Tutorials ├── Tutorial 02 ├── Turorial2Question1answer │ ├── Main.java │ └── Student.java └── Tutorial 02.pdf ├── Tutorial 03 ├── Tut03Ex01 │ └── DemoApp.java ├── Tut03Ex02 │ └── Main.java └── Tutorial 03.pdf ├── Tutorial 04 ├── Tut04Ex01 │ ├── Book.java │ ├── IDisplay.java │ ├── IInput.java │ ├── Main.java │ └── Student.java ├── Tut04Ex02 │ ├── Account.java │ ├── FixedDepositAccount.java │ ├── Main.java │ └── SavingAccount.java └── Tutorial 04.pdf └── Tutorial 05 ├── Tut05Ex01Q01 └── Main.java ├── Tut05Ex01Q02 └── Main.java ├── Tut05Ex01Q03 └── Main.java └── Tutorial 05.pdf /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | 3 | jobs: 4 | contrib-readme-job: 5 | runs-on: ubuntu-latest 6 | name: A job to automate Contributors in README 7 | steps: 8 | - name: Contribute List 9 | uses: akhilmhdh/contributors-readme-action@v2.2.1 10 | with: 11 | image_size: 100(px) 12 | columns_per_row: 3 13 | committer_username: ThusharaX 14 | commit_message: Contributors list updated 15 | env: 16 | GITHUB_TOKEN: ${{ secrets.CON_TOKEN }} 17 | 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.project.sourcePaths": [ 3 | "Basics", 4 | "Basics/Classes", 5 | "Basics/Constructors", 6 | "Basics/GettersAndSetters", 7 | "Basics/Inheritance", 8 | "Basics/Polymorphism", 9 | "Exercises/L01", 10 | "Exercises/L02", 11 | "Exercises/L03", 12 | "Lab Sheets/Lab Sheet 01", 13 | "Lab Sheets/Lab Sheet 02", 14 | "" 15 | ] 16 | } -------------------------------------------------------------------------------- /Basics/Classes/MyMain.java: -------------------------------------------------------------------------------- 1 | public class MyMain { 2 | public static void main(String args[]) { 3 | Recepitionist recep1= new Recepitionist(); 4 | boolean status = recep1.checkRoomAvailability(3); 5 | double bill = recep1.generateBill(); 6 | recep1.takeCustomerFeddback(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Basics/Classes/Recepitionist.java: -------------------------------------------------------------------------------- 1 | public class Recepitionist { 2 | private int staffID; 3 | private String name; 4 | private String telephoneNumber; 5 | 6 | public Boolean checkRoomAvailability(int roomNumber){ 7 | return true; 8 | } 9 | public double generateBill(){ 10 | return 0; 11 | } 12 | public void takeCustomerFeddback() { } 13 | } 14 | -------------------------------------------------------------------------------- /Basics/Constructors/ConstructorsMain.java: -------------------------------------------------------------------------------- 1 | public class ConstructorsMain { 2 | public static void main(String args[]) { 3 | Recepitionist recep1= new Recepitionist(); 4 | boolean status = recep1.checkRoomAvailability(3); 5 | double bill = recep1.generateBill(); 6 | recep1.takeCustomerFeddback(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Basics/Constructors/Recepitionist.java: -------------------------------------------------------------------------------- 1 | public class Recepitionist { 2 | private int staffID; 3 | private String name; 4 | private String telephoneNumber; 5 | 6 | // Default Constructor 7 | public Recepitionist() { 8 | this.staffID = 0; 9 | this.name = "abc"; 10 | this. telephoneNumber = null; 11 | } 12 | 13 | // Overloaded Constructor 14 | public Recepitionist(int pID, String pName, String pTelephoneNumber) { 15 | this.staffID = pID; 16 | this.name = pName; 17 | this.telephoneNumber = pTelephoneNumber; 18 | } 19 | 20 | public Boolean checkRoomAvailability(int roomNumber){ 21 | return true; 22 | } 23 | public double generateBill(){ 24 | return 0; 25 | } 26 | public void takeCustomerFeddback() { } 27 | } 28 | -------------------------------------------------------------------------------- /Basics/ControlStatment/Continue.java: -------------------------------------------------------------------------------- 1 | //Java Program to illustrate the use of continue statement 2 | //inside an inner loop 3 | public class ContinueExample2 { 4 | public static void main(String[] args) { 5 | //outer loop 6 | for(int i=1;i<=3;i++){ 7 | //inner loop 8 | for(int j=1;j<=3;j++){ 9 | if(i==2&&j==2){ 10 | //using continue statement inside inner loop 11 | continue; 12 | } 13 | System.out.println(i+" "+j); 14 | } 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Basics/ControlStatment/DoWhile.java: -------------------------------------------------------------------------------- 1 | public class DoWhileExample2 { 2 | public static void main(String[] args) { 3 | do{ 4 | System.out.println("infinitive do while loop"); 5 | }while(true); 6 | } 7 | } -------------------------------------------------------------------------------- /Basics/ControlStatment/ForLoop.java: -------------------------------------------------------------------------------- 1 | public class PyramidExample { 2 | public static void main(String[] args) { 3 | for(int i=1;i<=5;i++){ 4 | for(int j=1;j<=i;j++){ 5 | System.out.print("* "); 6 | } 7 | System.out.println();//new line 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /Basics/ControlStatment/If-else.java: -------------------------------------------------------------------------------- 1 | //A Java Program to demonstrate the use of if-else statement. 2 | //It is a program of odd and even number. 3 | public class IfElseExample { 4 | public static void main(String[] args) { 5 | //defining a variable 6 | int number=13; 7 | //Check if the number is divisible by 2 or not 8 | if(number%2==0){ 9 | System.out.println("even number"); 10 | }else{ 11 | System.out.println("odd number"); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /Basics/ControlStatment/WhileLoop.java: -------------------------------------------------------------------------------- 1 | public class WhileExample2 { 2 | public static void main(String[] args) { 3 | // setting the infinite while loop by passing true to the condition 4 | while(true){ 5 | System.out.println("infinitive while loop"); 6 | } 7 | } 8 | } 9 | 10 | -------------------------------------------------------------------------------- /Basics/Data Structure/Queue-datastructure/QueueX.java: -------------------------------------------------------------------------------- 1 | package Task1; 2 | 3 | public class QueueX { 4 | private int[] arr; // array to store queue elements 5 | private int front; // front points to the front element in the queue 6 | private int rear; // rear points to the last element in the queue 7 | private int capacity; // maximum capacity of the queue 8 | private int count; // current size of the queue 9 | 10 | // Constructor to initialize a queue 11 | QueueX(int size) 12 | { 13 | arr = new int[size]; 14 | capacity = size; 15 | front = 0; 16 | rear = -1; 17 | count = 0; 18 | } 19 | 20 | // Utility function to dequeue the front element 21 | public int dequeue() 22 | { 23 | // check for queue underflow 24 | if (isEmpty()) 25 | { 26 | System.out.println("Underflow\nProgram Terminated"); 27 | System.exit(-1); 28 | } 29 | 30 | int x = arr[front]; 31 | 32 | System.out.println("Removing " + x); 33 | 34 | front = (front + 1) % capacity; 35 | count--; 36 | 37 | return x; 38 | } 39 | 40 | // Utility function to add an item to the queue 41 | public void enqueue(int item) 42 | { 43 | // check for queue overflow 44 | if (isFull()) 45 | { 46 | System.out.println("Overflow\nProgram Terminated"); 47 | System.exit(-1); 48 | } 49 | 50 | System.out.println("Inserting " + item); 51 | 52 | rear = (rear + 1) % capacity; 53 | arr[rear] = item; 54 | count++; 55 | } 56 | 57 | // Utility function to return the front element of the queue 58 | public int peek() 59 | { 60 | if (isEmpty()) 61 | { 62 | System.out.println("Underflow\nProgram Terminated"); 63 | System.exit(-1); 64 | } 65 | return arr[front]; 66 | } 67 | 68 | // Utility function to return the size of the queue 69 | public int size() { 70 | return count; 71 | } 72 | 73 | // Utility function to check if the queue is empty or not 74 | public boolean isEmpty() { 75 | return (size() == 0); 76 | } 77 | 78 | // Utility function to check if the queue is full or not 79 | public boolean isFull() { 80 | return (size() == capacity); 81 | } 82 | 83 | public static void main(String[] args) { 84 | // TODO Auto-generated method stub 85 | // create a queue of capacity 5 86 | QueueX q = new QueueX(5); 87 | 88 | q.enqueue(1); 89 | q.enqueue(2); 90 | q.enqueue(3); 91 | 92 | System.out.println("The front element is " + q.peek()); 93 | q.dequeue(); 94 | System.out.println("The front element is " + q.peek()); 95 | 96 | System.out.println("The queue size is " + q.size()); 97 | 98 | q.dequeue(); 99 | q.dequeue(); 100 | 101 | if (q.isEmpty()) { 102 | System.out.println("The queue is empty"); 103 | } 104 | else { 105 | System.out.println("The queue is not empty"); 106 | } 107 | 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /Basics/Data Structure/Queue-ticketingProcess/Queue.java: -------------------------------------------------------------------------------- 1 | package Task1; 2 | 3 | public class Queue { 4 | 5 | private int maxSize; // size of queue array 6 | private int [] queArray; 7 | private int front; //front of the queue 8 | private int rear; //rear of the queue 9 | private int nItems; //no of items of the queue 10 | private int tickets;//no of tickets per day 11 | 12 | 13 | // constructor 14 | public Queue(int s) { 15 | maxSize = s; // set array size 16 | queArray = new int [maxSize]; 17 | front = 0; 18 | rear = -1; 19 | nItems = 0; // no items 20 | tickets = s*3; 21 | } 22 | 23 | //insert 24 | public void insert(int j) { 25 | // check whether queue is full 26 | if (rear == (maxSize-1)){ 27 | System.out.println("Queue is full"); 28 | } 29 | 30 | else { 31 | System.out.println("Inserting " + j); 32 | queArray[++rear] = j; 33 | nItems++; 34 | } 35 | } 36 | 37 | //remove 38 | public int remove(int n) { 39 | if (nItems == 0) { 40 | System.out.println("Queue is empty"); 41 | return -99; 42 | } 43 | else { 44 | if(tickets != 0){ 45 | 46 | tickets = tickets - n; 47 | int x = queArray[front++]; 48 | 49 | System.out.println("Removing " + x); 50 | nItems--; 51 | return x; 52 | } 53 | else 54 | System.out.println("There are no tickets available"); 55 | return -99; 56 | 57 | } 58 | } 59 | 60 | 61 | 62 | //peekFront 63 | public int peekFront() { 64 | if (nItems == 0) { 65 | System.out.println("Queue is empty"); 66 | return -99; 67 | } 68 | else { 69 | return queArray[front]; 70 | } 71 | } 72 | 73 | public class main { 74 | 75 | public static void main(String[] args) { 76 | // TODO Auto-generated method stub 77 | Queue queue = new Queue(5); 78 | 79 | queue.insert(1); 80 | queue.insert(2); 81 | queue.remove(1); 82 | 83 | System.out.println("No of tickets available: " + queue.tickets); 84 | } 85 | 86 | } 87 | 88 | 89 | } 90 | -------------------------------------------------------------------------------- /Basics/Data Structure/Queue-ticketingProcess/main.java: -------------------------------------------------------------------------------- 1 | package Task1; 2 | 3 | 4 | public class main { 5 | 6 | public static void main(String[] args) { 7 | // TODO Auto-generated method stub 8 | Queue queue = new Queue(5); 9 | 10 | queue.insert(1); 11 | queue.insert(2); 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Basics/Data Structure/Stack-Paranthesizer/Paranthesizer.java: -------------------------------------------------------------------------------- 1 | package Stack; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Paranthesizer { 6 | static String expr; 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | System.out.println("Enter your expression : "); 10 | expr = sc.nextLine(); 11 | boolean res = checker(); 12 | System.out.println(res?"Valid Expression":"Invalid Expression"); 13 | sc.close(); 14 | } 15 | 16 | public static int countParanthesis(){ 17 | int count = 0; 18 | for (int i = 0; i < expr.length(); i++) { 19 | if (expr.charAt(i) == '(' || expr.charAt(i) == '{' || expr.charAt(i) == '[') { 20 | count++; 21 | } 22 | } 23 | 24 | return count; 25 | } 26 | 27 | public static boolean checker(){ 28 | StackX stk = new StackX(countParanthesis()); 29 | 30 | for (char element : expr.toCharArray()) { 31 | if(element == '(' || element == '{' || element == '['){ 32 | stk.push(element); 33 | } 34 | 35 | if (element == ')' && stk.peek() == '(') { 36 | stk.pop(); 37 | }else if (element == '}' && stk.peek() == '{') { 38 | stk.pop(); 39 | }else if (element == ']' && stk.peek() == '[') { 40 | stk.pop(); 41 | } 42 | } 43 | 44 | return stk.isEmpty(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Basics/Data Structure/Stack-Paranthesizer/StackX.java: -------------------------------------------------------------------------------- 1 | package Stack; 2 | 3 | public class StackX { 4 | private char[] data; //stack array 5 | private int top; //top of the stack 6 | private int size; //size of stack array 7 | 8 | public StackX(int size){ 9 | this.size = size; 10 | data = new char[size]; 11 | top = -1; 12 | } 13 | 14 | //push method 15 | public boolean push(char element){ 16 | if (top < size - 1) { 17 | data[++top] = element; 18 | return true; 19 | } 20 | 21 | return false; 22 | 23 | } 24 | 25 | public char pop(){ 26 | if (top != -1) { 27 | return data[top--]; 28 | } 29 | 30 | return ' '; 31 | } 32 | 33 | public char peek(){ 34 | if (top != -1) { 35 | return data[top]; 36 | } 37 | 38 | return ' '; 39 | } 40 | 41 | public boolean isFull(){ 42 | if (top == size - 1) { 43 | return true; 44 | } 45 | 46 | return false; 47 | } 48 | 49 | public boolean isEmpty(){ 50 | if (top == -1) { 51 | return true; 52 | } 53 | 54 | return false; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Basics/Data Structure/Stack-Reverser/Reverser.java: -------------------------------------------------------------------------------- 1 | package Stack; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Reverser { 6 | private String input,output; 7 | 8 | Reverser(String input){ 9 | this.input = input; 10 | output = ""; 11 | } 12 | 13 | public String reverse(){ 14 | StackX stack = new StackX(input.length()); 15 | 16 | for (char letter : input.toCharArray()) { 17 | stack.push(letter); 18 | } 19 | 20 | 21 | 22 | while (!stack.isEmpty()) { 23 | output += stack.pop(); 24 | 25 | } 26 | 27 | return output; 28 | } 29 | 30 | public static void main(String[] args) { 31 | Scanner sc = new Scanner(System.in); 32 | System.out.print("Enter any text : "); 33 | String str = sc.nextLine(); 34 | Reverser obj = new Reverser(str); 35 | System.out.println("Reversed :" + obj.reverse()); 36 | sc.close(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Basics/Data Structure/Stack-Reverser/StackX.java: -------------------------------------------------------------------------------- 1 | package Stack; 2 | 3 | public class StackX { 4 | private char[] data; //stack array 5 | private int top; //top of the stack 6 | private int size; //size of stack array 7 | 8 | public StackX(int size){ 9 | this.size = size; 10 | data = new char[size]; 11 | top = -1; 12 | } 13 | 14 | //push method 15 | public boolean push(char element){ 16 | if (top < size - 1) { 17 | data[++top] = element; 18 | return true; 19 | } 20 | 21 | return false; 22 | 23 | } 24 | 25 | public char pop(){ 26 | if (top != -1) { 27 | return data[top--]; 28 | } 29 | 30 | return ' '; 31 | } 32 | 33 | public char peek(){ 34 | if (top != -1) { 35 | return data[top]; 36 | } 37 | 38 | return ' '; 39 | } 40 | 41 | public boolean isFull(){ 42 | if (top == size - 1) { 43 | return true; 44 | } 45 | 46 | return false; 47 | } 48 | 49 | public boolean isEmpty(){ 50 | if (top == -1) { 51 | return true; 52 | } 53 | 54 | return false; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Basics/Data Structure/binaryTree/BinarySearchTree.java: -------------------------------------------------------------------------------- 1 | package binaryTree; 2 | 3 | public class BinarySearchTree { 4 | 5 | private TreeNode root; 6 | 7 | private class TreeNode { 8 | private int data; 9 | private TreeNode left; 10 | private TreeNode right; 11 | 12 | public TreeNode(int data) { 13 | this.data = data; 14 | } 15 | } 16 | 17 | public void insert(int value) { 18 | root = insert(root, value); 19 | } 20 | 21 | public TreeNode insert(TreeNode root, int value) { 22 | if(root == null) { 23 | root = new TreeNode(value); 24 | return root; 25 | } 26 | 27 | if(value < root.data) { 28 | root.left = insert(root.left, value); 29 | } else { 30 | root.right = insert(root.right, value); 31 | } 32 | return root; 33 | } 34 | 35 | public void inOrder() { 36 | inOrder(root); 37 | } 38 | 39 | public void inOrder(TreeNode root) { 40 | if(root == null) { 41 | return; 42 | } 43 | inOrder(root.left); 44 | System.out.print(root.data + " "); 45 | inOrder(root.right); 46 | } 47 | 48 | public TreeNode search(int key) { 49 | return search(root, key); 50 | } 51 | 52 | public TreeNode search(TreeNode root, int key) { 53 | if(root == null || root.data == key) { // base case 54 | return root; 55 | } 56 | 57 | if(key < root.data) { 58 | return search(root.left, key); 59 | } else { 60 | return search(root.right, key); 61 | } 62 | 63 | } 64 | 65 | public static void main(String[] args) { 66 | 67 | BinarySearchTree tree = new BinarySearchTree(); 68 | tree.insert(10); 69 | tree.insert(8); 70 | tree.insert(4); 71 | tree.insert(1); 72 | 73 | tree.inOrder(); 74 | 75 | System.out.println(); 76 | 77 | if(null != tree.search(10)) { 78 | System.out.println("Key found !!!"); 79 | } else { 80 | System.out.println("Key not found !!!"); 81 | } 82 | } 83 | 84 | } 85 | 86 | 87 | -------------------------------------------------------------------------------- /Basics/Data Structure/queue_datastructure/Queue.java: -------------------------------------------------------------------------------- 1 | package queue_datastructure; 2 | 3 | import java.util.NoSuchElementException; 4 | 5 | 6 | public class Queue { 7 | 8 | 9 | private nodeList front; 10 | private nodeList rear; 11 | private int length; 12 | 13 | private class nodeList { 14 | private int data; // Can be a generic type 15 | private nodeList next; // Reference to next ListNode in list 16 | 17 | public nodeList(int data) { 18 | this.data = data; 19 | this.next = null; 20 | } 21 | } 22 | 23 | public Queue() { 24 | front = null; 25 | rear = null; 26 | length = 0; 27 | } 28 | 29 | public int length() { 30 | return length; 31 | } 32 | 33 | public boolean isEmpty() { 34 | return length == 0; 35 | } 36 | 37 | public void enqueue(int data) { 38 | nodeList temp = new nodeList(data); 39 | if(isEmpty()) { 40 | front = temp; 41 | } else { 42 | rear.next = temp; 43 | } 44 | 45 | rear = temp; 46 | length++; 47 | } 48 | 49 | public int dequeue() { 50 | if(isEmpty()) { 51 | throw new NoSuchElementException("Queue is empty"); 52 | } 53 | 54 | int result = front.data; 55 | front = front.next; 56 | if(front == null) { 57 | rear = null; 58 | } 59 | length--; 60 | return result; 61 | } 62 | 63 | public void print() { 64 | 65 | //checking weather queue is empty or not 66 | if(isEmpty()) { 67 | return; 68 | } 69 | 70 | nodeList current = front; 71 | while(current != null) { 72 | System.out.print(current.data + ","); 73 | current = current.next; 74 | } 75 | System.out.println("null"); 76 | } 77 | 78 | public static void main(String[] args) { 79 | 80 | Queue queue = new Queue(); 81 | queue.enqueue(10); 82 | queue.enqueue(15); 83 | queue.enqueue(20); 84 | 85 | queue.print(); 86 | 87 | queue.dequeue(); 88 | queue.dequeue(); 89 | queue.enqueue(30); 90 | 91 | queue.print(); 92 | 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /Basics/Data Structure/stack_datastructure/Stack.java: -------------------------------------------------------------------------------- 1 | package stack_datastructure; 2 | import java.util.EmptyStackException; 3 | 4 | public class Stack { 5 | 6 | private nodeList top; 7 | private int length; 8 | 9 | private class nodeList { 10 | private int data; 11 | private nodeList next; // Reference to next Node in list 12 | 13 | public nodeList(int data) { 14 | this.data = data; 15 | this.next = null; 16 | } 17 | } 18 | 19 | public Stack() { 20 | top = null; 21 | length = 0; 22 | } 23 | 24 | public int length() { 25 | return length; 26 | } 27 | 28 | public boolean isEmpty() { 29 | return length == 0; 30 | } 31 | 32 | public void push(int data) { 33 | nodeList temp = new nodeList(data); 34 | temp.next = top; 35 | top = temp; 36 | length++; 37 | } 38 | 39 | public int pop() { 40 | if(isEmpty()) { 41 | throw new EmptyStackException(); 42 | } 43 | int result = top.data; 44 | top = top.next; 45 | length--; 46 | return result; 47 | } 48 | 49 | public int peek() { 50 | if(isEmpty()) { 51 | throw new EmptyStackException(); 52 | } 53 | return top.data; 54 | } 55 | 56 | public static void main(String[] args) { 57 | 58 | 59 | Stack stack = new Stack(); 60 | stack.push(10); 61 | stack.push(15); 62 | stack.push(20); 63 | 64 | System.out.println(stack.peek()); 65 | stack.pop(); 66 | System.out.println(stack.peek()); 67 | stack.pop(); 68 | System.out.println(stack.peek()); 69 | 70 | } 71 | 72 | } 73 | 74 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Bridge Pattern/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Bridge Pattern/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bridge Pattern 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | 19 | 1664908487928 20 | 21 | 30 22 | 23 | org.eclipse.core.resources.regexFilterMatcher 24 | node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Bridge Pattern/src/Assemble.java: -------------------------------------------------------------------------------- 1 | 2 | public class Assemble implements WorkShop { 3 | 4 | public void work() { 5 | 6 | System.out.println("And Assemble"); 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Bridge Pattern/src/AutoMobile.java: -------------------------------------------------------------------------------- 1 | 2 | public abstract class AutoMobile { 3 | 4 | protected WorkShop workshop1; 5 | protected WorkShop workshop2; 6 | 7 | protected AutoMobile (WorkShop workshop1, WorkShop workshop2 ) { 8 | 9 | this.workshop1 = workshop1; 10 | this.workshop2 = workshop2; 11 | 12 | } 13 | 14 | abstract public void manufacture(); 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Bridge Pattern/src/Bus.java: -------------------------------------------------------------------------------- 1 | 2 | public class Bus extends AutoMobile { 3 | 4 | protected Bus(WorkShop workshop1, WorkShop workshop2) { 5 | super(workshop1, workshop2); 6 | // TODO Auto-generated constructor stub 7 | } 8 | 9 | public void manufacture() { 10 | 11 | System.out.println("Taxi is "); 12 | workshop1.work(); 13 | workshop2.work(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Bridge Pattern/src/Produce.java: -------------------------------------------------------------------------------- 1 | 2 | public class Produce implements WorkShop { 3 | 4 | public void work() { 5 | 6 | System.out.println("Procuded"); 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Bridge Pattern/src/User.java: -------------------------------------------------------------------------------- 1 | 2 | public class User { 3 | 4 | public static void main (String args[]) { 5 | 6 | AutoMobile bus = new Bus(new Produce(), new Assemble()); 7 | bus.manufacture(); 8 | 9 | 10 | } 11 | 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Bridge Pattern/src/WorkShop.java: -------------------------------------------------------------------------------- 1 | 2 | public interface WorkShop { 3 | 4 | abstract public void work(); 5 | 6 | } 7 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Command Pattern/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Command Pattern/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Command Pattern 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Command Pattern/src/Command.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Command { 3 | 4 | void execute(); 5 | 6 | } 7 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Command Pattern/src/KitchedLight.java: -------------------------------------------------------------------------------- 1 | 2 | public class KitchedLight implements Light { 3 | 4 | @Override 5 | public void on() { 6 | // TODO Auto-generated method stub 7 | 8 | System.out.println("Kitchen Light On"); 9 | } 10 | 11 | @Override 12 | public void off() { 13 | // TODO Auto-generated method stub 14 | System.out.println("Kitchen Light Off"); 15 | 16 | } 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Command Pattern/src/Light.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Light { 3 | 4 | void on(); 5 | 6 | void off(); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Command Pattern/src/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | 2 | public class LightOffCommand implements Command { 3 | 4 | Light light; 5 | 6 | public LightOffCommand(Light light) { 7 | 8 | this.light = light; 9 | 10 | } 11 | 12 | 13 | 14 | public void execute() { 15 | 16 | light.off(); 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Command Pattern/src/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | 2 | public class LightOnCommand implements Command { 3 | 4 | Light light; 5 | 6 | public LightOnCommand(Light light) { 7 | 8 | this.light = light; 9 | 10 | } 11 | 12 | 13 | 14 | public void execute() { 15 | 16 | light.on(); 17 | 18 | } 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Command Pattern/src/LivingRoomLight.java: -------------------------------------------------------------------------------- 1 | 2 | public class LivingRoomLight implements Light { 3 | 4 | 5 | public void on() { 6 | 7 | System.out.println("Living Room Light On"); 8 | 9 | } 10 | 11 | public void off() { 12 | 13 | System.out.println("Living Room Light Off"); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Command Pattern/src/Test.java: -------------------------------------------------------------------------------- 1 | 2 | public class Test { 3 | 4 | public static void main(String[] args) { 5 | // TODO Auto-generated method stub 6 | 7 | Light livingRoomLight = new LivingRoomLight(); 8 | Light kitchenLight = new KitchedLight(); 9 | 10 | Command lightoncommand = new LightOnCommand(livingRoomLight); 11 | lightoncommand.execute(); 12 | Command lightoffcommand = new LightOffCommand(livingRoomLight); 13 | lightoffcommand.execute(); 14 | 15 | Command lightoncommand1 = new LightOnCommand(kitchenLight); 16 | lightoncommand1.execute(); 17 | 18 | Command lightoffcommand2 = new LightOffCommand(kitchenLight); 19 | lightoffcommand2.execute(); 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Template Pattern/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Template Pattern/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Template Pattern 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | 19 | 1664908487944 20 | 21 | 30 22 | 23 | org.eclipse.core.resources.regexFilterMatcher 24 | node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Template Pattern/src/Beverages.java: -------------------------------------------------------------------------------- 1 | 2 | public abstract class Beverages { 3 | 4 | abstract void brew(); 5 | 6 | abstract void addCondiments(); 7 | 8 | void boilWater() { 9 | 10 | System.out.println("Boiling Water"); 11 | } 12 | 13 | void pourInCup() { 14 | 15 | System.out.println("Pour into cup"); 16 | } 17 | 18 | 19 | public void PrepareRecipie() { 20 | 21 | boilWater(); 22 | brew(); 23 | addCondiments(); 24 | pourInCup(); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Template Pattern/src/Coffee.java: -------------------------------------------------------------------------------- 1 | 2 | public class Coffee extends Beverages { 3 | 4 | public Coffee() { 5 | 6 | super(); 7 | } 8 | 9 | @Override 10 | void brew() { 11 | // TODO Auto-generated method stub 12 | System.out.println("Stripping coffe through filter"); 13 | } 14 | 15 | @Override 16 | void addCondiments() { 17 | // TODO Auto-generated method stub 18 | System.out.println("Add Suger and milk"); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Template Pattern/src/Tea.java: -------------------------------------------------------------------------------- 1 | 2 | public class Tea extends Beverages { 3 | 4 | public Tea() { 5 | 6 | super(); 7 | } 8 | 9 | 10 | @Override 11 | void brew() { 12 | // TODO Auto-generated method stub 13 | 14 | System.out.println("Steeping the tea"); 15 | 16 | } 17 | 18 | @Override 19 | void addCondiments() { 20 | // TODO Auto-generated method stub 21 | 22 | System.out.println("Adding Lemon"); 23 | 24 | } 25 | 26 | 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Basics/Design Pattern/Template Pattern/src/Test.java: -------------------------------------------------------------------------------- 1 | 2 | public class Test { 3 | 4 | static Beverages beverages = null; 5 | 6 | public static void main(String[] args) { 7 | // TODO Auto-generated method stub 8 | 9 | Beverages tea = new Tea(); 10 | tea.PrepareRecipie(); 11 | 12 | Beverages coffee = new Coffee(); 13 | coffee.PrepareRecipie(); 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/AbstractFactory.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public abstract class AbstractFactory { 4 | 5 | public abstract Color getColor(String type); 6 | 7 | public abstract Shape getShape(String type); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/AbstractFactoryPatternDemo.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public class AbstractFactoryPatternDemo { 4 | 5 | private static final String SHAPE = "SHAPE"; 6 | private static final String CIRCLE = "CIRCLE"; 7 | private static final String RECTANGLE = "RECTANGLE"; 8 | private static final String SQUARE = "SQUARE"; 9 | 10 | private static final String COLOR = "COLOR"; 11 | private static final String RED = "RED"; 12 | private static final String GREEN = "GREEN"; 13 | private static final String BLUE = "BLUE"; 14 | 15 | 16 | public static void main(String[] args) { 17 | 18 | AbstractFactory shapeFactory = FactoryProducer.getFactory(SHAPE); 19 | Shape shape = shapeFactory.getShape(CIRCLE); 20 | shape.draw(); 21 | 22 | FactoryProducer.getFactory(SHAPE).getShape(RECTANGLE).draw(); 23 | FactoryProducer.getFactory(SHAPE).getShape(SQUARE).draw(); 24 | 25 | FactoryProducer.getFactory(COLOR).getColor(RED).fill(); 26 | FactoryProducer.getFactory(COLOR).getColor(GREEN).fill(); 27 | FactoryProducer.getFactory(COLOR).getColor(BLUE).fill(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/Blue.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public class Blue implements Color{ 4 | 5 | @Override 6 | public void fill() { 7 | System.out.println("Inside the Blue:fill() method"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/Circle.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public class Circle implements Shape{ 4 | 5 | @Override 6 | public void draw() { 7 | System.out.println("Inside the Circle:draw() method"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/Color.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public interface Color { 4 | 5 | void fill(); 6 | } 7 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/ColorFactory.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public class ColorFactory extends AbstractFactory{ 4 | 5 | @Override 6 | public Color getColor(String color) { 7 | 8 | if(color.equalsIgnoreCase("RED")){ 9 | return new Red(); 10 | } 11 | else if(color.equalsIgnoreCase("GREEN")){ 12 | return new Green(); 13 | } 14 | else if(color.equalsIgnoreCase("BLUE")){ 15 | return new Blue(); 16 | } 17 | else{ 18 | return null; 19 | } 20 | } 21 | 22 | @Override 23 | public Shape getShape(String type) { 24 | return null; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/FactoryProducer.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public class FactoryProducer { 4 | 5 | public static AbstractFactory getFactory(String choice){ 6 | 7 | if(choice.equalsIgnoreCase("SHAPE")){ 8 | return new ShapeFactory(); 9 | } 10 | else if(choice.equalsIgnoreCase("COLOR")){ 11 | return new ColorFactory(); 12 | } 13 | else{ 14 | return null; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/Green.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public class Green implements Color{ 4 | 5 | @Override 6 | public void fill() { 7 | System.out.println("Inside Green:fill() method"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/Rectangle.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public class Rectangle implements Shape{ 4 | 5 | @Override 6 | public void draw() { 7 | System.out.println("Inside the Rectangle:draw() method"); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/Red.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public class Red implements Color{ 4 | 5 | @Override 6 | public void fill() { 7 | System.out.println("Inside Red:fill() method."); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/Shape.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public interface Shape { 4 | 5 | void draw(); 6 | } 7 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/ShapeFactory.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public class ShapeFactory extends AbstractFactory{ 4 | 5 | @Override 6 | public Shape getShape(String shapeType) { 7 | 8 | if(shapeType == null){ 9 | return null; 10 | } 11 | else if(shapeType.equalsIgnoreCase("CIRCLE")){ 12 | return new Circle(); 13 | } 14 | else if(shapeType.equalsIgnoreCase("RECTANGLE")){ 15 | return new Rectangle(); 16 | } 17 | else if(shapeType.equalsIgnoreCase("SQUARE")){ 18 | return new Square(); 19 | } 20 | else{ 21 | return null; 22 | } 23 | } 24 | 25 | @Override 26 | public Color getColor(String type) { 27 | return null; 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Basics/Design Pattern/abstractFactory/Square.java: -------------------------------------------------------------------------------- 1 | package design.pattern.abstractFactory; 2 | 3 | public class Square implements Shape{ 4 | 5 | @Override 6 | public void draw() { 7 | System.out.println("Inside the Square:draw() method"); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /Basics/Design Pattern/decorator/Consultant.java: -------------------------------------------------------------------------------- 1 | package design.pattern.decorator; 2 | 3 | public class Consultant extends Employee { 4 | 5 | public Consultant() { 6 | description = "Consultant"; 7 | } 8 | 9 | @Override 10 | public double getSalary() { 11 | return 70000; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Basics/Design Pattern/decorator/Employee.java: -------------------------------------------------------------------------------- 1 | package design.pattern.decorator; 2 | 3 | public abstract class Employee { 4 | 5 | public String description; 6 | 7 | public Employee() { 8 | description = "General employee details"; 9 | } 10 | 11 | abstract public double getSalary(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Basics/Design Pattern/decorator/EmployeeCertification.java: -------------------------------------------------------------------------------- 1 | package design.pattern.decorator; 2 | 3 | public class EmployeeCertification extends EmployeeQualification { 4 | 5 | Employee employee; 6 | 7 | public EmployeeCertification(Employee employee) { 8 | this.employee = employee; 9 | } 10 | 11 | @Override 12 | public String getDescription() { 13 | return " Employee Certification "; 14 | } 15 | 16 | @Override 17 | public double getSalary() { 18 | return 30000 + employee.getSalary(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Basics/Design Pattern/decorator/EmployeeQualification.java: -------------------------------------------------------------------------------- 1 | package design.pattern.decorator; 2 | 3 | public abstract class EmployeeQualification extends Employee{ 4 | 5 | public abstract String getDescription(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Basics/Design Pattern/decorator/Engineer.java: -------------------------------------------------------------------------------- 1 | package design.pattern.decorator; 2 | 3 | public class Engineer extends Employee { 4 | 5 | public Engineer() { 6 | description = "Engineer"; 7 | } 8 | 9 | @Override 10 | public double getSalary() { 11 | return 60000; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Basics/Design Pattern/decorator/Manager.java: -------------------------------------------------------------------------------- 1 | package design.pattern.decorator; 2 | 3 | public class Manager extends Employee { 4 | 5 | public Manager() { 6 | description = "Manager"; 7 | } 8 | 9 | @Override 10 | public double getSalary() { 11 | return 80000; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Basics/Design Pattern/decorator/TestPattern.java: -------------------------------------------------------------------------------- 1 | package design.pattern.decorator; 2 | 3 | public class TestPattern { 4 | 5 | /** 6 | * @param args 7 | */ 8 | public static void main(String[] args) { 9 | 10 | Employee employee = new Engineer(); 11 | employee = new EmployeeCertification(employee); 12 | employee = new WorkExperience(employee); 13 | 14 | System.out.println(employee.description + " " + employee.getSalary()); 15 | 16 | Employee employee2 = new Consultant(); 17 | employee2 = new WorkExperience(new EmployeeCertification(employee2)); 18 | 19 | System.out.println(employee2.description + " " + employee2.getSalary()); 20 | 21 | System.out.println("PM salary = " 22 | + new WorkExperience(new EmployeeCertification(new Manager())) 23 | .getSalary()); 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Basics/Design Pattern/decorator/WorkExperience.java: -------------------------------------------------------------------------------- 1 | package design.pattern.decorator; 2 | 3 | public class WorkExperience extends EmployeeQualification { 4 | 5 | Employee employee; 6 | 7 | public WorkExperience(Employee employee) { 8 | this.employee = employee; 9 | } 10 | 11 | @Override 12 | public String getDescription() { 13 | return "Add Work experience."; 14 | } 15 | 16 | @Override 17 | public double getSalary() { 18 | return 20000 + employee.getSalary(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Basics/Design Pattern/singleton/Singleton.java: -------------------------------------------------------------------------------- 1 | package design.pattern.singleton; 2 | 3 | public class Singleton { 4 | 5 | private static Singleton singleton; 6 | 7 | private Singleton() { } 8 | 9 | public static Singleton getInstance(){ 10 | 11 | if(singleton == null){ 12 | singleton = new Singleton(); 13 | System.out.println("Singleton invocation"); 14 | } 15 | return singleton; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Basics/Design Pattern/singleton/Test.java: -------------------------------------------------------------------------------- 1 | package design.pattern.singleton; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class Test { 8 | 9 | /** 10 | * @param args 11 | * @throws IOException 12 | */ 13 | public static void main(String[] args) throws IOException { 14 | BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); 15 | System.out.println("You inserted: " + bf.readLine()); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Basics/Design Pattern/singleton/TestSingleton.java: -------------------------------------------------------------------------------- 1 | package design.pattern.singleton; 2 | 3 | public class TestSingleton { 4 | 5 | /** 6 | * @param args 7 | */ 8 | public static void main(String[] args) { 9 | 10 | for (int i = 0; i < 10; i++) { 11 | Singleton.getInstance(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Basics/Design Pattern/singleton/TestThreadSingleton.java: -------------------------------------------------------------------------------- 1 | package design.pattern.singleton; 2 | 3 | public class TestThreadSingleton implements Runnable{ 4 | 5 | /** 6 | * @param args 7 | */ 8 | public static void main(String[] args) { 9 | 10 | new Thread(new TestThreadSingleton()).start(); 11 | 12 | for (int i = 0; i < 10; i++) { 13 | Singleton.getInstance(); 14 | ThreadSafeSingleton.getInstance(); 15 | } 16 | } 17 | 18 | /** 19 | * Invoke thread 20 | */ 21 | public void run(){ 22 | for (int i = 0; i < 10; i++) { 23 | Singleton.getInstance(); 24 | ThreadSafeSingleton.getInstance(); 25 | } 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Basics/Design Pattern/singleton/ThreadSafeSingleton.java: -------------------------------------------------------------------------------- 1 | package design.pattern.singleton; 2 | 3 | public class ThreadSafeSingleton { 4 | 5 | private static ThreadSafeSingleton threadSafeSingleton; 6 | 7 | private ThreadSafeSingleton() {} 8 | 9 | public static ThreadSafeSingleton getInstance(){ 10 | 11 | if(threadSafeSingleton == null){ 12 | synchronized (ThreadSafeSingleton.class) { 13 | if(threadSafeSingleton == null){ 14 | threadSafeSingleton = new ThreadSafeSingleton(); 15 | System.out.println("Object created for ThreadSafeSingleton."); 16 | } 17 | } 18 | } 19 | return threadSafeSingleton; 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Basics/Design Pattern/strategy/Duck.java: -------------------------------------------------------------------------------- 1 | package design.pattern.stratergy; 2 | 3 | public abstract class Duck { 4 | 5 | IFlyBehaviour flyBehaviour; 6 | 7 | IQuackBehaviour quackBehaviour; 8 | 9 | public abstract void display(); 10 | 11 | public void performFly(){ 12 | flyBehaviour.fly(); 13 | } 14 | 15 | public void performQuack(){ 16 | quackBehaviour.quack(); 17 | } 18 | 19 | public void swim(){ 20 | System.out.println("All ducks float even Decoy"); 21 | } 22 | 23 | public void setFlyBehaviour(IFlyBehaviour flyBehaviour) { 24 | this.flyBehaviour = flyBehaviour; 25 | } 26 | 27 | public void setQuackBehaviour(IQuackBehaviour quackBehaviour) { 28 | this.quackBehaviour = quackBehaviour; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Basics/Design Pattern/strategy/FlyNoWay.java: -------------------------------------------------------------------------------- 1 | package design.pattern.stratergy; 2 | 3 | public class FlyNoWay implements IFlyBehaviour{ 4 | 5 | @Override 6 | public void fly() { 7 | System.out.println("I can't fly"); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /Basics/Design Pattern/strategy/FlyRocketPower.java: -------------------------------------------------------------------------------- 1 | package design.pattern.stratergy; 2 | 3 | public class FlyRocketPower implements IFlyBehaviour{ 4 | 5 | @Override 6 | public void fly() { 7 | System.out.println("I am flying with a rocket"); 8 | } 9 | } 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Basics/Design Pattern/strategy/FlyWithWings.java: -------------------------------------------------------------------------------- 1 | package design.pattern.stratergy; 2 | 3 | public class FlyWithWings implements IFlyBehaviour{ 4 | 5 | @Override 6 | public void fly() { 7 | System.out.println("I am flying with wings"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Basics/Design Pattern/strategy/IFlyBehaviour.java: -------------------------------------------------------------------------------- 1 | package design.pattern.stratergy; 2 | 3 | public interface IFlyBehaviour { 4 | 5 | public void fly(); 6 | } 7 | 8 | 9 | -------------------------------------------------------------------------------- /Basics/Design Pattern/strategy/IQuackBehaviour.java: -------------------------------------------------------------------------------- 1 | package design.pattern.stratergy; 2 | 3 | public interface IQuackBehaviour { 4 | 5 | public void quack(); 6 | } 7 | 8 | 9 | -------------------------------------------------------------------------------- /Basics/Design Pattern/strategy/ModelDuck.java: -------------------------------------------------------------------------------- 1 | 2 | package design.pattern.stratergy; 3 | 4 | public class ModelDuck extends Duck{ 5 | 6 | public ModelDuck() { 7 | quackBehaviour = new Quack(); 8 | flyBehaviour = new FlyNoWay(); 9 | } 10 | 11 | @Override 12 | public void display() { 13 | System.out.println("I am a model Duck"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Basics/Design Pattern/strategy/ModelQuack.java: -------------------------------------------------------------------------------- 1 | package design.pattern.stratergy; 2 | 3 | public class ModelQuack implements IQuackBehaviour{ 4 | 5 | @Override 6 | public void quack() { 7 | System.out.println("Quack Model duck"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Basics/Design Pattern/strategy/MollardDuck.java: -------------------------------------------------------------------------------- 1 | package design.pattern.stratergy; 2 | 3 | public class MollardDuck extends Duck{ 4 | 5 | public MollardDuck() { 6 | quackBehaviour = new Quack(); 7 | flyBehaviour = new FlyWithWings(); 8 | } 9 | 10 | @Override 11 | public void display() { 12 | System.out.println("I am a real Mollard Duck."); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Basics/Design Pattern/strategy/Quack.java: -------------------------------------------------------------------------------- 1 | package design.pattern.stratergy; 2 | 3 | public class Quack implements IQuackBehaviour{ 4 | 5 | @Override 6 | public void quack() { 7 | System.out.println("Quack..Quack..."); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /Basics/Design Pattern/strategy/TestDuck.java: -------------------------------------------------------------------------------- 1 | package design.pattern.stratergy; 2 | 3 | public class TestDuck { 4 | 5 | /** 6 | * @param args 7 | */ 8 | public static void main(String[] args) { 9 | 10 | System.out.println("Start Mollard Duck"); 11 | System.out.println("=================="); 12 | Duck mollard = new MollardDuck(); 13 | mollard.performFly(); 14 | mollard.performQuack(); 15 | 16 | System.out.println("Start Model Duck"); 17 | System.out.println("=================="); 18 | Duck model = new ModelDuck(); 19 | 20 | model.performFly(); 21 | model.setFlyBehaviour(new FlyRocketPower()); 22 | model.performFly(); 23 | 24 | model.performQuack(); 25 | model.setQuackBehaviour(new ModelQuack()); 26 | model.performQuack(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Basics/GettersAndSetters/GettersAndSettersMain.java: -------------------------------------------------------------------------------- 1 | public class GettersAndSettersMain { 2 | public static void main(String args[]) { 3 | Recepitionist recep1 = new Recepitionist(); 4 | 5 | recep1.setID(12); 6 | int id = recep1.getID(); 7 | 8 | System.out.println(id); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Basics/GettersAndSetters/Recepitionist.java: -------------------------------------------------------------------------------- 1 | public class Recepitionist { 2 | private int staffID; 3 | private String name; 4 | private String telephoneNumber; 5 | 6 | // Setter 7 | public void setID(int ID) { 8 | this.staffID = ID; 9 | } 10 | 11 | // Getter 12 | public int getID() { 13 | return this.staffID; 14 | } 15 | 16 | public Boolean checkRoomAvailability(int roomNumber){ 17 | return true; 18 | } 19 | public double generateBill(){ 20 | return 0; 21 | } 22 | public void takeCustomerFeddback() { } 23 | } 24 | -------------------------------------------------------------------------------- /Basics/Inheritance/InheritanceMain.java: -------------------------------------------------------------------------------- 1 | public class InheritanceMain { 2 | public static void main(String args[]) { 3 | Rectangle rec1 = new Rectangle("Rec1", 15, 10); 4 | rec1.printDetails(); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Basics/Inheritance/Rectangle.java: -------------------------------------------------------------------------------- 1 | public class Rectangle extends Shape { 2 | private float length; 3 | private float width; 4 | 5 | public Rectangle(String pName, float pLength, float pWidth) { 6 | super(pName); 7 | this.length = pLength; 8 | this.width = pWidth; 9 | } 10 | 11 | public void printDetails() { 12 | System.out.println("Name = " + name); 13 | System.out.println("Length = " + length); 14 | System.out.println("Width = " + width); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Basics/Inheritance/Shape.java: -------------------------------------------------------------------------------- 1 | public class Shape { 2 | protected String name; 3 | 4 | public Shape() { 5 | this.name = "Shape Name"; 6 | } 7 | 8 | public Shape(String pName) { 9 | this.name = pName; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Basics/Input.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; // Import the Scanner class 2 | 3 | public class Input { 4 | public static void main(String args[]) { 5 | // Variable declaration 6 | String name; 7 | int age; 8 | 9 | // Create a Scanner object 10 | Scanner myScanner = new Scanner(System.in); 11 | 12 | System.out.print("Enter your name : "); 13 | name = myScanner.nextLine(); // Read user input - String(with all white spaces) 14 | System.out.print("Enter your age : "); 15 | age = myScanner.nextInt(); // Read user input - Int 16 | 17 | // Output user input 18 | System.out.println(name); 19 | System.out.println(age); 20 | } 21 | } 22 | 23 | /* 24 | Method Description 25 | 26 | nextBoolean() Reads a boolean value from the user 27 | nextByte() Reads a byte value from the user 28 | nextDouble() Reads a double value from the user 29 | nextFloat() Reads a float value from the user 30 | nextInt() Reads a int value from the user 31 | nextLine() Reads a String value from the user 32 | nextLong() Reads a long value from the user 33 | nextShort() Reads a short value from the user 34 | */ -------------------------------------------------------------------------------- /Basics/Polymorphism/PolymorphismMain.java: -------------------------------------------------------------------------------- 1 | class Animal { 2 | // protected: 3 | protected String name;; 4 | // public: 5 | public Animal() {} 6 | public Animal(String tname) { 7 | name = tname; 8 | } 9 | public void speak() {} 10 | public void song() { 11 | System.out.println(name + "'s Song "); 12 | speak(); 13 | System.out.println("la la la la"); 14 | speak(); 15 | System.out.println("la la la la"); 16 | speak(); 17 | System.out.println("====================="); 18 | } 19 | } 20 | class Cat extends Animal { 21 | // public: 22 | public Cat() {} 23 | public Cat(String tname) { 24 | super(tname); 25 | } 26 | public void speak() { 27 | System.out.println("Meow... Meow..."); 28 | } 29 | } 30 | class Dog extends Animal { 31 | // public: 32 | public Dog() {} 33 | public Dog(String tname) { 34 | super(tname); 35 | } 36 | public void speak() { 37 | System.out.println("Bow... Bow..."); 38 | } 39 | } 40 | class Cow extends Animal { 41 | // public: 42 | public Cow() {} 43 | public Cow(String tname) { 44 | super(tname); 45 | } 46 | public void speak() { 47 | System.out.println("Moo... Moo..."); 48 | } 49 | } 50 | class PolymorphismMain { 51 | public static void main(String args[]) { 52 | Animal ani[] = new Animal[4]; 53 | ani[0] = new Cat("Micky the Cat"); 54 | ani[1] = new Dog("Rover the Dog"); 55 | ani[2] = new Cow("roo the Cow"); 56 | ani[3] = new Animal("no name"); 57 | 58 | for (int r=0;r<4; r++) { 59 | ani[r].song(); 60 | } 61 | } 62 | } -------------------------------------------------------------------------------- /Basics/Printing.java: -------------------------------------------------------------------------------- 1 | public class Printing { 2 | public static void main(String args[]) { 3 | // Print without newline 4 | System.out.print("Thushara"); 5 | 6 | // Print with newline 7 | System.out.println("Thushara"); 8 | System.out.print("Thiwanka\n"); 9 | 10 | // Print variable 11 | int num1 = 10; 12 | System.out.println(num1); 13 | 14 | // Print variable with string 15 | int num2 = 20; 16 | System.out.println("Number = " + num2); 17 | 18 | System.out.println("Number1 = " + num1 + "\n" + "Number2 = " + num2); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Basics/Thread/ThreadJoin.java: -------------------------------------------------------------------------------- 1 | public class ThreadJoin extends Thread{ 2 | 3 | public void run () { 4 | 5 | Thread t = Thread.currentThread(); 6 | System.out.println("Started Executing " +t.getName()); 7 | 8 | for(int i=0; i <10; i++) { 9 | 10 | System.out.println(t.getName() + i); 11 | 12 | } 13 | 14 | System.out.println("Finished Executing " +t.getName()); 15 | 16 | } 17 | 18 | public static void main(String[] args) throws Exception{ 19 | 20 | Thread t = new Thread (new ThreadJoin(), "New Thread"); 21 | t.start(); 22 | System.out.println("Started executing main thread"); 23 | 24 | t.join(); 25 | 26 | 27 | for(int i =0; i<10; i++) { 28 | 29 | System.out.println(Thread.currentThread().getName() +i); 30 | 31 | } 32 | 33 | System.out.println("Finished Executing " +Thread.currentThread().getName()); 34 | 35 | } 36 | 37 | 38 | } -------------------------------------------------------------------------------- /Basics/Thread/ThreadNotifyTest.java: -------------------------------------------------------------------------------- 1 | public class ThreadNotifyTest { 2 | 3 | public static void main (String args[]) { 4 | 5 | ThreadNotifyTest threadnotifytest = new ThreadNotifyTest(); 6 | 7 | Thread t1 = new Thread1(threadnotifytest, "New Thread1"); 8 | Thread t2 = new Thread2(threadnotifytest, "New Thread2"); 9 | Thread t3 = new Thread3(threadnotifytest, "New Thread3"); 10 | 11 | t1.start(); 12 | t2.start(); 13 | t3.start(); 14 | 15 | 16 | 17 | 18 | 19 | } 20 | 21 | 22 | 23 | 24 | } -------------------------------------------------------------------------------- /Basics/Thread/ThreadYield.java: -------------------------------------------------------------------------------- 1 | public class ThreadYield extends Thread { 2 | 3 | public void run() { 4 | 5 | Thread t = Thread.currentThread(); 6 | System.out.println("Started Executing " +t.getName()); 7 | 8 | for(int i=0; i<10; i++) { 9 | 10 | System.out.println(t.getName() + i); 11 | 12 | } 13 | 14 | System.out.println("Finished Executing " +t.getName()); 15 | 16 | } 17 | 18 | public static void main(String args[]) throws Exception { 19 | 20 | Thread t = new Thread(new ThreadYield(),"New Thread"); 21 | t.start(); 22 | 23 | System.out.println("Started executing main thread"); 24 | 25 | t.yield(); 26 | 27 | for(int i= 0; i<10; i++) { 28 | 29 | System.out.println(Thread.currentThread().getName() +i); 30 | 31 | } 32 | 33 | System.out.println("Finished Executing " +Thread.currentThread().getName()); 34 | 35 | 36 | } 37 | 38 | 39 | 40 | } -------------------------------------------------------------------------------- /Basics/Variables.java: -------------------------------------------------------------------------------- 1 | public class Variables { 2 | public static void main(String args[]) { 3 | 4 | // Integer 5 | int number = 4; 6 | System.out.println(number); 7 | 8 | // Float 9 | float marks = 45.25F; 10 | System.out.println(marks); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Exercises/L01/Exercise1.java: -------------------------------------------------------------------------------- 1 | // Lecture 01 Exercise 01 2 | /* 3 | Write a Java program to display your name and address in 3 lines. 4 | */ 5 | 6 | public class Exercise1 { 7 | 8 | public static void main(String args[]) { 9 | System.out.println("Thushara Thiwanka"); 10 | 11 | System.out.println("Z/00,"); 12 | System.out.println("Earth,"); 13 | System.out.println("Solar System"); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /Exercises/L01/Exercise2.java: -------------------------------------------------------------------------------- 1 | // Lecture 01 Exercise 02 2 | /* 3 | Write a java program to input the length and the width of a rectangle and 4 | calculate and print the perimeter. 5 | */ 6 | 7 | import java.util.Scanner; 8 | 9 | public class Exercise2 { 10 | public static void main(String args[]) { 11 | float length, width; 12 | float perimeter = 0; 13 | 14 | Scanner myScanner = new Scanner(System.in); 15 | 16 | System.out.print("Enter length : "); 17 | length = myScanner.nextFloat(); 18 | System.out.print("Enter width : "); 19 | width = myScanner.nextFloat(); 20 | 21 | perimeter = (length + width) * 2; 22 | 23 | System.out.println("Perimeter = " + perimeter); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Exercises/L01/Exercise3.java: -------------------------------------------------------------------------------- 1 | // Lecture 01 Exercise 03 2 | /* 3 | WWrite a program to input 3 integers and print the largest and 4 | the smallest of the 3 numbers entered. 5 | */ 6 | 7 | import java.util.Scanner; 8 | 9 | public class Exercise3 { 10 | public static void main(String args[]) { 11 | int max = 0, min = 0, num = 0, count = 1; 12 | 13 | Scanner myScanner = new Scanner(System.in); 14 | 15 | System.out.print("Enter Number : "); 16 | max = myScanner.nextInt(); 17 | min = max; 18 | while(count < 3) { 19 | System.out.print("Enter Number : "); 20 | num = myScanner.nextInt(); 21 | if(num > max) { 22 | max = num; 23 | } 24 | else if(num < min) { 25 | min = num; 26 | } 27 | count++; 28 | } 29 | 30 | System.out.println("Largest Number = " + max); 31 | System.out.println("Smallest Number = " + min); 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Exercises/L01/Exercise4.java: -------------------------------------------------------------------------------- 1 | // Lecture 01 Exercise 04 2 | /* 3 | Write a program to input 10 numbers from the keyboard and 4 | find how many odd numbers and how many even numbers were entered. 5 | */ 6 | 7 | import java.util.Scanner; 8 | 9 | public class Exercise4 { 10 | public static void main(String args[]) { 11 | int number, evenCount = 0, oddCount = 0, count = 0; 12 | 13 | Scanner myScanner = new Scanner(System.in); 14 | 15 | while (count < 10) { 16 | System.out.print("Enter Number : "); 17 | number = myScanner.nextInt(); 18 | if (number % 2 == 0) { 19 | evenCount += 1; 20 | } else { 21 | oddCount += 1; 22 | } 23 | ++count; 24 | } 25 | 26 | System.out.println("Even Numbers : " + evenCount); 27 | System.out.println("Odd Numbers : " + oddCount); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /HelloWorld.java: -------------------------------------------------------------------------------- 1 | // Java Hello World 2 | 3 | public class HelloWorld { 4 | public static void main(String args[]) { 5 | System.out.println("Hello World !"); 6 | } 7 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Thushara Thiwanka 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 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 01/Lab Sheet 01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Lab Sheets/Lab Sheet 01/Lab Sheet 01.pdf -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 01/Lab1Ex1.java: -------------------------------------------------------------------------------- 1 | /* Write a program that produces the following output: 2 | 3 | Hello World! 4 | It's been nice knowing you. 5 | Goodbye world! 6 | */ 7 | 8 | class Lab1Ex1 { 9 | public static void main(String[] args) { 10 | System.out.println("Hello World!\nIt's been nice knowing you.\nGoodbye world!"); 11 | } 12 | } 13 | 14 | // -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 01/Lab1Ex2.java: -------------------------------------------------------------------------------- 1 | /* Write a program to get the following output: 2 | =================================== 3 | = Student Information = 4 | =================================== 5 | = Name : ARDP Ranasignhe = 6 | = Reg No : DIS\08\M4\1234 = 7 | = Address : Malabe = 8 | =================================== 9 | */ 10 | 11 | class Lab1Ex2 { 12 | public static void main(String[] args) { 13 | System.out.println("========================================="); 14 | System.out.println("=\tStudent Information\t\t="); 15 | System.out.println("========================================="); 16 | System.out.println("= Name\t\t: ARDP Ranasignhe\t="); 17 | System.out.println("= Reg No\t: DIS\\08\\M4\\1234\t="); 18 | System.out.println("= Address\t: Malabe\t\t="); 19 | System.out.println("========================================="); 20 | } 21 | } 22 | 23 | // -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 01/Lab1Ex3.java: -------------------------------------------------------------------------------- 1 | /* Write a program to validate a given age of a person using if-else statements. 2 | If age is greater than 18 it should print “Adult” otherwise print “Child”. 3 | */ 4 | 5 | class Lab1Ex3 { 6 | public static void main(String[] args) { 7 | int age = 19; 8 | 9 | if(age > 18) { 10 | System.out.println("Adult"); 11 | } 12 | else { 13 | System.out.println("Child"); 14 | } 15 | } 16 | } 17 | 18 | // -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 01/Lab1Ex4.java: -------------------------------------------------------------------------------- 1 | /* Write a program to display the day of the week when you provide the value. 2 | Expected Output: 3 | Value of the day : 4 4 | Day of the week : Thursday 5 | Good Bye! 6 | 7 | You must use “switch –case” statement. 8 | Display “Invalid Day” when some invalid value is provided. 9 | Message “Good Bye” must appear at last 10 | */ 11 | 12 | class Lab1Ex4 { 13 | public static void main(String[] args) { 14 | 15 | int dayNumber = 4; 16 | String day; 17 | 18 | System.out.println("Value of the day : " + dayNumber); 19 | 20 | switch (dayNumber) { 21 | case 1: 22 | day = "Monday"; 23 | break; 24 | case 2: 25 | day = "Tuesday"; 26 | break; 27 | case 3: 28 | day = "Wednesday"; 29 | break; 30 | case 4: 31 | day = "Thursday"; 32 | break; 33 | case 5: 34 | day = "Friday"; 35 | break; 36 | case 6: 37 | day = "Saturday"; 38 | break; 39 | case 7: 40 | day = "Sunday"; 41 | break; 42 | 43 | default: 44 | day = "Invalid Day"; 45 | } 46 | 47 | System.out.println("Day of the week : " + day); 48 | 49 | System.out.println("Good Bye!"); 50 | } 51 | } 52 | 53 | // -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 01/Lab1Ex5.java: -------------------------------------------------------------------------------- 1 | /* Write a program that draws the following figures one above the other. 2 | Using whileloop 3 | * * * * * 4 | * * * * * 5 | * * * * * 6 | * * * * * 7 | * * * * * 8 | 9 | Using forloop 10 | * 11 | * * 12 | * * * 13 | * * * * 14 | * * * * * 15 | */ 16 | 17 | class Lab1Ex5 { 18 | public static void main(String[] args) { 19 | 20 | int column = 0; 21 | int raw = 0; 22 | 23 | while (column < 5) { 24 | while(raw < 5) { 25 | System.out.print("* "); 26 | raw ++; 27 | } 28 | System.out.print("\n"); 29 | raw = 0; 30 | column ++; 31 | } 32 | 33 | for(column = 1; column <= 5; column ++) { 34 | for(raw = 0; raw < 5 - column; raw ++) { 35 | System.out.print(" "); 36 | } 37 | 38 | for(raw = 0; raw < column; raw ++) { 39 | System.out.print("* "); 40 | } 41 | System.out.print("\n"); 42 | } 43 | } 44 | } 45 | 46 | // -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 01/Lab1Ex6.java: -------------------------------------------------------------------------------- 1 | /* Write a program that draws the following figures one next the other. 2 | 3 | * * * * * * 4 | * * * * * * * 5 | * * * * * * * * 6 | * * * * * * * * * 7 | * * * * * * * * * * 8 | 9 | */ 10 | 11 | class Lab1Ex6 { 12 | public static void main(String[] args) { 13 | 14 | int column = 0; 15 | int raw = 0; 16 | 17 | for(column = 1; column <= 5; column++) { 18 | for(raw = 0; raw < 5; raw++) { 19 | System.out.print("* "); 20 | } 21 | 22 | System.out.print("\t"); 23 | 24 | for(raw = 0; raw < 5 - column; raw ++) { 25 | System.out.print(" "); 26 | } 27 | 28 | for(raw = 0; raw < column; raw ++) { 29 | System.out.print("* "); 30 | } 31 | 32 | System.out.print("\n"); 33 | } 34 | } 35 | } 36 | 37 | // -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 01/Lab1Ex7.java: -------------------------------------------------------------------------------- 1 | /* Write a Java program to add two integers and display the sum and the average of two Integers. 2 | Declare two integer variables to store the numbers to be added. 3 | */ 4 | 5 | class Lab1Ex7 { 6 | public static void main(String[] args) { 7 | 8 | // Declare two integer variables to store the numbers to be added or find average. 9 | int num1 = 4, num2 = 6; 10 | 11 | // Call the method to add two integers 12 | System.out.println("Sum = " + sum(num1, num2)); 13 | 14 | // Call the method to find average of two integers 15 | System.out.println("Average = " + average(num1, num2)); 16 | 17 | } 18 | 19 | // Method to add two integers 20 | private static int sum(int num1, int num2) { 21 | return num1 + num2; 22 | } 23 | 24 | // Method to find the average of two integers 25 | private static int average(int num1, int num2) { 26 | return sum(num1, num2) / 2; 27 | } 28 | } 29 | 30 | // -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 02/Lab Sheet 02.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Lab Sheets/Lab Sheet 02/Lab Sheet 02.pdf -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 02/Lakshan/Lab2Ex1.java: -------------------------------------------------------------------------------- 1 | public class Lab2Ex1 { 2 | public static void main(String[] args) { 3 | int miles, yards; 4 | double kilometers; 5 | miles = 26; 6 | yards = 365; 7 | 8 | kilometers = (miles * 1.609) + (yards * 1.609 / 1760); 9 | System.out.println("Kilometers: " + kilometers); 10 | } 11 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 02/Lakshan/Lab2Ex2.java: -------------------------------------------------------------------------------- 1 | public class Lab2Ex2 { 2 | public static void main(String[] args) { 3 | System.out.println("DitNo : " + args[1]); 4 | System.out.println("Name : " + args[0]); 5 | System.out.println("District : " + args[2]); 6 | } 7 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 02/Lakshan/Lab2Ex3.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Lab2Ex3 { 3 | public static void main(String[] args) { 4 | int width,length,height,volume; 5 | Scanner input = new Scanner(System.in); 6 | System.out.print("Enter the width of the cube: "); 7 | width = input.nextInt(); 8 | System.out.print("Enter the length of the cube: "); 9 | length = input.nextInt(); 10 | System.out.print("Enter the height of the cube: "); 11 | height = input.nextInt(); 12 | input.close(); 13 | volume = width * length * height; 14 | System.out.println("The volume of the cube is: " + volume); 15 | } 16 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 02/Lakshan/Lab2Ex4.java: -------------------------------------------------------------------------------- 1 | import java.io.InputStreamReader; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | public class Lab2Ex4 { 5 | public static void main(String[] args) throws IOException{ 6 | int width,length,height,volume; 7 | 8 | InputStreamReader isr = new InputStreamReader(System.in); 9 | BufferedReader br = new BufferedReader(isr); 10 | System.out.print("Enter the width of the box: "); 11 | width = Integer.parseInt(br.readLine()); 12 | System.out.print("Enter the length of the box: "); 13 | length = Integer.parseInt(br.readLine()); 14 | System.out.print("Enter the height of the box: "); 15 | height = Integer.parseInt(br.readLine()); 16 | volume = width * length * height; 17 | System.out.print("The volume of the box is: " + volume); 18 | } 19 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 02/Lakshan/Lab2Ex5/Demo.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Demo { 3 | public static void main(String[] args) { 4 | EvenOddNumber evenOdd = new EvenOddNumber(); 5 | int num; 6 | Scanner input = new Scanner(System.in); 7 | System.out.print("Enter a number: "); 8 | num = input.nextInt(); 9 | input.close(); 10 | if (evenOdd.findEvenOrOdd(num)) { 11 | System.out.println("The number is even."); 12 | } else { 13 | System.out.println("The number is odd."); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 02/Lakshan/Lab2Ex5/EvenOddNumber.java: -------------------------------------------------------------------------------- 1 | public class EvenOddNumber { 2 | boolean findEvenOrOdd(int i) { 3 | if (i % 2 == 0) { 4 | return true; 5 | } else { 6 | return false; 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 02/Sahan/Exercise1.java: -------------------------------------------------------------------------------- 1 | // Exercise 1 2 | 3 | /* a) Declare two integer variables, miles and yards, and one double variable for 4 | kilometers 5 | b) Initialize the variables to hold the number of miles and yards in a marathon respectively 6 | (miles to 26 and yards to 385). 7 | c) Write an expression to calculate kilometers from miles and yards. 8 | d) Save the result of the expression in the variable kilometers. 9 | Note: One mile is 1.609 kilometers. There are 1760.0 yards in a mile */ 10 | 11 | public class Exercise1 { // begining of the class 12 | 13 | public static void main(String[] args) { // Begining of the method 14 | 15 | // Declare given variables 16 | 17 | int miles = 26, yards = 385; 18 | double kilometers; 19 | 20 | kilometers = (miles * 26) + (yards * 1.609 / 1760); // Calculation of Kilometers 21 | 22 | System.out.print("Kilometers :" + kilometers); // Display Kilometers 23 | 24 | } // End of the method 25 | 26 | } // End of the class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 02/Sahan/Exercise2.java: -------------------------------------------------------------------------------- 1 | public class Exercise2 { 2 | 3 | public static void main(String args[]) { 4 | 5 | System.out.println("DitNo :" + args[1]); 6 | System.out.println("Name :" + args[0]); 7 | System.out.println("District :" + args[2]); 8 | 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 02/Sahan/Exercise3.java: -------------------------------------------------------------------------------- 1 | // Exercise 3 2 | 3 | /* Write a program to calculate the volume of a cube when length, width and height are 4 | given as keyboard inputs. */ 5 | 6 | import java.util.Scanner; // Use java Scanner package 7 | 8 | public class Exercise3 { // Begining of the class 9 | 10 | public static void main(String[] args) { // Begining of the method 11 | 12 | int length, width, height, volume; // Declare variable 13 | 14 | Scanner sc = new Scanner(System.in); 15 | 16 | System.out.print("Enter the Length of Cube : "); 17 | length = sc.nextInt(); 18 | 19 | System.out.print("Enter the Width of Cube :"); 20 | width = sc.nextInt(); 21 | 22 | System.out.print("Enter the Height of the Cube :"); 23 | height = sc.nextInt(); 24 | 25 | volume = length * width * height; 26 | 27 | System.out.print("The Volume of the Cube is :" + volume); 28 | 29 | } // End of the Method 30 | 31 | } // End of the class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 02/Sahan/Exercise4.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Exercise4 { 4 | 5 | public static void main(String[] args) throws IOException { 6 | 7 | int length, width, height, volume; 8 | 9 | InputStreamReader isr = new InputStreamReader(System.in); 10 | 11 | // BufferedReader in = new BufferedReader(isr); 12 | 13 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 14 | 15 | System.out.print("Enter the Length of the Cube :"); 16 | length = Integer.parseInt(in.readLine()); 17 | 18 | System.out.print("Enter the Width of the Cube :"); 19 | width = Integer.parseInt(in.readLine()); 20 | 21 | System.out.print("Enter the Height of the Cube :"); 22 | height = Integer.parseInt(in.readLine()); 23 | 24 | volume = length * width * height; 25 | 26 | System.out.print("The Volume is :" + volume); 27 | 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lab Sheet 03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Lab Sheets/Lab Sheet 03/Lab Sheet 03.pdf -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 1/Student.Java: -------------------------------------------------------------------------------- 1 | public class Student { 2 | String name; 3 | String ditno; 4 | String address; 5 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 1/Test.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 | public static void main(String[] args) { 3 | // Create Students Objects 4 | Student s1 = new Student(); 5 | Student s2 = new Student(); 6 | 7 | // Assign values to the objects 8 | s1.name = "Pasindu"; 9 | s1.ditno = "IT20123456"; 10 | s1.address = "Galle"; 11 | 12 | s2.name = "Lakshan"; 13 | s2.ditno = "IT20123457"; 14 | s2.address = "Matara"; 15 | 16 | // Display the values 17 | System.out.println("Student 1"); 18 | System.out.println("Name: " + s1.name + "\nDitno: " + s1.ditno + "\nAddress: " + s1.address + "\n"); 19 | System.out.println("Student 2"); 20 | System.out.println("Name: " + s2.name + "\nDitno: " + s2.ditno + "\nAddress: " + s2.address); 21 | } 22 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 10/Cat.java: -------------------------------------------------------------------------------- 1 | public class Cat extends Pet{ 2 | private int livesLeft; 3 | public Cat(String n, String o, int a, int l) { 4 | super(n, o, a); 5 | this.livesLeft = l; 6 | } 7 | public Cat(String n, String o) { 8 | super(n, o); 9 | this.livesLeft = 7; 10 | } 11 | }//end of the pet class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 10/Dog.java: -------------------------------------------------------------------------------- 1 | public class Dog extends Pet{ 2 | int noOfMasters; 3 | public Dog(String n, String o, int a, int m) { 4 | super(n, o, a); 5 | this.noOfMasters = m; 6 | } 7 | public Dog(String n, String o) { 8 | super(n, o); 9 | this.noOfMasters = 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 10/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args){ 3 | 4 | Dog dog1 = new Dog("Lissie","Smith",10,3); 5 | dog1.showDetails(); 6 | Dog dog2 = new Dog("Kyan","Silva"); 7 | dog2.showDetails(); 8 | } 9 | }//end of the demo class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 10/Pet.java: -------------------------------------------------------------------------------- 1 | public class Pet { 2 | private String name; 3 | private String owner; 4 | private int age; 5 | 6 | public Pet(String n, String o, int a) { 7 | this.name = n; 8 | this.owner = o; 9 | this.age = a; 10 | } 11 | public Pet(String n, String o) { 12 | this.name = n; 13 | this.owner = o; 14 | this.age = 0; 15 | } 16 | public void showDetails(){ 17 | System.out.println("I am a pet. My name is " +this.name+". My owner is "+this.owner); 18 | } 19 | }//end of the pet class 20 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 11/Cat.java: -------------------------------------------------------------------------------- 1 | public class Cat extends Pet{ 2 | private int livesLeft; 3 | public Cat(String n, String o, int a, int l) { 4 | super(n, o, a); 5 | this.livesLeft = l; 6 | } 7 | public Cat(String n, String o) { 8 | super(n, o); 9 | this.livesLeft = 7; 10 | } 11 | public void showDetails(){ 12 | super.showDetails(); 13 | System.out.println("I am a cat. " + 14 | this.livesLeft + " lives remain for me."); 15 | } 16 | }//end of the pet class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 11/Dog.java: -------------------------------------------------------------------------------- 1 | public class Dog extends Pet{ 2 | int noOfMasters; 3 | public Dog(String n, String o, int a, int m) { 4 | super(n, o, a); 5 | this.noOfMasters = m; 6 | } 7 | public Dog(String n, String o) { 8 | super(n, o); 9 | this.noOfMasters = 1; 10 | } 11 | public void showDetails(){ 12 | super.showDetails(); 13 | System.out.println("I am a dog. I have "+this.noOfMasters+" masters at home"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 11/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args){ 3 | Cat cat1 = new Cat("Kitty", "James"); 4 | cat1.showDetails(); 5 | Dog dog1 = new Dog("Jimi", "Jhon"); 6 | dog1.showDetails(); 7 | } 8 | }//end of the demo class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 11/Pet.java: -------------------------------------------------------------------------------- 1 | public class Pet { 2 | private String name; 3 | private String owner; 4 | private int age; 5 | 6 | public Pet(String n, String o, int a) { 7 | this.name = n; 8 | this.owner = o; 9 | this.age = a; 10 | } 11 | public Pet(String n, String o) { 12 | this.name = n; 13 | this.owner = o; 14 | this.age = 0; 15 | } 16 | public void showDetails(){ 17 | System.out.println("I am a pet. My name is " +this.name+". My owner is "+this.owner); 18 | } 19 | }//end of the pet class 20 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 2/Student.Java: -------------------------------------------------------------------------------- 1 | public class Student { 2 | private String name; 3 | private String ditno; 4 | private String address; 5 | 6 | //default constructor 7 | public Student() { 8 | this.name = null; 9 | this.ditno = null; 10 | this.address = null; 11 | } 12 | //constructor with parameters 13 | public Student(String name, String ditno, String address) { 14 | this.name = name; 15 | this.ditno = ditno; 16 | this.address = address; 17 | } 18 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 2/Test.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 | public static void main(String[] args) { 3 | //Create Objects using the constructor 4 | Student s1 = new Student("Pasindu","IT20123456","Galle"); 5 | Student s2 = new Student("Lakshan","IT20123457","Matara"); 6 | } 7 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 3/Student.Java: -------------------------------------------------------------------------------- 1 | public class Student { 2 | private String name; 3 | private String ditno; 4 | private String address; 5 | 6 | //default constructor 7 | public Student() { 8 | this.name = null; 9 | this.ditno = null; 10 | this.address = null; 11 | } 12 | //constructor with parameters 13 | public Student(String name, String ditno, String address) { 14 | this.name = name; 15 | this.ditno = ditno; 16 | this.address = address; 17 | } 18 | 19 | //Mutator methods 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | public void setDitno(String ditno) { 24 | this.ditno = ditno; 25 | } 26 | public void setAddress(String address) { 27 | this.address = address; 28 | } 29 | 30 | //Accessor methods 31 | public String getName() { 32 | return this.name; 33 | } 34 | public String getDitno() { 35 | return this.ditno; 36 | } 37 | public String getAddress() { 38 | return this.address; 39 | } 40 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 3/Test.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 | public static void main(String[] args) { 3 | //Create Objects using the constructor 4 | Student s1 = new Student("Pasindu","IT20123456","Galle"); 5 | 6 | //Change values using mutator methods 7 | s1.setName("Lakshan"); 8 | s1.setDitno("IT20657567"); 9 | s1.setAddress("Colombo"); 10 | 11 | //Dislplay changed values 12 | System.out.println("Name: " + s1.getName()+ "\nDitno: " + s1.getDitno() + "\nAddress: " + s1.getAddress()); 13 | } 14 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 4/Student.Java: -------------------------------------------------------------------------------- 1 | public class Student { 2 | private String name; 3 | private String ditno; 4 | private String address; 5 | 6 | //default constructor 7 | public Student() { 8 | this.name = null; 9 | this.ditno = null; 10 | this.address = null; 11 | } 12 | //constructor with parameters 13 | public Student(String name, String ditno, String address) { 14 | this.name = name; 15 | this.ditno = ditno; 16 | this.address = address; 17 | } 18 | 19 | //Mutator methods 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | public void setDitno(String ditno) { 24 | this.ditno = ditno; 25 | } 26 | public void setAddress(String address) { 27 | this.address = address; 28 | } 29 | 30 | //Accessor methods 31 | public String getName() { 32 | return this.name; 33 | } 34 | public String getDitno() { 35 | return this.ditno; 36 | } 37 | public String getAddress() { 38 | return this.address; 39 | } 40 | public String getDetails() { 41 | return "I am a Student.\nMy name is " + this.name + "\nI am from " + this.address + "\nMy dit no is " + this.ditno; 42 | } 43 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 4/Test.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 | public static void main(String[] args) { 3 | //Create Objects using the constructor 4 | Student s1 = new Student("Pasindu","IT20123456","Galle"); 5 | 6 | //Desplay Values using getDetails() method 7 | System.out.println(s1.getDetails()); 8 | } 9 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 5/InheritanceDemo.java: -------------------------------------------------------------------------------- 1 | public class InheritanceDemo { 2 | public static void main(String[] args) { 3 | // Create an object of Person class 4 | Person p1 = new Person("Lakshan", "Galle"); 5 | 6 | // Call showDetail() method 7 | p1.showDetail(); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 5/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private String name; 3 | private String address; 4 | 5 | //default constructor 6 | public Person() { 7 | this.name = ""; 8 | this.address = ""; 9 | } 10 | //constructor with parameters 11 | public Person(String name, String address) { 12 | this.name = name; 13 | this.address = address; 14 | } 15 | //showDetails method 16 | public void showDetail() { 17 | System.out.println("Name: " + name); 18 | System.out.println("Address: " + address); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 6/InheritanceDemo.java: -------------------------------------------------------------------------------- 1 | public class InheritanceDemo { 2 | public static void main(String[] args) { 3 | // Create an object of Person class 4 | Student s1 = new Student("Lakshan", "Matara", "IT20123456"); 5 | 6 | // Call showDetail() method 7 | s1.showDetail(); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 6/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private String name; 3 | private String address; 4 | 5 | //default constructor 6 | public Person() { 7 | this.name = ""; 8 | this.address = ""; 9 | } 10 | //constructor with parameters 11 | public Person(String name, String address) { 12 | this.name = name; 13 | this.address = address; 14 | } 15 | //Mutator methods 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | public void setAddress(String address) { 20 | this.address = address; 21 | } 22 | //Accessor methods 23 | public String getName() { 24 | return this.name; 25 | } 26 | public String getAddress() { 27 | return this.address; 28 | } 29 | //showDetails method 30 | public void showDetail() { 31 | System.out.println("Name: " + name); 32 | System.out.println("Address: " + address); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 6/Student.java: -------------------------------------------------------------------------------- 1 | public class Student extends Person { 2 | private String studentId; 3 | //constructor 4 | public Student(String name, String adress, String studentId) { 5 | super(name, adress); 6 | this.studentId = studentId; 7 | } 8 | //Mutator methods 9 | public void setStudentId(String studentId) { 10 | this.studentId = studentId; 11 | } 12 | //Accessor methods 13 | public String getStudentId() { 14 | return studentId; 15 | } 16 | //showDetail method 17 | public void showDetail() { 18 | System.out.println("Student Details"); 19 | super.showDetail(); 20 | System.out.println("Student ID: " + studentId); 21 | } 22 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 7/InheritanceDemo.java: -------------------------------------------------------------------------------- 1 | public class InheritanceDemo { 2 | public static void main(String[] args) { 3 | // Create an object of Person class 4 | PartTimeStudent ps1 = new PartTimeStudent("Lakshan", "Matara", "IT20123456", 50); 5 | 6 | // Call showDetail() method 7 | ps1.showDetail(); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 7/PartTimeStudent.java: -------------------------------------------------------------------------------- 1 | public class PartTimeStudent extends Student{ 2 | private int noOfHours; 3 | 4 | //constructor 5 | public PartTimeStudent(String name, String address, String studentId, int noOfHours){ 6 | super(name, address, studentId); 7 | this.noOfHours = noOfHours; 8 | } 9 | //mutator methods 10 | public void setNoOfHours(int noOfHours){ 11 | this.noOfHours = noOfHours; 12 | } 13 | //accessor methods 14 | public int getNoOfHours(){ 15 | return this.noOfHours; 16 | } 17 | //showDetail method 18 | public void showDetail(){ 19 | System.out.println("Part-Time Student"); 20 | super.showDetail(); 21 | System.out.println("No. of Working Hours: " + this.noOfHours); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 7/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private String name; 3 | private String address; 4 | 5 | //default constructor 6 | public Person() { 7 | this.name = ""; 8 | this.address = ""; 9 | } 10 | //constructor with parameters 11 | public Person(String name, String address) { 12 | this.name = name; 13 | this.address = address; 14 | } 15 | //Mutator methods 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | public void setAddress(String address) { 20 | this.address = address; 21 | } 22 | //Accessor methods 23 | public String getName() { 24 | return this.name; 25 | } 26 | public String getAddress() { 27 | return this.address; 28 | } 29 | //showDetails method 30 | public void showDetail() { 31 | System.out.println("Name: " + name); 32 | System.out.println("Address: " + address); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 7/Student.java: -------------------------------------------------------------------------------- 1 | public class Student extends Person { 2 | private String studentId; 3 | //constructor 4 | public Student(String name, String adress, String studentId) { 5 | super(name, adress); 6 | this.studentId = studentId; 7 | } 8 | //Mutator methods 9 | public void setStudentId(String studentId) { 10 | this.studentId = studentId; 11 | } 12 | //Accessor methods 13 | public String getStudentId() { 14 | return studentId; 15 | } 16 | //showDetail method 17 | public void showDetail() { 18 | System.out.println("Student Details"); 19 | super.showDetail(); 20 | System.out.println("Student ID: " + studentId); 21 | } 22 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 8/Cat.java: -------------------------------------------------------------------------------- 1 | public class Cat extends Pet{ 2 | private int livesLeft; 3 | public Cat(String n, String o, int a, int l) { 4 | super(n, o, a); 5 | this.livesLeft = l; 6 | } 7 | }//end of the pet class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 8/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args){ 3 | Pet p = new Pet("Lissie","Smith",3); 4 | p.showDetails(); 5 | Cat c = new Cat("Kyan", "Silva", 4, 4); 6 | c.showDetails(); 7 | } 8 | }//end of the demo class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 8/Pet.java: -------------------------------------------------------------------------------- 1 | public class Pet { 2 | private String name; 3 | private String owner; 4 | private int age; 5 | 6 | public Pet(String n, String o, int a) { 7 | this.name = n; 8 | this.owner = o; 9 | this.age = a; 10 | } 11 | 12 | public void showDetails(){ 13 | System.out.println("I am a pet. My name is " +this.name+". My owner is "+this.owner); 14 | } 15 | }//end of the pet class 16 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 9/Cat.java: -------------------------------------------------------------------------------- 1 | public class Cat extends Pet{ 2 | private int livesLeft; 3 | public Cat(String n, String o, int a, int l) { 4 | super(n, o, a); 5 | this.livesLeft = l; 6 | } 7 | public Cat(String n, String o) { 8 | super(n, o); 9 | this.livesLeft = 7; 10 | } 11 | }//end of the pet class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 9/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args){ 3 | Pet p = new Pet("Lissie","Smith",3); 4 | p.showDetails(); 5 | Cat c = new Cat("Kyan", "Silva", 4, 4); 6 | c.showDetails(); 7 | 8 | Pet p2 = new Pet("Kitty","Jhon"); 9 | p2.showDetails(); 10 | Cat c2 = new Cat("Jimi","James"); 11 | c2.showDetails(); 12 | } 13 | }//end of the demo class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Lakshan/Exercise 9/Pet.java: -------------------------------------------------------------------------------- 1 | public class Pet { 2 | private String name; 3 | private String owner; 4 | private int age; 5 | 6 | public Pet(String n, String o, int a) { 7 | this.name = n; 8 | this.owner = o; 9 | this.age = a; 10 | } 11 | public Pet(String n, String o) { 12 | this.name = n; 13 | this.owner = o; 14 | this.age = 0; 15 | } 16 | public void showDetails(){ 17 | System.out.println("I am a pet. My name is " +this.name+". My owner is "+this.owner); 18 | } 19 | }//end of the pet class 20 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 1/Student.java: -------------------------------------------------------------------------------- 1 | 2 | public class Student { 3 | 4 | String name; 5 | String ditno; 6 | String address; 7 | 8 | 9 | } 10 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 1/Test.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 | 3 | public static void main(String[] args) { 4 | 5 | Student s1 = new Student(); 6 | 7 | s1.name = "Kamal"; 8 | s1.ditno = "IT123456"; 9 | s1.address = "Colombo"; 10 | 11 | System.out.println("Student Name :" +s1.name); 12 | System.out.println("Student ID :" +s1.ditno); 13 | System.out.println("Student Address :" +s1.address); 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 2/Student.java: -------------------------------------------------------------------------------- 1 | 2 | public class Student { 3 | 4 | private String name; 5 | private String ditno; 6 | private String address; 7 | private int age; 8 | 9 | public Student() { 10 | 11 | name = null; 12 | ditno = null; 13 | address = null; 14 | 15 | } 16 | 17 | public Student(String name, String ditno, String address) { 18 | 19 | this.name = name; 20 | this.ditno = ditno; 21 | this.address = address; 22 | 23 | 24 | 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 2/Test.java: -------------------------------------------------------------------------------- 1 | 2 | public class Test { 3 | 4 | public static void main(String[] args) { 5 | 6 | Student s1 = new Student(); 7 | 8 | Student s2 = new Student("Kamal", "IT123456", "Colombo"); 9 | 10 | 11 | 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 3/Student.java: -------------------------------------------------------------------------------- 1 | 2 | public class Student { 3 | 4 | private String name; 5 | private String ditno; 6 | private String address; 7 | 8 | // Default constructor 9 | public Student() { 10 | 11 | name = null; 12 | ditno = null; 13 | address = null; 14 | } 15 | 16 | public Student(String name, String ditno, String address) { 17 | 18 | this.name = name; 19 | this.ditno = ditno; 20 | this.address = address; 21 | } 22 | 23 | // Getters 24 | 25 | public String getName() { 26 | return this.name; 27 | } 28 | 29 | public String getAddress() { 30 | return this.address; 31 | } 32 | 33 | public String getDit() { 34 | return this.ditno; 35 | } 36 | 37 | //Setters 38 | 39 | public void setName(String name) { 40 | 41 | this.name = name; 42 | } 43 | 44 | public void setAddress(String address) { 45 | 46 | this.address = address; 47 | } 48 | 49 | public void setDit(String ditno) { 50 | 51 | this.ditno =ditno; 52 | } 53 | 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 3/Test.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 | 3 | public static void main(String[] args) { 4 | 5 | Student s1 = new Student(); // Default constructor 6 | s1.setName(null); 7 | s1.setDit(null); 8 | s1.setAddress(null); 9 | 10 | System.out.println("Student Name :" +s1.getName()); 11 | System.out.println("Student ID :" +s1.getDit()); 12 | System.out.println("Student Address :" +s1.getAddress()); 13 | System.out.println(""); 14 | 15 | Student s2 = new Student(); 16 | 17 | s2.setName("Amal"); 18 | s2.setDit("IT258963"); 19 | s2.setAddress("Colombo"); 20 | 21 | System.out.println("Student Name :" +s2.getName()); 22 | System.out.println("Student ID :" +s2.getDit()); 23 | System.out.println("Student Address :" +s2.getAddress()); 24 | 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 4/Student.java: -------------------------------------------------------------------------------- 1 | public class Student { 2 | 3 | private String name; 4 | private String ditno; 5 | private String address; 6 | 7 | // Default constructor 8 | public Student() { 9 | 10 | name = null; 11 | ditno = null; 12 | address = null; 13 | } 14 | 15 | public Student(String name, String ditno, String address) { 16 | 17 | this.name = name; 18 | this.ditno = ditno; 19 | this.address = address; 20 | 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public String getDitno() { 28 | return ditno; 29 | } 30 | 31 | public String getAddress() { 32 | return address; 33 | } 34 | 35 | public void setName(String name) { 36 | 37 | this.name = name; 38 | } 39 | 40 | public void setDitno(String ditno) { 41 | this.ditno = ditno; 42 | } 43 | 44 | public void setAddress(String address) { 45 | this.address = address; 46 | } 47 | 48 | public String getDetails() { 49 | 50 | String details = "I am a Student. \nMy name is "+this.name+". \nI am from "+this.address+ ". \nMy dit no is "+this.ditno; 51 | return details; 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 4/Test.java: -------------------------------------------------------------------------------- 1 | 2 | public class Test { 3 | 4 | public static void main(String[] args) { 5 | 6 | Student s1 = new Student(); 7 | 8 | s1.setName(null); 9 | s1.setDitno(null); 10 | s1.setAddress(null); 11 | 12 | System.out.println("Student Name :" +s1.getName()); 13 | System.out.println("Student ID :" +s1.getDitno()); 14 | System.out.println("Student Address :" +s1.getAddress()); 15 | System.out.println(""); 16 | 17 | Student s2 = new Student(); 18 | 19 | s2.setName("Avishka"); 20 | s2.setAddress("Colombo"); 21 | s2.setDitno("IT258963"); 22 | 23 | String res = s2.getDetails(); 24 | 25 | System.out.println(res); 26 | 27 | 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 5/InheritanceDemo.java: -------------------------------------------------------------------------------- 1 | 2 | public class InheritanceDemo { 3 | 4 | public static void main(String[] args) { 5 | 6 | Person p1 = new Person("Sahan", "Colombo"); 7 | p1.showDetails(); 8 | 9 | 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 5/Person.java: -------------------------------------------------------------------------------- 1 | 2 | public class Person { 3 | 4 | private String name; 5 | private String address; 6 | 7 | 8 | public Person() { 9 | 10 | name = null; 11 | address = null; 12 | } 13 | 14 | public Person(String name, String address) { 15 | 16 | this.name = name; 17 | this.address = address; 18 | } 19 | 20 | /*// Setters 21 | 22 | public void setName(String name) { 23 | 24 | this.name = name; 25 | } 26 | 27 | public void setAddress(String address) { 28 | 29 | this.address = address; 30 | }*/ 31 | 32 | public void showDetails() { 33 | 34 | System.out.println("The Person Name :" +this.name); 35 | System.out.println("The Person Address :" +this.address); 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 6/InheritanceDemo.java: -------------------------------------------------------------------------------- 1 | 2 | public class InheritanceDemo { // Begining of the Class 3 | 4 | public static void main(String[] args) { // Begining of the Method 5 | 6 | Person p1 = new Person(); 7 | p1.showDetails(); // Display default constructor 8 | System.out.println(""); 9 | 10 | Student s1 = new Student("Kamal", "Colombo", "IT0014"); 11 | s1.showDetails(); // Display details with Student ID 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | } // End of the Method 20 | 21 | } // End of the class 22 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 6/Person.java: -------------------------------------------------------------------------------- 1 | 2 | public class Person { 3 | 4 | private String name; 5 | private String address; 6 | 7 | public Person() { 8 | 9 | name = null; 10 | address = null; 11 | 12 | } 13 | 14 | public Person(String pName, String pAddress) { 15 | 16 | this.name = pName; 17 | this.address = pAddress; 18 | 19 | } 20 | 21 | public void showDetails() { 22 | 23 | System.out.println("Name :" +this.name); 24 | System.out.println("Address :" +this.address); 25 | 26 | } 27 | 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 6/Student.java: -------------------------------------------------------------------------------- 1 | 2 | public class Student extends Person{ 3 | 4 | private String StudentID; 5 | 6 | 7 | public Student(String pName, String pAddress, String StudentID) { 8 | 9 | super(pName, pAddress); 10 | this.StudentID = StudentID; 11 | } 12 | 13 | public void showDetails() { 14 | super.showDetails(); 15 | System.out.println("Student ID :" +this.StudentID); 16 | 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 7/InheritanceDemo.java: -------------------------------------------------------------------------------- 1 | 2 | public class InheritanceDemo { // Begining of the Class 3 | 4 | public static void main(String[] args) { // Begining of the Method 5 | 6 | Person p1 = new Person(); 7 | p1.showDetails(); // Default Constructor Called 8 | System.out.println(""); 9 | 10 | Student s1 = new Student("Sahan", "Colombo", "IT2011470"); 11 | s1.showDetails(); 12 | System.out.println(""); 13 | 14 | PartTimeStudent pts = new PartTimeStudent("Kamal", "Kurunagala", "IT01117",6); 15 | pts.showDetails(); 16 | 17 | } // End of the Method 18 | 19 | } // End of the Class 20 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 7/PartTimeStudent.java: -------------------------------------------------------------------------------- 1 | public class PartTimeStudent extends Student{ 2 | 3 | private int workHours; 4 | 5 | public PartTimeStudent(String name, String Address, String StudentID, int workHours) { 6 | 7 | super(name, Address, StudentID); 8 | this.workHours = workHours; 9 | } 10 | 11 | public void showDetails() { 12 | 13 | super.showDetails(); 14 | System.out.println("Working Hours :" +this.workHours); 15 | 16 | } 17 | 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 7/Person.java: -------------------------------------------------------------------------------- 1 | 2 | public class Person { 3 | 4 | private String name; 5 | private String Address; 6 | 7 | public Person() { 8 | 9 | name = null; 10 | Address = null; 11 | } 12 | 13 | public Person(String name, String Address) { 14 | 15 | this.name = name; 16 | this.Address = Address; 17 | } 18 | 19 | public void showDetails() { 20 | 21 | System.out.println("Name :" +this.name); 22 | System.out.println("Address :" +this.Address); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Sahan/Exercise 7/Student.java: -------------------------------------------------------------------------------- 1 | public class Student extends Person { 2 | 3 | private String StudentID; 4 | 5 | public Student(String name, String Address, String StudentID) { 6 | 7 | super(name,Address); 8 | this.StudentID = StudentID; 9 | } 10 | 11 | public void showDetails() { 12 | 13 | super.showDetails(); 14 | System.out.println("Student ID :" +this.StudentID); 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex01/Student.java: -------------------------------------------------------------------------------- 1 | 2 | public class Student { 3 | String name; 4 | String ditno; 5 | String address; 6 | } 7 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex01/Test.java: -------------------------------------------------------------------------------- 1 | 2 | public class Test { 3 | 4 | public static void main(String[] args) { 5 | 6 | Student s1 = new Student(); 7 | Student s2 = new Student(); 8 | 9 | s1.name = "Thushara"; 10 | s1.ditno = "DIT/11/C1/0010"; 11 | s1.address = "Colombo"; 12 | 13 | s2.name = "Thiwanka"; 14 | s2.ditno = "DIT/11/C1/0020"; 15 | s2.address = "Kandy"; 16 | 17 | System.out.println(s1.name); 18 | System.out.println(s1.ditno); 19 | System.out.println(s1.address); 20 | 21 | System.out.println(s2.name); 22 | System.out.println(s2.ditno); 23 | System.out.println(s2.address); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex02/Student.java: -------------------------------------------------------------------------------- 1 | 2 | public class Student { 3 | String name; 4 | String ditno; 5 | String address; 6 | 7 | Student(String name, String ditno, String address) { 8 | this.name = name; 9 | this.ditno = ditno; 10 | this.address = address; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex02/Test.java: -------------------------------------------------------------------------------- 1 | 2 | public class Test { 3 | 4 | public static void main(String[] args) { 5 | 6 | Student s1 = new Student("Thushara", "DIT/11/C1/0010", "Colombo"); 7 | Student s2 = new Student("Thiwanka", "DIT/11/C1/0020", "Kandy"); 8 | 9 | System.out.println(s1.name); 10 | System.out.println(s1.ditno); 11 | System.out.println(s1.address); 12 | 13 | System.out.println(s2.name); 14 | System.out.println(s2.ditno); 15 | System.out.println(s2.address); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex03/Student.java: -------------------------------------------------------------------------------- 1 | 2 | public class Student { 3 | private String name; 4 | private String ditno; 5 | private String address; 6 | 7 | Student(String name, String ditno, String address) { 8 | this.name = name; 9 | this.ditno = ditno; 10 | this.address = address; 11 | } 12 | 13 | public String getName() { 14 | return this.name; 15 | } 16 | public String getAddress() { 17 | return this.address; 18 | } 19 | public String getDit() { 20 | return this.ditno; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | public void setAddress(String address) { 27 | this.address = address; 28 | } 29 | public void setDit(String ditno) { 30 | this.ditno = ditno; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex03/Test.java: -------------------------------------------------------------------------------- 1 | /* Accessor and Mutator Methods 2 | (Getters and Setters) 3 | */ 4 | 5 | public class Test { 6 | 7 | public static void main(String[] args) { 8 | 9 | Student s1 = new Student("Thushara", "DIT/11/C1/0010", "Colombo"); 10 | Student s2 = new Student("Thiwanka", "DIT/11/C1/0020", "Kandy"); 11 | 12 | s1.setName("Kasun"); 13 | s1.setDit("DIT/11/C1/0030"); 14 | s1.setAddress("Kaluthara"); 15 | 16 | s2.setName("Nimal"); 17 | s2.setDit("DIT/11/C1/0040"); 18 | s2.setAddress("Gampaha"); 19 | 20 | System.out.println(s1.getName()); 21 | System.out.println(s1.getDit()); 22 | System.out.println(s1.getAddress()); 23 | 24 | System.out.println(s2.getName()); 25 | System.out.println(s2.getDit()); 26 | System.out.println(s2.getAddress()); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex04/Student.java: -------------------------------------------------------------------------------- 1 | 2 | public class Student { 3 | private String name; 4 | private String ditno; 5 | private String address; 6 | 7 | Student(String name, String ditno, String address) { 8 | this.name = name; 9 | this.ditno = ditno; 10 | this.address = address; 11 | } 12 | 13 | public String getName() { 14 | return this.name; 15 | } 16 | public String getAddress() { 17 | return this.address; 18 | } 19 | public String getDit() { 20 | return this.ditno; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | public void setAddress(String address) { 27 | this.address = address; 28 | } 29 | public void setDit(String ditno) { 30 | this.ditno = ditno; 31 | } 32 | 33 | public String getDetails() { 34 | String details = 35 | "I am a Student.\r\n" 36 | + "My name is " + this.name + ".\r\n" 37 | + "I am from " + this.address + ".\r\n" 38 | + "My dit no is " + this.ditno; 39 | return details; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex04/Test.java: -------------------------------------------------------------------------------- 1 | /* Accessor and Mutator Methods 2 | (Getters and Setters) 3 | */ 4 | 5 | public class Test { 6 | 7 | public static void main(String[] args) { 8 | 9 | Student s1 = new Student("Thushara", "DIT/11/C1/0010", "Colombo"); 10 | Student s2 = new Student("Thiwanka", "DIT/11/C1/0020", "Kandy"); 11 | 12 | s1.setName("Kasun"); 13 | s1.setDit("DIT/11/C1/0030"); 14 | s1.setAddress("Kaluthara"); 15 | 16 | s2.setName("Nimal"); 17 | s2.setDit("DIT/11/C1/0040"); 18 | s2.setAddress("Gampaha"); 19 | 20 | System.out.println(s1.getName()); 21 | System.out.println(s1.getDit()); 22 | System.out.println(s1.getAddress()); 23 | 24 | System.out.println(s2.getName()); 25 | System.out.println(s2.getDit()); 26 | System.out.println(s2.getAddress()); 27 | 28 | System.out.println(s1.getDetails()); 29 | System.out.println(s2.getDetails()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex05-06-07/InheritanceDemo.java: -------------------------------------------------------------------------------- 1 | 2 | public class InheritanceDemo { 3 | 4 | public static void main(String[] args) { 5 | 6 | // Create Person object 7 | Person p = new Person("Thushara", "Gampaha"); 8 | 9 | // Create Student object 10 | Student s = new Student("Thiwanka", "Colombo", "IT1020"); 11 | 12 | // Create PartTimeStudent object 13 | PartTimeStudent pts = new PartTimeStudent("Thushara", "Kandy", "IT2040", 5); 14 | 15 | p.showDetails(); 16 | s.showDetails(); 17 | pts.showDetails(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex05-06-07/PartTimeStudent.java: -------------------------------------------------------------------------------- 1 | 2 | public class PartTimeStudent extends Student { 3 | 4 | int numOfWorkHours; 5 | 6 | PartTimeStudent(String name, String address, String studentid, int numOfWorkHours) { 7 | super(name, address, studentid); 8 | this.numOfWorkHours = numOfWorkHours; 9 | } 10 | 11 | public void showDetails() { 12 | super.showDetails(); 13 | System.out.println("Number of Working Hours : " + this.numOfWorkHours); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex05-06-07/Person.java: -------------------------------------------------------------------------------- 1 | 2 | public class Person { 3 | String name; 4 | String address; 5 | 6 | Person() {}; 7 | Person(String name, String address) { 8 | this.name = name; 9 | this.address = address; 10 | } 11 | 12 | public void showDetails() { 13 | System.out.println("Name : " + this.name); 14 | System.out.println("Address : " + this.address); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex05-06-07/Student.java: -------------------------------------------------------------------------------- 1 | 2 | public class Student extends Person { 3 | 4 | String studentid; 5 | 6 | Student() {} 7 | Student(String name, String address, String studentid) { 8 | super(name, address); 9 | this.studentid = studentid; 10 | } 11 | 12 | public void showDetails() { 13 | super.showDetails(); 14 | System.out.println("Student ID : " + this.studentid); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex08-09-10-11/Cat.java: -------------------------------------------------------------------------------- 1 | 2 | public class Cat extends Pet{ 3 | private int livesLeft; 4 | 5 | Cat() {} 6 | 7 | Cat(String n, String o) { 8 | super(n, o); 9 | this.livesLeft = 7; 10 | } 11 | 12 | Cat(String n, String o, int a, int l) { 13 | super(n, o, a); 14 | this.livesLeft = l; 15 | } 16 | 17 | public void showDetails() { 18 | super.showDetails(); 19 | System.out.println("I am a cat. " + 20 | this.livesLeft + " lives remain for me."); 21 | } 22 | }//end of the pet class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex08-09-10-11/Dog.java: -------------------------------------------------------------------------------- 1 | 2 | public class Dog extends Pet { 3 | private int noOfMasters; 4 | 5 | Dog() {} 6 | 7 | Dog(String n, String o, int a, int m) { 8 | super(n, o, a); 9 | this.noOfMasters = m; 10 | } 11 | 12 | Dog(String n, String o) { 13 | super(n, o); 14 | this.noOfMasters = 1; 15 | } 16 | 17 | public void showDetails() { 18 | super.showDetails(); 19 | System.out.println("I am a dog. " + 20 | "I have " + noOfMasters + " masters at home."); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex08-09-10-11/Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | public static void main(String[] args){ 5 | 6 | Pet p = new Pet("Lissie","Smith",3); 7 | p.showDetails(); 8 | 9 | Cat c = new Cat("Kyan", "Silva", 4, 4); 10 | c.showDetails(); 11 | 12 | // New born Pet 13 | Pet p1 = new Pet("Roxy", "John"); 14 | p1.showDetails(); 15 | 16 | // New born Cat 17 | Cat c1 = new Cat("Kitty", "Jack"); 18 | c1.showDetails(); 19 | 20 | // New born Dog 21 | Dog d1 = new Dog("Siba", "Harry"); 22 | 23 | // Dog with 2 age & 5 masters 24 | Dog d2 = new Dog("Rinky", "Ron", 2, 5); 25 | 26 | d1.showDetails(); 27 | d2.showDetails(); 28 | 29 | Pet p3 = c; 30 | p3.showDetails(); 31 | } 32 | }//end of the demo class 33 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 03/Thushara/Lab03Ex08-09-10-11/Pet.java: -------------------------------------------------------------------------------- 1 | 2 | public class Pet { 3 | private String name; 4 | private String owner; 5 | private int age; 6 | 7 | Pet() {} 8 | 9 | // create a newborn pet object 10 | Pet(String n, String o) { 11 | this.name = n; 12 | this.owner = o; 13 | this.age = 0; 14 | } 15 | 16 | public Pet(String n, String o, int a) { 17 | this.name = n; 18 | this.owner = o; 19 | this.age = a; 20 | } 21 | public void showDetails(){ 22 | System.out.println("I am a pet. My name is " 23 | + this.name + ". My owner is " + this.owner); 24 | } 25 | }//end of the pet class -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 04/Lab Sheet 04.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Lab Sheets/Lab Sheet 04/Lab Sheet 04.pdf -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 04/Lab04Ex01/Student.java: -------------------------------------------------------------------------------- 1 | // Create a Student class with the properties studentID (int), name (string), degree 2 | // (string), mobile (string). 3 | 4 | // Create a constructor for the Student class to set values for name, degree and mobile 5 | // number. The studentID should be generated by the system making use of a static 6 | // property called max which is initialized to 100 initially to assign a unique value for each 7 | // student id. 8 | 9 | 10 | public class Student { 11 | private int studentID; 12 | private String name; 13 | private String degree; 14 | private String mobile; 15 | 16 | static int max = 100; 17 | 18 | public Student() { 19 | this.studentID = max; 20 | max++; 21 | } 22 | 23 | public Student(String name, String degree, String mobile) { 24 | this.name = name; 25 | this.degree = degree; 26 | this.mobile = mobile; 27 | this.studentID = max++; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | 38 | public String getDegree() { 39 | return degree; 40 | } 41 | 42 | public void setDegree(String degree) { 43 | this.degree = degree; 44 | } 45 | 46 | public String getMobile() { 47 | return mobile; 48 | } 49 | 50 | public void setMobile(String mobile) { 51 | this.mobile = mobile; 52 | } 53 | 54 | 55 | public void print() { 56 | System.out.println("Student ID: " + studentID); 57 | System.out.println("Name: " + name); 58 | System.out.println("Degree: " + degree); 59 | System.out.println("Mobile: " + mobile); 60 | } 61 | 62 | public static int getNextStudentID() { 63 | return max; 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 04/Lab04Ex01/StudentApp.java: -------------------------------------------------------------------------------- 1 | public class StudentApp { 2 | public static void main(String[] args) { 3 | 4 | Student[] students = new Student[5]; 5 | 6 | 7 | students[0] = new Student("John", "Bachelor", "1234567890"); 8 | students[1] = new Student("Jane", "Master", "0987654321"); 9 | students[2] = new Student("Jack", "PhD", "0123456789"); 10 | 11 | students[3] = new Student(); 12 | students[3].setName("Mary"); 13 | students[3].setDegree("Bachelor"); 14 | students[3].setMobile("9876543210"); 15 | 16 | students[4] = new Student(); 17 | students[4].setName("Tom"); 18 | students[4].setDegree("Master"); 19 | students[4].setMobile("0123456789"); 20 | 21 | 22 | 23 | for (int i = 0; i < students.length; i++) { 24 | students[i].print(); 25 | } 26 | 27 | System.out.println(Student.getNextStudentID()); 28 | } 29 | } -------------------------------------------------------------------------------- /Lab Sheets/Lab Sheet 05/Lab Sheet 05.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Lab Sheets/Lab Sheet 05/Lab Sheet 05.pdf -------------------------------------------------------------------------------- /MID Exam/Revision/Answers.txt: -------------------------------------------------------------------------------- 1 | 1 - 3 2 | 2 -5* 3 | 3-5 4 | 4-2,4,5* 5 | 5 -4 6 | 6-2 7 | 7-1* 8 | 8-4 -------------------------------------------------------------------------------- /MID Exam/Revision/MID revision.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/MID Exam/Revision/MID revision.docx -------------------------------------------------------------------------------- /Online Assignement 2021 June/Online Assignement 2021 - IT2030.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Online Assignement 2021 June/Online Assignement 2021 - IT2030.pdf -------------------------------------------------------------------------------- /Online Assignement 2021 June/Randula[Answer]/BlueSoldier.java: -------------------------------------------------------------------------------- 1 | package com.dot_hunter; 2 | 3 | public class BlueSoldier extends Soldier{ 4 | 5 | 6 | 7 | public BlueSoldier() { 8 | super(); 9 | // TODO Auto-generated constructor stub 10 | } 11 | 12 | @Override 13 | void hunt() { 14 | // TODO Auto-generated method stub 15 | System.out.println("Killed using a knife"); 16 | 17 | //call game over 18 | Board.gameOver(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Randula[Answer]/Board.java: -------------------------------------------------------------------------------- 1 | package com.dot_hunter; 2 | 3 | public class Board { 4 | 5 | //attributes 6 | private int dots; 7 | private int superDots; 8 | private String dotType; 9 | private Hunter myHunter; 10 | private Soldier[] threeSoldiers; 11 | 12 | //return Dots 13 | public int getDots() { 14 | return dots; 15 | } 16 | 17 | //assign Dots 18 | public void setDots(int dots) { 19 | this.dots = dots; 20 | } 21 | 22 | //return Super Dots 23 | public int getSuperDots() { 24 | return superDots; 25 | } 26 | 27 | //assign Super Dots 28 | public void setSuperDots(int superDots) { 29 | this.superDots = superDots; 30 | } 31 | 32 | //return Dot Type 33 | public String getDotType() { 34 | return dotType; 35 | } 36 | 37 | //assign Dot Type 38 | public void setDotType(String dotType) { 39 | this.dotType = dotType; 40 | } 41 | 42 | //return MyHunter 43 | public Hunter getMyHunter() { 44 | return myHunter; 45 | } 46 | 47 | //assign MyHunter 48 | public void setMyHunter(Hunter myHunter) { 49 | this.myHunter = myHunter; 50 | } 51 | 52 | //return Three Solders 53 | public Soldier[] getThreeSoldiers() { 54 | return threeSoldiers; 55 | } 56 | 57 | // 58 | public void setThreeSoilders(Soldier[] threeSoilders) { 59 | this.threeSoldiers = threeSoilders; 60 | } 61 | 62 | public Board(String dotType, Hunter myHunter, Soldier[] threeSoldiers) { 63 | 64 | super(); 65 | this.dotType = dotType; 66 | this.myHunter = myHunter; 67 | this.threeSoldiers = threeSoldiers; 68 | 69 | this.init(); 70 | } 71 | 72 | public void init() { 73 | 74 | //set initial values for Dots and Super Dots 75 | this.setDots(97); 76 | this.setSuperDots(3); 77 | 78 | //set soldiers' positions 79 | //generate a random number using Math.random function 80 | for (int i = 0 ; i < threeSoldiers.length ; i++) 81 | { 82 | threeSoldiers[i].setxPOS((int) Math.random() * 250); 83 | threeSoldiers[i].setyPOS((int) Math.random() * 360); 84 | 85 | } 86 | 87 | //set hunter positions 88 | myHunter.setxPOS(0); 89 | myHunter.setyPOS(0); 90 | 91 | //prompt as ready 92 | System.out.println("Board is ready and three soldiers and the hunter is positioned in the board"); 93 | } 94 | 95 | //game over method 96 | public static void gameOver(){ 97 | 98 | //Prompt 99 | System.out.println("Game Over"); 100 | 101 | //Terminate the program 102 | System.exit(0); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Randula[Answer]/GreenSoldier.java: -------------------------------------------------------------------------------- 1 | package com.dot_hunter; 2 | 3 | public class GreenSoldier extends Soldier{ 4 | 5 | 6 | public GreenSoldier() { 7 | super(); 8 | // TODO Auto-generated constructor stub 9 | } 10 | 11 | @Override 12 | void hunt() { 13 | // TODO Auto-generated method stub 14 | System.out.println("Killed using a gun"); 15 | 16 | //call game over 17 | Board.gameOver(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Randula[Answer]/Hunter.java: -------------------------------------------------------------------------------- 1 | package com.dot_hunter; 2 | 3 | public class Hunter { 4 | 5 | //attributes 6 | private String name; 7 | private String color; 8 | private int x; 9 | private int y; 10 | 11 | //default constructor 12 | public Hunter() { 13 | super(); 14 | } 15 | 16 | //overload constructor 17 | public Hunter(String name, String color) { 18 | super(); 19 | this.name = name; 20 | this.color = color; 21 | } 22 | 23 | //return name 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | //assign name 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | //return color 34 | public String getColor() { 35 | return color; 36 | } 37 | 38 | //assign color 39 | public void setColor(String color) { 40 | this.color = color; 41 | } 42 | 43 | //return x value 44 | public int getxPOS() { 45 | return x; 46 | } 47 | 48 | //set x value 49 | public void setxPOS(int x) { 50 | this.x = x; 51 | } 52 | 53 | //return y value 54 | public int getyPOS() { 55 | return y; 56 | } 57 | 58 | //set y value 59 | public void setyPOS(int y) { 60 | this.y = y; 61 | } 62 | 63 | //move method 64 | public void move(Hunter myHunter) 65 | { 66 | //handle the exception using try-catch 67 | try { 68 | 69 | //condition checking if the hunter hits the wall 70 | if(myHunter.x >= 250 || myHunter.y >= 360) 71 | { 72 | throw new SoundException("Oh oo!!"); 73 | } 74 | else { 75 | System.out.println("Hunter is moving, X:" + myHunter.x + " Y:" + myHunter.y); 76 | } 77 | //handle sound exception 78 | } catch (SoundException e) { 79 | // TODO: handle exception 80 | System.out.println(e.getMessage()); 81 | 82 | } 83 | } 84 | 85 | //hunt method 86 | public void hunt(Board myBoard) 87 | { 88 | 89 | //dot type of the board 90 | String dotType = myBoard.getDotType(); 91 | 92 | //position of soldiers 93 | Soldier[] threeSoldiers = myBoard.getThreeSoldiers(); 94 | int x = myBoard.getMyHunter().getxPOS(); 95 | int y = myBoard.getMyHunter().getyPOS(); 96 | 97 | //game over when all dots are hunted 98 | if (myBoard.getDots() == 0 && myBoard.getSuperDots() == 0) 99 | { 100 | Board.gameOver(); 101 | } 102 | 103 | //Hunter get special power when hunting superDot 104 | for(int i = 0; i < threeSoldiers.length; i++) { 105 | 106 | if(x == threeSoldiers[i].getxPOS() && y == threeSoldiers[i].getyPOS()) { 107 | 108 | //if hunted dot is a normalDot, no soldier gets killed 109 | if(dotType.equals("dot")) { 110 | Board.gameOver(); 111 | } 112 | 113 | //if hunted dot is a superDot, hunter gets special power 114 | //soldier gets killed 115 | else if(dotType.equals("superDot")) { 116 | System.out.println("Soldier KILLS"); 117 | 118 | } 119 | } 120 | } 121 | 122 | //reduce Dot by one 123 | if(dotType.equals("dot")) { 124 | myBoard.setDots(myBoard.getDots() - 1); 125 | System.out.println("Hunting Dots"); 126 | } 127 | 128 | //reduce superDot by one 129 | else if(dotType.equals("superDot")) { 130 | myBoard.setSuperDots(myBoard.getSuperDots() - 1); 131 | System.out.println("Hunting Super Dots"); 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Randula[Answer]/MainApp.java: -------------------------------------------------------------------------------- 1 | package com.dot_hunter; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MainApp { 6 | public static void main(String[] args) 7 | { 8 | System.out.println("Hello world"); 9 | Scanner scan = new Scanner(System.in); 10 | Hunter myhunter = new Hunter("Maha Deva" , "Brown"); 11 | Soldier threeSoilders[] = {new RedSoldier() , new RedSoldier() , new GreenSoldier()}; 12 | Board myboard = new Board("superDot" , myhunter , threeSoilders); 13 | 14 | System.out.println("Use the keyboard up , down , left , right arrow keys to move the hunter"); 15 | myhunter.setxPOS(scan.nextInt()); 16 | myhunter.setyPOS(scan.nextInt()); 17 | myhunter.move(myhunter); 18 | myhunter.hunt(myboard); 19 | threeSoilders[2].hunt(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Randula[Answer]/RedSoldier.java: -------------------------------------------------------------------------------- 1 | package com.dot_hunter; 2 | 3 | public class RedSoldier extends Soldier{ 4 | 5 | 6 | public RedSoldier() { 7 | super(); 8 | // TODO Auto-generated constructor stub 9 | } 10 | 11 | @Override 12 | void hunt() { 13 | // TODO Auto-generated method stub 14 | System.out.println("Killed using a knife"); 15 | 16 | //call game over 17 | Board.gameOver(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Randula[Answer]/Soldier.java: -------------------------------------------------------------------------------- 1 | package com.dot_hunter; 2 | 3 | abstract class Soldier { 4 | 5 | //attributes 6 | private int x; 7 | private int y; 8 | 9 | //define abstract method 10 | abstract void hunt(); 11 | 12 | public Soldier() 13 | { 14 | x = 0; 15 | y = 0; 16 | } 17 | 18 | //return x value 19 | public int getxPOS() { 20 | return x; 21 | } 22 | 23 | //set x value 24 | public void setxPOS(int x) { 25 | this.x = x; 26 | } 27 | 28 | //return y value 29 | public int getyPOS() { 30 | return y; 31 | } 32 | 33 | //set y value 34 | public void setyPOS(int y) { 35 | this.y = y; 36 | //y = 6; 37 | } 38 | 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Randula[Answer]/SoundException.java: -------------------------------------------------------------------------------- 1 | package com.dot_hunter; 2 | 3 | public class SoundException extends Exception { 4 | 5 | SoundException (String message){ 6 | super(message); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Sahan [Answer]/BlueSoldier.java: -------------------------------------------------------------------------------- 1 | public class BlueSoldier extends Soldier { // Blue Soldier Class 2 | 3 | public void hunt() { // Hunt Method 4 | 5 | System.out.println("Killed using gun"); // Dispalay killed Unique weapon 6 | System.out.println("Game over"); // Display Game over 7 | } 8 | 9 | 10 | } // End of the Blue Soldier class -------------------------------------------------------------------------------- /Online Assignement 2021 June/Sahan [Answer]/Board.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; // Import Random 2 | 3 | public class Board { // Begining of the Board class 4 | 5 | // Attributes 6 | 7 | private int dot; 8 | private int superdot; // int 9 | private int hunterdot; 10 | private String DotType; 11 | Hunter myhunter; 12 | private Soldier[] threeSoldier; 13 | 14 | 15 | 16 | public Board() { // default Constructor 17 | 18 | dot = 0; 19 | superdot = 0; 20 | hunterdot = 0; 21 | DotType = null; 22 | 23 | } 24 | 25 | public Board(String DotType, Hunter myhunter, Soldier[] threeSoldier) { // Overload Constructor 26 | 27 | 28 | this.DotType = DotType; 29 | this.myhunter = myhunter; 30 | this.threeSoldier = threeSoldier; 31 | this.init(); 32 | 33 | } 34 | 35 | public void setDotType(String DotType) { // Set Dot Type Method 36 | 37 | this.DotType = DotType; 38 | } 39 | 40 | public String getDotType() { // Get Dot Type Method 41 | 42 | return DotType; 43 | } 44 | 45 | public void setDot(int dot) { // Set Dot 46 | 47 | this.dot = dot; 48 | } 49 | 50 | public int getDot(){ // Get Dot 51 | 52 | return dot; 53 | } 54 | 55 | public void setSuperDot(int superdot) { // Set Super Dot 56 | 57 | this.superdot = superdot; 58 | } 59 | 60 | public int getSuperDot(){ // Get Super Dot 61 | 62 | return 3; // Return 3 Super Dot 63 | } 64 | 65 | public void setHunterDot(int hunterdot) { // Set Hunter Dot 66 | 67 | this.hunterdot = hunterdot; 68 | } 69 | 70 | public int getHunterDot(){ // Get Hunter Dot 71 | 72 | return 97; // Return 97 Hunter Dot 73 | } 74 | 75 | public void init() { // init Method 76 | 77 | System.out.println("Board is Ready and there soldiers and the hunter is positioned in the board"); 78 | for(int i = 0; i<3; i++) { 79 | 80 | threeSoldier[i].setxPos((int)Math.random()* 250); // Random Values 81 | threeSoldier[i].setyPos((int)Math.random()* 360); 82 | 83 | } 84 | 85 | } 86 | 87 | } // End of the board class -------------------------------------------------------------------------------- /Online Assignement 2021 June/Sahan [Answer]/GreenSoldier.java: -------------------------------------------------------------------------------- 1 | public class GreenSoldier extends Soldier { // Green Soldier Class 2 | 3 | public void hunt() { // Hunt Method 4 | 5 | System.out.println("Killed using a knife"); // Dispalay killed Unique weapon 6 | System.out.println("Game over"); // Display Game over 7 | } 8 | } // End of the Green Soldier Class -------------------------------------------------------------------------------- /Online Assignement 2021 June/Sahan [Answer]/Hunt.java: -------------------------------------------------------------------------------- 1 | public class Hunter { // Beginiing of the Hunter class 2 | 3 | // Attributes 4 | 5 | private String name; 6 | private String color; 7 | private int xPos; 8 | private int yPos; 9 | 10 | public Hunter() { // Default Constructor 11 | 12 | name = null; 13 | color = null; 14 | xPos = 0; 15 | yPos = 0; 16 | } 17 | 18 | 19 | public Hunter(String name, String color) { // Overload Constructor 20 | 21 | this.name = name; 22 | this.color = color; 23 | } 24 | 25 | public void setxPos(int xPos) { // Set X Pos Method 26 | 27 | this.xPos = xPos; 28 | } 29 | 30 | public int getxpos() { 31 | 32 | return xPos; 33 | } 34 | 35 | public void setyPos(int yPos) { // Set Y Pos Method 36 | 37 | this.yPos =yPos; 38 | } 39 | 40 | public int getyPos() { 41 | 42 | return yPos; 43 | } 44 | 45 | 46 | public void move(Hunter myhunter)throws SoundException { // Move Method and Sound Exception 47 | 48 | 49 | if(myhunter.xPos<1 || myhunter.xPos >= 250 || myhunter.yPos < 1 || myhunter.yPos >=360) { // if condition for x axis and y axis 50 | 51 | System.out.println("Hunter is moving, " + "x :" +myhunter.xPos+ " Y :" +myhunter.yPos); 52 | 53 | throw new SoundException("Oh oo!"); // Throw Sound Exception 54 | 55 | } 56 | 57 | else { 58 | 59 | System.out.println("Hunter is moving, X: " +myhunter.xPos + " Y :" +myhunter.yPos); 60 | } 61 | } 62 | 63 | public void hunt(Board myboard, String dotType ) { // Hunt Method 64 | 65 | if(dotType == "superDot") { // Condition for Super 66 | 67 | System.out.println("Hunting super dots"); 68 | myboard.setDot(myboard.getDot() -1); 69 | if(myboard.getSuperDot() == 0 && myboard.getDot() == 0){ 70 | System.out.println("Game Over, You Win"); 71 | } 72 | } 73 | 74 | else if(dotType == "hunterdot") { // Condition for hunter dots 75 | System.out.println("Hunting hunter dots"); 76 | myboard.setDot(myboard.getDot() -1); 77 | if(myboard.getSuperDot() == 0 && myboard.getDot() == 0) { 78 | System.out.println("Game Over, You Win"); 79 | System.exit(0); 80 | } 81 | 82 | } 83 | 84 | 85 | } 86 | 87 | 88 | } // End of the Hunt class 89 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Sahan [Answer]/Hunter.java: -------------------------------------------------------------------------------- 1 | public class Hunter { // Beginiing of the Hunter class 2 | 3 | // Attributes 4 | 5 | private String name; 6 | private String color; 7 | private int xPos; 8 | private int yPos; 9 | 10 | public Hunter() { // Default Constructor 11 | 12 | name = null; 13 | color = null; 14 | xPos = 0; 15 | yPos = 0; 16 | } 17 | 18 | 19 | public Hunter(String name, String color) { // Overload Constructor 20 | 21 | this.name = name; 22 | this.color = color; 23 | } 24 | 25 | public void setxPos(int xPos) { // Set X Pos Method 26 | 27 | this.xPos = xPos; 28 | } 29 | 30 | public int getxpos() { 31 | 32 | return xPos; 33 | } 34 | 35 | public void setyPos(int yPos) { // Set Y Pos Method 36 | 37 | this.yPos =yPos; 38 | } 39 | 40 | public int getyPos() { 41 | 42 | return yPos; 43 | } 44 | 45 | 46 | public void move(Hunter myhunter)throws SoundException { // Move Method and Sound Exception 47 | 48 | 49 | if(myhunter.xPos<1 || myhunter.xPos >= 250 || myhunter.yPos < 1 || myhunter.yPos >=360) { // if condition for x axis and y axis 50 | 51 | System.out.println("Hunter is moving, " + "x :" +myhunter.xPos+ " Y :" +myhunter.yPos); 52 | 53 | throw new SoundException("Oh oo!"); // Throw Sound Exception 54 | 55 | } 56 | 57 | else { 58 | 59 | System.out.println("Hunter is moving, X: " +myhunter.xPos + " Y :" +myhunter.yPos); 60 | } 61 | } 62 | 63 | public void hunt(Board myboard, String dotType ) { // Hunt Method 64 | 65 | if(dotType == "superDot") { // Condition for Super 66 | 67 | System.out.println("Hunting super dots"); 68 | myboard.setDot(myboard.getDot() -1); 69 | if(myboard.getSuperDot() == 0 && myboard.getDot() == 0){ 70 | System.out.println("Game Over, You Win"); 71 | } 72 | } 73 | 74 | else if(dotType == "hunterdot") { // Condition for hunter dots 75 | System.out.println("Hunting hunter dots"); 76 | myboard.setDot(myboard.getDot() -1); 77 | if(myboard.getSuperDot() == 0 && myboard.getDot() == 0) { 78 | System.out.println("Game Over, You Win"); 79 | System.exit(0); 80 | } 81 | 82 | } 83 | 84 | 85 | } 86 | 87 | 88 | } // End of the Hunt class 89 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Sahan [Answer]/MainApp.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; // Import java Scanner 2 | import java.util.Random; // Import Random 3 | 4 | public class MainApp { // Begining of the Main class 5 | 6 | public static void main(String[] args)throws SoundException { // Begining of the method and throws Sound Exception 7 | 8 | Scanner sc = new Scanner(System.in); // Scanner 9 | Hunter myhunter = new Hunter("Maha Deva", "Brown"); 10 | Soldier threeSoldiers[] = {new RedSoldier(), new RedSoldier(), new GreenSoldier()}; 11 | Board myboard = new Board("superDot", myhunter, threeSoldiers); 12 | 13 | 14 | System.out.println("Use the keyboard up, down, left, right arrow keys to move the hunter"); 15 | 16 | myhunter.setxPos(sc.nextInt()); 17 | myhunter.setyPos(sc.nextInt()); 18 | myhunter.move(myhunter); 19 | myhunter.hunt(myboard, "superDot"); 20 | threeSoldiers[2].hunt(); 21 | 22 | } // End the of the Method 23 | 24 | } // End of the Main Class -------------------------------------------------------------------------------- /Online Assignement 2021 June/Sahan [Answer]/RedSoldier.java: -------------------------------------------------------------------------------- 1 | public class RedSoldier extends Soldier { // Red Soldier Class 2 | 3 | 4 | public void hunt() { // Hunt Method 5 | 6 | System.out.println("Killed using by hand"); // Dispalay killed Unique weapon 7 | System.out.println("Game over"); // Display Game over 8 | } 9 | 10 | } // End of the Red Soldier class -------------------------------------------------------------------------------- /Online Assignement 2021 June/Sahan [Answer]/Soldier.java: -------------------------------------------------------------------------------- 1 | 2 | public class Soldier { // Soldier class 3 | 4 | 5 | private int xPos; 6 | private int yPos; 7 | 8 | 9 | public Soldier() { 10 | 11 | xPos = 0; 12 | yPos = 0; 13 | } 14 | 15 | public Soldier(int xPos, int yPos) { 16 | 17 | this.xPos = xPos; 18 | this.yPos = yPos; 19 | 20 | } 21 | 22 | 23 | public void setxPos(int xPos) { // Set x pos 24 | 25 | this.xPos = xPos; 26 | } 27 | 28 | public int getxPos() { // get x pos 29 | 30 | return xPos; 31 | } 32 | 33 | public void setyPos(int yPos) { // set Y pos 34 | 35 | this.yPos = yPos; 36 | } 37 | 38 | public int getyPos() { // get Y pos 39 | 40 | return yPos; 41 | } 42 | 43 | public void hunt() {}; 44 | 45 | 46 | } // End of the Soldier class -------------------------------------------------------------------------------- /Online Assignement 2021 June/Sahan [Answer]/SoundException.java: -------------------------------------------------------------------------------- 1 | public class SoundException extends Exception { // Sound Exception class 2 | 3 | public SoundException(String message) { 4 | super(message); 5 | } 6 | } // End of the Sound exception class 7 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Thushara [Answer]/Board.java: -------------------------------------------------------------------------------- 1 | public class Board { 2 | 3 | // Attributes 4 | private String dotType; 5 | private int dots; 6 | private int superDots; 7 | private Hunter myhunter; 8 | private Soldier[] threeSoldiers; 9 | 10 | /** 11 | * @return the dotType 12 | */ 13 | public String getDotType() { 14 | return dotType; 15 | } 16 | 17 | /** 18 | * @param dotType the dotType to set 19 | */ 20 | public void setDotType(String dotType) { 21 | this.dotType = dotType; 22 | } 23 | 24 | /** 25 | * @return the dots 26 | */ 27 | public int getDots() { 28 | return dots; 29 | } 30 | 31 | /** 32 | * @param dots the dots to set 33 | */ 34 | public void setDots(int dots) { 35 | this.dots = dots; 36 | } 37 | 38 | /** 39 | * @return the superDots 40 | */ 41 | public int getSuperDots() { 42 | return superDots; 43 | } 44 | 45 | /** 46 | * @param superDots the superDots to set 47 | */ 48 | public void setSuperDots(int superDots) { 49 | this.superDots = superDots; 50 | } 51 | 52 | /** 53 | * @return the myhunter 54 | */ 55 | public Hunter getMyhunter() { 56 | return myhunter; 57 | } 58 | 59 | /** 60 | * @param myhunter the myhunter to set 61 | */ 62 | public void setMyhunter(Hunter myhunter) { 63 | this.myhunter = myhunter; 64 | } 65 | 66 | // Set threeSoldiers[] 67 | public Soldier[] getThreeSoldiers() { 68 | return this.threeSoldiers; 69 | } 70 | 71 | // Initialization 72 | public void init() { 73 | 74 | // Set initial values for dots and superDots 75 | this.setDots(97); 76 | this.setSuperDots(3); 77 | 78 | // Set Soldier positions 79 | // Looping on threeSoldiers[] 80 | for(int i = 0; i < threeSoldiers.length; i++) { 81 | 82 | // Generating random number for X and Y 83 | // Using Math.random 84 | // Range for X = 250, Y = 360 85 | threeSoldiers[i].setXpos((int) (Math.random() * 250)); 86 | threeSoldiers[i].setYpos((int) (Math.random() * 360)); 87 | } 88 | 89 | // Set Hunter Positions 90 | myhunter.setxPOS(0); 91 | myhunter.setyPOS(0); 92 | 93 | // Prompt 94 | System.out.println("Board is ready and three soldiers and the hunter is positioned in the board"); 95 | } 96 | 97 | public Board(String dotType, Hunter myhunter, Soldier[] threeSoldiers) { 98 | 99 | // Set Values 100 | this.setDotType(dotType); 101 | this.setMyhunter(myhunter); 102 | this.threeSoldiers = threeSoldiers; 103 | 104 | init(); 105 | } 106 | 107 | // Game Over method 108 | public static void gameOver() { 109 | 110 | // Prompt 111 | System.out.println("Game over"); 112 | 113 | // Exit from program 114 | System.exit(0); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Thushara [Answer]/Hunter.java: -------------------------------------------------------------------------------- 1 | public class Hunter { 2 | 3 | // Attributes 4 | private String name; 5 | private String color; 6 | private int x; 7 | private int y; 8 | 9 | // Hunter overloaded constructor 10 | public Hunter(String name, String color) { 11 | this.setName(name); 12 | this.setColor(color); 13 | } 14 | 15 | /** 16 | * @param name the name to set 17 | */ 18 | public void setName(String name) { 19 | this.name = name; 20 | } 21 | 22 | 23 | public void setColor(String color) { 24 | this.color = color; 25 | } 26 | 27 | public void setxPOS(int x) { 28 | this.x = x; 29 | } 30 | public int getxPOS() { 31 | return this.x; 32 | } 33 | 34 | public void setyPOS(int y) { 35 | this.y = y; 36 | } 37 | public int getyPOS() { 38 | return this.y; 39 | } 40 | 41 | // move method 42 | public void move(Hunter myhunter) { 43 | 44 | // Exception Handling using try-catch 45 | try { 46 | 47 | // Checking if hunter hit the wall 48 | if (myhunter.x >= 250 || myhunter.y >= 360) { 49 | 50 | // throw an Exception 51 | throw new SoundException("Oh oo!!"); 52 | } 53 | else { 54 | System.out.println("Hunter is moving, X:" + myhunter.x + " Y:" + myhunter.y); 55 | } 56 | } 57 | // Handle SoundException 58 | catch (SoundException e) { 59 | 60 | // Print Exception message 61 | System.out.println(e.getMessage()); 62 | } 63 | } 64 | 65 | // hunt method 66 | public void hunt(Board myboard) { 67 | 68 | // dot type of myboard 69 | String dotType = myboard.getDotType(); 70 | 71 | Soldier[] threeSoldiers = myboard.getThreeSoldiers(); 72 | int xPOS = myboard.getMyhunter().getxPOS(); 73 | int yPOS = myboard.getMyhunter().getyPOS(); 74 | 75 | // Game over when all dots are hunted 76 | if(myboard.getDots() == 0 && myboard.getSuperDots() == 0) { 77 | Board.gameOver(); 78 | } 79 | 80 | // Hunter get super power when hunting superDot 81 | for(int i = 0; i < threeSoldiers.length; i++) { 82 | 83 | if(xPOS == threeSoldiers[i].getXpos() && yPOS == threeSoldiers[i].getXpos()) { 84 | 85 | // if hunted dot is normal dot no super power hunter killed 86 | if(dotType == "dot") { 87 | Board.gameOver(); 88 | } 89 | 90 | // if hunted dot is superDot hunter get super power Soldier killed 91 | else if(dotType == "superDot") { 92 | System.out.println("Soldier KILLS"); 93 | 94 | } 95 | } 96 | } 97 | 98 | // reduce dot by 1 99 | if(dotType == "dot") { 100 | myboard.setDots(myboard.getDots() - 1); 101 | System.out.println("Hunting dots"); 102 | } 103 | 104 | // reduce superDot by 1 105 | else if(dotType == "superDot") { 106 | myboard.setSuperDots(myboard.getSuperDots() - 1); 107 | System.out.println("Hunting super dots"); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Thushara [Answer]/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | Hunter myhunter = new Hunter("Maha Deva", "Brown"); 8 | 9 | Soldier threeSoldiers[] = { new RedSoldier(), new RedSoldier(), new GreenSoldier() }; 10 | 11 | Board myboard = new Board("superDot", myhunter, threeSoldiers); 12 | 13 | System.out.println("Use the keyboard up,down,left,right arrow keys to move the hunter"); 14 | 15 | myhunter.setxPOS(sc.nextInt()); 16 | myhunter.setyPOS(sc.nextInt()); 17 | 18 | myhunter.move(myhunter); 19 | 20 | myhunter.hunt(myboard); 21 | 22 | threeSoldiers[2].hunt(); 23 | 24 | sc.close(); 25 | } 26 | } -------------------------------------------------------------------------------- /Online Assignement 2021 June/Thushara [Answer]/Soldier.java: -------------------------------------------------------------------------------- 1 | abstract class Soldier { 2 | 3 | // Attributes 4 | private int x; 5 | private int y; 6 | 7 | // abstract class hunt() 8 | abstract void hunt(); 9 | 10 | /** 11 | * @return the x 12 | */ 13 | public int getXpos() { 14 | return x; 15 | } 16 | 17 | /** 18 | * @param x the x to set 19 | */ 20 | public void setXpos(int x) { 21 | this.x = x; 22 | } 23 | 24 | /** 25 | * @return the y 26 | */ 27 | public int getYpos() { 28 | return y; 29 | } 30 | 31 | /** 32 | * @param y the y to set 33 | */ 34 | public void setYpos(int y) { 35 | this.y = y; 36 | } 37 | } 38 | 39 | class RedSoldier extends Soldier { 40 | 41 | // Implementation of hunt 42 | public void hunt() { 43 | System.out.println("Killed using a hand"); 44 | 45 | // Calling game over 46 | Board.gameOver(); 47 | } 48 | } 49 | 50 | class GreenSoldier extends Soldier { 51 | 52 | // Implementation of hunt 53 | public void hunt() { 54 | System.out.println("Killed using a knife"); 55 | 56 | // Calling game over 57 | Board.gameOver(); 58 | } 59 | } 60 | 61 | class BlueSoldier extends Soldier { 62 | 63 | // Implementation of hunt 64 | public void hunt() { 65 | System.out.println("Killed using a gun"); 66 | 67 | // Calling game over 68 | Board.gameOver(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Online Assignement 2021 June/Thushara [Answer]/SoundException.java: -------------------------------------------------------------------------------- 1 | // Custom Exception 2 | class SoundException extends Exception { 3 | 4 | SoundException (String message) { 5 | super(message); 6 | } 7 | } -------------------------------------------------------------------------------- /Other Questions/ATM/Atm.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Assignment_1_implement_atm { 4 | public static void main(String args[]) 5 | 6 | { 7 | int choice; 8 | int balance = 6000; 9 | int num1; 10 | int pin=1000, newpin; 11 | Scanner sc = new Scanner(System.in); 12 | 13 | System.out.println("Enter your pin"); 14 | newpin = sc.nextInt(); 15 | 16 | if (pin == newpin) 17 | { 18 | System.out.println("Pin is correct"); 19 | } 20 | 21 | else 22 | { 23 | System.out.println("Pin is Incorrect"); 24 | } 25 | 26 | 27 | int num2; 28 | 29 | 30 | System.out.println("1.Balance Enquiry"); 31 | 32 | System.out.println("2.Cash Deposite"); 33 | 34 | System.out.println("3.Cash Withdrawl"); 35 | 36 | System.out.println("4.Pin change"); 37 | 38 | 39 | System.out.println("Enter your choice"); 40 | choice = sc.nextInt(); 41 | 42 | 43 | switch(choice) 44 | { 45 | case 1 :{ 46 | 47 | System.out.println("Your Current balance is " + balance); 48 | } 49 | break; 50 | 51 | case 2 :{ 52 | 53 | System.out.println("Enter the amount"); 54 | num1 = sc.nextInt(); 55 | balance = balance + num1; 56 | System.out.println("The current balance is " + balance); 57 | 58 | 59 | } 60 | break; 61 | 62 | case 3 :{ 63 | 64 | System.out.println("Enter the amount"); 65 | num1 = sc.nextInt(); 66 | balance = balance - num1; 67 | System.out.println("The current balance is " + balance); 68 | } 69 | break; 70 | 71 | case 4 :{ 72 | 73 | System.out.println("Enter the current print"); 74 | System.out.println("Enter new pin"); 75 | newpin= sc.nextInt(); 76 | pin=newpin; 77 | } 78 | break; 79 | 80 | } 81 | 82 | } 83 | 84 | } 85 | 86 | -------------------------------------------------------------------------------- /Other Questions/Gender/Gender.java: -------------------------------------------------------------------------------- 1 | package c_10_String_Assignment; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Gender { 6 | 7 | public static void main(String[] args) 8 | { 9 | String str; 10 | Scanner sc = new Scanner(System.in); 11 | System.out.println("Enter The Name = "); 12 | str = sc.next(); 13 | 14 | char ch = str.charAt(str.length()-1); 15 | 16 | if(ch == 'a' || ch =='u' || ch =='i') 17 | { 18 | System.out.println("It is Girl"); 19 | } 20 | 21 | else 22 | { 23 | System.out.println("It is Boy"); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 11/Question 1/A.java: -------------------------------------------------------------------------------- 1 | interface A{ // Inteface class A 2 | 3 | void meth1(); // meth 1 4 | void meth2(); // meth 2 5 | 6 | } -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 11/Question 1/MyClass.java: -------------------------------------------------------------------------------- 1 | public class MyClass implements A { // MyClass class implements A class 2 | 3 | public void meth1(int meth1){ // meth 1 4 | 5 | 6 | 7 | } 8 | 9 | public void meth2(int meth2){ // meth 2 10 | 11 | 12 | 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 11/Question 2/InheritanceDemo.java: -------------------------------------------------------------------------------- 1 | 2 | public class InheritanceDemo { // Begining of the Class 3 | 4 | public static void main(String[] args) { // Begining of the Method 5 | 6 | Person p1 = new Person(); 7 | p1.showDetails(); // Default Constructor Called 8 | System.out.println(""); 9 | 10 | Student s1 = new Student("Sahan", "Colombo", "IT2011470"); 11 | s1.showDetails(); 12 | System.out.println(""); 13 | 14 | PartTimeStudent pts = new PartTimeStudent("Kamal", "Kurunagala", "IT01117",6); 15 | pts.showDetails(); 16 | 17 | } // End of the Method 18 | 19 | } // End of the Class 20 | -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 11/Question 2/PartTimeStudent.java: -------------------------------------------------------------------------------- 1 | public class PartTimeStudent extends Student{ 2 | 3 | private int workHours; 4 | 5 | public PartTimeStudent(String name, String Address, String StudentID, int workHours) { 6 | 7 | super(name, Address, StudentID); 8 | this.workHours = workHours; 9 | } 10 | 11 | public void showDetails() { 12 | 13 | super.showDetails(); 14 | System.out.println("Working Hours :" +this.workHours); 15 | 16 | } 17 | 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 11/Question 2/Person.java: -------------------------------------------------------------------------------- 1 | 2 | public class Person { 3 | 4 | private String name; 5 | private String Address; 6 | 7 | public Person() { 8 | 9 | name = null; 10 | Address = null; 11 | } 12 | 13 | public Person(String name, String Address) { 14 | 15 | this.name = name; 16 | this.Address = Address; 17 | } 18 | 19 | public void showDetails() { 20 | 21 | System.out.println("Name :" +this.name); 22 | System.out.println("Address :" +this.Address); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 11/Question 2/Student.java: -------------------------------------------------------------------------------- 1 | public class Student extends Person { 2 | 3 | private String StudentID; 4 | 5 | public Student(String name, String Address, String StudentID) { 6 | 7 | super(name,Address); 8 | this.StudentID = StudentID; 9 | } 10 | 11 | public void showDetails() { 12 | 13 | super.showDetails(); 14 | System.out.println("Student ID :" +this.StudentID); 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 11/Question 3/Arithmetic.java: -------------------------------------------------------------------------------- 1 | public class Arithmetic implements Test { // Arithmetic class 2 | 3 | public void Square(int x) { // Square method 4 | 5 | 6 | 7 | } 8 | 9 | 10 | 11 | } -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 11/Question 3/Test.java: -------------------------------------------------------------------------------- 1 | interface Test{ // implement test class 2 | 3 | void Square(); // Square method 4 | 5 | 6 | } -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 11/Question 3/TestInt.java: -------------------------------------------------------------------------------- 1 | public class TestInt{ // Test Int class 2 | 3 | public static void main(String[] args) { // main method 4 | 5 | Arithmetic arithmetic = new Arithmetic(); // object of Arithmetic class 6 | 7 | arithmetic.Square(); 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 12/Question 1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main{ // Main Class 2 | 3 | public static void main(String args[]) { // method 4 | try { // try catch block 5 | 6 | int c[] = {1}; 7 | c[42] = 99; 8 | 9 | } catch (ArrayIndexOutOfBoundsException e) { // exception 10 | System.out.println("Array index oob: " + e); 11 | } 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 12/Question 2/MutipleCatch.java: -------------------------------------------------------------------------------- 1 | public class MutipleCatch { 2 | 3 | public static void main(String args[]) { 4 | 5 | try{ 6 | 7 | // int a = args.length; 8 | // System.out.println("a = " +a); 9 | // int b =42 /a; 10 | int c[] = {1}; 11 | c[42] = 99; 12 | 13 | } catch (ArithmeticException e3) { 14 | 15 | System.out.println("Divide by 0 :" +e); 16 | }catch (ArrayIndexOutOfBoundsException ex) { 17 | 18 | System.out.println("Array index oob :" +ex); 19 | } 20 | 21 | 22 | System.out.println("After try/ catch block"); 23 | } 24 | 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 12/Question 3/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { // Beiginng of the Main Class 2 | public static void main(String[] args) { // Beiginng of the method 3 | 4 | try { // try catch block 5 | 6 | String number = "123a"; 7 | int value = Integer.parseInt(number); // Type Casting 8 | 9 | } catch (NumberFormatException ex) { // Exception 10 | 11 | System.out.println("Wrong number format"); // Display Exception 12 | } 13 | } // End of the method 14 | } // End of the Main Class -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 12/Question 4/TestFinallyBlock.java: -------------------------------------------------------------------------------- 1 | class TestFinallyBlock{ // Test Finally Class 2 | public static void main(String args[]){ // Method 3 | 4 | try{ // Try Catch block 5 | 6 | int data=25/0; 7 | 8 | System.out.println(data); 9 | 10 | } catch(ArithmeticException e){ // Exception 11 | 12 | System.out.println(e); // Print Excpetion 13 | 14 | } finally{ // Final block 15 | 16 | System.out.println("finally block is always executed"); // display finally block execution 17 | 18 | } 19 | 20 | System.out.println("rest of the code..."); 21 | } 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 12/Question 5/Demo.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Demo { 4 | 5 | public static void main(String[] args) throws IOException { 6 | 7 | int number; 8 | 9 | EvenOddNumber eon = new EvenOddNumber(); 10 | 11 | // boolean result = eon.findEvenOrOdd(8); 12 | 13 | InputStreamReader isr = new InputStreamReader(System.in); 14 | 15 | BufferedReader bf = new BufferedReader(isr); 16 | 17 | System.out.print("Enter the number :"); 18 | number = Integer.parseInt(bf.readLine()); 19 | 20 | if (eon.findEvenOrOdd(number)) { // if(result == true) 21 | 22 | System.out.print("This is an Even Number"); 23 | 24 | } 25 | 26 | else { 27 | 28 | System.out.print("This is an Odd Number"); 29 | 30 | } 31 | 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 12/Question 6/SoundException.java: -------------------------------------------------------------------------------- 1 | public class SoundException extends Exception { // Sound Exception class 2 | 3 | public SoundException(String message) { 4 | super(message); 5 | } 6 | } // End of the Sound exception class -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 2/Main.java: -------------------------------------------------------------------------------- 1 | // Question 02 2 | 3 | // Write a program that input, calculates and prints the product of three integers 4 | 5 | 6 | import java.util.Scanner; // Import java Scanner class 7 | 8 | 9 | public class Main{ // Beginning of the Main class 10 | 11 | public static void main(String[] args) { // Beginning of the Method 12 | 13 | Scanner scanner = new Scanner(System.in); // Create Object for Scanner Class 14 | 15 | System.out.print("Input Number 1 :"); // Input Number 1 16 | int Number1 = scanner.nextInt(); // User Input Number 1 17 | 18 | System.out.print("Input Number 2 :"); // Input Number 2 19 | int Number2 = scanner.nextInt(); // User Input Number 2 20 | 21 | int sum = Number1 + Number2; // Calculate the Sum of two numbers 22 | 23 | System.out.println("Input First Number is :" + Number1); // Display first Number user input 24 | System.out.println("Input Second Number is :" + Number2); // Display second Number user input 25 | System.out.println("Calculate of Two Numbers is : " +sum); // Display Calculate of two Numbers user input 26 | 27 | } // End of the Method 28 | 29 | }// End of the Main class -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 3/Main.java: -------------------------------------------------------------------------------- 1 | // Question 03 2 | 3 | // Write a program that converts a Fahrenheit degree to Celsius using the formula (input values) 4 | 5 | 6 | import java.util.*; // Import Java Scanner Class 7 | 8 | public class Main{ // Beginnging of the Main Class 9 | 10 | public static void main(String[] args) { // Beginnging of the method 11 | 12 | Scanner scanner = new Scanner(System.in); // Creeate object for Scanner Class 13 | 14 | // Declare variable fahremheit and celsius 15 | int fahrenheit; 16 | int celsius; 17 | 18 | System.out.print("Input Fahrenheit :"); // Input Fahrenheit Values 19 | fahrenheit = scanner.nextInt(); // User Input 20 | 21 | celsius = (9/5) * (fahrenheit -32); // Calculation 22 | 23 | System.out.print("Celsius :" + celsius); // Display the Converted Celsius Value 24 | 25 | } // end of the method 26 | 27 | } // end of the Main Class -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 4/Main.java: -------------------------------------------------------------------------------- 1 | // Question 04 2 | 3 | // Write an application that inputs three integers from the user and displays the sum, average, 4 | // product, smallest and largest of the numbers. 5 | 6 | import java.util.*; // Import java Util class 7 | 8 | 9 | public class Main{ // Beginning of the Main calss 10 | 11 | public static void main(String[] args) { // Beginning of the Method 12 | 13 | Scanner scanner = new Scanner(System.in); // Create a object for Scanner Calss 14 | 15 | System.out.print("Input Number 1 : "); // Input Number 1 16 | int Number1 = scanner.nextInt(); // User Input Number 1 17 | 18 | System.out.print("Input Number 2 : "); // Input Number 2 19 | int Number2 = scanner.nextInt(); // User Input Number 2 20 | 21 | System.out.print("Input Number 3 : "); // Input Number 3 22 | int Number3 = scanner.nextInt(); // User Input Number 3 23 | 24 | int sum = Number1 + Number2 + Number3; // Calculation of three Integer values 25 | 26 | System.out.println("Sum of three Integer Number is : "+sum); // Display the three Integer values 27 | 28 | int average = sum / 3; // Calculate the average value three Integer values 29 | 30 | System.out.println("Average of three Integer Number is : "+average); // Display the average of three Integer values 31 | 32 | if(Number1 > Number2 && Number2 > Number3) { // Check the Condtion 33 | 34 | System.out.println("The Largest Number :" + Number1); // Display the Largest Number 35 | System.out.println("The Smallest Number :" + Number3); // Display the Smallest Number 36 | 37 | } 38 | 39 | else if (Number1 < Number2 && Number2 > Number3) { // Check the Condtion 40 | 41 | System.out.println("The Largest Number :" +Number2); // Display the Largest Number 42 | System.out.println("The Smallest Number" + Number3); // Display the Smallest Number 43 | } 44 | else if (Number1< Number2 && Number2 < Number3) { // Check the Condtion 45 | 46 | System.out.println("The Largest Number :" + Number3); // Display the Largest Number 47 | System.out.println("The Smallest Number :" +Number1); // Display the Smallest Number 48 | 49 | } 50 | 51 | } // End of the method 52 | 53 | } // End of the Main Class -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 6/Date.java: -------------------------------------------------------------------------------- 1 | public class Date{ // Date Class 2 | 3 | // Attributes of Date Class 4 | private int month; 5 | private int day; 6 | private int year; 7 | 8 | public Date() { // Default Constructer of Date Class 9 | 10 | month = 0; 11 | day = 0; 12 | year =0 ; 13 | 14 | } 15 | 16 | public Date(int month, int day, int year) { // Overload Constructer of Date Class 17 | 18 | this.month = month; 19 | this.day = day; 20 | this.year = year; 21 | 22 | } 23 | 24 | public void setMonth(int month) { // Set method for month 25 | 26 | this.month = month; 27 | 28 | } 29 | public int getMoth() { // get method for month 30 | 31 | return month; // return month value 32 | } 33 | 34 | public void setDay(int day) { // Set method for date 35 | 36 | this.day = day; 37 | } 38 | 39 | public int getDay() { // get method for date 40 | 41 | return day; // return day value 42 | } 43 | 44 | public void setYear(int year) { // Set method for year 45 | 46 | this.year = year; 47 | } 48 | 49 | public int getYear() { // get method for year 50 | 51 | return year; // return day value 52 | } 53 | 54 | public void DisplyDate() { // Method for display date 55 | 56 | System.out.println(+month+ "/" +day+ "/" + year); // Display month dat and year 57 | 58 | 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 6/DateTest.java: -------------------------------------------------------------------------------- 1 | // Question 06 2 | 3 | // Create a class called Date that includes three pieces of information as instance variables—a 4 | // month (typeint), a day (typeint) and a year (typeint). Your class should have a constructor that 5 | // initializes the three instance variables and assumes that the values provided are correct. 6 | // Provide a set and a get method for each instance variable. Provide a method displayDate that 7 | // displays the month, day and year separated by forward slashes(/). Write a test application 8 | // named DateTest that demonstrates classDate’s capabilities. 9 | 10 | 11 | import java.util.Scanner; // Import java Scanner Class 12 | 13 | 14 | public class DateTest{ // Beginning of the Main Class 15 | 16 | public static void main(String[] args) { // Beginning of the Method 17 | 18 | int month, date ,year; // Declare variables 19 | 20 | 21 | Date d = new Date(); // Create Date Class Object 22 | 23 | Scanner scanner = new Scanner(System.in); // Create of Scanner Calss 24 | 25 | System.out.print("Input Month :"); // Input Month 26 | month = scanner.nextInt(); // User Input Month 27 | d.setMonth(month); // Set Input Value 28 | 29 | if(month <= 12) { // Check the month is more than 12 30 | 31 | 32 | } 33 | else { 34 | 35 | System.out.println("Invalid Month"); // if Month more than 12 display invalid month 36 | 37 | } 38 | 39 | System.out.print("Input Day :"); // Input Date 40 | date = scanner.nextInt(); // User Input Date 41 | d.setDay(date); // Set Input Value 42 | 43 | 44 | if(date <= 31) { // Check the date is more than 31 45 | 46 | 47 | } 48 | else { 49 | 50 | System.out.println("Invalid Date"); // if date more than 31 display invalid date 51 | } 52 | 53 | System.out.print("Input Year :"); // Input Year 54 | year = scanner.nextInt(); // User Input year 55 | d.setYear(year); // Set Input Value 56 | 57 | d.DisplyDate(); // Call display method 58 | 59 | 60 | 61 | } // End of the Method 62 | 63 | 64 | 65 | }// End of the Main Class -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 9/Car.java: -------------------------------------------------------------------------------- 1 | public class Car{ 2 | 3 | private int speed; 4 | protected double reqularPrice; 5 | private String color; 6 | protected double SalePrice; 7 | 8 | 9 | public Car() { 10 | 11 | speed =0; 12 | reqularPrice = 0; 13 | color = "null"; 14 | SalePrice = 0; 15 | } 16 | 17 | public Car(int speed,double reqularPrice,String color, double SalePrice) { 18 | 19 | this.speed = speed; 20 | this.reqularPrice = reqularPrice; 21 | this.color = color; 22 | this.SalePrice = SalePrice; 23 | } 24 | 25 | public void setSpeed(int speed) { 26 | 27 | this.speed = speed; 28 | } 29 | 30 | public int getSpeed() { 31 | 32 | return speed; 33 | } 34 | 35 | public void setRequestularPrice(double reqularPrice) { 36 | 37 | this.reqularPrice = reqularPrice; 38 | } 39 | 40 | public double getRequestularPrice() { 41 | 42 | return reqularPrice; 43 | } 44 | 45 | public void setColor(String color) { 46 | 47 | this.color = color; 48 | } 49 | 50 | public String getColor() { 51 | 52 | return color; 53 | } 54 | 55 | 56 | public double getSalePrice() { 57 | 58 | return reqularPrice; 59 | } 60 | 61 | public void setSalePrice(double SalePrice) { 62 | 63 | this.salePrice = SalePrice; 64 | } 65 | 66 | public double get() { 67 | 68 | return SalePrice; 69 | } 70 | 71 | } 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 9/Ford.java: -------------------------------------------------------------------------------- 1 | public class Ford extends Car{ 2 | 3 | private int year; 4 | private int manufacturerDiscount; 5 | 6 | public Ford() { 7 | 8 | 9 | year = 0; 10 | manufacturerDiscount = 0; 11 | 12 | } 13 | 14 | public Ford(int speed,double reqularPrice,String color, double SalePrice, int year, int manufacturerDiscount) { 15 | 16 | super(speed,reqularPrice,color,SalePrice,); 17 | this.year = year; 18 | this.manufacturerDiscount = manufacturerDiscount; 19 | 20 | } 21 | 22 | public void setYear(int year) { 23 | 24 | this.year = year; 25 | } 26 | 27 | public int getYear() { 28 | 29 | return year; 30 | } 31 | public void setManufacturerDiscount(int manufacturerDiscount) { 32 | 33 | this.manufacturerDiscount = manufacturerDiscount; 34 | } 35 | 36 | public int getManufacturerDiscount() { 37 | 38 | return manufacturerDiscount; 39 | } 40 | 41 | public double getSalePrice() { 42 | 43 | return salePrice; 44 | } 45 | } -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 9/Sedan.java: -------------------------------------------------------------------------------- 1 | public class Sedan extends Car{ 2 | 3 | private int length; 4 | 5 | public Sedan() {}; 6 | 7 | public Sedan(int speed,double reqularPrice,String color, double SalePrice,int length) { 8 | 9 | super(speed,reqularPrice,color,SalePrice); 10 | this.length = length; 11 | } 12 | 13 | public void setLength(int length) { 14 | 15 | 16 | this.length = length; 17 | } 18 | 19 | public int getLength() { 20 | 21 | return length; 22 | } 23 | 24 | public double getSalePrice() { 25 | 26 | if (length > 20) { 27 | 28 | SalePrice = reqularPrice * 5/100; 29 | 30 | return SalePrice; 31 | } 32 | else { 33 | 34 | SalePrice = reqularPrice * 5/100; 35 | 36 | return SalePrice; 37 | } 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /Other Questions/Sahan/Question 9/Truck.java: -------------------------------------------------------------------------------- 1 | public class Truck extends Car{ 2 | 3 | private int weight; 4 | 5 | public Truck() { 6 | 7 | weight = 0; 8 | } 9 | 10 | public Truck(int speed,double reqularPrice,String color, double SalePrice, int weight) { 11 | 12 | super(speed, reqularPrice, color, SalePrice); 13 | this.weight = weight; 14 | 15 | } 16 | 17 | public void setWeight(int weight) { 18 | 19 | this.weight = weight; 20 | } 21 | 22 | public int getWeight() { 23 | 24 | return weight; 25 | } 26 | 27 | 28 | public double getSalePrice() { 29 | 30 | if(weight >2000) { 31 | 32 | SalePrice = reqularPrice * 10/100; 33 | 34 | return SalePrice; 35 | } 36 | 37 | else { 38 | 39 | SalePrice = reqularPrice * 20/100; 40 | 41 | return SalePrice; 42 | } 43 | 44 | } 45 | } -------------------------------------------------------------------------------- /Other Questions/Sahan/Questions/Page 1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Other Questions/Sahan/Questions/Page 1.jpg -------------------------------------------------------------------------------- /Other Questions/Sahan/Questions/Page 2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Other Questions/Sahan/Questions/Page 2.jpg -------------------------------------------------------------------------------- /Other Questions/Sahan/Questions/Page 3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Other Questions/Sahan/Questions/Page 3.jpg -------------------------------------------------------------------------------- /Other Questions/Sahan/Questions/Page 4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Other Questions/Sahan/Questions/Page 4.jpg -------------------------------------------------------------------------------- /Other Questions/Sahan/Questions/Page 5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Other Questions/Sahan/Questions/Page 5.jpg -------------------------------------------------------------------------------- /Other Questions/Sahan/Questions/Page 6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Other Questions/Sahan/Questions/Page 6.jpg -------------------------------------------------------------------------------- /Other Questions/Sahan/Questions/Page 7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Other Questions/Sahan/Questions/Page 7.jpg -------------------------------------------------------------------------------- /Past Papers/2021 OOP Final.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Past Papers/2021 OOP Final.pdf -------------------------------------------------------------------------------- /Past Papers/IT2030 - Mock paper.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Past Papers/IT2030 - Mock paper.pdf -------------------------------------------------------------------------------- /Past Papers/Mock Exam.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Past Papers/Mock Exam.pdf -------------------------------------------------------------------------------- /Past Papers/Mock paper Answers-20211018/Mock Esay Answers/Designpattern3/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Past Papers/Mock paper Answers-20211018/Mock Esay Answers/Designpattern3/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Designpattern3 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | 19 | 1664908487937 20 | 21 | 30 22 | 23 | org.eclipse.core.resources.regexFilterMatcher 24 | node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Past Papers/Mock paper Answers-20211018/Mock Esay Answers/Designpattern3/src/Action.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Action { 3 | public void Do(); 4 | } 5 | -------------------------------------------------------------------------------- /Past Papers/Mock paper Answers-20211018/Mock Esay Answers/Designpattern3/src/Developer.java: -------------------------------------------------------------------------------- 1 | 2 | public class Developer { 3 | public static void main(String args[]) { 4 | IDE ide=new IDE(); 5 | Receiver intendedreceiver = new Receiver(); 6 | System.out.println("Pressed save button!"); 7 | SaveAction saveaction = new SaveAction(intendedreceiver); 8 | ide.SetAction(saveaction); 9 | ide.ExecuteAction(); 10 | 11 | System.out.println("Pressed saveAll button!"); 12 | SaveAllAction saveallaction = new SaveAllAction(intendedreceiver); 13 | ide.SetAction(saveallaction); 14 | ide.ExecuteAction(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Past Papers/Mock paper Answers-20211018/Mock Esay Answers/Designpattern3/src/IDE.java: -------------------------------------------------------------------------------- 1 | 2 | public class IDE { 3 | Action action; 4 | public void SetAction(Action action) { 5 | this.action = action; 6 | } 7 | public void ExecuteAction() { 8 | action.Do(); 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /Past Papers/Mock paper Answers-20211018/Mock Esay Answers/Designpattern3/src/Receiver.java: -------------------------------------------------------------------------------- 1 | 2 | public class Receiver { 3 | public void PerformSave() { 4 | System.out.println("Saving file..."); 5 | } 6 | public void PerformSaveAll() { 7 | System.out.println("Saving all the files..."); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Past Papers/Mock paper Answers-20211018/Mock Esay Answers/Designpattern3/src/SaveAction.java: -------------------------------------------------------------------------------- 1 | 2 | public class SaveAction implements Action{ 3 | Receiver receiver; 4 | public SaveAction(Receiver receiver) { 5 | this.receiver = receiver; 6 | } 7 | public void Do() { 8 | receiver.PerformSave(); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Past Papers/Mock paper Answers-20211018/Mock Esay Answers/Designpattern3/src/SaveAllAction.java: -------------------------------------------------------------------------------- 1 | 2 | public class SaveAllAction implements Action{ 3 | Receiver receiver; 4 | public SaveAllAction(Receiver receiver) { 5 | this.receiver = receiver; 6 | } 7 | public void Do() { 8 | receiver.PerformSaveAll(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Past Papers/Mock paper Answers-20211018/Mock Esay Answers/Threads/WrappingPaperArt.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class WrappingPaperArt { 4 | 5 | public static void main(String[] args) { 6 | Object lock = new Object(); 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter Pattern 1 = "); 9 | String pattern1 = sc.next(); 10 | System.out.print("Enter Pattern 2 = "); 11 | String pattern2 = sc.next(); 12 | System.out.print("Enter count = "); 13 | int count = sc.nextInt(); 14 | 15 | System.out.println("========Threads start printing patterns.======="); 16 | 17 | Thread patternThread1 = new Thread(new Pattern01(lock, pattern1, count)); 18 | Thread patternThread2 = new Thread(new Pattern02(lock, pattern2, count)); 19 | 20 | patternThread1.setName("Pattern 01 Thread"); 21 | patternThread2.setName("Pattern 02 Thread"); 22 | patternThread1.start(); 23 | patternThread2.start(); 24 | } 25 | } 26 | 27 | class Pattern01 implements Runnable{ 28 | 29 | Object lock; 30 | String pattern; 31 | int count; 32 | 33 | public Pattern01(Object lock, String pattern, int count) { 34 | this.lock = lock; 35 | this.pattern = pattern; 36 | this.count = count; 37 | } 38 | 39 | @Override 40 | public void run() { 41 | synchronized (lock) { 42 | for (int x = 0; x < count; x++){ 43 | try { 44 | lock.wait(); 45 | for (int y = count; y >= x; y--) { 46 | System.out.print(" "); 47 | } 48 | for (int y = 0; y <= x; y++) { 49 | System.out.print("+ "); 50 | } 51 | System.out.println(); 52 | Thread.sleep(1000); 53 | lock.notify(); 54 | } catch (InterruptedException e) { 55 | e.printStackTrace(); 56 | } 57 | } 58 | } 59 | } 60 | } 61 | 62 | class Pattern02 implements Runnable{ 63 | 64 | Object lock; 65 | String pattern; 66 | int count; 67 | 68 | public Pattern02(Object lock, String pattern, int count) { 69 | this.lock = lock; 70 | this.pattern = pattern; 71 | this.count = count; 72 | } 73 | 74 | @Override 75 | public void run() { 76 | synchronized (lock) { 77 | for (int x = 0; x < count; x++){ 78 | try { 79 | lock.notify(); 80 | 81 | for (int y = count; y >= x; y--) { 82 | System.out.print(" "); 83 | } 84 | for (int y = 0; y <= x; y++) { 85 | System.out.print("- "); 86 | } 87 | System.out.println(); 88 | Thread.sleep(1000); 89 | lock.wait(); 90 | } catch (InterruptedException e) { 91 | e.printStackTrace(); 92 | } 93 | } 94 | } 95 | } 96 | } 97 | 98 | -------------------------------------------------------------------------------- /Past Papers/Mock paper Answers-20211018/mock MCQ - answers.txt: -------------------------------------------------------------------------------- 1 | 1->E 2 | 2->C 3 | 3->A,C 4 | 4->B,C,D,E 5 | 5->C 6 | 6->E 7 | 7->C 8 | 8->C 9 | 9->B 10 | 10->A,B,C,D -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java programming 2 | 3 |

IT 2030 Object Oriented Programming

4 | 5 |
6 | 7 | ![GitHub top Language](https://img.shields.io/github/languages/top/ThusharaX/Java-programming) 8 | ![visitor badge](https://visitor-badge.glitch.me/badge?page_id=ThusharaX.Java-programming) 9 | ![GitHub forks](https://img.shields.io/github/forks/ThusharaX/Java-programming?style=social) 10 | ![GitHub contributors](https://img.shields.io/github/contributors/ThusharaX/Java-programming) 11 | ![GitHub Repo stars](https://img.shields.io/github/stars/ThusharaX/Java-programming?style=social) 12 | ![GitHub repo size](https://img.shields.io/github/repo-size/ThusharaX/Java-programming) 13 | ![GitHub watchers](https://img.shields.io/github/watchers/ThusharaX/Java-programming?style=social) 14 | ![GitHub issues](https://img.shields.io/github/issues/ThusharaX/Java-programming) 15 | ![GitHub pull requests](https://img.shields.io/github/issues-pr/ThusharaX/Java-programming) 16 | ![GitHub labels](https://img.shields.io/github/labels/ThusharaX/Java-programming/help%20wanted) 17 | ![GitHub](https://img.shields.io/github/license/ThusharaX/Java-programming) 18 | 19 | ![Java-programming](https://socialify.git.ci/ThusharaX/Java-programming/image?description=1&forks=1&language=1&logo=https%3A%2F%2Fraw.githubusercontent.com%2FBinaryMatter%2FBinaryMatter.github.io%2Fgh-pages%2FlogoRoundwithBorder.png&owner=1&pattern=Circuit%20Board&stargazers=1&theme=Dark) 20 | 21 | - 🌱 This is a repository for Java programming related questions. 22 | - 👯 Everyone is welcome to contribute. 23 | 24 | ## Contributors ✨ 25 | 26 | Thanks goes to these wonderful people :heart: 27 | 28 | 29 | 30 | 31 | 38 | 45 | 52 | 53 | 60 | 67 | 74 | 75 | 82 | 89 | 96 |
32 | 33 | ThusharaX 34 |
35 | Thushara Thiwanka 36 |
37 |
39 | 40 | pasindulakshan 41 |
42 | Pasindu Lakshan 43 |
44 |
46 | 47 | SahanPunchihewa 48 |
49 | Sahan Punchihewa 50 |
51 |
54 | 55 | ShanukaPrabodha 56 |
57 | Shanuka Prabodha 58 |
59 |
61 | 62 | kavindu-chethani 63 |
64 | Kavindu Chethani 65 |
66 |
68 | 69 | chetanbhasney02 70 |
71 | Chetanbhasney02 72 |
73 |
76 | 77 | ShehaniHettiarachchi 78 |
79 | Shehani Hettiarachchi 80 |
81 |
83 | 84 | ImgBotApp 85 |
86 | Imgbot 87 |
88 |
90 | 91 | Randula98 92 |
93 | Randula Yashasmith Mawaththa 94 |
95 |
97 | 98 | -------------------------------------------------------------------------------- /Resources/Code Conventions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Resources/Code Conventions.pdf -------------------------------------------------------------------------------- /Resources/Exceptions.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Resources/Exceptions.jpg -------------------------------------------------------------------------------- /Resources/Hierarchy of Java Exception Classes.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Resources/Hierarchy of Java Exception Classes.jpg -------------------------------------------------------------------------------- /Resources/Java Exception - CheatSheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Resources/Java Exception - CheatSheet.pdf -------------------------------------------------------------------------------- /Resources/Java Generics - CheatSheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Resources/Java Generics - CheatSheet.pdf -------------------------------------------------------------------------------- /Resources/Java OOP - CheatSheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Resources/Java OOP - CheatSheet.pdf -------------------------------------------------------------------------------- /Resources/Javabook.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Resources/Javabook.pdf -------------------------------------------------------------------------------- /Resources/Useful Links.txt: -------------------------------------------------------------------------------- 1 | https://www.javatpoint.com/java-tutorial 2 | 3 | https://www.tutorialspoint.com/java/ 4 | 5 | https://www.udacity.com/course/java-programming-basics--ud282 6 | 7 | https://docs.oracle.com/javase/tutorial/ -------------------------------------------------------------------------------- /Tutorials/Tutorial 02/Turorial2Question1answer/Main.java: -------------------------------------------------------------------------------- 1 | class Main { 2 | public static void main(String[] args) { 3 | Student s1 = new Student(); 4 | s1.get("che",21,78,50); 5 | s1.disp(); 6 | } 7 | } -------------------------------------------------------------------------------- /Tutorials/Tutorial 02/Turorial2Question1answer/Student.java: -------------------------------------------------------------------------------- 1 | class Student{ 2 | String name; 3 | int sub1; 4 | int sub2; 5 | int sub3; 6 | int Avg; 7 | void get(String n, int a, int b, int c) { 8 | name = n; 9 | sub1 = a; 10 | sub2 = b; 11 | sub3 = c; 12 | } 13 | 14 | void disp() { 15 | Avg = (sub1 + sub2+sub3 )/2; 16 | System.out.println("Name=" + name); 17 | System.out.println("Average=" + Avg); 18 | } 19 | } -------------------------------------------------------------------------------- /Tutorials/Tutorial 02/Tutorial 02.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Tutorials/Tutorial 02/Tutorial 02.pdf -------------------------------------------------------------------------------- /Tutorials/Tutorial 03/Tut03Ex01/DemoApp.java: -------------------------------------------------------------------------------- 1 | 2 | class Calculation { 3 | 4 | public static int addition(int n1, int n2) { 5 | return n1 + n2; 6 | } 7 | 8 | public static int subtraction(int n1, int n2) { 9 | return n1 - n2; 10 | } 11 | } 12 | 13 | class DemoApp { 14 | 15 | public static void main(String[] args) { 16 | 17 | Calculation.addition(3, 2); 18 | Calculation.subtraction(5, 3); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Tutorials/Tutorial 03/Tut03Ex02/Main.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Tutorials/Tutorial 03/Tut03Ex02/Main.java -------------------------------------------------------------------------------- /Tutorials/Tutorial 03/Tutorial 03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Tutorials/Tutorial 03/Tutorial 03.pdf -------------------------------------------------------------------------------- /Tutorials/Tutorial 04/Tut04Ex01/Book.java: -------------------------------------------------------------------------------- 1 | // Implement a class called Book with properties bookID, title, publisher which uses 2 | // the interfaces IDisplay and IInput 3 | 4 | import java.util.Scanner; 5 | 6 | public class Book implements IDisplay, IInput { 7 | Scanner input = new Scanner(System.in); 8 | private int bookID; 9 | private String title; 10 | private String publisher; 11 | 12 | // default constructor 13 | public Book() { 14 | this.bookID = 0; 15 | this.title = ""; 16 | this.publisher = ""; 17 | } 18 | 19 | public Book(int bookID, String title, String publisher) { 20 | this.bookID = bookID; 21 | this.title = title; 22 | this.publisher = publisher; 23 | } 24 | 25 | public int getBookID() { 26 | return bookID; 27 | } 28 | 29 | public void setBookID(int bookID) { 30 | this.bookID = bookID; 31 | } 32 | 33 | public String getTitle() { 34 | return title; 35 | } 36 | 37 | public void setTitle(String title) { 38 | this.title = title; 39 | } 40 | 41 | 42 | public String getPublisher() { 43 | return publisher; 44 | } 45 | 46 | 47 | public void setPublisher(String publisher) { 48 | this.publisher = publisher; 49 | } 50 | 51 | @Override 52 | public void print() { // Print in one line 53 | System.out.println("Book ID: " + bookID + " Title: " + title + " Publisher: " + publisher); 54 | } 55 | 56 | @Override 57 | public void printDetails() { // Print in multiple Lines 58 | System.out.println("Book ID: " + bookID); 59 | System.out.println("Title: " + title); 60 | System.out.println("Publisher: " + publisher); 61 | } 62 | 63 | @Override 64 | public void input() { 65 | 66 | System.out.println("Enter Book ID: "); 67 | bookID = input.nextInt(); 68 | 69 | System.out.println("Enter Title: "); 70 | title = input.next(); 71 | 72 | System.out.println("Enter Publisher: "); 73 | publisher = input.next(); 74 | 75 | } 76 | } 77 | 78 | -------------------------------------------------------------------------------- /Tutorials/Tutorial 04/Tut04Ex01/IDisplay.java: -------------------------------------------------------------------------------- 1 | interface IDisplay { 2 | void print(); // Print in one line 3 | 4 | void printDetails(); // Print in multiple Lines 5 | } 6 | -------------------------------------------------------------------------------- /Tutorials/Tutorial 04/Tut04Ex01/IInput.java: -------------------------------------------------------------------------------- 1 | interface IInput { 2 | void input(); 3 | } -------------------------------------------------------------------------------- /Tutorials/Tutorial 04/Tut04Ex01/Main.java: -------------------------------------------------------------------------------- 1 | // Create objects of the Book and Student in the main method 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Book book1 = new Book(1, "Java Programming", "John Wiley & Sons"); 6 | Book book2 = new Book(2, "C Programming", "Pearson"); 7 | Book book3 = new Book(3, "C++ Programming", "Pearson"); 8 | 9 | Student student1 = new Student(1, "John"); 10 | Student student2 = new Student(2, "Mary"); 11 | Student student3 = new Student(3, "Peter"); 12 | 13 | // Create a variable of the IDisplay and IInput interfaces and call the 14 | // printDetails() and input() methods respectively of Book and Student objects 15 | 16 | // For Book 17 | IDisplay displayBook = book1; 18 | displayBook.printDetails(); 19 | IInput inputBook = book1; 20 | inputBook.input(); 21 | 22 | // For Student 23 | IDisplay displayStudent = student1; 24 | displayStudent.printDetails(); 25 | IInput inputStudent = student1; 26 | inputStudent.input(); 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /Tutorials/Tutorial 04/Tut04Ex01/Student.java: -------------------------------------------------------------------------------- 1 | // Implement a class called Student with properties studentID, and name which uses the 2 | // interfaces IDisplay and IInput 3 | 4 | import java.util.Scanner; 5 | 6 | public class Student implements IDisplay, IInput { 7 | Scanner input = new Scanner(System.in); 8 | private int studentID; 9 | private String name; 10 | 11 | // default constructor 12 | public Student() { 13 | this.studentID = 0; 14 | this.name = ""; 15 | } 16 | 17 | public Student(int studentID, String name) { 18 | this.studentID = studentID; 19 | this.name = name; 20 | } 21 | 22 | public int getStudentID() { 23 | return studentID; 24 | } 25 | 26 | public void setStudentID(int studentID) { 27 | this.studentID = studentID; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | 38 | @Override 39 | public void print() { 40 | System.out.println("Student ID: " + studentID + " Name: " + name); 41 | } 42 | 43 | @Override 44 | public void printDetails() { // Print in multiple Lines 45 | System.out.println("Student ID: " + studentID); 46 | System.out.println("Name: " + name); 47 | } 48 | 49 | @Override 50 | public void input() { 51 | 52 | System.out.print("Enter Student ID: "); 53 | studentID = input.nextInt(); 54 | 55 | System.out.print("Enter Student Name: "); 56 | name = input.next(); 57 | 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Tutorials/Tutorial 04/Tut04Ex02/Account.java: -------------------------------------------------------------------------------- 1 | // Implement an abstract class called Account. Have the following properties accountNo, 2 | // name, balance. 3 | 4 | public abstract class Account { 5 | protected int accountNo; 6 | protected String name; 7 | protected double balance; 8 | 9 | // default constructor 10 | public Account() { 11 | this.accountNo = 0; 12 | this.name = ""; 13 | this.balance = 0.0; 14 | } 15 | 16 | public Account(int accountNo, String name, double balance) { 17 | this.accountNo = accountNo; 18 | this.name = name; 19 | this.balance = balance; 20 | } 21 | 22 | public int getAccountNo() { 23 | return accountNo; 24 | } 25 | 26 | public void setAccountNo(int accountNo) { 27 | this.accountNo = accountNo; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | 38 | public double getBalance() { 39 | return balance; 40 | } 41 | 42 | public void setBalance(double balance) { 43 | this.balance = balance; 44 | } 45 | 46 | 47 | // Implement a Deposit() method to deposit money. The amount deposited should 48 | // update the balance. 49 | public void deposit(double amount) { 50 | this.balance += amount; 51 | } 52 | 53 | // Have an abstract method called calculateInterest() which returns a double value 54 | public abstract double calculateInterest(); 55 | 56 | // Implement a method to display() the account details. 57 | public void display() { 58 | System.out.println("Account No: " + this.accountNo); 59 | System.out.println("Name: " + this.name); 60 | System.out.println("Balance: " + this.balance); 61 | } 62 | } -------------------------------------------------------------------------------- /Tutorials/Tutorial 04/Tut04Ex02/FixedDepositAccount.java: -------------------------------------------------------------------------------- 1 | // Implement a new class called FixedDepositAccount which extends the Account class 2 | // It should have a new property called interestRate and Interest. 3 | // Write a setter and getter for the interestRate. 4 | // Implement the calculateInterest() method assuming that the Balance has been 5 | // held for the entire year. interest = balance * interestRate/100; 6 | 7 | public class FixedDepositAccount extends Account { 8 | protected double interestRate; 9 | protected double interest; 10 | 11 | // default constructor 12 | public FixedDepositAccount() { 13 | super(); 14 | interestRate = 0.0; 15 | interest = 0.0; 16 | } 17 | 18 | // parameterized constructor 19 | public FixedDepositAccount(int accountNumber, String accountHolderName, double balance, double interestRate) { 20 | super(accountNumber, accountHolderName, balance); 21 | this.interestRate = interestRate; 22 | } 23 | 24 | public void setInterestRate(double interestRate) { 25 | this.interestRate = interestRate; 26 | } 27 | 28 | public double getInterestRate() { 29 | return interestRate; 30 | } 31 | 32 | public double calculateInterest() { 33 | interest = super.getBalance() * interestRate / 100; 34 | return interest; 35 | } 36 | } -------------------------------------------------------------------------------- /Tutorials/Tutorial 04/Tut04Ex02/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | // Create objects from the FixedDeposit and SavingAccount. Call the Deposit() and withdraw() methods (Only SavingsAccount have withdrawals) 4 | 5 | FixedDepositAccount fixedDepositAccount = new FixedDepositAccount(1, "John", 1000, 0.05); 6 | fixedDepositAccount.deposit(500); 7 | fixedDepositAccount.display(); 8 | double interest = fixedDepositAccount.calculateInterest(); 9 | System.out.println("Interest: " + interest); 10 | 11 | SavingAccount savingAccount = new SavingAccount(2, "Jane", 1000, 0.05); 12 | savingAccount.deposit(500); 13 | savingAccount.withdraw(100); 14 | savingAccount.display(); 15 | interest = savingAccount.calculateInterest(); 16 | System.out.println("Interest: " + interest); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Tutorials/Tutorial 04/Tut04Ex02/SavingAccount.java: -------------------------------------------------------------------------------- 1 | // Implement a new class called SavingAccount which inherits the 2 | // FixedDepositAccount class 3 | // Implement a withdraw() method that allows you to withdraw money from the SavingsAccount. 4 | // Implement the calculateInterest() method assuming that the Balance has been held for the one Month. Interest = balance * interestRate/100/12; 5 | 6 | class SavingAccount extends FixedDepositAccount { 7 | 8 | // default constructor 9 | public SavingAccount() { 10 | super(); 11 | } 12 | 13 | // parameterized constructor 14 | public SavingAccount(int accountNumber, String accountHolderName, double balance, double interestRate) { 15 | super(accountNumber, accountHolderName, balance, interestRate); 16 | } 17 | 18 | public void withdraw(double amount) { 19 | if (amount > super.getBalance()) { 20 | System.out.println("You can't withdraw more than your balance"); 21 | } else { 22 | super.setBalance(super.getBalance() - amount); 23 | } 24 | } 25 | 26 | public double calculateInterest() { 27 | double interest = super.getBalance() * interestRate / 100 / 12; 28 | return interest; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Tutorials/Tutorial 04/Tutorial 04.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Tutorials/Tutorial 04/Tutorial 04.pdf -------------------------------------------------------------------------------- /Tutorials/Tutorial 05/Tut05Ex01Q01/Main.java: -------------------------------------------------------------------------------- 1 | // Write a simple program that calculates the square root of a number (Math.sqrt()). 2 | // Identify the exception that can occur and write code to handle this using a try catch block. 3 | 4 | 5 | import java.util.InputMismatchException; // Import InputMismatchException 6 | import java.util.Scanner; // Import the Scanner class 7 | 8 | public class Main { // Main class 9 | 10 | public static void main(String[] args) { // main method 11 | 12 | 13 | Scanner input = new Scanner(System.in); // Create a scanner object 14 | 15 | try { 16 | System.out.print("Enter a number: "); // Prompt the user to enter a number 17 | double num = input.nextDouble(); // Store the number entered by the user 18 | 19 | double sqrt = Math.sqrt(num); // Calculate the square root of the number 20 | 21 | if(Double.isNaN(sqrt)) { // If the number is not a number 22 | throw new ArithmeticException("Answer unreal"); // Throw an exception 23 | } 24 | 25 | System.out.println("The square root of " + num + " is " + sqrt); // Print the answer 26 | } 27 | 28 | catch (InputMismatchException e) { // If the user enters a non-numeric value 29 | System.out.println(e); // Print the exception 30 | System.out.println("Invalid input"); // Print an error message 31 | } 32 | 33 | catch (ArithmeticException e) { // If the number is not a number 34 | System.out.println(e.getMessage()); // Print the exception 35 | } 36 | 37 | input.close(); // Close the scanner object 38 | } 39 | } -------------------------------------------------------------------------------- /Tutorials/Tutorial 05/Tut05Ex01Q02/Main.java: -------------------------------------------------------------------------------- 1 | // The Following code would generate some Exceptions. Identify the Exception. Extend the 2 | // code and handle all Exceptions. 3 | 4 | class Main { 5 | static int divide(String s1[]) { 6 | int x, y; 7 | 8 | x = Integer.parseInt(s1[0]); 9 | y = Integer.parseInt(s1[1]); 10 | 11 | return x / y; 12 | } 13 | 14 | public static void main(String args[]) { 15 | String a[] = { "10", "0" }; 16 | 17 | try { 18 | System.out.println(a[0] + "/" + a[1] + " = " + divide(a)); 19 | } catch (ArithmeticException e) { 20 | System.out.println(e); 21 | } catch (Exception e) { 22 | System.out.println(e); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Tutorials/Tutorial 05/Tut05Ex01Q03/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Main { 4 | public static void main(String args[]) { 5 | 6 | Scanner myscanner = new Scanner(System.in); 7 | int size; 8 | int count = 0; 9 | int total = 0; 10 | float avg; 11 | 12 | System.out.println("Enter size of data : "); 13 | size = myscanner.nextInt(); 14 | 15 | int data[] = new int[size]; 16 | int index; 17 | 18 | System.out.print("Enter Index : "); 19 | index = myscanner.nextInt(); 20 | 21 | while (index != -1) { 22 | System.out.print("Enter a marks : "); 23 | 24 | try { 25 | data[index] = myscanner.nextInt(); 26 | count++; 27 | 28 | try { 29 | total += data[index]; 30 | } catch (ArrayIndexOutOfBoundsException e) { 31 | System.out.println(e); 32 | } 33 | } catch (ArrayIndexOutOfBoundsException e) { 34 | System.out.println(e); 35 | } 36 | 37 | System.out.print("Enter Index : "); 38 | index = myscanner.nextInt(); 39 | } 40 | 41 | for (int r = 0; r < size; r++) 42 | System.out.println(r + " - " + data[r]); 43 | 44 | avg = (float) total / count; 45 | 46 | System.out.println("Average : " + avg); 47 | 48 | myscanner.close(); 49 | } 50 | } -------------------------------------------------------------------------------- /Tutorials/Tutorial 05/Tutorial 05.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThusharaX/Java-programming/addbd1e355e53816a704ea9d8fbea38c4240d66a/Tutorials/Tutorial 05/Tutorial 05.pdf --------------------------------------------------------------------------------