Skip to content
Open
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
32 changes: 32 additions & 0 deletions JavaCourse/src/main/java/com/rinftech/Ex10.java
Original file line number Diff line number Diff line change
@@ -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<String, List<String>> anagrams = groupAnagrams(input);

for(List<String> anagram:anagrams.values())
{
System.out.println(anagram);
}
}

public static Map<String, List<String>> groupAnagrams(String[] input) {
Map<String, List<String>> 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;
}
}
34 changes: 34 additions & 0 deletions JavaCourse/src/main/java/com/rinftech/Exercices/Ex1.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
39 changes: 39 additions & 0 deletions JavaCourse/src/main/java/com/rinftech/Exercices/Ex2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.rinftech.Exercices;

import java.util.HashMap;
import java.util.Map;

public class Ex2
{
private static Map<String,Integer> countCharacters(String s)
{
Map<String,Integer> counterMap = new HashMap<>();
counterMap.put("Letters", 0);
counterMap.put("Spaces", 0);
counterMap.put("Numbers", 0);

for(int i = 0;i<s.length();i++)
{
char c = s.charAt(i);
if(c == ' ')
{
counterMap.put("Spaces", counterMap.get("Spaces") + 1);
}
else if ((c>='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));
}
}
22 changes: 22 additions & 0 deletions JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Employee.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 13 additions & 0 deletions JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Ex3.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
20 changes: 20 additions & 0 deletions JavaCourse/src/main/java/com/rinftech/Exercices/Ex3/Person.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
31 changes: 31 additions & 0 deletions JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Car.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
19 changes: 19 additions & 0 deletions JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Ex4.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
31 changes: 31 additions & 0 deletions JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Truck.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
73 changes: 73 additions & 0 deletions JavaCourse/src/main/java/com/rinftech/Exercices/Ex4/Vehicle.java
Original file line number Diff line number Diff line change
@@ -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 + '\'' +
'}';
}
}
32 changes: 32 additions & 0 deletions JavaCourse/src/main/java/com/rinftech/Exercices/Ex5.java
Original file line number Diff line number Diff line change
@@ -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<String> firstSet = new HashSet<>();
Set<String> 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);

}
}
Loading