0% found this document useful (0 votes)
4 views7 pages

JavaLab 10 OOP1 Basics

Uploaded by

ananan27062015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

JavaLab 10 OOP1 Basics

Uploaded by

ananan27062015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

HaQT Java Programming

Lab 10. OOP-Basics

Writing Good Programs


The only way to learn programming is program, program and program. Learning programming
is like learning cycling, swimming or any other sports. You can’t learn by watching or reading
books. Start to program immediately. On the other hands, to improve your programming, you
need to read many books and study how the masters program.
It is easy to write programs that work. It is much harder to write programs that not only work
but also easy to maintain and understood by others – I call these good programs. In the real
world, writing program is not meaningful. You have to write good programs, so that others can
understand and maintain your programs.
Pay particular attention to:

1. Coding style:

• Read Java code convention: "Google Java Style Guide" or "Oracle Java Code Con-
ventions".

• Follow the Java Naming Conventions for variables, methods, and classes STRICTLY.
Use CamelCase for names. Variable and method names begin with lowercase, while
class names begin with uppercase. Use nouns for variables (e.g., radius) and class
names (e.g., Circle). Use verbs for methods (e.g., getArea(), isEmpty()).

• Use Meaningful Names: Do not use names like a, b, c, d, x, x1, x2, and x1688
- they are meaningless. Avoid single-alphabet names like i, j, k. They are easy to
type, but usually meaningless. Use single-alphabet names only when their meaning
is clear, e.g., x, y, z for co-ordinates and i for array index. Use meaningful names like
row and col (instead of x and y, i and j, x1 and x2), numStudents (not n), maxGrade,
size (not n), and upperbound (not n again). Differentiate between singular and plural
nouns (e.g., use books for an array of books, and book for each item).

• Use consistent indentation and coding style. Many IDEs (such as Eclipse / Net-
Beans) can re-format your source codes with a single click.

2. Program Documentation: Comment! Comment! and more Comment to explain your


code to other people and to yourself three days later.

3. The only way to learn programming is program, program and program on challenging
problems. The problems in this tutorial are certainly NOT challenging. There are tens of
thousands of challenging problems available – used in training for various programming
contests (such as International Collegiate Programming Contest (ICPC), International
Olympiad in Informatics (IOI)).

1
HaQT Java Programming

1 Exercises on Classes
1.1 An Introduction to Classes and Instances by Example - The Circle Class
This first exercise shall lead you through all the basic concepts in OOP.

A class called circle is designed as shown in the following class diagram. It contains:

• Two private instance variables: radius (of the type double) and color (of the type String), with
default value of 1.0 and "red", respectively.

• Two overloaded constructors - a default constructor with no argument, and a constructor which
takes a double argument for radius.

• Two public methods: getRadius() and getArea(), which return the radius and area of this in-
stance, respectively.

The source codes for Circle.java is as follows:

1 /∗ ∗
∗ The C i r c l e c l a s s models a c i r c l e w i t h a r a d i u s and c o l o r .
3 ∗/
public class Circle { // Save a s " C i r c l e . j a v a "
5

// p r i v a t e i n s t a n c e v a r i a b l e , n o t a c c e s s i b l e from o u t s i d e this class


7 p r i v a t e double ra di us ;
private String color ;
9

// C o n s t r u c t o r s ( o v e r l o a d e d )
11 /∗ ∗
∗ Constructs a Circle instance with default value for radius and color
13 ∗/
public Circle () { // 1 s t ( d e f a u l t ) c o n s t r u c t o r
15 radius = 1.0;

2
HaQT Java Programming

c o l o r = " red " ;


17 }

19 /∗ ∗
∗ Constructs a Circle instance with the given radius and default color
21 ∗/
public C i r c l e ( double r ) { // 2nd c o n s t r u c t o r
23 radius = r ;
c o l o r = " red " ;
25 }

27 /∗ ∗
∗ Returns the r a d i u s
29 ∗/
p u b l i c double getRadius ( ) {
31 return radius ;
}
33

/∗ ∗
35 ∗ Returns the area o f this Circle instance
∗/
37 p u b l i c double getArea ( ) {
r e t u r n r a d i u s ∗ r a d i u s ∗Math . PI ;
39 }
}

Compile "Circle.java". Can you run the Circle class? Why?

• This Circle class does not have a main() method. Hence, it cannot be run directly. This Circle
class is a "building block" and is meant to be used in another program.

Let us write a test program called TestCircle (in another source file called TestCircle.java) which uses
the Circle class, as follows:

/∗ ∗
2 ∗ A Test Driver f o r the C i r c l e class
∗/
4 public class TestCircle { // Save a s " T e s t C i r c l e . j a v a "
public s t a t i c v o i d main ( S t r i n g [ ] args ) {
6 // D e c l a r e an i n s t a n c e o f C i r c l e class called circle1 .
// Construct the instance circle1 by invoking the "default" constructor
8 // which sets its radius and color to their default value .
Circle c i r c l e 1 = new C i r c l e ( ) ;
10 // I n v o k e p u b l i c methods on i n s t a n c e circle1 , v i a dot o p e r a t o r .
System . o u t . p r i n t l n ( "The c i r c l e has r a d i u s o f " + c i r c l e 1 . g e t R a d i u s ( )
12 + " and a r e a o f " + c i r c l e 1 . g e t A r e a ( ) ) ;
//The c i r c l e has r a d i u s o f 1 . 0 and a r e a o f 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3
14

3
HaQT Java Programming

// D e c l a r e an i n s t a n c e o f class circle called circle2 .


16 // Construct the instance circle2 by invoking the second constructor
// w i t h t h e g i v e n r a d i u s and d e f a u l t color .
18 Circle c i r c l e 2 = new C i r c l e ( 2 . 0 ) ;
// I n v o k e p u b l i c methods on i n s t a n c e circle2 , v i a dot o p e r a t o r .
20 System . o u t . p r i n t l n ( "The c i r c l e has r a d i u s o f " + c i r c l e 2 . g e t R a d i u s ( )
+ " and a r e a o f " + c i r c l e 2 . g e t A r e a ( ) ) ;
22 // The c i r c l e has r a d i u s o f 2 . 0 and a r e a o f 1 2 . 5 6 6 3 7 0 6 1 4 3 5 9 1 7 2
}
24 }

