File tree 28 files changed +379
-0
lines changed
28 files changed +379
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Animal02 {
2
+
3
+ int legs = 4 ; // class member
4
+
5
+ // 01 add a method
6
+ public int getLegs () {
7
+ return this .legs ;
8
+ }
9
+
10
+ void constructsTest () {
11
+ int x = 5 ;
12
+ if (x == 5 )
13
+ System .out .println ("X is 5" );
14
+
15
+ for (int i = 0 ; i < 3 ; i ++)
16
+ System .out .println ("Looping:" + i );
17
+ }
18
+
19
+ public static void main (String args []) {
20
+ System .out .println ("Main function called ... " );
21
+ Animal02 a = new Animal02 (); // create an instance
22
+ System .out .println ("Legs: " + a .legs );
23
+
24
+ // add call to method
25
+ System .out .println ("Legs from method: " + a .getLegs ());
26
+
27
+
28
+ a .constructsTest ();
29
+ }
30
+ }
31
+
Original file line number Diff line number Diff line change
1
+ public class Animal04 {
2
+
3
+ public Animal04 () {
4
+ // Constructor
5
+ System .out .println ("Animal class created..." );
6
+ }
7
+
8
+ // java doesn't have a destructor
9
+
10
+ public static void main (String args []) {
11
+ System .out .println ("Main function called ... " );
12
+
13
+ Animal04 a = new Animal04 ();
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ public class Animal05 {
2
+ int legs ;
3
+
4
+ // String msg = "Test message"; // 03 add this and change msg below
5
+
6
+ public int getLegs () { // 02 make this private
7
+ return this .legs ;
8
+ }
9
+
10
+ public void printLegCount (String msg ) {
11
+ System .out .println (msg + ": " + getLegs ());
12
+ }
13
+
14
+ // java doesn't have a destructor
15
+
16
+ public static void main (String args []) {
17
+ Animal05 a = new Animal05 ();
18
+
19
+ a .printLegCount ("Legs are" );
20
+
21
+ // 01
22
+ a .legs += 1 ;
23
+ a .printLegCount ("Legs are" );
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ public class Intro01 {
2
+ public static void main (String args []) {
3
+
4
+ System .out .println ("Hello world!" );
5
+ System .out .println (args [0 ]);
6
+ System .out .println (args [1 ]);
7
+ }
8
+ }
Original file line number Diff line number Diff line change
1
+ public class Math06 {
2
+
3
+ public double sqrt (int x , double guess ) {
4
+ System .out .println ("Calculating sqrt of " + x + " starting with guess: " + guess );
5
+ return 1.0 ;
6
+ }
7
+
8
+ // Overloaded method -- for 'default' arguments
9
+ public double sqrt (int x ) {
10
+ double guess = 1.0 ;
11
+ return sqrt (x , guess );
12
+ }
13
+
14
+ public double sqrt (double x ) {
15
+ double guess = 1.0 ;
16
+ System .out .println ("... Calculating sqrt of " + x + " starting with guess: " + guess );
17
+ return 1.0 ;
18
+ }
19
+
20
+
21
+ public static void main (String args []) {
22
+ Math06 m = new Math06 ();
23
+
24
+ m .sqrt (36 , 4.0 );
25
+
26
+ m .sqrt (36 ); // 01 make guess optional
27
+
28
+ m .sqrt (36.0 ); // 02 send a float
29
+ }
30
+
31
+ }
Original file line number Diff line number Diff line change
1
+ class Polygon {
2
+ int numSides ;
3
+
4
+ public Polygon (int numSides ) {
5
+ this .numSides = numSides ;
6
+ }
7
+
8
+ public double getArea () {
9
+ return 2.0 ; // no idea how to calculate this!
10
+ }
11
+ }
12
+
13
+ // 01 -- add a class
14
+ class Rectangle extends Polygon {
15
+ // // 02 add a constructor
16
+ public Rectangle () {
17
+ super (4 );
18
+ }
19
+ //
20
+ double length ;
21
+ double width ;
22
+ public void setSides (double length , double width ) {
23
+ this .length = length ;
24
+ this .width = width ;
25
+ }
26
+
27
+ // // // 03 overriding
28
+ @ Override // and change fn name to get error
29
+ public double getArea () {
30
+ return this .length * this .width ;
31
+ }
32
+ }
33
+
34
+
35
+ public class Runner07 {
36
+ public static void main (String args []) {
37
+ Polygon p = new Polygon (4 ); // error because no default constructor
38
+ System .out .println ("Area of poly: " + p .getArea ()); // this does not make sense
39
+
40
+ Rectangle r = new Rectangle (); // also an error due to no default constructor
41
+ r .setSides (4 , 4 );
42
+ System .out .println ("Area of rect: " + r .getArea ()); // this does not make sense
43
+
44
+ }
45
+ }
Original file line number Diff line number Diff line change
1
+ class Polygon {
2
+ int numSides;
3
+
4
+ public Polygon(int numSides) {
5
+ this.numSides = numSides;
6
+ }
7
+
8
+ // 01: add abstract, then make class abstract then don't instantiate
9
+ public double getArea() {
10
+ return 2.0; // no idea how to calculate this!
11
+ }
12
+ }
13
+
14
+ class Rectangle extends Polygon {
15
+
16
+ public Rectangle() {
17
+ super(4);
18
+ }
19
+
20
+ double length;
21
+ double width;
22
+ public void setSides(double length, double width) {
23
+ this.length = length;
24
+ this.width = width;
25
+ }
26
+
27
+ @Override
28
+ public double getArea() {
29
+ return this.length * this.width;
30
+ }
31
+ }
32
+
33
+
34
+ public class Runner08 {
35
+ public static void main(String args[]) {
36
+ Polygon p = new Polygon(3);
37
+ System.out.println("Area of poly: " + p.getArea()); // this does not make sense
38
+
39
+ Rectangle r = new Rectangle();
40
+ // Polygon r = new Rectangle(); // 02 -- parent class reference var, child object
41
+
42
+ r.setSides(4, 4);
43
+ System.out.println("Area of rect: " + r.getArea());
44
+
45
+ }
46
+ }
Original file line number Diff line number Diff line change
1
+ class Polygon {
2
+ int numSides ;
3
+
4
+ public Polygon (int numSides ) {
5
+ this .numSides = numSides ;
6
+ }
7
+
8
+ // 01: add abstract, then make class abstract then don't instantiate
9
+ public double getArea () {
10
+ return 2.0 ; // no idea how to calculate this!
11
+ }
12
+ }
13
+
14
+ class Rectangle extends Polygon {
15
+
16
+ public Rectangle () {
17
+ super (4 );
18
+ }
19
+
20
+ double length ;
21
+ double width ;
22
+ public void setSides (double length , double width ) {
23
+ this .length = length ;
24
+ this .width = width ;
25
+ }
26
+
27
+ @ Override
28
+ public double getArea () {
29
+ return this .length * this .width ;
30
+ }
31
+ }
32
+
33
+
34
+ public class Runner08 {
35
+ public static void main (String args []) {
36
+ Polygon p = new Polygon (3 );
37
+ System .out .println ("Area of poly: " + p .getArea ()); // this does not make sense
38
+
39
+ Rectangle r = new Rectangle ();
40
+ // Polygon r = new Rectangle(); // 02 -- parent class reference var, child object
41
+
42
+ r .setSides (4 , 4 );
43
+ System .out .println ("Area of rect: " + r .getArea ());
44
+
45
+ }
46
+ }
Original file line number Diff line number Diff line change
1
+ interface Plugin {
2
+ // serves as a contract
3
+ public void draw ();
4
+ public void save ();
5
+
6
+ }
7
+
8
+ class TextEditorPlugin implements Plugin {
9
+ public void draw () {
10
+ System .out .println ("Drawing text" );
11
+ }
12
+
13
+ public void save () {
14
+ System .out .println ("Saved text" );
15
+ }
16
+ }
17
+
18
+ class PDFViewerPlugin implements Plugin {
19
+ public void draw () {
20
+ System .out .println ("Showing PDF" );
21
+ }
22
+
23
+ public void save () {
24
+ System .out .println ("Saved PDF annotations" );
25
+ }
26
+ }
27
+
28
+
29
+ public class Runner09 {
30
+ public static void main (String args []) {
31
+ // assume this is your atom editor
32
+
33
+ Plugin p = new TextEditorPlugin (); // reference variable of interface can hold implementing class object
34
+ // Plugin p = new PDFViewerPlugin();
35
+
36
+ p .draw ();
37
+ p .save ();
38
+
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Download slf4j binary from: https://www.slf4j.org/download.html
3
+ Extract to get jar files:
4
+ - slf4j-api-1.7.25.jar (or whichever version they have)
5
+ - slf4j-simple-1.7.25.jar
6
+ Copy them to your working_directory/lib/ folder
7
+ Compile: javac -cp .:lib/slf4j-api-1.7.25.jar:lib/slf4j-simple-1.7.25.jar Runner10.java (replace colon with semicolon on Windows)
8
+ Execute: java -cp .:lib/slf4j-api-1.7.25.jar:lib/slf4j-simple-1.7.25.jar Runner10
9
+ */
10
+
11
+ import org .slf4j .Logger ;
12
+ import org .slf4j .LoggerFactory ;
13
+
14
+
15
+
16
+
17
+ public class Runner10 {
18
+
19
+
20
+ public static void main (String args []) {
21
+ Logger logger = LoggerFactory .getLogger (Runner10 .class );
22
+
23
+ logger .info ("A message with the info level" );
24
+ logger .warn ("Warning level message ..." );
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .Vector ; // import Vector class from java.util package
2
+
3
+ public class Runner11 {
4
+
5
+ public void printVector (Vector v ) {
6
+ for (int i = 0 ; i < v .size (); i ++){
7
+ System .out .println (v .get (i ));
8
+
9
+ }
10
+ }
11
+
12
+ public void intVectorTest () {
13
+ Vector vi = new Vector ();
14
+ vi .add (4 );
15
+ vi .add (5 );
16
+ vi .add ("something" );
17
+
18
+ printVector (vi );
19
+ // System.out.println(vi); // can also output directly
20
+
21
+ }
22
+
23
+ // public void stringVectorTest() {
24
+ // Vector<String> vs = new Vector<String>(); // specify type of vector
25
+ // vs.add("first");
26
+ // vs.add("second");
27
+ // vs.add("third");
28
+ //
29
+ // System.out.println(vs);
30
+ //
31
+ // }
32
+
33
+ public static void main (String args []) {
34
+ Runner11 r = new Runner11 ();
35
+ r .intVectorTest ();
36
+
37
+ // r.stringVectorTest();
38
+
39
+ // comment out intVectorTest definition to get rid of warning
40
+ }
41
+ }
42
+
43
+
44
+ // add a CLI argument
Original file line number Diff line number Diff line change
1
+ public class Student03 {
2
+ static int num = 0 ;
3
+ int rollNo ;
4
+
5
+ public static void main (String args []) {
6
+
7
+ Student03 s1 = new Student03 ();
8
+ Student03 s2 = new Student03 ();
9
+
10
+ System .out .println ("num: " + Student03 .num );
11
+
12
+ Student03 .num += 1 ;
13
+ System .out .println ("num: " + Student03 .num );
14
+
15
+ System .out .println ("s1.num: " + s1 .num );
16
+ System .out .println ("s2.num: " + s2 .num );
17
+
18
+ System .out .println ("rollNo:" + s1 .rollNo );
19
+
20
+
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments