From 471f98588abe9dacb05a884eba746f00b309a266 Mon Sep 17 00:00:00 2001 From: aasashi <162731783+aasashi@users.noreply.github.com> Date: Fri, 27 Dec 2024 19:40:50 +0530 Subject: [PATCH 1/2] Added Java Activity files --- Java/Activities/Activity1.java | 11 +++++ Java/Activities/Activity10.java | 36 +++++++++++++++ Java/Activities/Activity11.java | 37 +++++++++++++++ Java/Activities/Activity12.java | 22 +++++++++ Java/Activities/Activity13.java | 31 +++++++++++++ Java/Activities/Activity14.java | 41 +++++++++++++++++ Java/Activities/Activity2.java | 39 ++++++++++++++++ Java/Activities/Activity3.java | 6 +++ Java/Activities/Activity4.java | 34 ++++++++++++++ Java/Activities/Activity5.java | 37 +++++++++++++++ Java/Activities/Activity6.java | 62 ++++++++++++++++++++++++++ Java/Activities/Activity7.java | 79 +++++++++++++++++++++++++++++++++ Java/Activities/Activity9.java | 30 +++++++++++++ Java/Activities/Car.java | 36 +++++++++++++++ 14 files changed, 501 insertions(+) create mode 100644 Java/Activities/Activity1.java create mode 100644 Java/Activities/Activity10.java create mode 100644 Java/Activities/Activity11.java create mode 100644 Java/Activities/Activity12.java create mode 100644 Java/Activities/Activity13.java create mode 100644 Java/Activities/Activity14.java create mode 100644 Java/Activities/Activity2.java create mode 100644 Java/Activities/Activity3.java create mode 100644 Java/Activities/Activity4.java create mode 100644 Java/Activities/Activity5.java create mode 100644 Java/Activities/Activity6.java create mode 100644 Java/Activities/Activity7.java create mode 100644 Java/Activities/Activity9.java create mode 100644 Java/Activities/Car.java diff --git a/Java/Activities/Activity1.java b/Java/Activities/Activity1.java new file mode 100644 index 0000000000..50085863b4 --- /dev/null +++ b/Java/Activities/Activity1.java @@ -0,0 +1,11 @@ +package activities; + +public class Activity1 { + + public static void main(String[] args) { + Car carobj=new Car("Red","Manual",2024); + carobj.displayCharacteristics(); + carobj.accelarate(); + carobj.brake(); + } +} diff --git a/Java/Activities/Activity10.java b/Java/Activities/Activity10.java new file mode 100644 index 0000000000..5e004e7226 --- /dev/null +++ b/Java/Activities/Activity10.java @@ -0,0 +1,36 @@ +package activities; + +import java.util.HashSet; +import java.util.Set; + +public class Activity10 { + public static void main(String[] args) { + Set hs = new HashSet(); + // Adding element to HashSet + hs.add("M"); + hs.add("B"); + hs.add("C"); + hs.add("A"); + hs.add("M"); + hs.add("X"); + + //Print HashSet + System.out.println("Original HashSet: " + hs); + //Print size of HashSet + System.out.println("Size of HashSet: " + hs.size()); + + //Remove element + System.out.println("Removing A from HashSet: " + hs.remove("A")); + //Remove element that is not present + if(hs.remove("Z")) { + System.out.println("Z removed from the Set"); + } else { + System.out.println("Z is not present in the Set"); + } + + //Search for element + System.out.println("Checking if M is present: " + hs.contains("M")); + //Print updated HashSet + System.out.println("Updated HashSet: " + hs); + } +} diff --git a/Java/Activities/Activity11.java b/Java/Activities/Activity11.java new file mode 100644 index 0000000000..38836cff8c --- /dev/null +++ b/Java/Activities/Activity11.java @@ -0,0 +1,37 @@ +package activities; + +import java.util.HashMap; + +public class Activity11 { + + public static void main(String[] args) { + HashMap hashmap = new HashMap(); + hashmap.put(1, "Red"); + hashmap.put(2, "Green"); + hashmap.put(3, "Blue"); + hashmap.put(4, "White"); + hashmap.put(5, "Black"); + + // Print the Map + System.out.println("The Original map: " + hashmap); + + // Remove one colour + hashmap.remove(4); + // Map after removing a colour + System.out.println("After removing White: " + hashmap); + + // Check if green exists + if(hashmap.containsValue("Green")) { + System.out.println("Green exists in the Map"); + } else { + System.out.println("Green does not exist in the Map"); + } + + // Print the size of the Map + System.out.println("Number of pairs in the Map is: " + hashmap.size()); + //loop through Map + for(int key:hashmap.keySet()) { + System.out.println(hashmap.get(key)); + } + } +} diff --git a/Java/Activities/Activity12.java b/Java/Activities/Activity12.java new file mode 100644 index 0000000000..c62400f0eb --- /dev/null +++ b/Java/Activities/Activity12.java @@ -0,0 +1,22 @@ +package activities; + +@FunctionalInterface +interface Addable { + int add(int a, int b); +} +public class Activity12 { + + public static void main(String[] args) { + + // Lambda expression without return keyword. + Addable ad1 = (a, b) -> (a + b); + System.out.println(ad1.add(10, 20)); + + // Lambda expression with return keyword. + Addable ad2 = (int a, int b) -> { + return (a + b); + }; + System.out.println(ad2.add(100, 200)); + } + +} diff --git a/Java/Activities/Activity13.java b/Java/Activities/Activity13.java new file mode 100644 index 0000000000..c95ae2c321 --- /dev/null +++ b/Java/Activities/Activity13.java @@ -0,0 +1,31 @@ +package activities; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class Activity13 { + public static void main(String[] args) { + //Create the Objects + Scanner scan = new Scanner(System.in); + List list = new ArrayList<>(); + Random indexGen = new Random(); + //statements to show users to indicate when they can type + System.out.print("Enter integers for the list "); + System.out.println("Enter a EOF or non-integer to terminate): "); + //Loop to take only integers from console + while(scan.hasNextInt()) { + list.add(scan.nextInt()); + } + + Integer nums[] = list.toArray(new Integer[0]); + //Generate random Index + int index = indexGen.nextInt(nums.length); + System.out.println("Random Index value generated: " + index); + System.out.println("Value in arary at generated index: " + nums[index]); + + scan.close(); + } + +} diff --git a/Java/Activities/Activity14.java b/Java/Activities/Activity14.java new file mode 100644 index 0000000000..dcea5d290f --- /dev/null +++ b/Java/Activities/Activity14.java @@ -0,0 +1,41 @@ +package activities; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; + +public class Activity14 { + + public static void main(String[] args) throws IOException { + try { + File file = new File("src/session4/newfile.txt"); + boolean fStatus = file.createNewFile(); + if(fStatus) { + System.out.println("File created successfully!"); + } else { + System.out.println("File already exists at this path."); + } + + //get the file Object + File fileUtil = FileUtils.getFile("src/session4/newfile.txt"); + //Read file + System.out.println("Data in file: " + FileUtils.readFileToString(fileUtil, "UTF8")); + + //Create directory + File destDir = new File("resources"); + //Copy file to directory + FileUtils.copyFileToDirectory(file, destDir); + + //Get file from new directory + File newFile = FileUtils.getFile(destDir, "newfile.txt"); + //Read data from file + String newFileData = FileUtils.readFileToString(newFile, "UTF8"); + //Print it + System.out.println("Data in new file: " + newFileData); + } catch(IOException errMessage) { + System.out.println(errMessage); + } + } + +} diff --git a/Java/Activities/Activity2.java b/Java/Activities/Activity2.java new file mode 100644 index 0000000000..2a97c20cd9 --- /dev/null +++ b/Java/Activities/Activity2.java @@ -0,0 +1,39 @@ +package activities; + +public class Activity2 { + + public static void main(String[] args) { + // creates the object for the class to call the functions exist in this class + Activity2 actobj=new Activity2(); + int[] numarray= {10, 77, 10, 54, -11, 10}; + //set the numbers to serach for + int searchNum=10; + + int fixedsum=30; + + //call the result function and print the result + System.out.println("Result :"+actobj.result(numarray,searchNum,fixedsum)); + } + + public boolean result(int[] numarray, int searchNum, int fixedsum) { + // TODO Auto-generated method stub + int tempsum =0; + for(int number:numarray) { + //check if the number is search Num + if(number == searchNum) { + //Add the Value + tempsum=tempsum+searchNum; + + //tempsum +=searchNum; + } + + //check if tempsum is greater than fixedNum + if(tempsum > fixedsum) { + //condition fails, break loop + break; + } + } + return fixedsum == tempsum; + } + +} diff --git a/Java/Activities/Activity3.java b/Java/Activities/Activity3.java new file mode 100644 index 0000000000..e773f35bf1 --- /dev/null +++ b/Java/Activities/Activity3.java @@ -0,0 +1,6 @@ +package activities; + +public class Activity3 { + + +} diff --git a/Java/Activities/Activity4.java b/Java/Activities/Activity4.java new file mode 100644 index 0000000000..bc574126d7 --- /dev/null +++ b/Java/Activities/Activity4.java @@ -0,0 +1,34 @@ +package activities; + +import java.util.Arrays; + +public class Activity4 { + + public static void main(String args[]) { + Activity4 actobj=new Activity4(); + int[] data = { 9, 5, 1, 4, 3 }; + //call the function to sort the array + actobj.ascendingSort(data); + System.out.println("Sorted Array in Ascending Order: "); + System.out.println(Arrays.toString(data)); + } + + public void ascendingSort(int[] data) { + //set the size of the array + int size = data.length; + //loop through the array + //Starting from second value + for (int i = 1; i < size; i++) { + //Set the Key value and the first compare value + int key = data[i]; + int j = i - 1; + //Check if Key is lesser + //If it is, + while (j >= 0 && key < data[j]) { + data[j + 1] = data[j]; + --j; + } + data[j + 1] = key; + } + } +} diff --git a/Java/Activities/Activity5.java b/Java/Activities/Activity5.java new file mode 100644 index 0000000000..b192913b20 --- /dev/null +++ b/Java/Activities/Activity5.java @@ -0,0 +1,37 @@ +package activities; + + public class Activity5 { + + public static void main(String []args) { + //Initialize title of the book + String title = "Hover Car Racer"; + //Create object for MyBook + Book newNovel = new MyBook(); + //Set title + newNovel.setTitle(title); + + //Print result + System.out.println("The title is: " + newNovel.getTitle()); + } + } + + //Abstract class + abstract class Book { + String title; + //Abstract method + abstract void setTitle(String s); + + //Concrete method + String getTitle() { + return title; + } + } + + class MyBook extends Book { + //Define abstract method + public void setTitle(String s) { + title = s; + } + } + + diff --git a/Java/Activities/Activity6.java b/Java/Activities/Activity6.java new file mode 100644 index 0000000000..cb2b69e3b7 --- /dev/null +++ b/Java/Activities/Activity6.java @@ -0,0 +1,62 @@ +package activities; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +class Plane { + private List passengers; + private int maxPassengers; + private Date lastTimeTookOf; + private Date lastTimeLanded; + + public Plane(int maxPassengers) { + this.maxPassengers = maxPassengers; + this.passengers = new ArrayList<>(); + } + + public void onboard(String passenger) { + this.passengers.add(passenger); + } + + public Date takeOff() { + this.lastTimeTookOf = new Date(); + return lastTimeTookOf; + } + + public void land() { + this.lastTimeLanded = new Date(); + this.passengers.clear(); + } + + public Date getLastTimeLanded() { + return lastTimeLanded; + } + + public List getPassengers() { + return passengers; + } +} + +public class Activity6 { + + public static void main(String[] args) throws InterruptedException { + //There is a plane with max 10 passengers + Plane plane = new Plane(10); + //Add passengers on the list + plane.onboard("John"); + plane.onboard("Steve"); + plane.onboard("Anna"); + //Plane takes off + System.out.println("Plane took off at: " + plane.takeOff()); + //Print list of people on board + System.out.println("People on the plane: " + plane.getPassengers()); + //Flying..... + Thread.sleep(5000); + //Plane has landed + plane.land(); + //Plane lands + System.out.println("Plane landed at: " + plane.getLastTimeLanded()); + System.out.println("People on the plane after landing: " + plane.getPassengers()); + } +} diff --git a/Java/Activities/Activity7.java b/Java/Activities/Activity7.java new file mode 100644 index 0000000000..8beede8c86 --- /dev/null +++ b/Java/Activities/Activity7.java @@ -0,0 +1,79 @@ +package activities; + +interface BicycleParts { + /* + * public int gears = 0; public int currentSpeed = 0; + */ + + public int tyres=2; + public int maxspeed = 25; +} + +interface BicycleOperations { + public void applyBrake(int decrement); + public void speedUp(int increment); +} + +//Base class +class Bicycle implements BicycleParts, BicycleOperations { + + public int gears; + public int currentSpeed; + + //the Bicycle class has one constructor + public Bicycle(int gears, int currentSpeed) { + this.gears = gears; + this.currentSpeed = currentSpeed; + } + + //Bicycle class has three methods + public void applyBrake(int decrement) { + currentSpeed -= decrement; + System.out.println("Current speed: " + currentSpeed); + } + + public void speedUp(int increment) { + currentSpeed += increment; + System.out.println("Current speed: " + currentSpeed); + } + + //Method to print info of Bicycle + public String bicycleDesc() { + //return("No of gears are "+ gears + "\nSpeed of bicycle is " + currentSpeed); + + return("No of gears are "+ gears + "\nNo.of tyres are " + tyres); + } +} + +//Derived class +class MountainBike extends Bicycle { + + //The MountainBike subclass adds one more field + public int seatHeight; + + //The MountainBike subclass has one constructor + public MountainBike(int gears, int currentSpeed, int startHeight) { + //Invoking base-class(Bicycle) constructor + super(gears, currentSpeed); + seatHeight = startHeight; + } + + // the MountainBike subclass adds one more method + public void setHeight(int newValue) { + seatHeight = newValue; + } + + public String bicycleDesc() { + return (super.bicycleDesc()+ "\nSeat height is " + seatHeight); + } +} + +//Driver class +public class Activity7 { + public static void main(String args[]) { + MountainBike mb = new MountainBike(3, 0, 25); + System.out.println(mb.bicycleDesc()); + mb.speedUp(20); + mb.applyBrake(5); + } +} diff --git a/Java/Activities/Activity9.java b/Java/Activities/Activity9.java new file mode 100644 index 0000000000..af5a996524 --- /dev/null +++ b/Java/Activities/Activity9.java @@ -0,0 +1,30 @@ +package activities; + +import java.util.ArrayList; + +public class Activity9 { + public static void main(String[] args) { + //declaring Arraylist of String objects + ArrayList myList = new ArrayList(); + //Adding objects to Array List at default index + myList.add("Apple"); + myList.add("Mango"); + myList.add("Orange"); + //Adding object at specific index + myList.add(3, "Grapes"); + myList.add(1, "Papaya"); + + System.out.println("Print All the Objects:"); + for(String s:myList){ + System.out.println(s); + } + + System.out.println("3rd element in the list is: " + myList.get(2)); + System.out.println("Is Chicku is in list: " + myList.contains("Chicku")); + System.out.println("Size of ArrayList: " + myList.size()); + + myList.remove("Papaya"); + + System.out.println("New Size of ArrayList: " + myList.size()); + } +} diff --git a/Java/Activities/Car.java b/Java/Activities/Car.java new file mode 100644 index 0000000000..fc783ae5fb --- /dev/null +++ b/Java/Activities/Car.java @@ -0,0 +1,36 @@ +package activities; + +public class Car { + + String color; + String transmission; + int make; + int tyres; + int doors; + + Car(String color,String transmission,int make) { + this.color=color; + this.transmission=transmission; + this.make=make; + this.tyres=4; + this.doors=4; + } + + public void displayCharacteristics() { + System.out.println("Color :"+color); + System.out.println("Transmission Type :"+transmission); + System.out.println("yearof Making :"+make); + System.out.println("No.of Tyres :"+tyres); + System.out.println("No.of Doors :"+doors); + + } + + public void accelarate() { + System.out.println("Car is moving forward"); + } + + public void brake() { + System.out.println("Car has stopped"); + } + +} From db6ccffc8b24cc295028be9fa374e41f282ed407 Mon Sep 17 00:00:00 2001 From: aasashi <162731783+aasashi@users.noreply.github.com> Date: Mon, 17 Feb 2025 23:36:29 +0530 Subject: [PATCH 2/2] Added Activity8 to Java --- Java/Activities/Activity8.java | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Java/Activities/Activity8.java diff --git a/Java/Activities/Activity8.java b/Java/Activities/Activity8.java new file mode 100644 index 0000000000..02ab1886c0 --- /dev/null +++ b/Java/Activities/Activity8.java @@ -0,0 +1,46 @@ +package activities; + +class CustomException extends Exception { + private String message= null; + + public CustomException(String message) { + this.message=message; + + } + public String getMessage(){ + return message; + } + +} + +public class Activity8 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + try { + //Method call with Correct input + Activity8.exceptionTest("Will print to control"); + //Method call with incorrect input + Activity8.exceptionTest(null); + + Activity8.exceptionTest("Won't execute"); + }catch(CustomException ex) { + + ex.printStackTrace(); + + System.out.println("Inside catch block:" +ex.getMessage()); + } + } + + static void exceptionTest(String str)throws CustomException { + // TODO Auto-generated method stub + if(str == null) { + throw new CustomException("String Value is null"); + } + else { + System.out.println(str); + } + } + +}