Skip to content

Commit

Permalink
11b - inheritance and Object
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioBenov committed Nov 5, 2024
1 parent 6d4a9f9 commit 0a89444
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 0 deletions.
29 changes: 29 additions & 0 deletions materials/2024-2025/11b/2024-11-04-object-class/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions materials/2024-2025/11b/2024-11-04-object-class/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
44 changes: 44 additions & 0 deletions materials/2024-2025/11b/2024-11-04-object-class/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Main {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();

System.out.println(o1);
System.out.println(o2);
System.out.println(o1 == o2);
System.out.println(o1.equals(o2));
System.out.println(o1.toString());

Pastry p1 = new Pastry("Prava", false);
Pastry p2 = new Pastry("Sofiiska", true);
Pastry p3 = new Pastry("Prava", false);

System.out.println(p1);
System.out.println(p2);
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));
System.out.println(p1.hashCode());
System.out.println(p2.hashCode());

System.out.println(p1 == p3);
System.out.println(p1.equals(p3));

Map<Pastry, Pastry> map1 = new HashMap<>();
map1.put(p1, p1);
map1.put(p2, p2);
map1.put(p3, p3);
System.out.println(map1);
System.out.println(map1.keySet());

Map<Pastry, Pastry> map2 = new TreeMap<>();
map2.put(p1, p1);
map2.put(p2, p2);
map2.put(p3, p3);
System.out.println(map2);
System.out.println(map2.keySet());
}
}
42 changes: 42 additions & 0 deletions materials/2024-2025/11b/2024-11-04-object-class/src/Pastry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.Objects;

public class Pastry {
private String name;
private boolean hasCheese;

public Pastry(String name, boolean hasCheese) {
this.name = name;
this.hasCheese = hasCheese;
}

public String getName() {
return name;
}

public boolean getHasCheese() {
return hasCheese;
}

@Override
public String toString() {
return "Pastry{" +
"name='" + name + '\'' +
", hasCheese=" + hasCheese +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pastry pastry = (Pastry) o;
return hasCheese == pastry.hasCheese &&
// Objects.equals(name, pastry.name);
name.equals(pastry.name);
}

@Override
public int hashCode() {
return Objects.hash(name, hasCheese);
}
}

0 comments on commit 0a89444

Please sign in to comment.