├── README.md
└── project
├── project.iml
└── src
├── Main.java
└── lk
└── himash
├── Illustration_01.java
├── Illustration_02.java
├── Illustration_03.java
├── Illustration_04.java
├── model
└── Car.java
└── util
└── DB.java
/README.md:
--------------------------------------------------------------------------------
1 | # Core-Java-Patterns
2 |
3 | Includes Java related questions and answer.
4 |
5 | ## Requirements
6 |
7 | 01) Java 17
8 |
9 | ## Project setup
10 |
11 | 01) Clone the project
12 |
13 | https://github.com/himash79/Core-Java-patterns.git
14 |
15 | 02) Figure out the features
16 |
--------------------------------------------------------------------------------
/project/project.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/project/src/Main.java:
--------------------------------------------------------------------------------
1 | // Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
2 | // then press Enter. You can now see whitespace characters in your code.
3 | public class Main {
4 | public static void main(String[] args) {
5 | // Press Alt+Enter with your caret at the highlighted text to see how
6 | // IntelliJ IDEA suggests fixing it.
7 | System.out.printf("Hello and welcome!");
8 |
9 | // Press Shift+F10 or click the green arrow button in the gutter to run the code.
10 | for (int i = 1; i <= 5; i++) {
11 |
12 | // Press Shift+F9 to start debugging your code. We have set one breakpoint
13 | // for you, but you can always add more by pressing Ctrl+F8.
14 | System.out.println("i = " + i);
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/project/src/lk/himash/Illustration_01.java:
--------------------------------------------------------------------------------
1 | package lk.himash;
2 |
3 | import java.util.*;
4 |
5 | public class Illustration_01 {
6 | public static void main(String[] args) {
7 |
8 | // Write a program to find the first non-repeating number in the following array
9 | int [] arr = new int [] {5, 4, 2, 5, 3, 8, 5, 2, 1, 8};
10 | HashSet uniqueValues = new HashSet<>();
11 | List duplicates = new ArrayList<>();
12 | List list = new ArrayList<>();
13 |
14 | for (int j : arr) {
15 | list.add(j);
16 | if (!uniqueValues.add(j)) { // If the value added success true otherwise false
17 | duplicates.add(j);
18 | }
19 | }
20 |
21 | list.removeAll(duplicates);
22 |
23 | System.out.println("Initial non-duplicate value is : " + list.get(0));
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/project/src/lk/himash/Illustration_02.java:
--------------------------------------------------------------------------------
1 | package lk.himash;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashSet;
5 | import java.util.List;
6 | import java.util.Set;
7 |
8 | public class Illustration_02 {
9 | public static void main(String[] args) {
10 |
11 | // Write a program to find the all repeating numbers & non-repeating numbers & remove duplicates in the following array
12 | int [] arr = new int [] {5, 4, 2, 5, 3, 8, 5, 2, 1, 8};
13 | List list = new ArrayList<>();
14 | Set nonRepeatNo = new HashSet<>();
15 | List repeatNo = new ArrayList<>();
16 |
17 | for(int i=0; i= 0; i--) {
13 | out.append(charArr[i]);
14 | }
15 | System.out.println(out);
16 |
17 | StringBuilder strBuilder = new StringBuilder(word);// method 02
18 | System.out.println(strBuilder.reverse());
19 |
20 | StringBuffer strBuffer = new StringBuffer(word);// method 03
21 | System.out.println(strBuffer.reverse());
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/project/src/lk/himash/Illustration_04.java:
--------------------------------------------------------------------------------
1 | package lk.himash;
2 |
3 | import lk.himash.model.Car;
4 | import lk.himash.util.DB;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | public class Illustration_04 {
10 | public static void main(String[] args) {
11 |
12 | List initDetails = DB.fetchCarsIniDetails();
13 | List otherDetails = DB.fetchCarsOtherDetails();
14 | List totalDetails = new ArrayList<>();
15 |
16 | // If the both list contains same count of records
17 | for (int i = 0; i < initDetails.size(); i++) {
18 | Car c = new Car();
19 | c.setId(initDetails.get(i).getId());
20 | c.setManufacture(initDetails.get(i).getManufacture());
21 | c.setModel(initDetails.get(i).getModel());
22 | if(i != otherDetails.size()) {
23 | c.setCost(otherDetails.get(i).getCost());
24 | c.setStatus(otherDetails.get(i).isStatus());
25 | }
26 | totalDetails.add(c);
27 | }
28 |
29 | // If the both list contains different count of records
30 | for (int i = 0; i < initDetails.size(); i++) {
31 | Car c = new Car();
32 | c.setId(initDetails.get(i).getId());
33 | c.setManufacture(initDetails.get(i).getManufacture());
34 | c.setModel(initDetails.get(i).getModel());
35 | totalDetails.add(c);
36 | }
37 | for (int j = 0; j < otherDetails.size(); j++) {
38 | if (j == totalDetails.size()) {
39 | Car car = new Car(otherDetails.get(j).getCost(), otherDetails.get(j).isStatus());
40 | totalDetails.add(car);
41 | } else {
42 | totalDetails.get(j).setCost(otherDetails.get(j).getCost());
43 | totalDetails.get(j).setStatus(otherDetails.get(j).isStatus());
44 | }
45 | }
46 | System.out.println(totalDetails);
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/project/src/lk/himash/model/Car.java:
--------------------------------------------------------------------------------
1 | package lk.himash.model;
2 |
3 | public class Car {
4 |
5 | private int id;
6 | private String manufacture;
7 | private String model;
8 | private double cost;
9 | private boolean status;
10 |
11 | @Override
12 | public String toString() {
13 | return "Car{" +
14 | "id=" + id +
15 | ", manufacture='" + manufacture + '\'' +
16 | ", model='" + model + '\'' +
17 | ", cost=" + cost +
18 | ", status=" + status +
19 | '}';
20 | }
21 |
22 | public Car(int id, String manufacture, String model, double cost, boolean status) {
23 | this.id = id;
24 | this.manufacture = manufacture;
25 | this.model = model;
26 | this.cost = cost;
27 | this.status = status;
28 | }
29 |
30 | public Car() {
31 | }
32 |
33 | public Car(int id, String manufacture, String model) {
34 | this.id = id;
35 | this.manufacture = manufacture;
36 | this.model = model;
37 | }
38 |
39 | public Car(double cost, boolean status) {
40 | this.cost = cost;
41 | this.status = status;
42 | }
43 |
44 | public int getId() {
45 | return id;
46 | }
47 |
48 | public void setId(int id) {
49 | this.id = id;
50 | }
51 |
52 | public String getManufacture() {
53 | return manufacture;
54 | }
55 |
56 | public void setManufacture(String manufacture) {
57 | this.manufacture = manufacture;
58 | }
59 |
60 | public String getModel() {
61 | return model;
62 | }
63 |
64 | public void setModel(String model) {
65 | this.model = model;
66 | }
67 |
68 | public double getCost() {
69 | return cost;
70 | }
71 |
72 | public void setCost(double cost) {
73 | this.cost = cost;
74 | }
75 |
76 | public boolean isStatus() {
77 | return status;
78 | }
79 |
80 | public void setStatus(boolean status) {
81 | this.status = status;
82 | }
83 | }
--------------------------------------------------------------------------------
/project/src/lk/himash/util/DB.java:
--------------------------------------------------------------------------------
1 | package lk.himash.util;
2 |
3 | import lk.himash.model.Car;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | public class DB {
9 |
10 | public static List fetchCarsIniDetails() {
11 | List cars = new ArrayList<>();
12 | Car car1 = new Car(1, "Toyota", "Axio");
13 | Car car2 = new Car(2, "Honda", "Civic");
14 | Car car3 = new Car(3, "Audi", "A5");
15 | Car car4 = new Car(4, "BMW", "320SI");
16 | cars.add(car1);
17 | cars.add(car2);
18 | cars.add(car3);
19 | cars.add(car4);
20 | return cars;
21 | }
22 |
23 | public static List fetchCarsOtherDetails() {
24 | List cars = new ArrayList<>();
25 | Car car1 = new Car(30000.00, false);
26 | Car car2 = new Car(15000.00, true);
27 | Car car3 = new Car(80000.00, false);
28 | cars.add(car1);
29 | cars.add(car2);
30 | cars.add(car3);
31 | return cars;
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------