Unit 2: Using Objects
Click to expand
In unit 2, I learned about object-oriented programming concepts in Java, specifically focusing on classes, objects, and methods. The exercises included creating classes with fields and methods, instantiating objects from these classes, and calling methods to perform specific actions. Additionally, I practiced writing constructors to initialize object state and using accessor and mutator methods to get and set field values. This unit emphasized the importance of understanding classes and objects to model real-world entities and implement behavior in Java programs.
class Dog {
private String name;
private String breed;
private int age;
public String noise = "Woof!";
// Constructor to initialize the dog's name, breed, and age
public Dog(String name, String breed, int age) {
this.name = name;
this.breed = breed;
this.age = age;
}
// Instance method to make the dog bark
public void bark() {
System.out.println(noise);
}
}
public class Main {
public static void main(String[] args) {
// Create a Dog object with name, breed, and age
Dog myDog = new Dog("Shelby", "Golden Retriever", 5);
// Call the bark method to print "Woof!"
myDog.bark();
}
}
Main.main(new String[0]);
Unit 3: Booleans/If Conditions
Click to expand
In unit 3 I learned about control structures in Java, specifically focusing on loops and conditional statements. The exercises included writing and debugging various types of loops such as for, while, and do-while loops, as well as using if-else statements to control the flow of the program. This unit emphasized the importance of understanding loop constructs to iterate over arrays and collections efficiently and using conditional logic to make decisions within the code.
// Example of a for loop with an if-else statement
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
System.out.println(i + " is even");
} else {
System.out.println(i + " is odd");
}
}
Unit 4: Loops
Click to expand
In this unit, I learned about loops in Java, specifically focusing on while loops and for loops. The exercises included identifying and fixing common errors such as missing increments in while loops and understanding off-by-one errors in array traversal. Additionally, I practiced writing nested loops and explored different loop constructs to iterate over arrays and collections effectively. This unit reinforced the importance of proper loop control to avoid infinite loops and ensure accurate data processing.
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Unit 5: Writing Classes
Click to expand
In unit 5, I learned about object-oriented programming concepts in Java, specifically focusing on classes, constructors, accessor and mutator methods, and basic encapsulation principles. The exercises included creating a Student class with private fields for name and score, implementing default and overloaded constructors, and writing accessor and mutator methods to get and set the values of these fields. Additionally, I implemented a method to check if a student passes based on their score. This unit emphasized the importance of encapsulation and proper use of constructors and methods to manage object state.
public class Student {
private String name;
private int score;
// Default constructor
public Student() {
name = "";
score = 0;
}
// Overloaded constructor
public Student(String n, int s) {
name = n;
score = s;
}
// Accessor Methods
public String getName() {
return name;
}
public int getScore(){
return score;
}
// Mutator Methods
public void setName(String n){
name = n;
}
public void setScore(int s){
if(s < 0 || s > 100){
score = 0; // default value
}
else {
score = s;
}
}
public boolean pass(){
return (score >= 70);
}
}
Unit 6: Arrays
Click to expand
In unit 6, I learned about arrays in Java, focusing on creating, initializing, and manipulating arrays of different data types. The exercises included writing code to find the maximum and minimum values in an array, calculating the sum and average of array elements, and sorting arrays using selection sort. Additionally, I practiced working with multi-dimensional arrays and understanding the differences between arrays and array lists. This unit emphasized the importance of understanding array indexing, traversal, and manipulation to efficiently store and process data in Java programs.
public class MaxMinInArray {
public static void findMaxAndMin(int[] array) {
if (array == null || array.length == 0) {
System.out.println("Array is empty");
return;
}
int max = array[0];
int min = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
// The loop starts from the second element of the array (i = 1) and compares it with the first element (i = 0).
System.out.println("Maximum value: " + max);
System.out.println("Minimum value: " + min);
}
public static void main(String[] args) {
int[] array = {3, 5, 7, 2, 8, -1, 4, 10, 12};
findMaxAndMin(array);
}
}
MaxMinInArray.main(null)
Unit 7: ArrayLists
Click to expand
In unit 7, I learned about ArrayLists in Java, focusing on creating, initializing, and manipulating dynamic arrays using the ArrayList class. The exercises included adding, removing, and updating elements in an ArrayList, iterating over ArrayList elements using for-each loops, and sorting ArrayLists using the Collections class. Additionally, I practiced working with ArrayList methods such as add, remove, get, and size to manage collections of objects efficiently. This unit emphasized the importance of using ArrayLists to store and process data dynamically in Java programs.
ArrayList<String> arrayList;
arrayList = new ArrayList<>(Arrays.asList("apple", "banana", "cherry", "date", "elderberry"));
System.out.println(arrayList);
System.out.println(arrayList.size());
// add fig to it
arrayList.add("fig");
System.out.println(arrayList);
// add grape to index 2
arrayList.add(2, "grape");
System.out.println(arrayList);
// replace index 4 with guava
arrayList.set(4, "guava");
System.out.println(arrayList);
// print element at index 3
System.out.println(arrayList.get(3));
Unit 9: Inheritance
Click to expand
In unit 9, I learned about inheritance in Java, focusing on creating subclasses that inherit fields and methods from a superclass. The exercises included defining parent and child classes, using the super keyword to call superclass constructors and methods, and overriding methods in child classes to provide specific implementations. Additionally, I practiced understanding the concept of polymorphism and dynamic method dispatch in Java programs. This unit emphasized the importance of inheritance to promote code reuse and create class hierarchies for modeling real-world entities.
public class Vehicle {
float miles = 0;
float milesTraveled(float milesToAdd){
this.miles += milesToAdd;
return this.miles;
}
boolean hasMovement = true;
}
public class Car extends Vehicle {
boolean canFly = true;
}
public class Plane extends Vehicle {
boolean canFly = true;
}
Car myCar = new Car();
System.out.println("As you can see my Car also has the inherited varible hasMovement");
System.out.println("myCar.hasMovement: " + (new Boolean(myCar.hasMovement)).toString());