Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

JavaProgrammingForBeginners-CourseBook-71-80

The document discusses the implementation of multiplication tables in Java using method overloading and code reuse. It explains how to refactor code to avoid duplication and improve maintainability by utilizing more general methods. Additionally, it introduces Object Oriented Programming (OOP) concepts, emphasizing the importance of encapsulating data and behavior within objects.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JavaProgrammingForBeginners-CourseBook-71-80

The document discusses the implementation of multiplication tables in Java using method overloading and code reuse. It explains how to refactor code to avoid duplication and improve maintainability by utilizing more general methods. Additionally, it introduces Object Oriented Programming (OOP) concepts, emphasizing the importance of encapsulating data and behavior within objects.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

5 X 2 = 10

5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
8*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
6 X 11 = 66
6 X 12 = 72
6 X 13 = 78
6 X 14 = 84
6 X 15 = 90
6 X 16 = 96
6 X 17 = 102
6 X 18 = 108
6 X 19 = 114
6 X 20 = 120
Snippet-3 Explained
Humans make mistakes. The programmer missed out on changing the format symbol in the code for void
print(int, int) , but we don't recommend punishment. He could be overworked, or could only be the
maintainer of code someone else wrote years ago! The point is not to blame human flaws (there are many), but
to show how code duplication causes errors. This issue arises frequently with overloaded methods, point blank.
Instead of trying to change human nature, can we change our software? Let's have a closer look at print() :
The method void print() prints the multiplication table for 5 only, in the default range 1 to 10 .
The method void print(int) outputs the table for any number, but only in the fixed range from 1 to
10 .

The method void print(int,int,int) displays the table for any number, in any range of entries.
A Solution: Code Reuse
There is something huge we observe in the previous example. All overloaded versions of print() have nearly the
same code!
print(int) has wider usage potential than print() . The latter is a special case, as it prints only the for 5 .
We can achieve what print() does, by passing a fixed parameter value of 5 to print(int) . It's simple:
invoke print(int) within print() , by passing 5 to it.
The point to note is, that a more specialized function can be implemented-in-terms-of a more general
function.
Let's now reorganize this part of the code.
Snippet-4: Code Reuse
MultiplicationTable.java

public static void print() {


print(5);
}

public static void print(int number) {


for(int i=1; i<=10;i++) {
System.out.printf("%d * %d = %d", number, i, number*i).println();
}
}

MultiplicationRunner.java

package com.in28minutes.firstjavaproject;

public class MultiplicationRunner {


public static void main(String[] args) {
MultiplicationTable table = new MultiplicationTable();
table.print();
table.print(8);
}
}

Console Output
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
8*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
Snippet-4 Explained
When we call a method inside another, the method call statement is replaced by its body (with actual arguments
replacing the formal ones). In the new definition of int print() above, the code executed during the call will be:

for(int i=1; i<=10;i++) {


System.out.printf("%d * %d = %d", 5, i, 5*i).println();
}

Extending Code Reuse


The method int print(int,int,int) is a more general version of int print(int) . We can achieve what the
latter computes, by passing fixed range of indexes, namely 1 and 10 , as arguments to the former. have look
into he code that follows.
Snippet-5 : Extending code reuse
MultiplicationTable.java

public static void print(int number) {


print(number, 1, 10);
}

public static void print(int number, int from, int to) {


for(int i=from; i<=to;i++) {
System.out.printf("%d X %d = %d", number, i, number*i).println();
}
}

MultiplicationRunner.java
package com.in28minutes.firstjavaproject;

public class MultiplicationRunner {


public static void main(String[] args) {
MultiplicationTable table = new MultiplicationTable();
table.print(8);
table.print(6, 11, 20);
}
}

Console Output
8*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
6 * 11 = 66
6 * 12 = 72
6 * 13 = 78
6 * 14 = 84
6 * 15 = 90
6 * 16 = 96
6 * 17 = 102
6 * 18 = 108
6 * 19 = 114
6 * 20 = 120
Snippet-5 Explained
This example merely extended what we did in the previous example. We will will take this extension one level
further now! Yes, you guessed right. We will implement print() in terms of print(int,int,int) .
Snippet-6 : Extending code reuse (contd.)
MultiplicationTable.java
public static void print() {
print(5, 1, 10);
}

public static void print(int number, int from, int to) {


for(int i=from; i<=to;i++) {
System.out.printf("%d X %d = %d", number, i, number*i).println();
}
}

MultiplicationRunner.java

package com.in28minutes.firstjavaproject;

public class MultiplicationRunner {


public static void main(String[] args) {
MultiplicationTable table = new MultiplicationTable();
table.print();
table.print(6, 11, 20);
}
}

Console Output
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
6 * 11 = 66
6 * 12 = 72
6 * 13 = 78
6 * 14 = 84
6 * 15 = 90
6 * 16 = 96
6 * 17 = 102
6 * 18 = 108
6 * 19 = 114
6 * 20 = 120
Snippet-6 Explained
By extending the same logic of code reuse, the method int print(int,int,int) can be used to implement
the logic of int print() . Just pass in a number parameter 5 , as well as the fixed range parameters 1 and
10 , in a call to the former. int print() is thus implemented-in-terms-of int print(int,int,int) .

Our new version of class MultiplicationTable looks like this:


Snippet-7: Extending Code Reuse (Contd.)
MultiplicationTable.java

package com.in28minutes.firstjavaproject;

public class MultiplicationTable {


public static void print() {
print(5, 1, 10);
}

public static void print(int number) {


print(number, 1, 10);
}

public static void print(int number, int from, int to) {


for(int i=from; i<=to;i++) {
System.out.printf("%d X %d = %d", number, i, number*i).println();
}
}
}

The logic of the class MutliplicationRunner does not change at all:


MultiplicationRunner.java:

package com.in28minutes.firstjavaproject;

public class MultiplicationRunner {


public static void main(String[] args) {
MultiplicationTable table = new MultiplicationTable();
table.print();
table.print(8);
table.print(6, 11, 20);
}
}

Console Output
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
8*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
6 * 11 = 66
6 * 12 = 72
6 * 13 = 78
6 * 14 = 84
6 * 15 = 90
6 * 16 = 96
6 * 17 = 102
6 * 18 = 108
6 * 19 = 114
6 * 20 = 120
Snippet-7 Explained
Neat, isn't it! To make our program school kid friendly, we just need to change one character in the code, take a
peek below.
Snippet-8 : Extending Code Reuse (Contd.)
MultiplicationTable.java:

package com.in28minutes.firstjavaproject;

public class MultiplicationTable {


public static void print() {
print(5, 1, 10);
}
public static void print(int number) {
print(number, 1, 10);
}

public static void print(int number, int from, int to) {


for(int i=from; i<=to;i++) {
System.out.printf("%d X %d = %d", number, i, number*i).println();
}
}
}

Console Output
5X1=5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
8X1=8
8 X 2 = 16
8 X 3 = 24
8 X 4 = 32
8 X 5 = 40
8 X 6 = 48
8 X 7 = 56
8 X 8 = 64
8 X 9 = 72
8 X 10 = 80
6 X 11 = 66
6 X 12 = 72
6 X 13 = 78
6 X 14 = 84
6 X 15 = 90
6 X 16 = 96
6 X 17 = 102
6 X 18 = 108
6 X 19 = 114
6 X 20 = 120
Snippet-8 Explained
Software Development is an iterative process. The best code that we want to write does not happen at one go. It
starts off at a certain level, and can always be improved. More importantly, such improvements needs to be
remembered, to be applied again at different points in the same program, and across programs. This process is
called Code Refactoring. Thought we'd keep you posted.
Summary
In this step, we:
Explored how to reorganize the code for PMT-Challenge into a class
Understood that overloading works the same way for class methods
Observed that code reuse is possible across overloaded versions of a class method
Object Oriented Progamming (OOP)
How do you design great Object Oriented Programs?
Let's find out
Recommended Videos:
Object Oriented Progamming - Part 1 - https://www.youtube.com/watch?v=NOD802rMMCw
Object Oriented Progamming - Part 2 - https://www.youtube.com/watch?v=i6EztA-F8UI
Step 01: Object Oriented Progamming (OOP) - Basic Terminology
Let's consider a few examples before we get to Object Oriented Progamming.
Humans think in a step by step process.
Let's say I've to take a flight from London to New York. This is how I would think:
Take a cab to London Airport
Check in
Pass Security
Board the flight
Wish the Hostess
Take Off
Cruise
Land
Get off the plane
Take a cab to ..
Procedural programming is just a reflection of this thought process. A procedural program for above process would
look something like this:
takeACabToLondonAirport();
checkIn();
passSecurity();
boardPlane();
wishHostess();
takeOff();
cruiseMode();
land();
getOffPlane();
//...

Object Oriented Programming (OOP) brings in a new thought process around this.
How about thinking in terms of the different Actors? How about storing data related to each actor right beside
itself? How about giving them some responsiblity and let them do their own actions?
Here's how our program would look like when we think in terms of different actors and give them data and
responsibilities

Person
name
boardFlight(Plane flight), wishHostess (Hostess hostess), getOffFlight(Plane flight)

AirPlane
altitude, pilot, speed, flightMode
takeOff(), cruiseMode(), land()

Hostess
welcome()

Do not worry about the implementation details. Focus on the difference in approaches.
We have encapsulated data and methods into these entities, which are now called objects. We have defined object
boundaries, and what it can (and cannot) do.
An object has
State : Its data
Behavior : Its operations
The position of an Airplane can change over time. The operations that can be performed on an Airplane
include takeOff() , land() and cruiseMode() . Each of these actions can change its position . Therefore, an
object's behavior can affects its own state.
It's now time to introduce you to some core OOP terms, which will make our future discussions easier.
OOP Terminology
Let's visit and enhance the Planet example we had written a few sections ago. This time, let's also explore the
conceptual angle.
Planet

class Planet
name, location, distanceFromSun // data / state / fields
rotate(), revolve() // actions / behavior / methods

earth : new Planet


venus : new Planet

You might also like