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

23UBC JAVA RecordNoteContents

Uploaded by

Gajalakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

23UBC JAVA RecordNoteContents

Uploaded by

Gajalakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Date Exercise Description

Number
10/07/2024 Ex1 A Simple Java Program
19/07/2024 Ex2 Program using if else statement
26/07/2024 Ex3 Program if else if statement
26/07/2024 Ex4 Program using switch statement
06/08/2024 Ex5 Program using loop statements
14/08/2024 Ex6 Program using Arrays
09/09/2024 Ex7 Program using constructors, objects and methods
18/09/2024 Ex8 Program to demonstrate method overloading
25/09/2024 Ex9 Program to demonstrate inheritance
04/10/2024 Ex10 Program to demonstrate exception handling

Ex1: A Simple Java Program


Aim: To create a simple Java program to add two numbers.
Procedure:
1. Define a class (Ex1)
class Ex1 {
2. Define the main function inside the class
public static void main(String args[]) {
3. Declare three variables inside the main function to store the input and the output
int a=10, b=20, total=0;
4. Use to arithmetic operator (+) to add the two numbers.
total = a + b;
5. Use the System.out.println statement to print the output
System.out.println("The sum of " + a + " and " + b + " is " + total);
6. Close the main function and the class
}
}
7. Save the file as Ex1.java
8. Use javac to compile the source code
javac Ex1.java
9. To execute, use java and the class name
java Ex1
Result: Using the above procedure, the sum of two numbers was calculated and printed
successfully.

Ex2: Program using if else statement.


Aim: To develop a JAVA program to find the biggest of two numbers using if else statement
in JAVA

Description: The if...else statement executes some code if a condition is true and another code
if that condition is false.

Syntax:
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}

Procedure:
1. Define a class (Ex2) with the main function
2. Declare two variables to store the input
int n1,n2;
3. Declare a DataInputStream object and use readLine() to get the inputs.
DataInputStream abc = new DataInputStream(System.in);
n1 = Integer.parseInt(dis.readLine());
n2 = Integer.parseInt(dis.readLine());
4. Use the relational operator (>) in the if statement to compare the two numbers and
print the result as follows:
if (n1>n2)
System.out.println(n1 + " is greater than " + n2);
else
System.out.println(n2 + " is greater than " + n1);
5. Compile and execute the program.

Result:
A Java program to find the biggest of two numbers using the if else statement was developed
and executed successfully.

Ex3: Program using if-else-if statement


Aim: To develop a JAVA program to use the if-else-if statement to print a given number in
words.

Description: The if-else-if ladder is used to decide among multiple options. The if statements
are executed from top down. As soon as one of the conditions controlling the if is true, the
statement associated with that if is executed and the rest of the ladder is bypassed. If none of
the conditions is true, then the final else statement will be executed. The syntax is:
if(condition)
statement;
else if(condition)
statement;
else if(condition) statement;

...
else
statement;

Procedure:
1. Define a class (Ex3) with the main function
2. Declare a variable to store the input
int v1;
3. Declare a DataInputStream object and use readLine() to get the inputs.
DataInputStream abc = new DataInputStream(System.in);
v1 = Integer.parseInt(dis.readLine());
4. Use the if else if statement to check and print the output.
if (v1 == 1)
System.out.println(“ONE”);
else if(v1 == 2)
System.out.println(“TWO”);
else if(v1 == 3)
System.out.println(“THREE”);

else
System.out.println(“Not a valid number”);
5. Compile and execute the program

Result: A Java program using the if else if statement to print the given number in words was
developed and tested successfully.

Ex4: Program using Switch statement


Aim: To develop a JAVA program to use the switch statement to print a given number in
words.

Description: The switch statement is used to perform different actions based on different
conditions. It can be used as an alternative to if else if statement.
Syntax:
switch (n) {
case value1:
code to be executed if n=label1;
break;
case value2:
code to be executed if n=label2;
break;
case value3:
code to be executed if n=label3;
break;

default:
code to be executed if n is different from all values;
}

Procedure:
1. Define a class (Ex4) with the main function
2. Declare a variable to store the input
int v1;
3. Declare a DataInputStream object and use readLine() to get the inputs.
DataInputStream abc = new DataInputStream(System.in);
v1 = Integer.parseInt(dis.readLine());

4. Use the switch statement and System.out.println statement to print the output as
follows.
switch(v1)
{
case 1:
System.out.println(“ONE”);
break;

case 2:
System.out.println(“TWO”);
break;

default:
System.out.println(“Not a valid number”);
}
5. Compile and execute the program

Result: A Java program using the switch statement to print the given number in words was
developed and tested successfully.

Ex5: Program using loops


Aim: To develop a JAVA program to print a number sequence using for and while loop

Description:
while loop:
The while loop executes a block of code as long as the specified condition is true.

Syntax:
while (condition is true) {
code to be executed;
}

for loop:
The for loop - loops through a block of code a specified number of times. It is used when we
know in advance how many times the statements should run

Syntax:
for(expression1;expression2;expression3) {
statement;
}

Procedure:
1. Define a class (Ex5) with the main function
2. Declare two variables to store the input
int a=1, num;
3. Declare a DataInputStream object and use readLine() to get the inputs.
DataInputStream abc = new DataInputStream(System.in);
num = Integer.parseInt(dis.readLine());
4. Use the while loop to print the numbers:
while(a <= num )
{
System.out.println(a);
a += 1;
}
5. Use the for loop to print the numbers:
for( a= 1; a <= num; a++)
System.out.println(a);

