1. Introduction
1.1 Object
Object have both Properties (Data) & Methods (Actions) together.
1.2 Programming
Instructions to the Computer to solve certain problem Or to automate certain routine process is called programming.
1.3 Computer
Performs series of actions on data as per the instructions (program) to output desirable results.
· CPU: Central Processing Unit
· Memory: Use for temporary storage of program instructions and data. Memory users are CPU, Graphics Card and other components.
· HD: Hard Disk is used to store data permanently.
· Graphics Card: is use to display graphics.
· bus: is use to transfer data between computer components, such as CPU, Memory.
· I/O Controller: is used to connect I/O devices to the bus
Questions:
Q1. What is an Object? Give 5 examples.
Q2. What is the main job of CPU?
Q3. What is the difference between Memory and HD?
Q4. Major Difference between Procedural & Object Oriented
Programming?
2. OOP Features
Object Oriented programming (OOP) is based on below 4 important features, which promotes data hiding, reuse and building of the entire system using objects.
2.1 Encapsulation
Encapsulates the data and complex business logic of the object, and exposes the necessary data to external world only via interfaces.
2.2 Inheritance
- Inheritance promotes code reuse, extensibility, better maintenance
- Sub object inherits properties & methods from the super object
Concept:
Example:
Each planet has a mass, gravity and other properties, which can be abstracted to create super class, called Planet. The Planet class contains abstract properties and methods from the specific planet class, such as Earth, Mars.
Later, it is possible to add more Planets to the system just by choosing super class “Planet”
2.3 Abstraction
Provides simplified & abstract interfaces to the users (including systems) to interact with underlying system.
2.4 Polymorphism
Based on the conditions same object behaves differently.
Questions:
Q1. Explain with example How OOP promotes reuse?
Q2. Give 5 examples of real world Encapsulation usage.
Q3. Give 5 applications of Inheritance.
Q4. How Abstraction helps to simplify the interaction with the system?
3. Java program compilation
The source of java is written in the high level language, such as English. The source needs to be compiled to create java byte code. The byte code runs on JVM (Java Virtual Machine) as a program.
IDE: Integrated Development Environment, such as Eclipse. IDE is use to build applications based on pre-built project templates, version management integration, source structure management and testing and debugging environment all together consolidated for efficient programming.
- Java is case sensitive language, Earth <> earth.
- Java is WORA language, meaning Write Once Run Anywhere, hence it is platform independent language.
Questions:
Q1. How to create java source file?
Q2. How to compile java program by using IDE?
Q3. How to run java program using IDE?
Q4. Create a new class file, Mars.java, compile it and run it to print message, “Welcome to the Mars ! ”
4. Data types & Variables
Data types in java can be divided into two groups (Primitive & Reference) as shown below,
Examples:
-----------------------------------------------------------------------
[Earth.java]
public class Earth {
public static void
main(String[] args) {
System.out.println("Welcome
to Earth !");
}
}
-----------------------------------------------------------------------
-----------------------------------------------------------------------
Output:
Welcome to Earth !
-----------------------------------------------------------------------
-----------------------------------------------------------------------
[Calculator.java]
//Check the
output and fix the datatype !
public class Calculator {
public static void
main(String[] args) {
int n1 = 10;
int n2 = 100;
int result1 =
n1 / n2;
int result2 =
n1 * n2;
int result3 =
n1 + n2;
int result4 =
n1 - n2;
System.out.println("NUMBER 1 = " + n1 + ",
NUMBER 2 = " + n2);
System.out.println("Result of division = " + result1);
System.out.println("Result of multiplication = " + result2);
System.out.println("Result of addition = " + result3);
System.out.println("Result of subtraction = " + result4);
}
}
-----------------------------------------------------------------------
-----------------------------------------------------------------------
Output: (Need to fix data types in Program for correct results !)
NUMBER 1 = 10, NUMBER 2 = 100
Result of division = 0
Result of multiplication = 1000
Result of addition = 110
Result of subtraction = -90-----------------------------------------------------------------------
5. Programming Styles
5.1 Major Difference between Procedural programming and Object Oriented Programming
Focus is on getting the specific result by carrying out list of tasks. Data and Actions are defined separately.
Focus is on getting the specific result by carrying out interaction between the objects. Data and Actions are defined as a one object.
Write a program to carryout four actions (addition, subtraction, multiplication, division) on two numbers. Use procedural programming style.
-----------------------------------------------------------------------
[Calculator.java]
//Check the output and fix the datatype !
public class Calculator {
public static void main(String[] args) {
int n1 = 10;
int n2 = 100;
int result1 = n1 / n2;
int result2 = n1 * n2;
int result3 = n1 + n2;
int result4 = n1 - n2;
System.out.println("NUMBER 1 = " + n1 + ", NUMBER 2 = " + n2);
System.out.println("Result of division = " + result1);
System.out.println("Result of multiplication = " + result2);
System.out.println("Result of addition = " + result3);
System.out.println("Result of subtraction = " + result4);
}
}
-----------------------------------------------------------------------Lab #2
Write a program to carryout four actions (addition, subtraction, multiplication, division) on two numbers. Use OO programming style.
• No main() method inside Calculator, so that Calculator can be reused
• main() is to be implemented inside the other class (Client.java)
-----------------------------------------------------------------------
public class Calculator {
/**
* Method to print
result after adding
* two numbers
* @param n1 first
number to be added
* @param n2 second
number to be added
*/
public void add(double n1, double n2) {
double result = n1
+ n2;
System.out.println("Result
of addition = " + result);
}
public void sub(double n1, double n2) {
double result = n1
- n2;
System.out.println("Result
of subtraction = " + result);
}
public void mul(double n1, double n2) {
double result = n1
* n2;
System.out.println("Result
of multiplication = " + result);
}
public void div(double n1, double n2) {
double result = n1
/ n2;
System.out.println("Result
of division = " + result);
}
}
-----------------------------------------------------------------------
[Client.java]
public class Client {
public static void
main(String[] args) {
Calculator
cal = new Calculator();
cal.add(20,10);
cal.sub(20,10);
cal.mul(20,10);
cal.div(20,10);
}
}
-----------------------------------------------------------------------
Output:
Output:
Result of addition = 30.0
Result of subtraction = 10.0
Result of multiplication = 200.0
Result of division = 2.0
-----------------------------------------------------------------------
6. SDLC (Software Development Life Cycle)
Important Phases of SDLC are,
o Requirements
o Design
o Implementation
o Testing (Unit Testing, SIT, UAT)
o Production Release
o Maintenance (bug fixing, new feature addition)
o Decommissioning
SDLC can be more efficient and effective by using OOP. The OOP helps SDLC from Implementation (object reuse, inheritance, encapsulation), Testing (object behaviour can be tested individually or as part of a system) and Production release & maintenance (only release impacted objects instead of entire system).
7. Documenting with javadoc Comments
7.1.2 Command to generate Java doc
12.
Lab #3: Decision making.
Use if and ifelse block for decision making and flow control of the program.
12.1 Write a program to calculate yearly
salary using monthly salary of 1000.
Print the results.
Source:
public class
SalaryCalculator {
public static void
main(String[] args) {
//Input
double
monthlySalary = 600;
//Process
double
yearlySalary = monthlySalary * 12; //1 Year = 12 Months
//Output
System.out.println("Month Salary = " + monthlySalary);
System.out.println("Yearly Salary = " + yearlySalary);
}
}
Output:
Month Salary
= 600.0
Yearly
Salary = 7200.0
12.2 Write a program to calculate Net
yearly salary using monthly salary of 1000. Use below Tax rates and formulas.
Formula:
1.
Yearly
Salary = Monthly Salary * 12
2. Net Salary = Yearly Salary – Yearly Tax
3.
Yearly
Tax = Yearly Salary * Tax Rate
Tax rate table:
Monthly Salary
|
Tax rate (Yearly)
|
< 500
|
10%
|
>= 500 AND
< 1000
|
20%
|
>= 1000
|
40%
|
Print the
results in following format.
Results:
Monthly Salary = XXX
Yearly Salary (Gross) = XXX
Yearly Tax Rate = XXX
Yearly Tax Amount = XXX
Net Yearly Salary = XXX
Source:
public class
SalaryCalculatorWithTax {
public static void
main(String[] args) {
//Input
double
monthlySalary = 600;
double taxRate =
0;
//Process
double
yearlySalary = monthlySalary * 12; //1 Year = 12 Months
if
(yearlySalary < 500) {
taxRate =
0.1;
}
if
(yearlySalary >= 500 && yearlySalary < 1000) {
taxRate =
0.2;
}
if
(yearlySalary >= 1000) {
taxRate =
0.4;
}
double yearlyTax =
yearlySalary * taxRate;
double netSalary =
yearlySalary - yearlyTax;
System.out.println("Result:");
System.out.println("Monthly Salary =" + monthlySalary);
System.out.println("Yearly Salary (Gross) =" + yearlySalary);
System.out.println("Yearly Tax Rate =" + taxRate);
System.out.println("Yearly Tax Amount =" + yearlyTax);
System.out.println("Net Yearly Salary =" + netSalary);
}
}
Output:
Result:
Monthly
Salary =600.0
Yearly
Salary (Gross) =7200.0
Yearly Tax
Rate =0.4
Yearly Tax
Amount =2880.0
Net Yearly
Salary =4320.0
12.3 Write a program to calculate the ATM
service charges based on the day of the month. Please use following
relationship between month of the day & service charges. (Use ifelse…if)
Source:
public class TestIfElse
{
public static void
main(String[] args) {
double
serviceCharges = 0;
//on 1st
of every month service charges = 1%,
//on 2nd
day = 2%, on 3rd day = 3%, on 4th and above = 4% int day = 2;
if (day == 1)
{
serviceCharges
= 0.01;
} else if (day == 2)
{
serviceCharges
= 0.02;
} else if (day == 3)
{
serviceCharges
= 0.03;
} else if (day >=
4) {
serviceCharges
= 0.04;
} else {
System.out.println("Invalid service charges.");
}
System.out.println("Service charges are " + serviceCharges + ", for
the day " + day + " of
the month");
}
}
Output:
Service
charges are 0.02, for the day 2 of the month
12.4 Write a program to print name of the
weekday based on specified number of the weekday using switch() statement.
Source:
public static void main(String[] args) {
int
dayOfTheWeek = 1;
switch
(dayOfTheWeek) {
case 1 :
System.out.println("SUNDAY");
break;
case 2 :
System.out.println("MONDAY");
break;
case 3 :
System.out.println("TUESDAY");
break;
case 4 :
System.out.println("WEDNESDAY");
break;
case 5 :
System.out.println("THURSDAY");
break;
case 6 :
System.out.println("FRIDAY");
break;
case 7 :
System.out.println("SATURDAY");
break;
default :
System.out.println("Invalid
number, try again.");
}
}
}
Output:
SUNDAY
13.
Lab #4: Looping
Loops can be used to iterate through list of records or to read
an external file.
There are three types of loop,
i) for loop
ii) while loop
iii) do while loop
Each type of loop consists of atleast below 3 statements.
Only the difference is in how these statements are declared and their location
as shown below.
Statement
|
Location in
for
|
Location in while
|
Location in
do …while
|
Initialization
int i = 0;
|
Inside the for loop statement
|
Above while block
|
Above do…while block
|
Boolean expression
i < 5:
|
Inside the for loop statement
|
Entry of the while block
|
Exit of the do…while block
|
Increment or
Decrement expression
i = i + 1;
|
Inside for loop statement
|
Inside while block
|
Inside do…while block
|
13.1 Write a program to print numbers
from 0 to 4 using “for” loop.
Source:
public static void main(String[] args) {
//1.
Initialization: int i = 0;
//2.
Boolean expression: i < 5;
//3.
Increment: i = i + 1
for (int i = 0; i
< 5; i = i + 1) {
System.out.println("Value
of i = " + i);
}
}
}
Output:
Value of i =
0
Value of i =
1
Value of i =
2
Value of i =
3
Value of i =
4
13.2 Write a program to print numbers
from 0 to 4 using “while” loop.
Source:
public static void main(String[] args) {
//1.
Initialization
int i = 0;
while(i < 5) {
//2. Boolean expression
System.out.println("Value
of i = " + i);
i = i + 1; //3.
Increment
}
}
}
Output:
13.3 Write a program to print numbers
from 0 to 4 using “do…while” loop.
Source:
public class
DemoWhileLoop {
public static void
main(String[] args) {
//1.
Initialization
int i = 0;
do {
System.out.println("Value of i = " + i);
i = i +
1; //3. Increment
} while(i < 5); //2. Boolean expression at the exit
}
}
Output:
Value of i =
0
Value of i =
1
Value of i =
2
Value of i =
3
Value of i =
4
13.4 Write a program to print ODD number from
0 to 20. Use loop and modulus operator (%).
Source:
public class
DemoODDNumberPrinter {
public static void
main(String[] args) {
for(int n = 0; n
<= 20; n = n + 1) {
if ((n % 2) ==
1) { //Reminder is 1 for ODD number
System.out.println("ODD number is = " + n);
}
}
}
}
Output:
ODD number
is = 1
ODD number
is = 3
ODD number
is = 5
ODD number
is = 7
ODD number
is = 9
ODD number
is = 11
ODD number
is = 13
ODD number
is = 15
ODD number
is = 17
ODD number
is = 19
Java Quiz # 1 (Total 10 marks, 1 mark per
question)
Topic Name
|
Question
|
1. Object & Class
|
What is difference between
Class & Object? Give one example.
|
2. OOP
|
What are the features of
OOP (Object Oriented Programming)?
|
3. Compilation
|
What is the purpose of Java
source compilation?
|
4. Method
|
What is the purpose of main(String[] args) method ?
|
5. Data types
|
List primitive data types
in Java?
|
6. Decision making
|
Print output of below code
block,
public class TestAND {
public static void main(String[] args) {
double gravity = 9.81; //m/s2 ,3.711 on mars
double oxygen = 21; //21% , 0.14 % on mars
if (gravity >= 9.81 &&
oxygen <= 21) {
System.out.println("EARTH.");
}
else {
System.out.println("UNKNOWN PLANET.");
}
}
}
Output?
|
7. Looping
|
Write a program to print 0
to 5 numbers using any of the three loops (for, while, do … while)?
|
8. Modulus Or remainder
operator (%)
|
Print output of below code
block,
public class
DemoModulus {
public static void main(String[] args) {
int number = 10;
if ( number % 2 > 0 ) {
System.out.println("number is odd.");
}
else {
System.out.println("number is even.");
}
}
}
Output?
|
9. Fundamental
|
Java language is case
sensitive.
(True / False)
|
10. Fundamental
|
Java program needs to
recompile to create byte code as per the target operating system. (True /
False)
|
How to write Model class and call its methods from the caller (CasltleManager.java)
[Door.java]
/**
* Class to model
Door of the castle, which can be opened and
* closed by
calling respective open() & close() public methods.
*
* @author AUTHOR_NAME
*/
public class Door {
/**
* Method to open the castle door based
on the correct passcode.
* @param
passcode the passcode to open the door
* @return true if
the door has been opened, else return false
*/
public boolean open(int passcode) {
if (passcode == 123) {
System.out.println("Welcome to the Java
Kingdom.");
return true;
}
System.out.println("Sorry, the passcode is
incorrect.");
return false;
}
/**
* Method to close the already open
door
*/
public void close() {
System.out.println("The door has been
closed.");
}
}
|
|
[CastleManager.java]
public class
CastleManager {
public static void main(String[] args) {
System.out.println("= BEGIN =");
//create an Instance (Object) of
the Door class
Door
door = new Door();
//call method open() of door
object with the parameter of passcode (123)
door.open(123);
//call method close() of door
object
door.close();
System.out.println("= END =");
}
}
Output:
= BEGIN =
Welcome to
the Java Kingdom.
The door
has been closed.
= END =
|