diff --git a/JavaCourse/src/main/java/com/rinftech/Ex10.java b/JavaCourse/src/main/java/com/rinftech/Ex10.java new file mode 100644 index 0000000..3749a91 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Ex10.java @@ -0,0 +1,32 @@ +package com.rinftech; + +import java.util.*; + +public class Ex10 { + public static void main(String[] args) { + String[] input = {"eat", "tea", "tan", "ate", "nat", "bat", "listen", "silent", "enlist", "hello", "world", "dlrow"}; + Map> anagrams = groupAnagrams(input); + + for(List anagram:anagrams.values()) + { + System.out.println(anagram); + } + } + + public static Map> groupAnagrams(String[] input) { + Map> anagrams = new HashMap<>(); + + for (String str : input) { + char[] arrChar = str.toCharArray(); + Arrays.sort(arrChar); + String sortedLettersWord = new String(arrChar); + + if (!anagrams.containsKey(sortedLettersWord)) { + anagrams.put(sortedLettersWord, new ArrayList<>()); + } + anagrams.get(sortedLettersWord).add(str); + } + + return anagrams; + } +} \ No newline at end of file diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex1.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex1.java new file mode 100644 index 0000000..1995e74 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex1.java @@ -0,0 +1,34 @@ +package com.rinftech.Exercices; + +public class Ex1 +{ + private static int sumOfDigits(int n) + { + int sum = 0; + + while(n != 0) + { + sum += n%10; + n /= 10; + } + + return sum; + } + + public static void main(String[] args) + { + int n, sum; + + n = 8733; + sum = sumOfDigits(n); + System.out.println("Sum of digits of " + n + " = " + sum); + + n = 123456789; + sum = sumOfDigits(n); + System.out.println("Sum of digits of " + n + " = " + sum); + + n = 29293129; + sum = sumOfDigits(n); + System.out.println("Sum of digits of " + n + " = " + sum); + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex2.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex2.java new file mode 100644 index 0000000..ea40dd7 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex2.java @@ -0,0 +1,39 @@ +package com.rinftech.Exercices; + +import java.util.HashMap; +import java.util.Map; + +public class Ex2 +{ + private static Map countCharacters(String s) + { + Map counterMap = new HashMap<>(); + counterMap.put("Letters", 0); + counterMap.put("Spaces", 0); + counterMap.put("Numbers", 0); + + for(int i = 0;i='a' && c<='z') || (c>='A' && c<='Z')) + { + counterMap.put("Letters",counterMap.get("Letters") + 1); + } + else if(c>='0' && c<='9') + { + counterMap.put("Numbers",counterMap.get("Numbers") +1); + } + } + return counterMap; + } + + public static void main(String[] args) + { + String input = "The quick brown fox jumps over 42 lazy dogs"; + System.out.println("String statistics: " + countCharacters(input)); + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Employee.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Employee.java new file mode 100644 index 0000000..77eaf38 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Employee.java @@ -0,0 +1,22 @@ +package com.rinftech.Exercices.Ex3; + +public class Employee extends Person +{ + private int employeeId; + private String jobTitle; + + public Employee(String firstName, String lastName, int employeeId, String jobTitle) { + super(firstName, lastName); + this.employeeId = employeeId; + this.jobTitle = jobTitle; + } + + public int getEmployeeId() { + return employeeId; + } + + @Override + public String getLastName() { + return super.getLastName() + ", jobTitle: " + this.jobTitle; + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Ex3.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Ex3.java new file mode 100644 index 0000000..1215b3c --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Ex3.java @@ -0,0 +1,13 @@ +package com.rinftech.Exercices.Ex3; + +public class Ex3 +{ + public static void main(String[] args) + { + Person person = new Person("John", "Doe"); + System.out.println("Person " + person.getFirstName() + " " + person.getLastName()); + + Employee employee = new Employee("Jane","Doe",1234,"JobTitle"); + System.out.println("Employee " + employee.getFirstName() + " " + employee.getLastName()); + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Person.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Person.java new file mode 100644 index 0000000..ec6bc7a --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Person.java @@ -0,0 +1,20 @@ +package com.rinftech.Exercices.Ex3; + +public class Person +{ + private String firstName; + private String lastName; + + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Car.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Car.java new file mode 100644 index 0000000..bb8c331 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Car.java @@ -0,0 +1,31 @@ +package com.rinftech.Exercices.Ex4; + +public class Car extends Vehicle +{ + private int seats; + + public Car(String make, String model, int year, String fuelType, int seats) { + super(make, model, year, fuelType); + this.seats = seats; + } + + public int getSeats() { + return seats; + } + + public void setSeats(int seats) { + this.seats = seats; + } + + @Override + public double calculateFuelEfficiency(double distance, double fuelConsumed) { + return 5.4; + } + + @Override + public String toString() { + return "Car{" + + "seats=" + seats + + '}'; + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Ex4.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Ex4.java new file mode 100644 index 0000000..75174e0 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Ex4.java @@ -0,0 +1,19 @@ +package com.rinftech.Exercices.Ex4; + +public class Ex4 +{ + public static void main(String[] args) + { + Vehicle vehicle = new Vehicle("SomeMake","SomeModel",2000,"SomeFuelType"); + System.out.println(vehicle); + + Car car = new Car("CarMake","CarModel",2002,"CarFuelType",5); + System.out.println("Car's fuel efficiency: " + car.calculateFuelEfficiency(2.3,2)); + + Truck truck = new Truck("TruckMake","TruckModel",2004,"TruckFuelType",2500); + System.out.println("Truck's max speed: " + truck.getMaxSpeed()); + + Motorcycle motorcycle = new Motorcycle("Kawasaki","Ninja H2R",2022,"Diesel",false); + System.out.println("Motorcycle's distance: " + motorcycle.calculateDistance(25.5,30)); + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Motorcycle.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Motorcycle.java new file mode 100644 index 0000000..8afd71b --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Motorcycle.java @@ -0,0 +1,31 @@ +package com.rinftech.Exercices.Ex4; + +public class Motorcycle extends Vehicle +{ + private boolean hasSidecar; + + public Motorcycle(String make, String model, int year, String fuelType, boolean hasSidecar) { + super(make, model, year, fuelType); + this.hasSidecar = hasSidecar; + } + + public boolean isHasSidecar() { + return hasSidecar; + } + + public void setHasSidecar(boolean hasSidecar) { + this.hasSidecar = hasSidecar; + } + + @Override + public double calculateDistance(double speed, double time) { + return 27.5; + } + + @Override + public String toString() { + return "Motorcycle{" + + "hasSidecar=" + hasSidecar + + '}'; + } +} \ No newline at end of file diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Truck.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Truck.java new file mode 100644 index 0000000..ea4cf9d --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Truck.java @@ -0,0 +1,31 @@ +package com.rinftech.Exercices.Ex4; + +public class Truck extends Vehicle +{ + private double capacity; + + public Truck(String make, String model, int year, String fuelType, double capacity) { + super(make, model, year, fuelType); + this.capacity = capacity; + } + + public double getCapacity() { + return capacity; + } + + public void setCapacity(double capacity) { + this.capacity = capacity; + } + + @Override + public double getMaxSpeed() { + return 100; + } + + @Override + public String toString() { + return "Truck{" + + "capacity=" + capacity + + '}'; + } +} \ No newline at end of file diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Vehicle.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Vehicle.java new file mode 100644 index 0000000..7dc2cfa --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Vehicle.java @@ -0,0 +1,73 @@ +package com.rinftech.Exercices.Ex4; + +public class Vehicle +{ + private String make; + private String model; + private int year; + private String fuelType; + + public Vehicle(String make, String model, int year, String fuelType) { + this.make = make; + this.model = model; + this.year = year; + this.fuelType = fuelType; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public String getFuelType() { + return fuelType; + } + + public void setFuelType(String fuelType) { + this.fuelType = fuelType; + } + + public double calculateFuelEfficiency(double distance, double fuelConsumed) + { + return distance / fuelConsumed; + } + + public double calculateDistance(double speed, double time) + { + return speed * time; + } + + public double getMaxSpeed() + { + return 0; + } + + @Override + public String toString() { + return "Vehicle{" + + "make='" + make + '\'' + + ", model='" + model + '\'' + + ", year=" + year + + ", fuelType='" + fuelType + '\'' + + '}'; + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex5.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex5.java new file mode 100644 index 0000000..b43d374 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex5.java @@ -0,0 +1,32 @@ +package com.rinftech.Exercices; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class Ex5 +{ + public static void main(String[] args) + { + String[] firstArray = { "Python", "Java", "GoLang", "Lisp" }; + String[] secondArray = { "Java", "C++", "Apples", "Hamburger", "Lisp"}; + + Set firstSet = new HashSet<>(); + Set secondSet = new HashSet<>(); + + for(String str:firstArray) + { + firstSet.add(str); + } + + for (String str:secondArray) + { + secondSet.add(str); + } + + firstSet.retainAll(secondSet); + + System.out.println("Common elements between the arrays: " + firstSet); + + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Circle.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Circle.java new file mode 100644 index 0000000..8e3f9ce --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Circle.java @@ -0,0 +1,23 @@ +package com.rinftech.Exercices.Ex6; + +public class Circle extends Shape +{ + private double radius; + + public Circle(double radius) { + this.radius = radius; + } + + public double getRadius() { + return radius; + } + + public void setRadius(double radius) { + this.radius = radius; + } + + @Override + public double calculateArea() { + return Math.PI * radius * radius; + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Ex6.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Ex6.java new file mode 100644 index 0000000..9e58975 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Ex6.java @@ -0,0 +1,15 @@ +package com.rinftech.Exercices.Ex6; + +public class Ex6 +{ + public static void main(String[] args) + { + Circle circle = new Circle(10); + Rectangle rectangle = new Rectangle(4, 6); + Triangle triangle = new Triangle(10,6,8); + + System.out.println("Circle's area = " + circle.calculateArea()); + System.out.println("Rectangle's area = " + rectangle.calculateArea()); + System.out.println("Triangle's area = " + triangle.calculateArea()); + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Rectangle.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Rectangle.java new file mode 100644 index 0000000..21df1ac --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Rectangle.java @@ -0,0 +1,32 @@ +package com.rinftech.Exercices.Ex6; + +public class Rectangle extends Shape +{ + private double length, width; + + public Rectangle(double length, double width) { + this.length = length; + this.width = width; + } + + public double getLength() { + return length; + } + + public void setLength(double length) { + this.length = length; + } + + public double getWidth() { + return width; + } + + public void setWidth(double width) { + this.width = width; + } + + @Override + public double calculateArea() { + return length * width; + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Shape.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Shape.java new file mode 100644 index 0000000..0974767 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Shape.java @@ -0,0 +1,9 @@ +package com.rinftech.Exercices.Ex6; + +public class Shape +{ + public double calculateArea() + { + return 0.0; + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Triangle.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Triangle.java new file mode 100644 index 0000000..66dad68 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex6/Triangle.java @@ -0,0 +1,44 @@ +package com.rinftech.Exercices.Ex6; + +public class Triangle extends Shape +{ + private double l1, l2, l3; + + public Triangle(double l1, double l2, double l3) { + this.l1 = l1; + this.l2 = l2; + this.l3 = l3; + } + + public double getL1() { + return l1; + } + + public void setL1(double l1) { + this.l1 = l1; + } + + public double getL2() { + return l2; + } + + public void setL2(double l2) { + this.l2 = l2; + } + + public double getL3() { + return l3; + } + + public void setL3(double l3) { + this.l3 = l3; + } + + @Override + public double calculateArea() { + double s = this.l1 + this.l2 + this.l3; + s = s/2; + + return Math.sqrt(s * (s - this.l1) * (s - this.l2) * (s - this.l3)); + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Animal.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Animal.java new file mode 100644 index 0000000..94ad1a0 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Animal.java @@ -0,0 +1,14 @@ +package com.rinftech.Exercices.Ex7; + +public class Animal +{ + public void eat() + { + System.out.println("This animal eats something"); + } + + public void sound() + { + System.out.println("This animal makes a sound and it is very loud"); + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Ex7.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Ex7.java new file mode 100644 index 0000000..51116d0 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Ex7.java @@ -0,0 +1,26 @@ +package com.rinftech.Exercices.Ex7; + +public class Ex7 +{ + public static void main(String[] args) + { + Lion lion = new Lion(); + Tiger tiger = new Tiger(); + Panther panther = new Panther(); + + System.out.println("Lion:"); + lion.eat(); + lion.sound(); + System.out.println(); + + System.out.println("Tiger:"); + tiger.eat(); + tiger.sound(); + System.out.println(); + + System.out.println("Panther:"); + panther.eat(); + panther.sound(); + System.out.println(); + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Lion.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Lion.java new file mode 100644 index 0000000..0ee7350 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Lion.java @@ -0,0 +1,14 @@ +package com.rinftech.Exercices.Ex7; + +public class Lion extends Animal +{ + @Override + public void eat() { + System.out.println("This lion eats a gazelle"); + } + + @Override + public void sound() { + System.out.println("The lion roars. Roar :3"); + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Panther.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Panther.java new file mode 100644 index 0000000..52ccfd5 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Panther.java @@ -0,0 +1,14 @@ +package com.rinftech.Exercices.Ex7; + +public class Panther extends Animal +{ + @Override + public void eat() { + System.out.println("This panther ate some meat"); + } + + @Override + public void sound() { + System.out.println("...how does a panther sound like?"); + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Tiger.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Tiger.java new file mode 100644 index 0000000..58e1151 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex7/Tiger.java @@ -0,0 +1,14 @@ +package com.rinftech.Exercices.Ex7; + +public class Tiger extends Animal +{ + @Override + public void eat() { + System.out.println("This tiger hasn't eaten anything in a while : ("); + } + + @Override + public void sound() { + System.out.println("The tiger meows. It's just a big orange cat lmao"); + } +} diff --git a/JavaCourse/src/main/java/com/rinftech/Exercices/Ex8.java b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex8.java new file mode 100644 index 0000000..92a58a5 --- /dev/null +++ b/JavaCourse/src/main/java/com/rinftech/Exercices/Ex8.java @@ -0,0 +1,27 @@ +package com.rinftech.Exercices; + +public class Ex8 +{ + public static void main(String[] args) + { + String[] input = {"Red", "Green", "Black", "White", "Pink"}; + int index1 = 1; + int index2 = 4; + + String[] output = swapStrings(input, index1, index2); + + for (int i = 0;i input = new ArrayList<>(); + input = Arrays.asList("Red", "Green", "Black", "White", "Pink", "Green", "Green", "Black", "Yellow", "White"); + String element = "Green"; + + getFirstAndLastOccurences(input, element); + } + + private static void getFirstAndLastOccurences(List input, String element) + { + int firstOccurence = input.indexOf(element); + int lastOccurence = input.lastIndexOf(element); + System.out.println("First occurence = " + firstOccurence + " and last occurence = " + lastOccurence); + } +}