Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Java/Activities/Activity1.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
36 changes: 36 additions & 0 deletions Java/Activities/Activity10.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package activities;

import java.util.HashSet;
import java.util.Set;

public class Activity10 {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
// 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);
}
}
37 changes: 37 additions & 0 deletions Java/Activities/Activity11.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package activities;

import java.util.HashMap;

public class Activity11 {

public static void main(String[] args) {
HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
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));
}
}
}
22 changes: 22 additions & 0 deletions Java/Activities/Activity12.java
Original file line number Diff line number Diff line change
@@ -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));
}

}
31 changes: 31 additions & 0 deletions Java/Activities/Activity13.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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();
}

}
41 changes: 41 additions & 0 deletions Java/Activities/Activity14.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
39 changes: 39 additions & 0 deletions Java/Activities/Activity2.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
6 changes: 6 additions & 0 deletions Java/Activities/Activity3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package activities;

public class Activity3 {


}
34 changes: 34 additions & 0 deletions Java/Activities/Activity4.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
37 changes: 37 additions & 0 deletions Java/Activities/Activity5.java
Original file line number Diff line number Diff line change
@@ -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;
}
}


62 changes: 62 additions & 0 deletions Java/Activities/Activity6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package activities;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

class Plane {
private List<String> 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<String> 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());
}
}
Loading