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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Classes and Objects Exercises
*
* <p>
* Practice creating classes with fields, constructors, and methods.
* Learn about constructor chaining, toString(), and equals().
*/
Expand All @@ -13,30 +13,57 @@ public class ClassesAndObjects {
// TODO: 1 - Create a static inner class called Person with:
// - A private String field 'name'
// - A private int field 'age'


// TODO: 2 - Add a constructor to Person that takes String name and int age,
// and assigns them to the fields.


// TODO: 3 - Add a no-args constructor to Person that sets name to "Unknown"
// and age to 0. Use constructor chaining — call the other constructor
// using this("Unknown", 0) instead of setting fields directly.
// (See TODO 7 for more on constructor chaining.)


// TODO: 4 - Add a toString() method to Person that returns:
// "Person{name='<name>', age=<age>}"
// Annotate with @Override.


// TODO: 5 - Add an equals() method to Person that:
// - Returns true if the other object is a Person with the same name and age
// - Handles null and different types correctly
// - Annotate with @Override
// Hint: use instanceof, then cast and compare fields.
// Also override hashCode() using Objects.hash(name, age).

public static class Person {
private String name;
private int age;

// TODO: 2 - Add a constructor to Person that takes String name and int age,
// and assigns them to the fields.
public Person(String name, int age) {
this.name = name;
this.age = age;
}

// TODO: 3 - Add a no-args constructor to Person that sets name to "Unknown"
// and age to 0. Use constructor chaining — call the other constructor
// using this("Unknown", 0) instead of setting fields directly.
// (See TODO 7 for more on constructor chaining.)
public Person() {
this("Unknown", 0);
}
// TODO: 4 - Add a toString() method to Person that returns:
// "Person{name='<name>', age=<age>}"
// Annotate with @Override.

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


// TODO: 5 - Add an equals() method to Person that:
// - Returns true if the other object is a Person with the same name and age
// - Handles null and different types correctly
// - Annotate with @Override
// Hint: use instanceof, then cast and compare fields.
// Also override hashCode() using Objects.hash(name, age).

@Override
public boolean equals(Object o) {
if(this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}

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

public static void main(String[] args) {
// TODO: 6 - Create at least three Person objects:
Expand All @@ -47,14 +74,24 @@ public static void main(String[] args) {
// Test equals(): compare person1 with person3 (should be true),
// and person1 with person2 (should be false).
// Print the comparison results.
Person alice = new Person("Alice", 30);
Person unknown = new Person();
Person aliceDoppelganger = new Person("Alice", 30);

System.out.println(alice);
System.out.println(unknown);
System.out.println(aliceDoppelganger);

System.out.println(alice.equals(aliceDoppelganger));
System.out.println(alice.equals(unknown));

// TODO: 7 - Demonstrate constructor chaining with this():
// Add a comment explaining what constructor chaining is:
// When a constructor calls another constructor in the same class using this(...),
// it avoids duplicating initialization logic.
// The no-args constructor you created in TODO 3 already demonstrates this.
// Print: "No-args person: " + the no-args person to show the defaults.
System.out.println("No-args person: " + unknown);

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.amigoscode._2_developers._12_classes;

import java.util.Arrays;

/**
* Enum Exercises
*
* <p>
* Practice creating and using enums in Java. Enums are special classes that
* represent a fixed set of constants. They can have fields, constructors,
* and methods just like regular classes.
Expand Down Expand Up @@ -42,13 +44,37 @@ public static void main(String[] args) {
// For each season, print a message like "Spring: Flowers bloom"
// using the getDescription() method.
// Test with Season.SUMMER.
Season season = Season.WINTER;

switch (season) {
case WINTER:
case FALL:
case SUMMER:
case SPRING:
System.out.println(season.getDescription());
break;
default:
System.out.println("Invalid Season");
break;
}


System.out.println("\n=== Iterate Over Enum Values ===");
// TODO: 6 - Use Season.values() to get an array of all Season constants.
// Loop through them and print each one with its description and ordinal.
// Example output: "0: SPRING - Flowers bloom"
// Also iterate over Priority.values() and print each with its level.
Season[] seasons = Season.values();
for(int i = 0; i < seasons.length; i++) {
System.out.println(i + ": " + seasons[i] + " - " + seasons[i].getDescription());
}

System.out.println("============");

Priority[] priorities = Priority.values();
for(int i = 0; i < priorities.length; i++) {
System.out.println(i + ": " + priorities[i] + " - " + priorities[i].getLevel());
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.amigoscode._2_developers._12_classes;

public enum Priority {
LOW(1),
MEDIUM(2),
HIGH(3);

private final int level;

private Priority(int level) {
this.level = level;
}

public int getLevel() {
return level;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/amigoscode/_2_developers/_12_classes/Season.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.amigoscode._2_developers._12_classes;

public enum Season {
WINTER("Snow falls"),
SUMMER("Sun shines"),
SPRING("Flowers bloom"),
FALL("Leaves fall");

private final String description;

private Season(String description) {
this.description = description;
}

public String getDescription() {
return description;
}
}