-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemSet1A.java
More file actions
523 lines (443 loc) · 14.9 KB
/
ProblemSet1A.java
File metadata and controls
523 lines (443 loc) · 14.9 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package ProblemSets;
import org.w3c.dom.ls.LSOutput;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.HashSet;
import java.util.Set;
public class ProblemSet1A {
public static void main(String[] args) {
// Cohort Session 1 test cases.
System.out.println(fibonacciSequence(13));
// Cohort Session 2 test cases.
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
System.out.println(iterator(list));
List<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
list1.add(6);
list1.add(7);
list1.add(8);
list1.add(9);
list1.add(10);
System.out.println(forIterator(list1));
// Question 1 test cases.
int[] testCase1 = {4, 7, 14, 23, 99};
List<Integer> results = new ArrayList<Integer>();
for (int testCases : testCase1){
results.add(isPrime(testCases));
}
System.out.println(results);
}
// Cohort Session 1.
public static String fibonacciSequence(int n) {
StringBuilder result = new StringBuilder();
int firstTerm = 0;
int secondTerm = 1;
for (int i = 0; i < n; i++) {
result.append(firstTerm);
if (i < n - 1){
result.append(",");
}
int next = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = next;
}
return result.toString();
}
// Cohort Session 2
// Part 1: Iterating using Iterator
static int iterator(List<Integer> integer) {
int total = 0;
ListIterator<Integer> iter = integer.listIterator();
while (iter.hasNext()) {
total += iter.next();
}
return total;
}
// Part 2: For-each Loop
static int forIterator(List<Integer> integer) {
int total = 0;
for (int element : integer) {
total += element;
}
return total;
}
// Question 1: Prime Number Checker
public static int isPrime(int n){
for (int i = 2; i <= Math.sqrt(n); i++){
if (n % i == 0){
return 0;
}
}
return 1;
}
}
// Cohort Session 3
class Account {
// ID for the account
private int id = 0;
// Stores the balance for the account
private double balance = 0.0;
// Stores the current interest rate in %. Assume all accounts have the same interest rate.
private static double annualInterestRate = 0.0;
// Stores the date when the account was created.
private LocalDate dateCreated;
// constructor with default values.
public Account() {}
// constructor with ID and initial balance specified.
public Account(int ID, double initialBalance) {
this.id = ID;
this.balance = initialBalance;
}
// getter for id.
public int getId() {
return id;
}
// setter for id.
public void setId(int ID) {
this.id = ID;
}
// getter for balance.
public double getBalance() {
return balance;
}
// setter for balance.
public void setBalance(double initialBalance) {
this.balance = initialBalance;
}
// getter for annual interest rate.
public static double getAnnualInterestRate() {
return annualInterestRate;
}
// setter for annual interest rate.
public static void setAnnualInterestRate(double annualInterestRate) {
Account.annualInterestRate = annualInterestRate;
}
// getter for date account created.
public LocalDate getDateCreated() {
return dateCreated;
}
// method to calculate and return the monthly interest rate in %.
public static double getMonthlyInterestRate() {
return getAnnualInterestRate() / 12;
}
// method to calculate monthly interest in $.
public double getMonthlyInterest() {
double interestRate = getMonthlyInterestRate() / 100;
return getBalance() * interestRate;
}
// method to withdraw amount.
public void withdraw(double amount) {
setBalance(getBalance() - amount);
}
// method to deposit amount.
public void deposit(double amount) {
setBalance(getBalance() + amount);
}
public static void main(String[] args) {
Account account = new Account(1122, 20000);
Account.setAnnualInterestRate(4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " + account.getMonthlyInterest());
}
}
// Question 2: MyRectangle2D Class
class MyRectangle2D {
// Defining the point (x, y) where the rectangle will be created. Default: (0, 0)
private double x;
private double y;
// getter for x point.
public double getX(){
return x;
}
// setter for x point.
public void setX(double x){
this.x = x;
}
// getter for y point.
public double getY(){
return y;
}
// setter for y point.
public void setY(double y){
this.y = y;
}
// Defining the width and height of the rectangle. Default: 1.
private double width;
private double height;
// getter for width
public double getWidth(){
return width;
}
// setter for width.
public void setWidth(double width){
this.width = width;
}
// getter for height.
public double getHeight(){
return height;
}
// setter for height.
public void setHeight(double height){
this.height = height;
}
// constructor for default rectangle
public MyRectangle2D(){
this(0.0, 0.0, 1.0, 1.0);
};
// constructor with specified values
public MyRectangle2D(double x, double y, double width, double height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
// method to calculate the area of rectangle.
public double getArea(){
return width * height;
}
// method to calculate perimeter of rectangle.
public double getPerimeter(){
return 2 * (width + height);
}
// method to determine if a point is in the rectangle.
// for the point to be in the rectangle have to be within the bounds of the (x + width) & (y + height).
public boolean contains(double x, double y){
return x >= this.x - width / 2 &&
x <= this.x + width / 2 &&
y >= this.y - height / 2 &&
y <= this.y + height + 2;
}
// method to check if a rectangle is inside the rectangle.
public boolean contains(MyRectangle2D r){
double rX = r.getX();
double rY = r.getY();
double rWidth = r.getWidth();
double rHeight = r.getHeight();
return (contains(rX - rWidth / 2, rY - rHeight / 2) &&
contains(rX + rWidth / 2, rY - rHeight / 2) &&
contains(rX - rWidth / 2, rY + rHeight / 2) &&
contains(rX + rWidth / 2, rY + rHeight / 2));
}
// method to check if a rectangle overlaps.
public boolean overlaps(MyRectangle2D r){
return !((x + width / 2) < (r.x - r.width / 2) ||
(r.x + r.width / 2) < (x - width / 2) ||
(y + height / 2) < (r.y - r.height / 2) ||
(r.y + r.height / 2) < (y - height / 2));
}
}
// Week 2
// Question 1: 2x2 Linear Equations
class LinearEquation{
// Declaration of private data fields for the coefficients.
private double a;
private double b;
private double c;
private double d;
private double e;
private double f;
// constructor with arguments.
public LinearEquation(double A, double B, double C, double D, double E, double F){
this.a = A;
this.b = B;
this.c = C;
this.d = D;
this.e = E;
this.f = F;
}
// getter methods for the 6 coefficients.
public double getA(){
return this.a;
}
public double getB(){
return this.b;
}
public double getC(){
return this.c;
}
public double getD(){
return this.d;
}
public double getE(){
return this.e;
}
public double getF(){
return this.f;
}
// method: isSolvable to find if the 2x2 linear equation is solvable using the determinant (ad - cb)
// if determinant == 0 means that the linear equation is unsolvable and therefore return false vice versa.
public boolean isSolvable(){
return (getA() * getD()) - (getB() * getC()) != 0;
}
// method to get solutions to the equations: getX() & getY().
public double getX(){
// using Cramer's Rule to solve for x. D = determinant (ad - cb), dX = (e * d) - (f * b)
// x = dX / D; by replacing coefficients of x with the constants.
double determinant = (getA() * getD()) - (getB() * getC());
double dX = (getE() * getD()) - (getB() * getF());
return (dX / determinant);
}
public double getY(){
// using Cramer's Rule to solve for y. D = determinant (ad - cb), dY = (a * e) - (c * f)
// y = dY / D; by replacing coefficients of y with the constants.
double determinant = (getA() * getD()) - (getB() * getC());
double dY = (getA() * getF()) - (getE() * getC());
return (dY / determinant);
}
public static void main(String[] args) {
double a = 1.0;
double b = 2.0;
double c = 3.0;
double d = 5.0;
double e = 6.0;
double f = 7.0;
LinearEquation equation = new LinearEquation(a, b, c, d, e, f);
if (equation.isSolvable()){
System.out.println("x is " + equation.getX() + " and y is " + equation.getY());
}
else {
System.out.println("The equation has no solution.");
}
LinearEquation equation2 = new LinearEquation(1.25, 2.0, 2.0, 4.2, 3.0, 6.0);
if (equation2.isSolvable()){
System.out.println("x is " + equation2.getX() + " and y is " + equation2.getY());
}
LinearEquation equation3 = new LinearEquation(1.0, 2.0, 2.0, 4.0, 3.0, 6.0);
System.out.println(equation3.isSolvable());
}
}
// Question 2: The Triangle Class
class GeometricObject{
private String color;
GeometricObject(){
this("Green");
}
GeometricObject(String color){
this.color = color;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color = color;
}
public String getInfo(){
return ("Geometric Object of color " + this.color);
}
}
class Triangle extends GeometricObject{
// Default values of the 3 sides of a triangle.
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
// default constructor.
public Triangle(){};
// constructor with specified sides.
public Triangle(double s1, double s2, double s3){
this.side1 = s1;
this.side2 = s2;
this.side3 = s3;
}
// method to get the perimeter of the triangle: getPerimeter()
public double getPerimeter(){
return (this.side1 + this.side2 + this.side3);
}
// method to get the area of the triangle: getArea()
// using heron's formula: area = sqrt[s(s-a)(s-b)(s-c)] where s = semi-perimeter (a+b+c)/2 of the triangle.
public double getArea(){
double s = getPerimeter() / 2;
return Math.sqrt((s * (s - this.side1) * (s - this.side2) * (s - this.side3)));
}
// method to return the description of the triangle.
public String toString(){
return ("Triangle: " + "side1 = " + this.side1 + " side2 = " + this.side2 + " side3 = " + this.side3);
}
}
// Question 3: Subclasses of Account
class CheckingAccount extends Account{
private double overdraftLimit = 5000.0;
public CheckingAccount(int ID, double initialBalance){
super(ID, initialBalance);
}
@Override
public void withdraw(double amount){
if (getBalance() - amount < -overdraftLimit){
System.out.println("over limit");
}
else{
super.withdraw(amount);
}
}
public static void main(String[] args) {
CheckingAccount myCheckAcc = new CheckingAccount(1024, 8000.0);
myCheckAcc.deposit(2000);
myCheckAcc.withdraw(15000);
System.out.println(myCheckAcc.getBalance());
myCheckAcc.withdraw(200);
System.out.println(myCheckAcc.getBalance());
myCheckAcc.deposit(7000);
myCheckAcc.withdraw(200);
System.out.println(myCheckAcc.getBalance());
}
}
// Question 4: String Operation
// Part I: Design and implement a static method to determine if an input string has all unique characters.
// Assume the character set is ACII, encodes 128 characters into 7-bit binary characters.
class Pset1{
public static boolean isAllCharacterUnique(String stringIn){
// todo: add implementation
// using hashset we can check the input string if it is different.
// Create a new set to insert characters.
Set<Character> set = new HashSet<>();
// Get all characters from input string.
char[] characters = stringIn.toCharArray();
// loop through the characters and find the duplicate to add them into the set.
for (Character c : characters){
if (!set.add(c)){
return false;
}
}
return true;
}
public static boolean isPermutation(String string1, String string2){
// todo: implement a method which takes in 2 strings and compare if they are permutations
// todo: of each other
// Compare the length first, if length is not the same, straight away return false.
if (string1.length() != string2.length()){
return false;
}
// If length is the same, we change the string to an array and sort the array.
char[] array1 = string1.toCharArray();
char[] array2 = string2.toCharArray();
Arrays.sort(array1);
Arrays.sort(array2);
// After we just compare the arrays by using .equals.
return Arrays.equals(array1, array2);
}
public static void main(String[] args){
String input = "Java";
String input1 = "Python";
System.out.println(isAllCharacterUnique(input)); // return false
System.out.println(isAllCharacterUnique(input1)); // return true
String string1 = "test";
String string2 = "ttew";
String string3 = "ttse";
System.out.println(isPermutation(string1, string2)); // return false
System.out.println(isPermutation(string1, string3)); // return true
}
}