0% found this document useful (0 votes)
53 views

Java Unit Test-1

Here is an example of while loop: int i=1; while(i<=5){ System.out.println(i); i++; } This will print numbers from 1 to 5. for statement: For loop is used when you know how many times you want to repeat the loop. Example: for(int i=1; i<=5; i++){ System.out.println(i); } This will also print numbers from 1 to 5. The for loop has three sections separated by semicolons: 1. Initialization 2. Condition 3. Increment/decrement It initializes the variable

Uploaded by

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

Java Unit Test-1

Here is an example of while loop: int i=1; while(i<=5){ System.out.println(i); i++; } This will print numbers from 1 to 5. for statement: For loop is used when you know how many times you want to repeat the loop. Example: for(int i=1; i<=5; i++){ System.out.println(i); } This will also print numbers from 1 to 5. The for loop has three sections separated by semicolons: 1. Initialization 2. Condition 3. Increment/decrement It initializes the variable

Uploaded by

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

UNIT TEST-1

1) Explain the principles of OOP's

2) Evaluate the Method overloading and Constructor


Overloading

3) a) Describe the importance of this keyword


b) Explain JAVA Buzz words

4) a) Describe the following statements with example


i) While ii) for
b) Write a Java program factorial of a given number using
recursion.
OOP’s (Object-Oriented Programming)
Object-Oriented Programming is a methodology to design a
program using classes and objects.

 Object
 Class
 Inheritance
 Polymorphism
 Abstraction
 Encapsulation
CLASS
Introduction to Classes
 A class is a user-defined data type. It consists of data members
and member functions, which can be accessed and used by
creating an instance of that class.
 A class is like a Blueprint (or) logical entity for an object.
Before we create an object, we first need to define the
class.
 Class does not occupy memory.
Type of Classes
In Java classes are mainly divided into two types.
Those are:
1. Built-in Classes
2. User-Defined Classes
1. Built-in Classes
Built-in classes are just the predefined classes that
come along with the Java Development Kit (JDK). Some
examples of built-in classes include:
java.lang.System
java.util.Date
java.util.ArrayList
java.lang.Thread
User-Defined Classes
User-Defined Classes are defined by the user
according to their requirement.
4
A class in Java consists of the following components
1. Access Modifier
2. Class Name
3. Body of the Class
1. Access Modifier:
access modifiers specify the accessibility or scope of the class.
Java provides four types of access modifiers. Those are
a) Public : Access from any where
b) Private : Access with in the class where it is defined
c) Protected: Access with in same package and sub classes of
another package
d) Default: Access with in the same package only
2. Class Name
This describes the name given to the class.
3. Body of the Class
The body of the class mainly includes executable statements.
Note:
Apart from these, a class may include keywords like
"super" 5,”extends”, ”implements”.
User-defined classes are broken down into three types:
1. Concrete Class
2. Abstract Class
3. Interfaces
1.Concrete Class
A Concrete class is a standard class that the user
defines and stores the methods and data members in.
Rules for Creating Classes:
1. The keyword "class" must be used to declare a class.
2. Every class name should start with an upper case
character. If it include multiple words first letter of each
word must be in capital letter(camel case).
3. A Java project can contain any number of default classes
but should not hold more than one public class
General form of a class is as follows:

6
Syntax:
class Classname
{
datatype variable1;
datatype variable2;
………..
datatype variablen;
type method1(parameterlist)
{
statements;
}
type method2(parameterlist)
{
statements;
}
……..
} 7
Example 1:

class Example
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

Save: Example.java
Compilation: javac Example.java
Run: java Example
Output:
Hello World!

8
Object
An object is called an instance of a
class. Object means a real-world entity such as a pen, chair,
table, computer, watch, etc.

An object in Java consists of the following:


1. State.
2. Behavior.

State:
represents the data (value) of an object.

Behavior:
represents the behavior (functionality) of an object such
as deposit, withdraw, etc.

9
There are 3 ways to initialize object in Java.
1. By reference variable
2. By method
3. By constructor

Accessing the members of a class


We can use the name of objects along with the . operator to access
members of a class.

1. By reference variable
class Student{
int id=200;
String name=“VITS”;
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id+" "+s1.name);
}
}
10
Inheritance
 Inheritance is a process where child class acquired all the properties
and behaviors of the parent class.
 It helps to reuse the code and establish a relationship between different
classes.
 Here parent class also called a super class(Base class) and child class
called as a subclass(Derived class).

Points about Inheritance


 extends the keyword used to implement inheritance.
Polymorphism
If one task is performed in different ways, it is known as
polymorphism.

For example: to convince the customer differently, to draw


something etc..

Polymorphism can be achieved in two ways:


 Runtime Polymorphism (Method Overriding)
 Compile-time Polymorphism (Method Overloading)
Abstraction
Hiding internal details and showing functionality is known as
abstraction.

Example:
In phone call we don’t know how internally work
In ATM we don’t know how internally work.

Abstraction can be achieved by two ways:


 Abstract Class (You can achieve 0-100% abstraction using abstract
class)
 Interface(You can achieve 100% abstraction using interfaces)
Encapsulation:
 Encapsulation is a mechanism where you bind your data and code together
as a single unit.
 The best way to understand encapsulation is to look at the example of a
