-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.java
More file actions
1496 lines (1166 loc) · 44.6 KB
/
Main.java
File metadata and controls
1496 lines (1166 loc) · 44.6 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.awt.Color;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import javax.sound.sampled.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import Tools.Toolbox;
public class Main {
// 2. VARIABLES
public static void main2(String[] args) {
System.out.println("Hello, World"); // println = "print line" adds newline char
System.out.print("I love pizza\n");
// Primitive (store data):
// DATA TYPE SIZE VALUE
// boolean 1 bit true or false
// byte 1 byte -128 to 127
// short 2 bytes -32,768 to 32,767
// int 4 bytes -2 billion to 2 billion
// long 8 bytes -9 quitillion to 9 quitillion
// float 4 bytes fractional number up to 6-7 digits
// double 8 bytes fractional number up to 15 digits
// char 2 bytes single character/letter/ASCII value
// Reference (store address):
// String varies sequence of characters (String is a reference datatype, the others are primitive)
int x; //declaration
x = 10; //assignment
int y = 20; //initialization
System.out.println(x);
x = 123; //reassignment
System.out.println("The number is: " + x);
long z = 123456789123456L; // must use suffix "L"
float f = 3.14f; // float must use suffix "f"
double g = 3.14;
char symbol = '@';
String name = "Bro";
System.out.println("Hello " + name);
}
// 4. USER INPUT
public static void main4(String[] args) {
// import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
System.out.println("What is your name?");
String name = scanner.nextLine();
System.out.println("How old are you?");
int age = scanner.nextInt();
System.out.println("What is your favorite food?");
scanner.nextLine(); // absorbs the \n from previous scanner
String food = scanner.nextLine();
System.out.println("Hello, " + name);
System.out.println("You are " + age + " years old");
System.out.println("You like to eat " + food);
}
// 5. EXPRESSIONS
public static void main5(String[] args) {
// expression = operands & operators
// operands = values, variables, numnbers, quantity
// operators = + - * / %
int friends = 10;
friends = friends + 1;
friends += 1;
friends++;
// friends = (double) friends / 3; //casting to a different type
System.out.println(friends);
}
// 6. GUI INTRO
public static void main6(String[] args) {
//import javax.swing.JOptionPane;
String name = JOptionPane.showInputDialog("Enter your name");
int age = Integer.parseInt((JOptionPane.showInputDialog("Enter your age"))); // it outputs an string, need to convert to int
double height = Double.parseDouble((JOptionPane.showInputDialog("Enter your height")));
JOptionPane.showMessageDialog(null, "Hello " + name + "\n" + "You are " + age + " years old and " + height + " cm tall");
}
// 7. MATH CLASS
public static void main7(String[] args) {
double x = 3.14;
double y = -10;
double z = Math.max(x, y); // max min abs sqrt round ceil floor
System.out.println(z);
//hypotenuse exercise
Scanner scanner = new Scanner(System.in);
System.out.println("Enter side x: ");
x = scanner.nextDouble();
System.out.println("Enter side y: ");
y = scanner.nextDouble();
z = Math.sqrt((x*x) + (y*y));
System.out.println("The hypotenuse is "+z);
scanner.close();
}
// 8. RANDOM NUMBERS
public static void main8(String[] args) {
// import java.util.Random;
Random random = new Random();
int x = random.nextInt(6)+1; // 6 is exclusive; passing a value will limit the range of random integers generated
double y = random.nextDouble(); // 0-1 float generated
boolean z = random.nextBoolean();
System.out.println(x);
}
// 9. IF STATEMENTS
public static void main9(String[] args) {
int age = 18;
if(age>=75) {
System.out.println("Ok Boomer");
}
else if(age>=18) {
System.out.println("You are an adult!");
}
else if (age>=13) {
System.out.println("You are a teenager");
}
else {
System.out.println("You are not an adult!");
}
}
// 10. SWITCHES
public static void main10(String[] args) {
int dayWeek = 2;
String day = "";
switch(dayWeek) {
case 1:
day = "Sunday";
break;
case 2:
day = "Monday";
break;
case 3:
day = "Tuesday";
break;
case 4:
day = "Wednesday";
break;
case 5:
day = "Thursday";
break;
case 6:
day = "Friday";
break;
case 7:
day = "Saturday";
break;
default:
System.out.println("This is not a valid week day");
}
System.out.println("Today is " + day);
}
// 11. LOGICAL OPERATORS
public static void main11(String[] args) {
// && = AND || = OR ! = NOT
Scanner scanner = new Scanner(System.in);
System.out.println("You are playing a game! Press q or Q to quit");
String response = scanner.next();
if(response.equals("q") || response.equals("Q")) {
System.out.println("You quit the game");
}
else {
System.out.println("You are still playing the game");
}
scanner.close();
}
// 12. WHILE LOOPS
public static void main12(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = "";
String name2 = "";
while(name.isBlank()) {
System.out.print("Enter your first name: ");
name = scanner.nextLine();
}
// perform block of code at least once
do {
System.out.print("Enter your second name: ");
name2 = scanner.nextLine();
}while(name2.isBlank());
System.out.println("Hello " + name);
scanner.close();
}
// 13. FOR LOOPS
public static void main13(String[] args) {
for(int i=0; i<10; i++) {
System.out.println(i);
}
}
// 14. NESTED LOOPS
public static void main14(String[] args) {
int rows = 5;
int cols = 5;
String symbol = "*";
for (int i=1; i<=rows; i++) {
for (int j=1; j<=cols; j++) {
System.out.print(symbol);
}
System.out.println();
}
}
// 15. ARRAYS
public static void main15(String[] args) {
// must have same datatypes
// pre assign
String[] cars = {"Camaro", "Fusca", "Tesla"};
cars[0] = "Mustang";
int[] nums = {1,2,3,4};
// pre alocate
String[] fruits = new String[5];
fruits[0] = "Apple";
fruits[1] = "Lemon";
for(int i=0; i<cars.length; i++) {
System.out.println(cars[i]);
}
}
// 16. 2D ARRAYS (array of arrays)
public static void main16(String[] args) {
// pre assign
String[][] cars = {
{"Camaro", "Fusca"},
{"Ferrari", "Corvette"}};
// pre alocate
String[][] cars2 = new String[2][2];
cars2[0][0] = "Camaro";
cars2[0][1] = "Fusca";
cars2[1][0] = "Ferrari";
cars2[1][1] = "Corvette";
for(int i=0; i<cars2.length; i++) {
for(int j=0; j<cars2[i].length; j++) {
System.out.print(cars2[i][j] + " ");
}
System.out.println();
}
}
// 17. STRING METHODS
public static void main17(String[] args) {
String name = "Bro";
boolean compare = name.equals("Bro");
boolean compare2 = name.equalsIgnoreCase("Bro");
int length = name.length();
char letter = name.charAt(0); // access letters
int pos = name.indexOf("B");
boolean isEmpty = name.isEmpty(); // isBlank() ignore spaces
String upper = name.toUpperCase(); // toLowerCase()
String trimmed = name.trim(); // remove trainling spaces
String replaced = name.replace('o', 'a');
}
// 18. WRAPPER CLASS
public static void main18(String[] args) {
// provides a way to use primitive data types as reference data types
// reference data types contain useful methods
// can be used with collections (ex. ArrayList)
// reference datatypes are slower than using primitive datatypes
//
// primitive wrapper (reference)
//-------- ---------
// boolean Boolean
// char Character
// int Integer
// double Double
// ...
// Strings are already datatypes
// autoboxing = the automatic conversion that the Java compiler makes between the primitive types and their coresponding objects wrapper classes
Boolean a = true; // will allow a.methods()
Character b = '$';
Integer c = 123;
Double d = 3.14;
String e = "Bro";
// unboxing = the reverse of autoboxing. Automatic conversion of wrapper class to primitive
if(a==true) {
System.out.println("This is true");
}
}
// 19. ARRAY LIST
public static void main19(String[] args) {
// a resizable array
// elements can be added and removed after compilation phase
// only store reference data types, not primitives
//import java.util.ArrayList;
ArrayList<String> foods = new ArrayList<String>();
foods.add("pizza");
foods.add("burger");
foods.add("hotdog");
foods.set(3, "sushi"); // replace element to index i; can't create elements, must be in range
foods.remove(2); // remove element at index i
foods.clear(); // remove everything
for(int i=0; i<foods.size(); i++) { // ArrayList.size() instead of String.length
System.out.println(foods.get(i)); // ArrayList.get(i) instaed of String[i]
}
}
// 20. 2D ARRAY LIST
public static void main20(String[] args) {
// a dynamic list of lists
// can change size of these lists during runtime
//import java.util.*; ?
//import java.util.ArrayList;
ArrayList<ArrayList<String>> groceryList = new ArrayList();
ArrayList<String> bakeryList = new ArrayList();
bakeryList.add("pasta");
bakeryList.add("bread");
bakeryList.add("donuts");
ArrayList<String> drinksList = new ArrayList();
drinksList.add("soda");
drinksList.add("coffee");
groceryList.add(bakeryList);
groceryList.add(drinksList);
System.out.println(groceryList.get(0).get(0));
}
// 21. FOR-EACH LOOPS
public static void main21(String[] args) {
// iterate through the elements in an array/collection
// less steps, more readable
// less flexible
String[] animals = {"cat", "dog", "rat"};
ArrayList<String> animals2 = new ArrayList<String>();
animals2.add("cat");
animals2.add("dog");
animals2.add("rat");
for(String i : animals) { // : represents `in`
System.out.println(i);
}
}
// 22. METHODS
public static void main22(String[] args) {
hello("Bro", 15);
}
static void hello(String name, int age) {
// `static` is needed to call it from main()
// a class is `private` by default
// `void` returns null
System.out.println("Hello " + name);
System.out.println("You are " + age + " years old");
}
// 23. OVERLOAD METHODS
public static void main23(String[] args) {
// methods that share same name but have different parameters
// method name + parameters = method signature
int res = add(2,3,4,5); // will call third method
}
static int add(int a, int b) {
System.out.println("Method #1");
return a + b;
}
static int add(int a, int b, int c) {
System.out.println("Method #2");
return a + b + c;
}
static int add(int a, int b, int c, int d) {
System.out.println("Method #3");
return a + b + c + d;
}
// 24. PRINTF
public static void main24(String[] args) {
// optional method to control, format and disply texto to the console window
// 2 arguments = format string + (object/variable/value)
// % [flags] [precision] [width] [conversion-character]
boolean myBool = true; // %b
char myChar = '@'; // %c
String myString = "Bro"; // %s
int myInt = 123; // %d (decimal)
double myDouble = 3.1415; // %f (default is 6 decimal places)
// [width]
// minimum number of characters to be written as output
// default pad with spaces right justify: ` Bro`
// adding `-` left justify
System.out.printf("Hello %10s %n", myString); // %n newline (can also be `\n`)
// [precision]
// sets number of digits of precision when outputting floating-point value
System.out.printf("Value is %.2f /n", myDouble);
// [flags]
// adds an effect to output based on the flag added to format specifier
// - : left-justify
// + : output a plus `+` or minus `-` sign for a numeric value
// 0 : numeric values are zero-padded
// , : comma grouping separator if numbers > 200
System.out.printf("The value is %020f /n", myDouble); // 20 length `zero` right-justify
}
// 25. FINAL KEYWORD
public static void main25(String[] args) {
// cannot be changed or updated in the future (const?)
final double PI = 3.14159;
}
// 26. OBJECTS (OOP)
public static void main26(String[] args) {
// Car.java file
Car car = new Car();
car.drive();
}
// 27. CONSTRUCTORS
public static void main27(String[] args) {
// special method that is called when an object is instantiated
// Human.java file
Human human1 = new Human("Bro", 12, 70);
Human human2 = new Human("Rick", 65, 90);
System.out.println(human1.name);
human1.eat();
human2.drink();
}
// 28. VARIABLE SCOPES
public static void main28(String[] args) {
// local = declared inside a method; visible only to that method
// global = declared outside a method but within a class; visible to all parts of a class
//DiceRoller.java file
DiceRoller diceRoller = new DiceRoller();
}
// 29. OVERLOADED CONSTRUCTORS
public static void main29(String[] args) {
// multiple constructos within a class with the same name but with different parameters
// name + parameters = signature
// Pizza.java file
Pizza pizza1 = new Pizza("crust");
Pizza pizza2 = new Pizza("crust", "tomate");
Pizza pizza3 = new Pizza("crust", "tomate", "mozzarella");
}
// 30. toString BUILT-IN METHOD
public static void main30(String[] args) {
// toString() = special method that all objects inherit,
// returns a string that "textually represents" an object
// can be used both implicity and explicity
// Car.java file
Car car = new Car();
// print overriden toString() method
// explicity
System.out.println(car.toString()); // default soString implementation is Class@memoryAddress
// implicity, same result
System.out.println(car);
}
// 31. ARRAY OF OBJECTS
public static void main31(String[] args) {
class Food {
String name;
Food(String name) {
this.name = name;
}
}
Food food1 = new Food("pizza");
Food food2 = new Food("burger");
Food food3 = new Food("sushi");
// post assigning
Food[] refrigerator = new Food[3];
refrigerator[0] = food1;
refrigerator[1] = food2;
refrigerator[2] = food3;
// pre assigning
Food[] refrigerator2 = {food1, food2, food3};
}
// 32. PASSING OBJECT AS ARGUMENT
public static void main32(String[] args) {
class Garage {
void park(Car car) {
System.out.println("The " + car.name + " is parked");
}
}
Garage garage = new Garage();
// Car.java file
Car car1 = new Car("Fusca");
Car car2 = new Car("Tesla");
garage.park(car1);
garage.park(car2);
}
// 33. STATIC KEYWORD
public static void main33(String[] args) {
// static = modifier. A single copy of a variable/,ethod is created and shared
// the class "owns" the static member
class Friend {
String name;
static int numberOfFriends;
Friend(String name) {
this.name = name;
numberOfFriends++;
}
static void displayFriends() {
System.out.println("You have " + numberOfFriends + " friends");
}
}
Friend friend1 = new Friend("Bro"); // will increment numberOfFriends for Class and all objects
Friend friend2 = new Friend("Patrick");
System.out.println(Friend.numberOfFriends); // access attribute without instantiate object
System.out.println(friend1.numberOfFriends); // access through object; possible but NOT recommended
Friend.displayFriends(); // access static method without instantiate
Math.round(3); // example of static built-in method
}
// 34. INHERITANCE
public static void main34(String[] args) {
// the process where one class acquires attributes and methods of another
// super/parent class
class Vehicle {
double speed;
void go() {
System.out.println("This vehicle is moving");
}
void stop() {
System.out.println("This vehicle is stopped");
}
}
// sub/child class
class Car extends Vehicle {
int doors = 4;
}
// sub/child class
class Bike extends Vehicle {
int pedals = 2;
}
Car car = new Car();
car.go();
Bike bike = new Bike();
bike.stop();
bike.speed = 10;
System.out.println(bike.speed);
}
// 35. METHOD OVERRIDING
public static void main35(String[] args) {
// declaring a method in sub class which is already present in parent class
// done so that a child class can give its own implementation
class Animal {
void speak() {
System.out.println("This animal speaks");
}
}
class Dog extends Animal {
@Override // optional, good practice
void speak() {
System.out.println("This dog barks");
}
}
Dog dog = new Dog();
dog.speak(); // bark
}
// 36. SUPER KEYWORD
public static void main36(String[] args) {
// keyword refers to the superclass (parent) of an object
// very similar to the `this` keyword
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String printInfo() {
return this.name;
}
}
class Hero extends Person {
String power;
Hero(String name, int age, String power) {
// invoking super constructor's attributes
super(name, age);
this.power = power;
}
public String printInfo() {
// invoking super method
return super.printInfo() + " has power " + this.power;
}
}
Hero hero1 = new Hero("Batman", 42, "money");
System.out.println(hero1.name);
System.out.println(hero1.age);
System.out.println(hero1.power);
System.out.println(hero1.printInfo());
}
// 37. ABSTRACTION
public static void main37(String[] args) {
// astract classes cannot be instantiated, but they can have a subclass
// abstract methods are declared without an implementation
// add layer of security
abstract class Vehicle {
// abstract method
// can't have a body; forces its implementation in child classes
abstract void go();
}
class Car extends Vehicle {
@Override
void go() {
System.out.println("This car is driving");
}
}
// Vehicle vehicle = new Vehicle(); // forbidden
Car car = new Car();
car.go();
}
// 38. ACCESS MODIFIERS
public static void main38(String[] args) {
/*
check .screens/access_levels.jpg
public
protected
<no modifier>
private
*/
}
// 39. ENCAPSULATION
public static void main39(String[] args) {
/*
atributes of a class will be hidden or private,
can be accessed only through methods (getters & setters)
you should make atrtibutes private if you don't have a reason to make them public/protected
*/
class Dog {
private String race;
private int age;
Dog(String race, int age) {
this.race = race;
this.age = age;
}
// getter
public String getRace() {
return race;
}
// setter
public void setRace(String race) {
this.race = race;
}
}
}
// 40. COPY OBJECTS
public static void main40(String[] args) {
// import Car.java
Car car1 = new Car("Chev", "Camaro");
Car car2 = new Car("Volks", "Fusca");
// car2 = car1; // wrong way: will have same memory address
car2.copy(car1); // copy after instantiate; must implement copy method in class Car
Car car3 = new Car(car1); // copy before instantiate; must implement copy constructor that calls previous copy in class Car
}
// 41. INTERFACE
public static void main41(String[] args) {
/*
a template that can be applied to a class
a special type of class that only contains abstract methods
similar to inheritance, but specifies what a class has/must do
classes can apply more than one interface, inheritance is limited to 1 super
*/
interface Prey {
void flee(); // abstract methods cannot have a body
}
interface Predator {
void hunt();
}
class Rabbit implements Prey {
@Override
public void flee() {
System.out.println("The rabbit is fleeing");
}
}
class Fish implements Prey, Predator {
@Override
public void hunt() {
System.out.println("This fish is hunting smaller fish");
}
@Override
public void flee() {
System.out.println("This fish is fleeing from a larger fish");
}
}
class Hawk implements Predator {
@Override
public void hunt() {
System.out.println("The hawk is hunting");
}
}
}
// 42. POLYMORPHISM
public static void main42(String[] args) {
/*
many - forms
the ability of an object to identify as more than one type
*/
class Vehicle {
public void go() {
}
}
class Bike extends Vehicle {
@Override
public void go() {
System.out.println("the bike begins moving");
}
}
class Boat extends Vehicle {
@Override
public void go() {
System.out.println("the boat begins moving");
}
}
Bike bike = new Bike();
Boat boat = new Boat();
Vehicle[] racers = {bike, boat};
for(Vehicle x : racers) {
x.go();
}
}
// 43. DYNAMIC POLYMORPHISM
public static void main43(String[] args) {
// polymorphism after compilation (during runtime)
class Animal {
public void speak() {
System.out.println("animal goes brrr");
}
}
class Dog extends Animal {
@Override
public void speak() {
System.out.println("dog goes bark");
}
}
class Cat extends Animal {
@Override
public void speak() {
System.out.println("cat goes meow");
}
}
Scanner scanner = new Scanner(System.in);
Animal animal;
System.out.println("What animal do you want?\n[1=dog 2=cat]: ");
int choice = scanner.nextInt();
if(choice==1) {
animal = new Dog();
}
else if(choice==2) {
animal = new Cat();
}
else {
animal = new Animal();
}
animal.speak();
scanner.close();
}
// 44. EXCEPTION HANDLING
public static void main44(String[] args) {
/*
an event that occurs during execution of a program
that disrupts the normal flow of instructions
*/
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Enter a whole number to divide by: ");
int x = scanner.nextInt();
System.out.println("Enter a whole number to divide by: ");
int y = scanner.nextInt();
int z = x/y;
System.out.println("result: " + z);
}
catch(ArithmeticException e) {
System.out.println("You can't divide by zero!");
}
catch(InputMismatchException e) {
System.out.println("Please enter a valid number");
}
// catch all Exceptions
catch(Exception e) {
System.out.println("Some error occured");
}
// always execute
finally {
System.out.println("This is always print");
scanner.close();
}
}
// 45. FILE CLASS
public static void main45(String[] args) {
// import java.io.File;
// an abstract representation of file and directory path names
File file = new File("files/secret_message.txt"); // can use `\\` or `/`
if(file.exists()) {
System.out.println("That file exists");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.isFile());
// file.delete();
} else {