Skip to content

Commit ff49a94

Browse files
committed
add more core java concepts
1 parent 8c399aa commit ff49a94

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+533
-10
lines changed

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.name

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/Java Problems and Solves.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/Java_Problems_and_Solves.xml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ComparableAndComparator;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class Book implements Comparable<Book> {
8+
private String title;
9+
private String author;
10+
private int price;
11+
12+
public String getTitle() {
13+
return title;
14+
}
15+
16+
public Book(String title, String author, int price) {
17+
this.title = title;
18+
this.author = author;
19+
this.price = price;
20+
}
21+
22+
@Override
23+
public int compareTo(Book o) {
24+
return price - (o.price);
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "Book{" +
30+
"title='" + title + '\'' +
31+
'}';
32+
}
33+
34+
public static void main(String[] args) {
35+
Book b1 = new Book("Java", "a", 200);
36+
Book b2 = new Book("Python", "b", 150);
37+
List<Book> booklist = new ArrayList<>();
38+
booklist.add(b1);
39+
booklist.add(b2);
40+
//Collections.sort(booklist);
41+
Collections.sort(booklist, new TitleComparator());
42+
System.out.println(Collections.binarySearch(booklist, b1));
43+
44+
System.out.println(booklist);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ComparableAndComparator;
2+
3+
import java.util.Comparator;
4+
5+
public class TitleComparator implements Comparator<Book> {
6+
@Override
7+
public int compare(Book o1, Book o2) {
8+
return o1.getTitle().compareTo(o2.getTitle());
9+
}
10+
}

More Core Java Concepts to Know/Core Java Questions.txt

+279
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package DefaultAndStaticMethodsInInterfaces;
2+
3+
public class Truck implements Vehicle {
4+
@Override
5+
public void start() {
6+
System.out.println("Truck has started");
7+
}
8+
9+
@Override
10+
public void stop() {
11+
System.out.println("Truck has stopped");
12+
}
13+
14+
public static void main(String []args) {
15+
Vehicle truck = new Truck();
16+
17+
truck.start();
18+
truck.honk();
19+
truck.stop();
20+
Vehicle.inspect();
21+
}
22+
}
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package DefaultAndStaticMethodsInInterfaces;
2+
3+
public interface Vehicle {
4+
void start();
5+
6+
void stop();
7+
8+
default void honk() {
9+
System.out.println("Vehicle Honk!!!");
10+
}
11+
12+
static void inspect() {
13+
System.out.println("Vehicle Inspect...");
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package FunctionalInterfaceAndLambdaExpressions;
2+
3+
public interface Calculator {
4+
int calculate(int a, int b);
5+
}
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package FunctionalInterfaceAndLambdaExpressions;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
add(( a, b) -> { return a+b; } ); // Using Lambda Expression
6+
add(Integer::sum); // Using Method Reference
7+
}
8+
9+
public static void add(Calculator calculator) {
10+
System.out.println(calculator.calculate(3,2));
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package MultipleInterfaceImplementation;
2+
3+
public class Duck implements Flyable, Swimmable {
4+
public Duck() {
5+
}
6+
7+
@Override
8+
public void fly() {
9+
System.out.println("Duck is Flying");
10+
}
11+
12+
@Override
13+
public void swim() {
14+
System.out.println("Duck is Swimming");
15+
}
16+
17+
public static void main(String []args) {
18+
Duck duck = new Duck();
19+
20+
duck.fly();
21+
duck.swim();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package MultipleInterfaceImplementation;
2+
3+
public interface Flyable {
4+
void fly();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package MultipleInterfaceImplementation;
2+
3+
public interface Swimmable {
4+
void swim();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package UseOfAbstractMethodsInInterfaces;
2+
3+
public class Circle implements Shape {
4+
public final double PI = 3.1416;
5+
public double radius;
6+
7+
public Circle(double radius) {
8+
this.radius = radius;
9+
}
10+
11+
@Override
12+
public double calculateArea() {
13+
return PI+radius*radius;
14+
}
15+
16+
@Override
17+
public double calculatePerimeter() {
18+
return 2*PI*radius;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package UseOfAbstractMethodsInInterfaces;
2+
3+
public class Main {
4+
public static void main(String []args) {
5+
Shape circle = new Circle(4);
6+
Shape square = new Square(2);
7+
8+
System.out.println(circle.calculatePerimeter());
9+
System.out.println(circle.calculateArea());
10+
11+
System.out.println(square.calculateArea());
12+
System.out.println(square.calculatePerimeter());
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package UseOfAbstractMethodsInInterfaces;
2+
3+
public interface Shape {
4+
double calculateArea();
5+
double calculatePerimeter();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package UseOfAbstractMethodsInInterfaces;
2+
3+
public class Square implements Shape {
4+
public double side;
5+
6+
public Square(double side) {
7+
this.side = side;
8+
}
9+
10+
@Override
11+
public double calculateArea() {
12+
return side*side;
13+
}
14+
15+
@Override
16+
public double calculatePerimeter() {
17+
return 4*side;
18+
}
19+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Task15/Greeting.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ interface Greeting {
55
}
66

77
class Task15 {
8-
public static void main(String args[]) {
8+
public static void main(String []args) {
99

10-
Greeting greeting= new Greeting(){
11-
public void sayHello(){
10+
greet(new Greeting() { // Using Anonymous Inner Class
11+
@Override
12+
public void sayHello() {
1213
System.out.println("Hello");
1314
}
14-
};
15-
// public void greet(Greeting greeting){
16-
// greeting.sayHello();
17-
// }
15+
});
1816

17+
greet(() -> System.out.println("hello")); // Using Lambda expression
1918

20-
greeting.sayHello();
21-
22-
// greet(greeting);
19+
}
2320

21+
public static void greet(Greeting greeting){
22+
greeting.sayHello();
2423
}
2524
}
File renamed without changes.

0 commit comments

Comments
 (0)