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
1 change: 1 addition & 0 deletions java-org/classes-and-objects/NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Person {
public Person(String name, int age) {
this.name = name; // 'this' refers to the current object
this.age = age;
return null;
}

// Methods - what a Person object can do
Expand Down
4 changes: 2 additions & 2 deletions java-org/classes-and-objects/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ After completing each exercise, test your classes and methods:

```java
// Example tests
Person person = createPerson("Alice", 30);
System.out.println(person.introduce());
Person Person = createPerson("Alice", 30);
return (person.introduce());

BankAccount account = createBankAccount("123456");
account.deposit(100.0);
Expand Down
242 changes: 179 additions & 63 deletions java-org/classes-and-objects/topic.jsh
Original file line number Diff line number Diff line change
Expand Up @@ -4,129 +4,245 @@ import java.util.ArrayList;

// Exercise 1: Basic Class Creation
// Create a Person class with name and age fields
class Person {
// Your fields here

// Your constructor here

// Your introduce() method here

class Person {
String name;
int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String introduce() {
return "Hello, I'm " + name + " and I'm " + age + " years old.";
}

}

// Exercise 2: Class with Methods
// Create a BankAccount class with account operations
class BankAccount {
// Your fields here

// Your constructor here
String accountNumber;
double balance;

// Your deposit method here

// Your withdraw method here

// Your getBalance method here

// Your getAccountInfo method here
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = 0.0;
}

public void deposit(double amount) {
balance += amount;
}

public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("insufficient funds");
}
}

public double getBalance() {
return balance;
}

public String getAccountInfo() {
return "Hello, " + accountNumber + " your balance is " + balance + " .";
}

}

// Exercise 3: Object Creation and Usage
// Create and return a Person object
public Person createPerson(String name, int age) {
// Your code here

return new Person(name ,age);
}

// Create and return a BankAccount object
public BankAccount createBankAccount(String accountNumber) {
// Your code here
return new BankAccount(accountNumber,0.0);

}

// Demonstrate creating and using a Person object
public void demonstratePersonUsage() {
// Your code here
Person person = new Person(name, age);
System.out.println(Person.introduce());

}

// Exercise 4: Working with Multiple Objects
// Create a Car class
class Car {
// Your fields here

// Your constructor here

// Your getCarInfo method here

// Your isClassic method here (car is classic if > 25 years old)

String brand;
String model;
int year;

public Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}

public String getCarInfo() {
return " Car brand: " + brand + " , Model: " + model + " ,Year made: " + year + ".";
}

public boolean isClassic() {
int currentYear = java.time.Year.now().getValue();
return (currentYear - year) > 25;
}
}

// Compare two cars and return which is older
public Car compareCars(Car car1, Car car2) {
// Your code here
if (car1.year < car2.year) {
return car1;
} else if (car2.year < car1.year) {
return car2;
} else {
return null;
}

}

// Exercise 5: Object State and Behavior
// Create a Counter class that can increment/decrement
class Counter {
// Your fields here

// Your constructor here

// Your increment method here

// Your decrement method here

// Your reset method here

// Your getCount method here

int count;

public Counter() {
this.count = 0;
}

public Counter(int count) {
this.count = count;
}

public int increment() {
count++;
return count;
}

public int decrement() {
count--;
return count;
}

public void reset() {
count = 0;
}

public int getCount() {
return count;
}
}

// Exercise 6: Class with Validation
// Create a Student class with input validation
class Student {
// Your fields here

// Your constructor with validation here

// Your isHonorStudent method here

// Your getStudentInfo method here
String name;
int grade;
double gpa;

public Student( String name, int grade, double gpa) {
this.name = name;
if (grade >= 1 && grade <= 12) {
this.grade = grade;
} else {
System.out.println("Grade must be between 1 and 12");
this.grade = 1;
}
if (gpa >= 0.0 && gpa <= 4.0) {
this.gpa = gpa;
} else {
System.out.println("GPA must be between 0.0 and 4.0");
this.gpa = 0.0;
}
}

public boolean isHonorStudent() {
return gpa >= 3.5;
}

public String getStudentInfo() {
return "Student record: Name: " + name + " Grade: " + grade + " GPA: " + gpa + ".";
}

}

// Exercise 7: Object Interaction
// Create a Book class
class Book {
// Your fields here
String title;
String author;
boolean isCheckedOut;

// Your constructor here
public Book(String title, String author, boolean isCheckedOut) {
this.title = title;
this.author = author;
this.isCheckedOut = isCheckedOut;
}

// Add any helpful methods here

}

// Create a Library class that manages books
class Library {
// Your fields here

// Your constructor here

// Your addBook method here

// Your checkOutBook method here

// Your returnBook method here
ArrayList<Book> books;
String title;

public Library() {
this.title = "Untitled Library";
this.books = new ArrayList<>();
}

// Your getAvailableBooks method here
public Library(String title) {
this.title = title;
this.books = new ArrayList<>();
}

public void addBook(Book book) {
books.add(book);
}

public void checkOutBook(String title) {
for (Book book : books) {
if (book.title.equals(title) && !book.isCheckedOut) {
book.isCheckedOut = true;
break;
}
}
}

public void returnBook(String title) {
for (Book book : books) {
if (book.title.equals(title) && book.isCheckedOut) {
book.isCheckedOut = false;
break;
}
}

}

public String getAvailableBooks() {
StringBuilder available = new StringBuilder();
for (Book book : books) {
if (!book.isCheckedOut) {
if (available.length() > 0) {
available.append(", ");
}
available.append(book.title);
}
}
return available.toString();
}

}

// Test your classes here - uncomment and modify as needed
/*

System.out.println("Testing Person class:");
Person person1 = createPerson("Alice", 30);
Person person2 = new Person("Bob", 25);
Expand Down Expand Up @@ -192,4 +308,4 @@ System.out.println("After checking out 1984: " + library.getAvailableBooks());

library.returnBook("1984");
System.out.println("After returning 1984: " + library.getAvailableBooks());
*/

Loading