Javaact 4,5,6 N 7
Javaact 4,5,6 N 7
ACTIVITY-04
Questions:
Bytecode is essentially the machine level language which runs on the Java
Virtual Machine. Whenever a class is loaded, it gets a stream of bytecode per
method of the class. Whenever that method is called during the execution of a
program, the bytecode for that method gets invoked.Javac not only compiles
the program but also generates the bytecode for the program.
ACTIVITY-05
Questions:
● If statement
● If else statement
● Switch Statement
The nested if-else statements are statements that incorporate more than one
if-else clause.
Syntax
if (condition)
{
//Statements set 1
}
else if (condition 2)
{
//Statements set 2
}
...
else
{
//Statements to be executed if no condition is satisfied.
}
Example
class nested_if_else_condition {
public static void main(String[] args) {
double total_marks = 382;
char grade;
double perc = (total_marks/500)*100;
if (perc >= 80)
{
grade = 'A';
}
else if ((perc >=70) && (perc <80))
{
grade = 'B';
}
else if ((perc >=60) && (perc <70))
{
grade = 'c';
}
else
{
grade = 'D';
}
System.out.println("The percentage of the student is: " +perc);
System.out.println("\n The grade of the student is: " +grade);
}
}
Output
The percentage of the student: 76.4
The grade of the student is: B
Nested Loop
Nested loop means a loop statement inside another loop statement. That is
why nested loops are also called “loop inside loop“.
If we have a for loop inside another loop, it is known as nested for loop. The
inner loop executes completely whenever the outer loop executes.
Example
NestedForExample.java
1. public class NestedForExample {
2. public static void main(String[] args) {
3. //loop of i
4. for(int i=1;i<=3;i++){
5. //loop of j
6. for(int j=1;j<=3;j++){
7. System.out.println(i+" "+j);
8. }//end of i
9. }//end of j
10. }
11. }
Output
11
12
13
21
22
23
31
32
33
Nested while loop
When a while loop exists inside the body of another while loop, it is known as
a nested while loop in Java. Initially, the outer loop executes once and the
afterwards inner loop begins to execute. Execution of the inner loop continues
until the condition of the inner loop is satisfied(until the test expression is
false).
Once the Condition of the inner loop is satisfied, the flow of control comes out
to the outer loop for next iteration.
Example
class nestedwhile{
public static void main(String args[]){
int i=1,j=1;
while(i<=10)
{
while(j<=10)
{
System.out.print(j);
j++;
}
i++;
System.out.println("");
j=1;
}
}
}
Output
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
Nested do-while Loop
one do-while loop inside another do-while loop is known as nested do -while
loop
Example
class NestedDoWhile{
public static void main(String args[]){
int row=1,column=1;
int x;
do{
x=4;
do{
System.out.print("");
x--;
}while(x>=row);
column=1;
do{
System.out.print(column+" ");
column++;
}while(column<=5);
System.out.println(" ");
row++;
}while (row<=5);
}
Output
12345
12345
12345
12345
12345
OBJECT ORIENTED PROGRAMMING and
DESIGN with JAVA
ACTIVITY-06
Questions:
a) Encapsulation
b) Inheritance
c) Abstraction
d) Polymorphism
a) Encapsulation in Java
Encapsulation in Java is a process of wrapping code and data together into a
single unit, for example, a capsule which is mixed with several medicines.
We can create a fully encapsulated class in Java by making all the data
members of the class private. Now we can use setter and getter methods to
set and get the data in it.
The Java Bean class is the example of a fully encapsulated class.
Advantages of Encapsulation
● Data Protection: The program runner will not be able to identify or see
which methods are present in the code. Therefore he/she doesn’t get
any chance to change any specific variable or data and hinder the
running of the program.
Disadvantages of Encapsulation
● Code Size:The length of the code increases drastically in the case of
encapsulation as we need to provide all the methods with the specifiers.
Advantages of Inheritance
● Overriding: With the help of Inheritance, you can override the methods
of the base class.
Disadvantages of Inheritance
● Refactoring the Code: If the user deletes the Super Class, then they
have to refactor it if they have used it.
c) Abstraction in Java
Data Abstraction is the property by virtue of which only the essential details
are displayed to the user. The trivial or the non-essential units are not
displayed to the user. Ex: A car is viewed as a car rather than its individual
components.
Advantages of Abstraction
The word polymorphism means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more than
one form.Polymorphism allows us to perform a single action in different ways.
ACTIVITY-07
Questions:
The Arrays class in the java.util package is a part of the Java Collection
Framework. This class provides static methods to dynamically create and
access Java arrays. It consists of only static methods and the methods of
Object class. The methods of this class can be used by the class name itself.
The Arrays class of the java.util package contains several static methods that
can be used to fill, sort, search, etc in arrays.
deepEquals(Object[] a1, Object[] Returns true if the two specified arrays are
a2) deeply equal to one another.
Returns a hash code based on the “deep
deepHashCode(Object[] a)
contents” of the specified Arrays.
fill(originalArray, fillValue) Assigns this fill value to each index of this arrays.
parallelPrefix(originalArray,
Performs parallelPrefix for the given range of the
fromIndex, endIndex,
array with the specified functional operator.
functionalOperator)
sort(T[] a, int fromIndex, int Sorts the specified range of the specified array of
toIndex, Comparator< super T> objects according to the order induced by the
c) specified comparator.
Method Description