Java Programming Lab Manual-22PLC25C 2022-23
Java Programming Lab Manual-22PLC25C 2022-23
Lab Manual
Examples:
80-element array with base type char:
char[] symbol = new char[80];
100-element array of doubles:
double[] reading = new double[100];
1 2
3 4
1 2
3 4
1 3
2 4
Marks:
Viva voce 5
15
Checked on
Illustration
Using Constructor 1 -
Hello
Students
How
are
you
Using Constructor 2 -
JAVA
Code
String
Marks:
Viva voce 5
15
Checked on
-----------------------------------
=========================================
=============================
=============================
=========================================
-----------------------------------
=========================================
=============================
=============================
(sample)
Marks:
Viva voce 5
15
Checked on
The while statement or loop continually executes a block of statements while a particular
condition is true. The while statement continues testing the expression and executing its block
until the expression evaluates to false. The syntax to use while loop in java is given below.
while(condition)
{
Statement(s);
}
The difference between do-while and while is that do-while evaluates its expression at the
bottom of the loop instead of the top. Therefore, the statements within the do block are always
executed at least once.
do{
Statement(s);
}
while(condition);
121
12321
1234321
Marks:
Viva voce 5
15
Checked on
class Student {
String name;
int roll_no;
}
In the above declaration, Student is the name of a class. In other words, Student is a class.
In the body of the class, the attributes name and roll_no are defined.
Attributes are variables defined in a class. They specify the features (properties) of the class.
we can also define methods in a class. The variables and methods defined in a class are called members
of the class.
class Test {
Using standard values, using command line arguments, inputs through scanner class and user-
defined method. Cumulative Grade Point Average ( CGPA ) is the overall grade point average Your
grade point average (GPA) is calculated by dividing the total amount of grade points earned by the
total amount of credit hours attempted. Your grade point average may range from 0.0 to a 4.0. To get
the example student’s GPA, the total grade points are divided by the total credit hours attempted. Here
is the simple formula to calculate the GPA ( Grade point average formula )
usn:
Name:
AA
Branch:
CS
Div:
Sem:
CGPA:
Student details
1 AA CS 1
Marks:
Viva voce 5
15
Checked on
Function Overloading in Java: If a class has multiple methods having same name but
different in parameters, it is known as Function Overloading. Suppose you have to perform
addition of the given numbers but there can be any number of arguments, if you write the
method such as a(int, int) for two parameters, and b(int,int,int) for three parameters then it may
be difficult for you as well as other programmers to understand the behavior of the method
because its name differs.
In this example, we have created two methods, first add() method performs addition of two
numbers and second add method performs addition of three numbers.
In this example, we are creating static methods so that we don't need to create instance for
calling methods.
class Adder{
In this example, we have created two methods that differs in data type. The first add method
receives two integer arguments and second add method receives two double arguments
class Adder{
Marks:
Viva voce 5
15
Checked on
Person
Student Employee
UG student PG student
Inheritance in Java
Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object. It is an important part of OOPs (Object Oriented programming
system).
The idea behind inheritance in Java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields of
the parent class. Moreover, you can add new methods and fields in your current class also.
In the terminology of Java, a class which is inherited is called a parent or super class, and the new
class is called child or subclass.
Marks:
Viva voce 5
15
Checked on
Person
Employee
Manager
Marks:
Viva voce 5
15
Checked on
Java Package:
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form, built-in package and user-defined
package.There are many built-in packages such as java, lang, awt, javax, swing, net, io, util,
sql etc.
1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
Marks:
Viva voce 5
15
Checked on
In Java, an exception is an event that disrupts the normal flow of the program. It is an object
which is thrown at runtime.
The core advantage of exception handling is to maintain the normal flow of the application.
An exception normally disrupts the normal flow of the application; that is why we need to
handle exceptions. Let's consider a scenario:
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
Suppose there are 10 statements in a Java program and an exception occurs at statement 5; the
rest of the code will not be executed, i.e., statements 6 to 10 will not be executed. However,
when we perform exception handling, the rest of the statements will be executed. That is why
we use exception handling in Java.
Keyword Description
Try The "try" keyword is used to specify a block where we should place an exception code. It means we ca
try block alone. The try block must be followed by either catch or finally.
Catch The "catch" block is used to handle the exception. It must be preceded by try block which means we ca
catch block alone. It can be followed by finally block later.
Finally The "finally" block is used to execute the necessary code of the program. It is executed whether an exc
is handled or not.
Throws The "throws" keyword is used to declare exceptions. It specifies that there may occur an exception
method. It doesn't throw an exception. It is always used with method signature.
Marks:
Viva voce 5
15
Checked on