-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtopic.jsh
More file actions
195 lines (138 loc) · 5.09 KB
/
Copy pathtopic.jsh
File metadata and controls
195 lines (138 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Classes and Objects Exercises - Complete the classes and methods below
// Save this file and load it in jshell with: /open topic.jsh
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
}
// Exercise 2: Class with Methods
// Create a BankAccount class with account operations
class BankAccount {
// Your fields here
// Your constructor here
// Your deposit method here
// Your withdraw method here
// Your getBalance method here
// Your getAccountInfo method here
}
// Exercise 3: Object Creation and Usage
// Create and return a Person object
public Person createPerson(String name, int age) {
// Your code here
}
// Create and return a BankAccount object
public BankAccount createBankAccount(String accountNumber) {
// Your code here
}
// Demonstrate creating and using a Person object
public void demonstratePersonUsage() {
// Your code here
}
// 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)
}
// Compare two cars and return which is older
public Car compareCars(Car car1, Car car2) {
// Your code here
}
// 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
}
// 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
}
// Exercise 7: Object Interaction
// Create a Book class
class Book {
// Your fields here
// Your constructor here
// 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
// Your getAvailableBooks method here
}
// 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);
System.out.println(person1.introduce());
System.out.println(person2.introduce());
System.out.println("\nTesting BankAccount class:");
BankAccount account = createBankAccount("123456");
System.out.println("Initial: " + account.getAccountInfo());
account.deposit(100.0);
System.out.println("After deposit: Balance = " + account.getBalance());
account.withdraw(30.0);
System.out.println("After withdrawal: Balance = " + account.getBalance());
account.withdraw(100.0); // Should fail - insufficient funds
System.out.println("Final: " + account.getAccountInfo());
System.out.println("\nTesting Car class:");
Car car1 = new Car("Toyota", "Camry", 1995);
Car car2 = new Car("Honda", "Civic", 2020);
System.out.println(car1.getCarInfo());
System.out.println("Is classic: " + car1.isClassic());
System.out.println(car2.getCarInfo());
System.out.println("Is classic: " + car2.isClassic());
Car older = compareCars(car1, car2);
System.out.println("Older car: " + older.getCarInfo());
System.out.println("\nTesting Counter class:");
Counter counter = new Counter();
System.out.println("Initial count: " + counter.getCount());
counter.increment();
counter.increment();
counter.increment();
System.out.println("After 3 increments: " + counter.getCount());
counter.decrement();
System.out.println("After 1 decrement: " + counter.getCount());
counter.reset();
System.out.println("After reset: " + counter.getCount());
System.out.println("\nTesting Student class:");
try {
Student student1 = new Student("John", 10, 3.8);
System.out.println(student1.getStudentInfo());
System.out.println("Is honor student: " + student1.isHonorStudent());
Student student2 = new Student("Jane", 12, 2.5);
System.out.println(student2.getStudentInfo());
System.out.println("Is honor student: " + student2.isHonorStudent());
} catch (Exception e) {
System.out.println("Validation error: " + e.getMessage());
}
System.out.println("\nTesting Library system:");
Library library = new Library();
Book book1 = new Book("1984", "George Orwell", false);
Book book2 = new Book("To Kill a Mockingbird", "Harper Lee", false);
library.addBook(book1);
library.addBook(book2);
System.out.println("Available books: " + library.getAvailableBooks());
library.checkOutBook("1984");
System.out.println("After checking out 1984: " + library.getAvailableBooks());
library.returnBook("1984");
System.out.println("After returning 1984: " + library.getAvailableBooks());
*/