diff --git a/src/main/java/com/amigoscode/_1_beginners/_1_thebasics/Variables.java b/src/main/java/com/amigoscode/_1_beginners/_1_thebasics/Variables.java deleted file mode 100644 index 3d2ef3b..0000000 --- a/src/main/java/com/amigoscode/_1_beginners/_1_thebasics/Variables.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.amigoscode._1_beginners._1_thebasics; - -/** - * Exercise: Variables and Data Types - * - * Learn how to declare and use variables of different types in Java. - * Java is a statically-typed language, meaning every variable must have a declared type. - */ -public class Variables { - - public static void main(String[] args) { - - // TODO: 1 - Declare an int variable called age and assign it the value 25 - - - // TODO: 2 - Declare a double variable called price and assign it the value 9.99 - - - // TODO: 3 - Declare a boolean variable called isJavaFun and assign it the value true - - - // TODO: 4 - Declare a String variable called name and assign it your name - - - // TODO: 5 - Declare a char variable called grade and assign it the value 'A' - - - // TODO: 6 - Print all the variables above using System.out.println - // Hint: You can print each variable on its own line, e.g.: - // System.out.println("Age: " + age); - - - // TODO: 7 - Declare a final (constant) variable called MAX_SCORE, set it to 100, and print it - // Hint: Use the 'final' keyword before the type to make a constant - - } -} diff --git a/src/main/java/com/amigoscode/_2_developers/_12_classes/ClassesAndObjects.java b/src/main/java/com/amigoscode/_2_developers/_12_classes/ClassesAndObjects.java index 12a7951..c919fc9 100644 --- a/src/main/java/com/amigoscode/_2_developers/_12_classes/ClassesAndObjects.java +++ b/src/main/java/com/amigoscode/_2_developers/_12_classes/ClassesAndObjects.java @@ -4,7 +4,7 @@ /** * Classes and Objects Exercises - * + *
* Practice creating classes with fields, constructors, and methods.
* Learn about constructor chaining, toString(), and equals().
*/
@@ -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='
* 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.
@@ -42,6 +44,19 @@ 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 ===");
@@ -49,6 +64,17 @@ public static void main(String[] args) {
// 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());
+ }
}
}
diff --git a/src/main/java/com/amigoscode/_2_developers/_12_classes/Priority.java b/src/main/java/com/amigoscode/_2_developers/_12_classes/Priority.java
new file mode 100644
index 0000000..3960095
--- /dev/null
+++ b/src/main/java/com/amigoscode/_2_developers/_12_classes/Priority.java
@@ -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;
+ }
+}
diff --git a/src/main/java/com/amigoscode/_2_developers/_12_classes/Season.java b/src/main/java/com/amigoscode/_2_developers/_12_classes/Season.java
new file mode 100644
index 0000000..9856ad6
--- /dev/null
+++ b/src/main/java/com/amigoscode/_2_developers/_12_classes/Season.java
@@ -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;
+ }
+}