Now, run the TestCircle and study the results.

More Basic OOP Concepts

1. Constructor: Modify the class Circle to include a third constructor for constructing a Circle
instance with two arguments - a double for radius and a String for color.

// 3rd constructor to construct a new instance of Circle with the given radius and color
2 public C i r c l e ( double r , String c ) {
......
4 }

Modify the test program TestCircle to construct an instance of Circle using this constructor.

2. Getter: Add a getter for variable color for retrieving the color of this instance.

1 // G e t t e r f o r instance variable color


public String getColor () {
3 ......
}

Modify the test program to test this method.

3. public vs. private: In TestCircle, can you access the instance variable radius directly (e.g.,
System.out.println(circle1.radius)); or assign a new value to radius (e.g., circle1.radius = 5.0)?
Try it out and explain the error messages.

4. Setter: Is there a need to change the values of radius and color of a Circle instance after it is
constructed? If so, add two public methods called setters for changing the radius and color of a
Circle instance as follows:

4
HaQT Java Programming

1 // S e t t e r for instance variable radius


p u b l i c v o i d s e t R a d i u s ( d o u b l e newRadius ) {
3 r a d i u s = newRadius ;
}
5

// S e t t e r for instance variable color


7 p u b l i c v o i d s e t C o l o r ( S t r i n g newColor ) {
......
9 }

Modify the TestCircle to test these methods, e.g.,

Circle c i r c l e 4 = new C i r c l e ( ) ; // construct an instance of Circle


2 c i r c l e 4 . setRadius ( 5 . 5 ) ; // change r a d i u s
System . o u t . p r i n t l n ( " r a d i u s i s : " + c i r c l e 4 . getRadius ( ) ) ; // P r i n t r a d i u s
,→ v i a g e t t e r
4

c i r c l e 4 . setColor ( " green " ) ; // Change c o l o r


6 System . o u t . p r i n t l n ( " c o l o r i s : " + c i r c l e 4 . getColor () ) ; // P r i n t c o l o r
,→ v i a g e t t e r

8 // You c a n n o t do t h e f o l l o w i n g b e c a u s e s e t R a d i u s ( ) r e t u r n s v o i d ,
// which c a n n o t be p r i n t e d
10 System . o u t . p r i n t l n ( c i r c l e 4 . s e t R a d i u s ( 4 . 4 ) ) ;

5. Keyword "this": Instead of using variable names such as r (for radius) and c (for color) in
the methods’ arguments, it is better to use variable names radius (for radius) and color (for
color) and use the special keyword "this" to resolve the conflict between instance variables and
methods’ arguments. For example,

1 // I n s t a n c e v a r i a b l e
p r i v a t e double ra di us ;
3

/∗ ∗
5 ∗ Constructs a Circle instance with the given radius and default color
∗/
7 public C i r c l e ( double r adi us ) {
this . radius = radius ; // "this.radius" refers to the instance variable
9 // " r a d i u s " r e f e r s t o t h e method ’ s p a r a m e t e r
c o l o r = " red " ;
11 }

13 /∗ ∗
∗ Sets the radius to the given value

5
HaQT Java Programming

15 ∗/
p u b l i c void setRadius ( double r ad ius ) {
17 this . radius = radius ; // "this.radius" refers to the instance variable
// " r a d i u s " r e f e r s t o t h e method ’ s argument
19 }

Modify ALL the constructors and setters in the Circle class to use the keyword "this".

6. Method toString(): Every well-designed Java class should contain a public method called
toString() that returns a description of the instance (in the return type of String). The toString()
method can be called explicitly (via instanceName.toString()) just like any other method; or
implicitly through println(). If an instance is passed to the println(anInstance) method, the
toString() method of that instance will be invoked implicitly. For example, include the following
toString() methods to the Circle class:

/∗ ∗
2 ∗ Return a s e l f −d e s c r i p t i v e string of this i n s t a n c e in the
∗ form o f C i r c l e [ r a d i u s = ? , c o l o r = ? ]
4 ∗/
public String toString () {
6 r e t u r n "Circle[radius=" + r a d i u s + " c o l o r=" + c o l o r + " ] " ;
}

Try calling toString() method explicitly, just like any other method:

Circle c i r c l e 5 = new C i r c l e ( 5 . 5 ) ;
2 System . o u t . p r i n t l n ( c i r c l e 5 . t o S t r i n g ( ) ) ; // e x p l i c i t call

toString() is called implicitly when an instance is passed to println() method, for example,

1 Circle c i r c l e 6 = new C i r c l e ( 6 . 6 ) ;
System . o u t . p r i n t l n ( c i r c l e 6 . t o S t r i n g ( ) ) ; // e x p l i c i t call
3 System . o u t . p r i n t l n ( c i r c l e 6 ) ; // p r i n t l n ( ) calls toString ()
,→ i m p l i c i t l y , same a s above
System . o u t . p r i n t l n ( " O p e r a t o r ’+ ’ i n v o k e s t o S t r i n g ( ) t o o : " + c i r c l e 6 ) ; //
,→ ’+ ’ i n v o k e s t o S t r i n g ( ) t o o

The final class diagram for the Circle class is as follows:

6
HaQT Java Programming

You might also like