23UBC JAVA RecordNoteContents
23UBC JAVA RecordNoteContents
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
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.
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.
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.
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);
Result: A Java program using the for and while loop was developed and tested successfully.
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.
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:
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.
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
Result:
A Java program with method overloading feature was developed and tested successfully.
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.
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.
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;
Result:
A Java program to handle arithmetic exception (divide by zero error) was developed and
tested successfully.