medical capsule, where the drug is always safe inside the capsule.
We can achieve encapsulation in Java by:
 Declaring the variables of a class as private.
 Providing public setter and getter methods to modify and view the variables
values.
Evaluate the Method overloading
and Constructor Overloading
METHOD OVERLOADING

 Method Overloading is a feature that allows a class to have


multiple methods with the same name but with different
number, sequence or type of parameters.

 In other words, multiple methods with same name but with


different signatures.

When an overloaded method is invoked, Java looks for a


match between arguments of the methods to determine which
version of the overloaded method to actually call.
 In order to overload a method, the parameter list of the methods
must differ in either of these:
1. Number of parameters.
For example: This is a valid case of overloading
add(int, int)
add(int, int, int)
2. Data type of parameters.
For example:
add(int, int)
add(int, float)
3. Sequence of Data type of parameters.
For example:
add(int, float)
add(float, int)
// EXAMPLE1: PROG FOR METHOD OVERLOADING
class Demo {
void multiply(int a, int b) {
System.out.printIn("Result is"+(a*b)) ;
}
void multiply(int a, int b, int c) {
System.out.printIn("Result is"+(a*b*c));
}
public static void main(String[] args) {
Demo obj = new Demo();
obj.multiply(8,5);
obj.multiply(4,6,2);
}
}
CONSTRUCTOR OVERLOADING
Similar to Java method overloading, we can also create
two or more constructors with different parameters. This is
called constructors overloading.
// Example for constructor overloading
public class Person {
int age;
String name;
float sal;

Person() { // constructor1 without parameters


System.out.println("Constructor with out parameters invoked");
}

Person(int a, String s) { // constructor2


System.out.println("Constructor1 invoked");
System.out.println("Age:" + a);
System.out.println("Name:" + s);
}
Person(int a, String s, float f) { // constructor3
System.out.println("Constructor3 invoked");
System.out.println("Age:" + a);
System.out.println("Name:" + s);
System.out.println("Sal:" + f);
}

public static void main(String[] args) {


Person m1 = new Person();
Person m2 = new Person(21,"sunil");
Person m3 = new Person(22,"Hari",23000);
}
}
3) a) Describe the importance of this keyword
b) Explain JAVA Buzz words
Features of java (or) Java BUZZ words (or)
Advantages of java
The primary objective of Java programming language creation was to
make it portable, simple and secure programming language.
 Simple
 Object oriented
 Robust
 Secure
 System independence/Platform independent
 Interpreted
 High performance
 Multithreaded
 Dynamic
this Keyword in Java
this keyword in Java is a reference variable that refers
to the current object of a method or a constructor.
The main purpose of using this keyword in Java is to remove
the confusion between class attributes and parameters that have
same names.
Example:
Consider the code:

30
The solution is the “this” keyword Append both ‘a’ and ‘b’
with the Java this keyword followed by a dot (.) operator.

During code execution when an object calls the method


‘setdata’. The keyword ‘this’ is replaced by the object handler
“obj.”
31
Usage of java this keyword
1.this can be used to refer current class instance variable.
2.this can be used to invoke current class constructor.
3.this can be used to invoke current class method.

32
Example:
class ThisExample {
int x, y; //instance variables
void display(int x, int y) { // x,y are arguments
this.x = x+100;
this.y = y+100;
System.out.println(“the instance variable x value is”+this.x);
System.out.println(“the instance variable y value is”+this.y);
System.out.println(“the display() local variable x value is”+x);
System.out.println(“the display() local variable y value is”+y);
}
public static void main(String args[]) {
ThisExample te=new ThisExample();
te.dispay(10,20);
}
}
33
4) a) Describe the following statements with example
i) While ii) for
b) Write a Java program factorial of a given number using
recursion.
while statement:
While statement is used for repetitive execution of statements
more than once.

Syntax: Flowchart

while (condition)
{
------
Block of the statements
------
}
First the condition is evaluated. If the condition is TRUE, then Block
Statements will be executed by the compiler. After executing the Block
Statements, again control reaches to condition section and is evaluated.
 The process is repeated until the condition becomes FALSE.
 When the condition reaches to FALSE, then the control is transferred out of
the loop.
Example:
class Demo{
public static void main(String args[]) {
int i = 1;
while( i <=10 ) {
System.out.print(i + ” “);
i++;
}
}
}
Output: 1 2 3 4 5 6 7 8 9 10
36
iii) For statement:
For statement is also used for repetitive execution of
same statements more than once.
Syntax:
for(Initialization ; Condition ; Increment/Decrement){
/* Block Statements */
}
Here,
The Initialization initializes and/or declares variables and executes only
once.
The condition is evaluated. If the condition is true, the body of
the for loop is executed.
The Increment/Decrement updates the value of variable.
The condition is evaluated again. The process continues until
the condition
37
is false
Example:

public class Demo {

public static void main(String args[]) {


for (int i = 1; i <= 10; i++)
System.out.println(i);
}

}
Output: 1 2 3 4 5 6 7 8 9 10
38
// Factorial of a given number using recursion
class Factorial {
static long factorial(long n) {
if (n == 1)
return 1;
else
return n * factorial(n - 1);
}
public static void main(String[] args) {
System.out.println("Factorial of the number 6 is: " + factorial(6));
}
}
Output:
Factorial of the number 6 is: 720

You might also like