6. Compile and execute the program

Result: A Java program using the for and while loop was developed and tested successfully.

Ex6: Program using Arrays


Aim: To develop a JAVA program to find the sum and average of the elements of a single
dimensional array
Description:
An array is a group of contiguous and related data items that share a common name. Arrays
can be single dimensional and multidimensional. Single dimensional array can be declared as
follows:
type arrayname[]; // declaring
arrayname = new type[size]; //Create memory for the array
The above two steps can be done in a single step as follows:
type arrayname[] = new type[size];

arrayname[0] = value1; // Assigning values to array elements

Procedure:
1. Define a class (Ex6) with the main function
2. Declare a single dimensional array and variables to store the sum and average
int i=0, sum=0;
float avg=0.0;
int marks[] = new int[5];
3. Declare a DataInputStream object and use readLine() to get the inputs inside a for
loop.
DataInputStream abc = new DataInputStream(System.in);
for( i = 0; i < 5; i++)
{
System.out.print("Enter the mark for subject#" + (i+1) + " : ");
marks[i] = Integer.parseInt(dis.readLine());
sum = sum + marks[i];
}
4. Calculate the average as follows:
avg = sum/5;

Result:
A Java program to calculate the sum and average of the array elements was developed and
tested successfully.

Ex7: Program using constructors, objects and methods


Aim: To develop a JAVA program which uses constructors and methods.

Description:
A class is a template/blueprint/prototype from which objects are created. It shares the
common properties and behaviour. The basic form of class definition is:

class className [extends superClassName]


{
[variable declaration;]
[constructors declaration;]
[methods declaration;]
}

Constructors are used for initializing new objects. Constructors have the same name as the
class itself. They do not have a return type. The general form of a constructor is
className(parameter list)
{
//body of the constructor
}

Methods are used to implement the behaviour of the class. The general form of a method
declaration is:
returnType methodName(parameter list)
{
//body of the method
}

Objects are instances of a class that are created to use the variables and methods of a class.
Objects are created using the new operator as follows:
className objectName = new className();
Procedure:
1. Declare a class (Student)
2. Declare three variables to store the register number, name and age of the student.
3. Define constructors to initialize the variables with some initial/specified values.
4. Define a method (printData()) to print the register number, name and age.
5. Define another class (Ex7) with the main function.
6. Create objects for the Student class
Student s1 = new Student();
Student s2 = new Student("23UBC502", "Roy", 19);
7. Call the printData() to print the student details
s1.printData();
s2.printData();

Result: A Java program with constructors and methods was developed and tested
successfully.

Ex8: Program to demonstrate method overloading


Aim:
To develop a JAVA program to demonstrate method overloading.

Description:
If a class has more than one method having the same name but different parameters, then the
method is said to be overloaded. Method overloading increases the readability of the
program.
Methods can be overloaded either by changing the number of arguments and/or data type.

Procedure:
1. Declare a class (Overload)
2. Define three methods with the same name (display) but with different parameters as
follows:
void display() // method with no arguments
void display(String msg) // method with one string argument
void display(int a, int b) // method with two integer argument

3. Define another class (Ex8) with the main function.


4. Create objects for the Overlaod class
Overload obj = new Overload();
5. Call each of the display() method with appropriate arguments
obj.display();
obj.display(“Hello Java”);
obj.display(5,7);

Result:
A Java program with method overloading feature was developed and tested successfully.

Ex9: Program to demonstrate inheritance


Aim:
To develop a JAVA program to demonstrate single inheritance.

Description:
Inheritance is a mechanism in Java by which a class (child class) is allowed to inherit the
features (variables and methods) of another class (parent class). Inheritance allows to create
flexible and reusable code. The keyword extends is used for inheritance in Java.
class Child extends Parent
{
//Variables and Methods
}

Procedure:
1. Declare a class (Animal) with two methods Eat() and Sleep()
2. Declare another class (Lion) with two methods Roar() and Hunt(). Inherit the Animal
class as follows:
class Lion extends Animal
3. Now the Lion class will inherit the Eat() and Sleep() methods of Animal class.
4. Create another class with the main method to test the inheritance.
5. Create objects of the Lion class as follows:
Lion Tom = new Lion();
6. Invoke the Eat(), Sleep(), Roar() and Hunt() methods of Tom:
Tom.Roar();
Tom.Hunt();
Tom.Eat();
Tim.Sleep();

Result:
A Java program with inheritance feature was developed and tested successfully.

Ex10: Program to demonstrate exception handling


Aim:
To develop a JAVA program to handle arithmetic exception.

Description:
An exception is an abnormal condition that arises in a code sequence at run time. An
exception is a runtime error. Exception handling is a mechanism to handle the runtime errors.

Java’s exception handling is managed via five keywords: try, catch, throw, throws, and
finally.

General form of an exception-handling block:


try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed before try block ends
}

Procedure:
1. Define a class (Ex10) with the main function.
2. Declare the required variables as follows:
int a =10, b =5, c=5;
int x,y;

3. Enclose the code to monitor inside a try block.


try {
x = a / (b-c);
System.out.println("x = " + x);
}
4. Include a catch clause that specifies the exception type to catch
catch (ArithmeticException e) {
System.out.println("Caught the Division by Zero error");
}
5. Include other program statements after the catch block.
y = a / (b+c);
System.out.println("y = " + y);
6. Compile and execute the program.

Result:
A Java program to handle arithmetic exception (divide by zero error) was developed and
tested successfully.

You might also like