File tree 8 files changed +136
-0
lines changed
1_What_are_Classes_Objects_and_Methods/1.1_Java_Introduction_to_Classes
1.1.1_Introduction_to_Classes
1.1.3_Classes_Constructors
1.1.4_Classes_Instance_Fields
1.1.5_Classes_Constructor_Parameters
1.1.6_Classes_Assigning_Values_to_Instance_Fields
1.1.7_Classes_Multiple_Fields
8 files changed +136
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Store {
2
+ // instance fields
3
+ String productType ;
4
+ int inventoryCount ;
5
+ double inventoryPrice ;
6
+
7
+ // constructor method
8
+ public Store (String product , int count , double price ) {
9
+ productType = product ;
10
+ inventoryCount = count ;
11
+ inventoryPrice = price ;
12
+ }
13
+
14
+ // main method
15
+ public static void main (String [] args ) {
16
+ Store lemonadeStand = new Store ("lemonade" , 42 , .99 );
17
+ Store cookieShop = new Store ("cookies" , 12 , 3.75 );
18
+
19
+ System .out .println ("Our first shop sells " + lemonadeStand .productType + " at " + lemonadeStand .inventoryPrice + " per unit." );
20
+
21
+ System .out .println ("Our second shop has " + cookieShop .inventoryCount + " units remaining." );
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ public class Store {
2
+ public static void main (String [] args ) {
3
+
4
+ }
5
+ }
Original file line number Diff line number Diff line change
1
+ public class Store {
2
+
3
+ // new method: constructor!
4
+ public Store () {
5
+ System .out .println ("I am inside the constructor method." );
6
+ }
7
+
8
+ // main method is where we create instances!
9
+ public static void main (String [] args ) {
10
+ System .out .println ("Start of the main method." );
11
+
12
+ // create the instance below
13
+ Store lemonadeStand = new Store ();
14
+
15
+ // print the instance below
16
+ System .out .println (lemonadeStand );
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ public class Store {
2
+ // declare instance fields here!
3
+ String productType ;
4
+
5
+ // constructor method
6
+ public Store () {
7
+ System .out .println ("I am inside the constructor method." );
8
+ }
9
+
10
+ // main method
11
+ public static void main (String [] args ) {
12
+ System .out .println ("This code is inside the main method." );
13
+
14
+ Store lemonadeStand = new Store ();
15
+
16
+ System .out .println (lemonadeStand );
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ public class Store {
2
+ // instance fields
3
+ String productType ;
4
+
5
+ // constructor method
6
+ public Store (String product ) {
7
+ productType = product ;
8
+ }
9
+
10
+ // main method
11
+ public static void main (String [] args ) {
12
+
13
+
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ public class Store {
2
+ // instance fields
3
+ String productType ;
4
+
5
+ // constructor method
6
+ public Store (String product ) {
7
+ productType = product ;
8
+ }
9
+
10
+ // main method
11
+ public static void main (String [] args ) {
12
+ Store lemonadeStand = new Store ("lemonade" );
13
+ System .out .println (lemonadeStand .productType );
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ public class Store {
2
+ // instance fields
3
+ String productType ;
4
+ int inventoryCount ;
5
+ double inventoryPrice ;
6
+
7
+ // constructor method
8
+ public Store (String product , int count , double price ) {
9
+ productType = product ;
10
+ inventoryCount = count ;
11
+ inventoryPrice = price ;
12
+ }
13
+
14
+ // main method
15
+ public static void main (String [] args ) {
16
+ Store cookieShop = new Store ("cookies" , 12 , 3.75 );
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ public class Dog {
2
+ String breed ;
3
+ boolean hasOwner ;
4
+ int age ;
5
+
6
+ public Dog (String dogBreed , boolean dogOwned , int dogYears ) {
7
+ System .out .println ("Constructor invoked!" );
8
+ breed = dogBreed ;
9
+ hasOwner = dogOwned ;
10
+ age = dogYears ;
11
+ }
12
+
13
+ public static void main (String [] args ) {
14
+ System .out .println ("Main method started" );
15
+ Dog fido = new Dog ("poodle" , false , 4 );
16
+ Dog nunzio = new Dog ("shiba inu" , true , 12 );
17
+ boolean isFidoOlder = fido .age > nunzio .age ;
18
+ int totalDogYears = nunzio .age + fido .age ;
19
+ System .out .println ("Two dogs created: a " + fido .breed + " and a " + nunzio .breed );
20
+ System .out .println ("The statement that fido is an older dog is: " + isFidoOlder );
21
+ System .out .println ("The total age of the dogs is: " + totalDogYears );
22
+ System .out .println ("Main method finished" );
23
+ }
24
+ }
You can’t perform that action at this time.
0 commit comments