Skip to content

Commit 2e5818d

Browse files
authored
Merge pull request #6 from formidablae/1-what-are-classes-objects-and-methods/1.1-java-introduction-to-classes
Did 1 What are Classes Objects and Methods / 1.1 Java Introduction to Classes
2 parents 755ead9 + 426c8cc commit 2e5818d

File tree

8 files changed

+136
-0
lines changed

8 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Store {
2+
public static void main(String[] args) {
3+
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)