Skip to content

Commit 28e36fe

Browse files
committed
Fixes and improves a few previous files and finishes code for module 7 about classes.
1 parent 18f8a56 commit 28e36fe

File tree

6 files changed

+165
-5
lines changed

6 files changed

+165
-5
lines changed

src/JF01_TheBasics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class JF01_TheBasics {
2121

2222
// This a program entry point. If a method (function inside of a class) has this exact signature:
2323
// public static void main(String[] args)
24-
// it can be where your program starts running: the entry point.
25-
public static void main(String[] args) {
24+
// This is where your program starts running, the entry point.
25+
public static void main(String[] args) { // we will explain the "public" keyword when we explain classes in detail.
2626

2727
// Statements end with a semicolon. Spaces (or tabs, or new line characters) do not matter for the compiler.
2828
// This is a statement and it prints the string "Hello Java Fundamentals" on the console (System.out).

src/com/fledger/javafundamentals/JF03_VarsTypesOperators.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static void main(String[] args) {
7272
// When you declare non primitive type variables (objects), space is allocated for the value and the
7373
// variable receives a reference to that space: objects are stored by reference, indirectly. See the example:
7474
Date now = new Date(); // creates a Date object variable, that contains current date and time... by reference!
75-
Date oneHourAgo = new Date(now.getTime() - 60 /*minutes*/ * 60 /*seconds*/ * 1000 /*milis*/);
75+
Date oneHourAgo = new Date(now.getTime() - 60 /* minutes */ * 60 /* seconds */ * 1000 /* milliseconds */);
7676
System.out.println("Objects are stored by reference: now and oneHourAgo");
7777
System.out.println(now); // prints current date and time
7878
System.out.println(oneHourAgo); // prints date and time of one hour ago

src/com/fledger/javafundamentals/JF06_ArraysLoops.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static void main(String[] args) {
7474
double d = myArrayOfDoubles[j];
7575
System.out.print(d + "; ");
7676
}
77-
System.out.println("These are the elements in myArrayOfDoubles");
77+
System.out.println("These are also the elements in myArrayOfDoubles");
7878

7979
// And with a while loop
8080
int j = 0;
@@ -83,7 +83,25 @@ public static void main(String[] args) {
8383
System.out.print(d + "; ");
8484
j++;
8585
}
86-
System.out.println("These are the elements in myArrayOfDoubles");
86+
System.out.println("And one more time, the elements in myArrayOfDoubles");
87+
88+
// A special type of loop is the forever loop. Make the loop run forever, then break out under a certain condition:
89+
while (true) {
90+
System.out.print(j + "... ");
91+
if (j-- <= 0) {
92+
System.out.println("Breaking out the forever loop");
93+
break; // Never, never forget to break out of a forever loop!
94+
}
95+
}
96+
97+
// Another way to write a forever loop:
98+
for (; ; ) { // If you leave the condition empty in the for loop, it is considered true.
99+
System.out.print(j + "... ");
100+
if (j++ >= 5) {
101+
System.out.println("You got the idea about breaking out the forever loop");
102+
break; // Never, never forget to break out of a forever loop!
103+
}
104+
}
87105
}
88106
}
89107

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.fledger.javafundamentals.jf07classes;
2+
3+
import java.util.Date;
4+
5+
// This is the class Alarm. It contains the definition for all data and all methods of an alarm:
6+
class Alarm {
7+
8+
// alarmDateAndTime and alarmMessage are attributes of the class Alarm.
9+
Date alarmDateAndTime;
10+
String alarmMessage;
11+
12+
// goOff() is a method of the class Alarm.
13+
void goOff() {
14+
System.out.println(alarmMessage);
15+
}
16+
}

src/com/fledger/javafundamentals/jf07classes/BetterAlarm.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,62 @@
22

33
import java.util.Date;
44

5+
// This is the class BetterAlarm. It is better because it encapsulates the attributes, by making them private.
6+
// Notice that this class uses the public access modifier. This means any class can import and use it.
57
public class BetterAlarm {
8+
9+
// The private access modifiers mean these attributes are only visible within the class
610
private Date dateAndTimeToGoOff;
711
private String message;
812

13+
// The public access modifiers mean these methods are visible to any class that uses (imports) the BetterAlarm class.
914
public void setToGoOffInTheseManySeconds(int i) {
1015
dateAndTimeToGoOff = new Date();
1116
dateAndTimeToGoOff.setTime(dateAndTimeToGoOff.getTime() + i * 1000L);
1217
}
1318

19+
// If no access modifier is used, the member is package private: it is visible anywhere in the same package.
20+
int defaultDelayToGoOff = 2;
21+
22+
// The next two methods below are visible anywhere in the com.fledger.javafundamentals.jf07classes package:
23+
void setDefaultDelayToGoOff(int i) {
24+
defaultDelayToGoOff = i;
25+
}
26+
27+
void prepareAlarm() {
28+
setMessage(defaultMessageForBetterAlarm);
29+
setToGoOffInTheseManySeconds(defaultDelayToGoOff);
30+
}
31+
32+
// There is one additional access modifier: protected. We will talk about protected when we see inheritance.
33+
34+
// Since private attributes are not visible outside this class, only methods defined by you can change them.
35+
// This guarantees that only your code will mess with the attribute, usually through a setter.
36+
// This is a setter, or mutator, method. It is used to set a value in a private attribute.
1437
public void setMessage(String s) {
1538
message = s;
1639
}
1740

41+
// This is a getter, or accessor, method. It is used to get the value of a private attribute.
1842
public Date getDateTime() {
1943
return dateAndTimeToGoOff;
2044
}
2145

46+
// This is a regular public method. Can be called by any code that uses this class.
2247
public void goOff() {
2348
System.out.println(message);
2449
}
50+
51+
// Class and Instance variables (or attributes)
52+
// If a member has the static keyword, it is a class member. It means it belongs to the class.
53+
// Class members can be used from classes or instances (objects). Instance members cannot be used from the class.
54+
private static String defaultMessageForBetterAlarm;
55+
56+
// The following is a static member, or a class method. It cannot access any instance member.
57+
static void setDefaultMessageForBetterAlarm(String s) {
58+
defaultMessageForBetterAlarm = s;
59+
// The compiler would not allow the following statement because setMessage is not static (it is an instance member)
60+
// setMessage(defaultMessageForBetterAlarm);
61+
}
62+
2563
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Java Fundamentals 07 - Classes, methods, fields, access modifiers
2+
package com.fledger.javafundamentals.jf07classes;
3+
4+
import java.util.Date;
5+
6+
// From now on we will have a separate package per module, as we may use multiple files for each module.
7+
// All files for this module are inside the com.fledger.javafundamentals.jf07classes package.
8+
// Java is an object oriented language. Objects are elements containing data, in the form of fields,
9+
// and code, in the form of functions. In Object Oriented (OO) lingo, object fields are known as attributes
10+
// and object functions are known as methods.
11+
// We say that objects contain a state (given by the attributes) and a behavior (given by the methods).
12+
// An object is a computational model of an entity, like a specific car, or a specific alarm, etc.
13+
// A class is a definition, or a blueprint, for a type of entity, like "Car" (the idea of a car) or "Alarm" (same).
14+
// Objects are created based on a class. For example, in Java, in the following statement:
15+
// Alarm wakeUpAlarm = new Alarm();
16+
// wakeUpAlarm is an object, or instance, of class Alarm. The class Alarm defines the fields and methods of all alarms.
17+
18+
// The following statement and block (com.fledger.javafundamentals.jf07classes.JF07_Classes) define a class:
19+
public class JF07_Classes {
20+
// Everything between the { } following the class name define the content of the class.
21+
// As we said, a class can have data... inside a class, data members, or fields, are called fields or attributes:
22+
int anIntegerField;
23+
24+
// A class can also have Java code within functions, also are called methods:
25+
void thisIsAMethod() {
26+
System.out.println("This is a method that returns nothing (void) and prints this message.");
27+
}
28+
29+
// This class contains the entry point (function main) for our program (see JF01_TheBasics.java)
30+
public static void main(String[] args) {
31+
32+
// A static method can call another static method. Static methods belong directly to the class (see BetterAlarm)
33+
printCurrentTime(); // prints current date and time
34+
// A class in java is a type, so you can declare variables of the class type (see Alarm.java in this package):
35+
Alarm wakeUpAlarm;
36+
// You can assign a value to a variable of the class type. The variable contains a reference to an object:
37+
wakeUpAlarm = new Alarm(); // the keyword "new", followed by a class type and (), creates a new object of that type;
38+
// You can access members (fields and methods) of an object by using a reference variable, dot (.), and member name:
39+
wakeUpAlarm.alarmMessage = "It is time to wake up... WAKE UP!!!";
40+
// You can have fields that are also references to other objects:
41+
wakeUpAlarm.alarmDateAndTime = new Date(); // sets alarmDateAndTime of WakeUpAlarm to a new Date object set to now.
42+
// You can access members of these fields with the dot (.) notation:
43+
Long currentTime = wakeUpAlarm.alarmDateAndTime.getTime(); // get the time (now) from the Date object as a long.
44+
Long inThreeSeconds = currentTime + 3000L; // Adds 3 thousand milliseconds (5 seconds) to the current time (now).
45+
wakeUpAlarm.alarmDateAndTime.setTime(inThreeSeconds);
46+
while (true) { // forever loop. There are better ways to wait for a timed event, but this will do for now.
47+
Date now = new Date(); // create a new object with the current time;
48+
if (now.after(wakeUpAlarm.alarmDateAndTime)) { // if current time passed the time set in the alarm
49+
wakeUpAlarm.goOff(); // alarm goes off and prints message on the console... this is an object behavior (method)
50+
break; // if you use forever, you need to break out of loop under some condition (see JF06_ArraysLoops)
51+
}
52+
}
53+
printCurrentTime(); // should be 3 seconds after previous printed time.
54+
55+
// One of the principles of object oriented programming is encapsulation or "data hiding".
56+
// You can hide the object fields from external access (hide the data, or encapsulate),
57+
// so only methods of the object can access them. This way you ensure no one else will corrupt your object data.
58+
// To do so we use access modifiers in the BetterAlarm.java file (look into that file):
59+
BetterAlarm anotherWakeUpAlarm = new BetterAlarm();
60+
anotherWakeUpAlarm.setToGoOffInTheseManySeconds(1);
61+
anotherWakeUpAlarm.setMessage("Please, wake up. Better alarms are more polite.");
62+
for (; ; ) { // another way to write the forever loop
63+
Date now = new Date(); // create a new object with the current time;
64+
if (now.after(anotherWakeUpAlarm.getDateTime())) { // if current time passed the time set in the alarm
65+
anotherWakeUpAlarm.goOff(); // alarm goes off and prints message on the console...
66+
break; // get out of the "forever" loop (see JF06_ArraysLoops)
67+
}
68+
}
69+
printCurrentTime(); // should be one second after previous printed time.
70+
71+
// BetterAlarm contains static members. You don't need to instantiate an object in order to access the static
72+
// member. You access it directly using the class name:
73+
BetterAlarm.setDefaultMessageForBetterAlarm("Better Alarm also asks you to wake up.");
74+
// Creates a betterAlarm object:
75+
BetterAlarm betterAlarm = new BetterAlarm();
76+
// All you need to do to prepare the alarm now is call one method.
77+
betterAlarm.prepareAlarm(); // This is a package private method: only classes in this package can call it.
78+
// This is even another way to write the code to make the alarm go off, with only two lines of code:
79+
for (Date now = new Date(); now.before(betterAlarm.getDateTime()); now = new Date()) ;
80+
betterAlarm.goOff();
81+
printCurrentTime(); // should be two seconds (default delay for BetterAlarm) after previous printed time.
82+
}
83+
84+
private static void printCurrentTime() {
85+
Date now = new Date();
86+
System.out.println("The date and time now is: " + now);
87+
}
88+
}

0 commit comments

Comments
 (0)