OOP Basics - Java Programming Tutorial
OOP Basics - Java Programming Tutorial
| HOME
Why reinvent the wheels? Why rewriting codes? Can you write better codes than those codes written by the experts?
In the early 1970s, the US Department of Defense DoD commissioned a task force to investigate why its IT budget always went
out of control; but without much to show for. The findings are:
1. 80% of the budget went to the software with the remaining 20% to the hardware.
2. More than 80% of the software budget went to maintenance only the remaining 20% for new software development.
3. Hardware components could be applied to various products, and their integrity normally did not affect other products.
Hardware can share and reuse! Hardware faults are isolated!
4. Software procedures were often nonsharable and not reusable. Software faults could affect other programs running in
computers.
The task force proposed to make software behave like hardware OBJECT. Subsequently, DoD replaces over 450 computer
languages, which were then used to build DoD systems, with an objectoriented language called Ada.
Most importantly, some of these classes such as Ball and Audience can be reused in another application, e.g., computer basketball
game, with little or no modification.
Benefits of OOP
The proceduraloriented languages focus on procedures, with function as the basic unit. You need to first figure out all the functions and
then think about how to represent data.
The objectoriented languages focus on components that the user perceives, with objects as the basic unit. You figure out all the objects
by putting all the data and operations that describe the user's interaction with the data.
2.OOP in Java
An instance is a realization of a particular item of a class. In other words, an instance is an instantiation of a class. All the instances of a
class have similar properties, as described in the class definition. For example, you can define a class called "Student" and create three
instances of the class "Student" for "Peter", "Paul" and "Pauline".
The term "object" usually refers to instance. But it is often used loosely, and may refer to a class or an instance.
In other words, a class encapsulates the static attributes data and dynamic behaviors
operations that operate on the data in a box.
The following figure shows two instances of the class Student, identified as "paul" and "peter".
Unified Modeling Language UML Class and Instance Diagrams: The above class diagrams are drawn according to the
UML notations. A class is represented as a 3compartment box, containing name, variables, and methods, respectively. Class name is
shown in bold and centralized. An instance is also represented as a 3compartment box, with instance name shown as
instanceName:Classname and underlined.
Brief Summar y
1. A class is a programmerdefined, abstract, selfcontained, reusable software entity that mimics a realworld thing.
2. A class is a 3compartment box containing the name, variables and the methods.
3. A class encapsulates the data structures in variables and algorithms in methods. The values of the variables constitute its state.
The methods constitute its behaviors.
4. An instance is an instantiation or realization of a particular item of a class.
publicclassCircle{//classname
doubleradius;//variables
Stringcolor;
doublegetRadius(){......}//methods
doublegetArea(){......}
}
publicclassSoccerPlayer{//classname
intnumber;//variables
Stringname;
intx,y;
voidrun(){......}//methods
voidkickBall(){......}
}
[AccessControlModifier]classClassName{
//Classbodycontainsmembers(variablesandmethods)
......
}
We shall explain the access control modifier, such as public and private, later.
Class Naming Convention: A class name shall be a noun or a noun phrase made up of several words. All the words shall be initial
capitalized camelcase. Use a singular noun for class name. Choose a meaningful and selfdescriptive classname. For examples,
SoccerPlayer, HttpProxyServer, FileInputStream, PrintStream and SocketFactory.
For examples, suppose that we have a class called Circle, we can create instances of Circle as follows:
//Declare3instancesoftheclassCircle,c1,c2,andc3
Circlec1,c2,c3;//Theyholdaspecialvaluecallednull
//Constructtheinstancesvianewoperator
c1=newCircle();
c2=newCircle(2.0);
c3=newCircle(3.0,"red");
//YoucanDeclareandConstructinthesamestatement
Circlec4=newCircle();
When an instance is declared but not constructed, it holds a special value called null.
2.5Dot . Operator
The variables and methods belonging to a class are formally called member variables and member methods. To reference a member
variable or method, you must:
1. First identify the instance you are interested in, and then,
2. Use the dot operator . to reference the desired member variable or method.
For example, suppose that we have a class called Circle, with two member variables radius and color and two member methods
getRadius() and getArea(). We have created three instances of the class Circle, namely, c1, c2 and c3. To invoke the method
getArea(), you must first identity the instance of interest, says c2, then use the dot operator, in the form of c2.getArea().
For example,
//SupposethattheclassCirclehasvariablesradiusandcolor,
//andmethodsgetArea()andgetRadius().
//Declareandconstructinstancesc1andc2oftheclassCircle
Circlec1=newCircle();
Circlec2=newCircle();
//Invokemembermethodsfortheinstancec1viadotoperator
System.out.println(c1.getArea());
System.out.println(c1.getRadius());
//Referencemembervariablesforinstancec2viadotoperator
c2.radius=5.0;
c2.color="blue";
Calling getArea() without identifying the instance is meaningless, as the radius is unknown there could be many instances of Circle
each maintaining its own radius. Furthermore, c1.getArea() and c2.getArea() are likely to produce different results.
In general, suppose there is a class called AClass with a member variable called aVariable and a member method called aMethod(). An
instance called anInstance is constructed for AClass. You use anInstance.aVariable and anInstance.aMethod().
2.6Member Variables
A member variable has a name or identifier and a type; and holds a value of that particular type as descried in the earlier chapter.
Variable Naming Convention: A variable name shall be a noun or a noun phrase made up of several words. The first word is in
lowercase and the rest of the words are initialcapitalized camelcase, e.g., fontSize, roomNumber, xMax, yMin and xTopLeft.
[AccessControlModifier]typevariableName[=initialValue];
[AccessControlModifier]typevariableName1[=initialValue1][,typevariableName2[=initialValue2]]...;
For example,
privatedoubleradius;
publicintlength=1,width=1;
2.7Member Methods
A method as described in the earlier chapter:
1. receives arguments from the caller,
2. performs the operations defined in the method body, and
3. returns a piece of result or void to the caller.
[AccessControlModifier]returnTypemethodName([parameterList]){
//methodbodyorimplementation
......
}
For examples:
//ReturntheareaofthisCircleinstance
publicdoublegetArea(){
returnradius*radius*Math.PI;
}
Method Naming Convention: A method name shall be a verb, or a verb phrase made up of several words. The first word is in
lowercase and the rest of the words are initialcapitalized camelcase. For example, getArea(), setRadius(), getParameterValues(),
hasNext().
Variable name vs. Method name vs. Class name : A variable name is a noun, denoting an attribute; while a method name is a
verb, denoting an action. They have the same naming convention the first word in lowercase and the rest are initialcapitalized.
Nevertheless, you can easily distinguish them from the context. Methods take arguments in parentheses possibly zero arguments with
empty parentheses, but variables do not. In this writing, methods are denoted with a pair of parentheses, e.g., println(), getArea() for
clarity.
Three instances of Circles, called c1, c2, and c3, shall be constructed with their respective data members, as shown in the instance
diagrams.
Circle.java
1 /*
2 *TheCircleclassmodelsacirclewitharadiusandcolor.
3 */
4 publicclassCircle{//Saveas"Circle.java"
5 //Privateinstancevariables
6 privatedoubleradius;
7 privateStringcolor;
8
9 //Constructors(overloaded)
10 publicCircle(){//1stConstructor
11 radius=1.0;
12 color="red";
13 }
14 publicCircle(doubler){//2ndConstructor
15 radius=r;
16 color="red";
17 }
18 publicCircle(doubler,Stringc){//3rdConstructor
19 radius=r;
20 color=c;
21 }
22
23 //Publicmethods
24 publicdoublegetRadius(){
25 returnradius;
26 }
27 publicStringgetColor(){
28 returncolor;
29 }
30 publicdoublegetArea(){
31 returnradius*radius*Math.PI;
32 }
33 }
Notice that the Circle class does not have a main() method. Hence, it is NOT a standalone program and you cannot run the Circle
class by itself. The Circle class is meant to be a building block to be used in other programs.
TestCircle.java
We shall now write another class called TestCircle, which uses the Circle class. The TestCircle class has a main() method and can
be executed.
1 /*
2 *ATestDriverforthe"Circle"class
3 */
4 publicclassTestCircle{//Saveas"TestCircle.java"
5 publicstaticvoidmain(String[]args){//Programentrypoint
6 //DeclareandConstructaninstanceoftheCircleclasscalledc1
7 Circlec1=newCircle(2.0,"blue");//Use3rdconstructor
8 System.out.println("Theradiusis:"+c1.getRadius());//usedotoperatortoinvokemembermethods
9 System.out.println("Thecoloris:"+c1.getColor());
10 System.out.printf("Theareais:%.2f%n",c1.getArea());
11
12 //DeclareandConstructanotherinstanceoftheCircleclasscalledc2
13 Circlec2=newCircle(2.0);//Use2ndconstructor
14 System.out.println("Theradiusis:"+c2.getRadius());
15 System.out.println("Thecoloris:"+c2.getColor());
16 System.out.printf("Theareais:%.2f%n",c2.getArea());
17
18 //DeclareandConstructyetanotherinstanceoftheCircleclasscalledc3
19 Circlec3=newCircle();//Use1stconstructor
20 System.out.println("Theradiusis:"+c3.getRadius());
21 System.out.println("Thecoloris:"+c3.getColor());
22 System.out.printf("Theareais:%.2f%n",c3.getArea());
23 }
24 }
Theradiusis:2.0
Thecoloris:blue
Theareais:12.57
Theradiusis:2.0
Thecoloris:red
Theareais:12.57
Theradiusis:1.0
Thecoloris:red
Theareais:3.14
2.9Constructors
A constructor is a special method that has the same method name as the class name. In the above Circle class, we define three
overloaded versions of constructor Circle(......). A constructor is used to construct and initialize all the member variables. To
construct a new instance of a class, you need to use a special "new" operator followed by a call to one of the constructors. For example,
Circlec1=newCircle();
Circlec2=newCircle(2.0);
Circlec3=newCircle(3.0,"red");
Constructor can only be invoked via the "new" operator. It can only be used once to initialize the instance constructed. Once an
instance is constructed, you cannot call the constructor anymore.
Constructors are not inherited to be explained later.
Default Constructor : A constructor with no parameter is called the default constructor. It initializes the member variables to their
default value. For example, the Circle() in the above example initialize member variables radius and color to their default value.
Example: The method average() has 3 versions, with different parameter lists. The caller can invoke the chosen version by supplying
the matching arguments.
1 /*
2 *ExampletoillustrateMethodOverloading
3 */
4 publicclassTestMethodOverloading{
5 publicstaticintaverage(intn1,intn2){//versionA
6 System.out.println("RunversionA");
7 return(n1+n2)/2;
8 }
9
10 publicstaticdoubleaverage(doublen1,doublen2){//versionB
11 System.out.println("RunversionB");
12 return(n1+n2)/2;
13 }
14
15 publicstaticintaverage(intn1,intn2,intn3){//versionC
16 System.out.println("RunversionC");
17 return(n1+n2+n3)/3;
18 }
19
20 publicstaticvoidmain(String[]args){
21 System.out.println(average(1,2));//UseA
22 System.out.println(average(1.0,2.0));//UseB
23 System.out.println(average(1,2,3));//UseC
24 System.out.println(average(1.0,2));//UseBint2implicitlycastedtodouble2.0
25 //average(1,2,3,4);//CompilationErrorNomatchingmethod
26 }
27 }
Circle()
Circle(doubler)
Circle(doubler,Stringc)
Depending on the actual argument list used when invoking the method, the matching constructor will be invoked. If your argument list
does not match any one of the methods, you will get a compilation error.
For example, in the above Circle definition, the member variable radius is declared private. As the result, radius is accessible inside
the Circle class, but NOT in the TestCircle class. In other words, you cannot use "c1.radius" to refer to c1's radius in TestCircle.
Try inserting the statement "System.out.println(c1.radius)" in TestCircle and observe the error message.
Try changing radius to public in the Circle class, and rerun the above statement.
On the other hand, the method getRadius() is declared public in the Circle class. Hence, it can be invoked in the TestCircle class.
UML Notation: In UML class diagram, public members are denoted with a "+"; while private members with a "".
Member variables of a class are typically hidden from the outside word i.e., the other classes, with private access control modifier.
Access to the member variables are provided via public assessor methods, e.g., getRadius() and getColor().
This follows the principle of information hiding. That is, objects communicate with each others using welldefined interfaces public
methods. Objects are not allowed to know the implementation details of others. The implementation details are hidden or encapsulated
within the class. Information hiding facilitates reuse of the class.
Rule of Thumb: Do not make any variable , unless you have a good reason.
Rule of Thumb: Do not make any variable public, unless you have a good reason.
To allow other classes to modify the value of a private variable says xxx, we provide a set method or setter or mutator method called
setXxx(). A set method could provide data validation such as range checking, or transform the raw data into the internal
representation.
For example, in our Circle class, the variables radius and color are declared private. That is to say, they are only accessible within the
Circle class and not visible in any other classes, such as the TestCircle class. You cannot access the private variables radius and
color from the TestCircle class directly via says c1.radius or c1.color. The Circle class provides two public accessor methods,
namely, getRadius() and getColor(). These methods are declared public. The class TestCircle can invoke these public accessor
methods to retrieve the radius and color of a Circle object, via says c1.getRadius() and c1.getColor().
There is no way you can change the radius or color of a Circle object, after it is constructed in the TestCircle class. You cannot issue
statements such as c1.radius=5.0 to change the radius of instance c1, as radius is declared as private in the Circle class and is
not visible to other classes including TestCircle.
If the designer of the Circle class permits the change the radius and color after a Circle object is constructed, he has to provide the
appropriate set methods or setters or mutator methods, e.g.,
//Setterforcolor
publicvoidsetColor(StringnewColor){
color=newColor;
}
//Setterforradius
publicvoidsetRadius(doublenewRadius){
radius=newRadius;
}
With proper implementation of information hiding, the designer of a class has full control of what the user of the class can and cannot do.
2.14Keyword "this"
You can use keyword "this" to refer to this instance inside a class definition.
publicclassCircle{
doubleradius;//Membervariablecalled"radius"
publicCircle(doubleradius){//Method'sargumentalsocalled"radius"
this.radius=radius;
//"radius=radius"doesnotmakesense!
//"this.radius"referstothisinstance'smembervariable
//"radius"resolvedtothemethod'sargument.
}
...
}
In the above codes, there are two identifiers called radius a member variable of the class and the method's argument. This causes
naming conflict. To avoid the naming conflict, you could name the method's argument r instead of radius. However, radius is more
approximate and meaningful in this context. Java provides a keyword called this to resolve this naming conflict. "this.radius" refers to
the member variable; while "radius" resolves to the method's argument.
Using the keyword "this", the constructor, getter and setter methods for a private variable called xxx of type T are as follows:
publicclassAaa{
//AprivatevariablenamedxxxofthetypeT
privateTxxx;
//Constructor
publicAaa(Txxx){
this.xxx=xxx;
}
//AgetterforvariablexxxoftypeTreceivesnoargumentandreturnavalueoftypeT
publicTgetXxx(){
returnxxx;//or"returnthis.xxx"forclarity
}
//AsetterforvariablexxxoftypeTreceivesaparameteroftypeTandreturnvoid
publicvoidsetXxx(Txxx){
this.xxx=xxx;
}
}
For a boolean variable xxx, the getter shall be named isXxx() or hasXxx(), which is more meaningful than getXxx(). The setter
remains setXxx().
//Privatebooleanvariable
privatebooleanxxx;
//Getter
publicbooleanisXxx(){
returnxxx;//or"returnthis.xxx"forclarity
}
//Setter
publicvoidsetXxx(booleanxxx){
this.xxx=xxx;
}
More on "this"
this.varName refers to varName of this instance; this.methodName(...) invokes methodName(...) of this instance.
In a constructor, we can use this(...) to call another constructor of this class.
Inside a method, we can use the statement "returnthis" to return this instance to the caller.
2.15Method toString()
Every welldesigned Java class should have a public method called toString() that returns a string description of this instance. You
can invoke the toString() method explicitly by calling anInstanceName.toString(), or implicitly via println() or String
concatenation operator '+'. That is, running println(anInstance) invokes the toString() method of that instance implicitly.
For example, include the following toString() method in our Circle class:
//ReturnaStringdescriptionofthisinstance
publicStringtoString(){
return"Circle[radius="+radius+",color="+color+"]";
}
In your TestCircle class, you can get a description of a Circle instance via:
Circlec1=newCircle();
System.out.println(c1.toString());//ExplicitlycallingtoString()
System.out.println(c1);//Implicitcalltoc1.toString()
System.out.println("c1is:"+c1);//'+'invokestoString()togetaStringbeforeconcatenation
publicStringtoString(){......}
2.16Constants final
Constants are variables defined with the modifier final. A final variable can only be assigned once and its value cannot be modified
once assigned. For example,
publicfinaldoubleX_REFERENCE=1.234;
privatefinalintMAX_ID=9999;
MAX_ID=10000;//error:cannotassignavaluetofinalvariableMAX_ID
//Youneedtoinitializeafinalmembervariableduringdeclaration
privatefinalintSIZE;//error:variableSIZEmightnothavebeeninitialized
Constant Naming Convention: A constant name is a noun, or a noun phrase made up of several words. All words are in uppercase
separated by underscores '_', for examples, X_REFERENCE, MAX_INTEGER and MIN_VALUE.
Advanced Notes:
1. A final primitive variable cannot be reassigned a new value.
Circle.java
1 /*
2 *TheCircleclassmodelsacirclewitharadiusandcolor.
3 */
4 publicclassCircle{//Saveas"Circle.java"
5 //Thepublicconstants
6 publicstaticfinaldoubleDEFAULT_RADIUS=8.8;
7 publicstaticfinalStringDEFAULT_COLOR="red";
8
9 //Theprivateinstancevariables
10 privatedoubleradius;
11 privateStringcolor;
12
13 //The(overloaded)constructors
14 publicCircle(){//1st(default)Constructor
15 this.radius=DEFAULT_RADIUS;
16 this.color=DEFAULT_COLOR;
17 }
18 publicCircle(doubleradius){//2ndConstructor
19 this.radius=radius;
20 this.color=DEFAULT_COLOR;
21 }
22 publicCircle(doubleradius,Stringcolor){//3rdConstructor
23 this.radius=radius;
24 this.color=color;
25 }
26
27 //Thepublicgettersandsettersfortheprivatevariables
28 publicdoublegetRadius(){
29 returnthis.radius;
30 }
31 publicvoidsetRadius(doubleradius){
32 this.radius=radius;
33 }
34 publicStringgetColor(){
35 returnthis.color;
36 }
37 publicvoidsetColor(Stringcolor){
38 this.color=color;
39 }
40
41 //ThetoString()returnsaStringdescriptionofthisinstance
42 publicStringtoString(){
43 return"Circle[radius="+radius+",color="+color+"]";
44 }
45
46 //ReturntheareaofthisCircle
47 publicdoublegetArea(){
48 returnradius*radius*Math.PI;
49 }
50
51 //ReturnthecircumferenceofthisCircle
52 publicdoublegetCircumference(){
53 return2.0*radius*Math.PI;
54 }
55 }
//TestSettersandGetters
c1.setRadius(2.2);
c1.setColor("green");
System.out.println(c1);//toString()toinspectthemodifiedinstance
System.out.println("Theradiusis:"+c1.getRadius());
System.out.println("Thecoloris:"+c1.getColor());
//TestgetArea()andgetCircumference()
System.out.printf("Theareais:%.2f%n",c1.getArea());
System.out.printf("Thecircumferenceis:%.2f%n",c1.getCircumference());
}
}
Circle[radius=1.1,color=blue]
Circle[radius=2.2,color=red]
Circle[radius=8.8,color=red]
Circle[radius=2.2,color=green]
Radiusis:2.2
Coloris:green
Areais:15.21
Circumferenceis:13.82
A toString(), which returns "A/Cno:xxx,Balance=$xxx.xx", with balance rounded to two decimal places.
Write the Account class and a test driver to test all the public methods.
A/Cno:1234,Balance=99.99
A/Cno:8888,Balance=0.00
A/Cno:1234,Balance=88.88
AccountNumberis:1234
Balanceis:88.88
A/Cno:1234,Balance=98.88
A/Cno:1234,Balance=93.88
amountwithdrawnexceedsthecurrentbalance!
A/Cno:1234,Balance=93.88
A Date class models a calendar date with day, month and year, is designed as shown in the class diagram. It contains the following
members:
3 private instance variables day, month, and year.
Constructors, public getters and setters for the private instance variables.
A method setDate(), which sets the day, month and year.
A toString(), which returns "DD/MM/YYYY", with leading zero for DD and MM if applicable.
Write the Date class and a test driver to test all the public methods. No Input validations are required for day, month, and year.
//Theconstructors
publicDate(intyear,intmonth,intday){
//Noinputvalidation
this.year=year;
this.month=month;
this.day=day;
}
//Thepublicgetters/settersfortheprivatevariables
publicintgetYear(){
returnthis.year;
}
publicintgetMonth(){
returnthis.month;
}
publicintgetDay(){
returnthis.day;
}
publicvoidsetYear(intyear){
this.year=year;//Noinputvalidation
}
publicvoidsetMonth(intmonth){
this.month=month;//Noinputvalidation
}
publicvoidsetDay(intday){
this.day=day;//Noinputvalidation
}
//Return"MM/DD/YYYY"withleadingzeroforMMandDD.
publicStringtoString(){
//UsebuiltinfunctionString.format()toformaformattedString
returnString.format("%02d/%02d/%4d",month,day,year);
//Specifier"0"toprintleadingzeros,ifavailable.
}
//Setyear,monthanddayNoinputvalidation
publicvoidsetDate(intyear,intmonth,intday){
this.year=year;
this.month=month;
this.day=day;
}
}
04/06/2016
12/23/2012
Yearis:2012
Monthis:12
Dayis:23
01/02/2988
Write the Time class and a test driver to test all the public methods. No input validations are required.
//Theconstructors(overloaded)
publicTime(intsecond,intminute,inthour){
//Noinputvalidation
this.second=second;
this.minute=minute;
this.hour=hour;
}
publicTime(){//thedefaultconstructor
this.second=0;
this.minute=0;
this.hour=0;
}
//Thepublicgetters/settersfortheprivatevariables.
publicintgetSecond(){
returnthis.second;
}
publicintgetMinute(){
returnthis.minute;
}
publicintgetHour(){
returnthis.hour;
}
publicvoidsetSecond(intsecond){
this.second=second;//Noinputvalidation
}
publicvoidsetMinute(intminute){
this.minute=minute;//Noinputvalidation
}
publicvoidsetHour(inthour){
this.hour=hour;//Noinputvalidation
}
//Return"hh:mm:ss"withleadingzeros.
publicStringtoString(){
//UsebuiltinfunctionString.format()toformaformattedString
returnString.format("%02d:%02d:%02d",hour,minute,second);
//Specifier"0"toprintleadingzeros,ifavailable.
}
//Setsecond,minuteandhour
publicvoidsetTime(intsecond,intminute,inthour){
//Noinputvalidation
this.second=second;
this.minute=minute;
this.hour=hour;
}
//Incrementthisinstancebyonesecond,andreturnthisinstance.
publicTimenextSecond(){
++second;
if(second>=60){
second=0;
++minute;
if(minute>=60){
minute=0;
++hour;
if(hour>=24){
hour=0;
}
}
}
returnthis;//Return"this"instance,tosupportchaining
//e.g.,t1.nextSecond().nextSecond()
}
}
//TestSettersandGetters
t1.setHour(4);
t1.setMinute(5);
t1.setSecond(6);
System.out.println(t1);//runtoString()toinspectthemodifiedinstance
System.out.println("Houris:"+t1.getHour());
System.out.println("Minuteis:"+t1.getMinute());
System.out.println("Secondis:"+t1.getSecond());
//TestsetTime()
t1.setTime(58,59,23);
System.out.println(t1);//toString()
//TestnextSecond()andchaining
System.out.println(t1.nextSecond());//ReturnaninstanceofTime.InvokeTime'stoString()
System.out.println(t1.nextSecond().nextSecond().nextSecond());
}
}
03:02:01
00:00:00
04:05:06
Houris:4
Minuteis:5
Secondis:6
23:59:58
23:59:59
00:00:02
/*
*TheTimeclassmodelsatimeinstancewithsecond,minuteandhour.
*Thisclassperformsinputvalidations.
*/
publicclassTime{
//Theprivateinstancevariableswithinputvalidations.
privateintsecond;//[0,59]
privateintminute;//[0,59]
privateinthour;//[0,23]
//Inputvalidationsaredoneinthesetters.
//Alltheothermethods(suchasconstructorsandsetTime())invoke
//thesesetterstoperforminputvalidationstoavoidcodeduplication.
publicvoidsetSecond(intsecond){
if(second>=0&&second<=59){
this.second=second;
}else{
this.second=0;//Setto0andprinterrormessage
System.out.println("error:invalidsecond");
}
}
publicvoidsetMinute(intminute){
if(minute>=0&&minute<=59){
this.minute=minute;
}else{
this.minute=0;
System.out.println("error:invalidminute");
}
}
publicvoidsetHour(inthour){
if(hour>=0&&hour<=23){
this.hour=hour;
}else{
this.hour=0;
System.out.println("error:invalidhour");
}
}
//Setsecond,minuteandhour.
publicvoidsetTime(intsecond,intminute,inthour){
//Invokesetterstodoinputvalidation
this.setSecond(second);
this.setMinute(minute);
this.setHour(hour);
}
//Constructors
publicTime(intsecond,intminute,inthour){
//Invokesetterstodoinputvaliation
this.setTime(second,minute,hour);
}
publicTime(){//Thedefaultconstructor
this.second=0;
this.minute=0;
this.hour=0;
}
//Thepublicgetters
publicintgetSecond(){
returnthis.second;
}
publicintgetMinute(){
returnthis.minute;
}
publicintgetHour(){
returnthis.hour;
}
//Return"hh:mm:ss"withleadingzeros.
publicStringtoString(){
returnString.format("%02d:%02d:%02d",hour,minute,second);
}
//Incrementthisinstancebyonesecond,returnthisinstance
publicTimenextSecond(){
++second;
if(second==60){//Wearesurethatsecond<=60here!
second=0;
++minute;
if(minute==60){
minute=0;
++hour;
if(hour==24){
hour=0;
}
}
}
returnthis;//Returnthisinstance,tosupportchaining
}
}
3.5EG. 5 Advanced: The Time Class with Input Validation via Exception Handling
In the previous example, we print a error message and set the variable to 0, if the input is invalid. This is less than perfect. The proper way
to handle invalid inputs is via the socalled exception handling mechanism.
/*
*TheTimeclassmodelsatimeinstancewithsecond,minuteandhour.
*Thisclassperformsinputvalidationsusingexceptionhandling.
*/
publicclassTime{
//Theprivateinstancevariableswithinputvalidation
privateintsecond;//[0,59]
privateintminute;//[0,59]
privateinthour;//[0,23]
//Inputvalidationsaredoneinsetters.
//Alltheothermethods(suchasconstructorsandsetTime())invoke
//thesesetterstoperforminputvalidationtoavoidcodeduplication.
publicvoidsetSecond(intsecond){
if(second>=0&&second<=59){
this.second=second;
}else{
thrownewIllegalArgumentException("Invalidsecond!");
}
}
publicvoidsetMinute(intminute){
if(minute>=0&&minute<=59){
this.minute=minute;
}else{
thrownewIllegalArgumentException("Invalidminute!");
}
}
publicvoidsetHour(inthour){
if(hour>=0&&hour<=23){
this.hour=hour;
}else{
thrownewIllegalArgumentException("Invalidhour!");
}
}
//Setsecond,minuteandhour
publicvoidsetTime(intsecond,intminute,inthour){
//Invokesetterstodoinputvaliation
this.setSecond(second);
this.setMinute(minute);
this.setHour(hour);
}
//Theconstructors(overloaded)
publicTime(intsecond,intminute,inthour){
//Invokesetterstodoinputvaliation
this.setTime(second,minute,hour);
}
publicTime(){//Thedefaultconstructor
this.second=0;
this.minute=0;
this.hour=0;
}
//Getters
publicintgetSecond(){
returnthis.second;
}
publicintgetMinute(){
returnthis.minute;
}
publicintgetHour(){
returnthis.hour;
}
//Return"hh:mm:ss"withleadingzeros.
publicStringtoString(){
returnString.format("%02d:%02d:%02d",hour,minute,second);
}
//Incrementthisinstancebyonesecond,returnthisinstance
publicTimenextSecond(){
++second;
if(second==60){
second=0;
++minute;
if(minute==60){
minute=0;
++hour;
if(hour==24){
hour=0;
}
}
}
returnthis;//Returnthisinstance,tosupportchaining
}
}
Exception Handling
What to do if an invalid hour, minute or second was given as input? Print an error message? Terminate the program Abruptly? Continue
operation by setting the parameter to its default? This is a really hard decision and there is no perfect solution that suits all situations.
In Java, instead of printing an error message, you can throw an socalled Exception object such as IllegalArgumentException to the
caller, and let the caller handles the exception gracefully. For example,
//Throwanexceptionifinputisinvalid
publicvoidsetHour(inthour){
if(hour>=0&&hour<=23){
this.hour=hour;
}else{
thrownewIllegalArgumentException("Invalidhour!");
}
}
The caller can use the trycatch construct to handle the exception gracefully. For example,
try{
Timet=newTime(60,59,12);//Invalidinput,throwexception
//Skiptheremainingstatementsintry,gotocatch
System.out.println("Thisandtheremainingwillbeskipped,ifexceptionoccurs");
}catch(IllegalArgumentExceptionex){
//Youhavetheopportunitytodosomethingtorecoverfromtheerror.
ex.printStackTrace();
}
//Continuethenextstatementafter"try"or"catch".
The statements in the tryclause will be executed. If all the statements in the tryclause are successful, the catchclause is ignored, and
execution continues to the next statement after trycatch. However, if one of the statement in the tryclause throws an exception in
this case, an IllegalArgumentException, the rest of tryclause will be skipped, and the execution will be transferred to the catch
clause. The program always continues to the next statement after the trycatch instead of abruptly terminated.
//Invalidinputs
//Timet2=newTime(60,59,12);
//programterminatesabruptly
//NOTcontinuetothenextstatement
//InvalidinputsHandledgracefullyviatrycatch
try{
Timet3=newTime(60,59,12);//throwIllegalArgumentException
//Skiptheremainingstatementsintry,gotocatch
System.out.println("Thislinewillbeskipped,ifexceptionoccurs");
}catch(IllegalArgumentExceptionex){
//Youhavetheopportunitytodosomethingtorecoverfromtheerror.
ex.printStackTrace();
}
//Continuethenextstatementafter"try"or"catch".
System.out.println("Continueafterexception!");
}
}
Without the proper trycatch, the "Timet2" will abruptly terminate the program, i.e., the rest of the program will not be run try it by
uncommenting the statement. With proper trycatch handling, the program can continue its operation i.e., graceful handling of
exception.
A Point class models a 2D point at (x,y), as shown in the class diagram. It contains the following members:
2 private instance variables x and y, which maintain the location of the point.
distance(Pointanother) returns the distance from this instance to the given Point instance called another.
distance() returns the distance from this instance to (0,0).
//Thepublicgettersandsetters
publicintgetX(){
returnthis.x;
}
publicvoidsetX(intx){
this.x=x;
}
publicintgetY(){
returnthis.y;
}
publicvoidsetY(inty){
this.y=y;
}
//Return"(x,y)"
publicStringtoString(){
return"("+this.x+","+this.y+")";
}
//Returna2elementintarraycontainingxandy.
publicint[]getXY(){
int[]results=newint[2];
results[0]=this.x;
results[1]=this.y;
returnresults;
}
//Setbothxandy.
publicvoidsetXY(intx,inty){
this.x=x;
this.y=y;
}
//Returnthedistancefromthisinstancetothegivenpointat(x,y).
publicdoubledistance(intx,inty){
intxDiff=this.xx;
intyDiff=this.yy;
returnMath.sqrt(xDiff*xDiff+yDiff*yDiff);
}
//ReturnthedistancefromthisinstancetothegivenPointinstance(calledanother).
publicdoubledistance(Pointanother){
intxDiff=this.xanother.x;
intyDiff=this.yanother.y;
returnMath.sqrt(xDiff*xDiff+yDiff*yDiff);
}
//Returnthedistancefromthisinstanceto(0,0).
publicdoubledistance(){
returnMath.sqrt(this.x*this.x+this.y*this.y);
}
}
//TestSettersandGetters
p1.setX(3);
p1.setY(4);
System.out.println(p1);//runtoString()toinspectthemodifiedinstance
System.out.println("Xis:"+p1.getX());
System.out.println("Yis:"+p1.getY());
//TestsetXY()andgetXY()
p1.setXY(5,6);
System.out.println(p1);//toString()
System.out.println("Xis:"+p1.getXY()[0]);
System.out.println("Yis:"+p1.getXY()[1]);
//Testthe3overloadedversionsofdistance()
p2.setXY(10,11);
System.out.printf("Distanceis:%.2f%n",p1.distance(10,11));
System.out.printf("Distanceis:%.2f%n",p1.distance(p2));
System.out.printf("Distanceis:%.2f%n",p2.distance(p1));
System.out.printf("Distanceis:%.2f%n",p1.distance());
}
}
A Ball class models a moving ball, is designed as shown in the class diagram. It contains the following members:
4 private variables x, y, xStep, yStep, which maintain the position of the ball and the displacement per move step.
Constructors, getters and setters.
Method setXY() and setXYStep(), which sets the position and step size of the ball; and getXY() and getXYSpeed().
//Constructor
publicBall(doublex,doubley,doublexStep,doubleyStep){
this.x=x;
this.y=y;
this.xStep=xStep;
this.yStep=yStep;
}
//Thepublicgettersandsetters
publicdoublegetX(){
returnthis.x;
}
publicvoidsetX(doublex){
this.x=x;
}
publicdoublegetY(){
returnthis.y;
}
publicvoidsetY(doubley){
this.y=y;
}
publicdoublegetXStep(){
returnthis.xStep;
}
publicvoidsetXStep(doublexStep){
this.xStep=xStep;
}
publicdoublegetYStep(){
returnthis.yStep;
}
publicvoidsetYStep(doubleyStep){
this.yStep=yStep;
}
//ReturnaStringtodescribethisinstance
publicStringtoString(){
return"Ball@("+x+","+y+"),speed=("+xStep+","+yStep+")";
}
publicdouble[]getXY(){
double[]results=newdouble[2];
results[0]=this.x;
results[1]=this.y;
returnresults;
}
publicvoidsetXY(doublex,doubley){
this.x=x;
this.y=y;
}
publicdouble[]getXYStep(){
double[]results=newdouble[2];
results[0]=this.xStep;
results[1]=this.yStep;
returnresults;
}
publicvoidsetXYStep(doublexStep,doubleyStep){
this.xStep=xStep;
this.yStep=yStep;
}
//MoveastepbyincrementxandybyxStepandyStep,respectively.
//Return"this"instancetosupportchainingoperation.
publicBallmove(){
x+=xStep;
y+=yStep;
returnthis;
}
}
//TestSettersandGetters
b1.setX(3);
b1.setY(4);
b1.setXStep(13);
b1.setYStep(14);
System.out.println(b1);//runtoString()toinspectthemodifiedinstance
System.out.println("xis:"+b1.getX());
System.out.println("yis:"+b1.getY());
System.out.println("xStepis:"+b1.getXStep());
System.out.println("yStepis:"+b1.getYStep());
//TestsetXY(),getXY(),setXYStep(),getXYStep()
b1.setXY(5,6);
b1.setXYStep(15,16);
System.out.println(b1);//toString()
System.out.println("xis:"+b1.getXY()[0]);
System.out.println("yis:"+b1.getXY()[1]);
System.out.println("xStepis:"+b1.getXYStep()[0]);
System.out.println("yStepis:"+b1.getXYStep()[1]);
//Testmove()andchaining
System.out.println(b1.move());//toString()
System.out.println(b1.move().move().move());
}
}
Try: To support bouncing ball within a rectangular boundary, add a variable called radius, and methods reflectHorizontal() and
reflectVertical().
Suppose that our application requires us to model students. A student has a name and an address. We are required to keep track of the
courses taken by each student, together with the grades between 0 and 100 for each of the courses. A student shall not take more than
30 courses for the entire program. We are required to print all course grades, and also the overall average grade.
We can design the Student class as shown in the class diagram. It contains the following members:
private instance variables name String, address String, numCourses int, course String[30] and grades int[30]. The
numCourses keeps track of the number of courses taken by this student so far. The courses and grades are two parallel arrays,
storing the courses taken e.g., {"IM101","IM102","IM103"} and their respective grades e.g. {89,56,98}.
A constructor that constructs an instance with the given name and Address. It also constructs the courses and grades arrays and set
the numCourses to 0.
Getters for name and address; setter for address. No setter is defined for name as it is not designed to be changed.
A toString(), which prints "name(address)".
A method addCourseGrade(course,grade), which appends the given course and grade into the courses and grades arrays,
respectively; and increments numCourses.
A method printGrades(), which prints "namecourse1:grade1,course2:grade2,...".
A method getAverageGrade(), which returns the average grade of all the courses taken.
TanAhTeck(1HappyAve)
TanAhTeck(8KgJava)
TanAhTeck
8KgJava
TanAhTeckIM101:89IM102:57IM103:96
Theaveragegradeis80.67
Notes : We used arrays In this example, which has several limitations. Arrays need to be preallocated with a fixedlength. Furthermore,
we need two parallel arrays to keep track of two entities. There are advanced data structures that could represent these data better and
more efficiently.
3.9Exercises
LINK TO EXERCISES
Feedback, comments, corrections, and errata can be sent to Chua HockChuan (ehchua@ntu.edu.sg) | HOME