Java Lab Manual (Bcs306a) - Cse (1) - 1
Java Lab Manual (Bcs306a) - Cse (1) - 1
BACHELOR’S IN ENGINEERING
3rd SEMESTER
LABORATORY MANUAL
PREPARED BY
Heena Kouser
Lab Supervisor
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
MISSION
Course Objectives:
To learn primitive constructs JAVA programming language.
To understand Object Oriented Programming Features of JAVA.
To gain knowledge on: packages, multithreaded programing and exceptions.
CIE for the theory component of the IPCC (maximum marks 50):
IPCC means practical portion integrated with the theory of the course.
CIE marks for the theory component are 25 marks and that for the practical
component is 25 marks.
25 marks for the theory component are split into 15 marks for two Internal
Assessment Tests (Two Tests, each of 15 Marks with 01-hour duration, are to
be conducted) and 10 marks for other assessment methods mentioned in
22OB4.2. The first test at the end of 40-50% coverage of the syllabus and the
second test after covering 85-90% of the syllabus.
Scaled-down marks of the sum of two tests and other assessment methods will
be CIE marks for the theory component of IPCC (that is for 25 marks).
The student has to secure 40% of 25 marks to qualify in the CIE of the theory
component of IPCC.
Assessment Method
● Programming Assignment / Course Project
1. Develop a Java program to add two matrices of suitable order N (The value of
N should be read from command line arguments).
import java.util.Scanner;
int n = Integer.parseInt(args[0]);
int a[][]= new int[n][n];
int b[][]= new int[n][n];
int c[][]= new int[n][n];
System.out.print("\n");
}
System.out.println("Enter the Matrix B elements");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
b[i][j]=sc.nextInt();
}
System.out.print("\n");
}
System.out.println("Display the Matrix A elements");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.print(a[i][j]+" ");
}
System.out.print("\n");
}
3
4
3
4
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
stack.push(60);
stack.push(70);
stack.push(80);
stack.push(90);
stack.push(100);
stack.push(110);
stack.display();
stack.display();
}
}
OUTPUT:
Element pushed to the stack 10
Element pushed to the stack 20
Element pushed to the stack 30
Element pushed to the stack 40
Element pushed to the stack 50
Element pushed to the stack 60
Element pushed to the stack 70
Element pushed to the stack 80
Element pushed to the stack 90
Element pushed to the stack 100
Stack is full
Stack elements are:
10 20 30 40 50 60 70 80 90 100
Peek: 100
Stack elements are:
10 20 30 40 50 60 70 80 90 100
3. A class called Employee, which models an employee with an ID, name and salary, is
designed as shown in the following class diagram. The method raiseSalary
(percent) increases the salary by the given percentage. Develop the Employee class
and suitable main method for demonstration.
import java.util.Scanner;
class EmployeeInfo {
String name;
int id;
float salary;
class Employee{
public static void main(String[] args) {
EmployeeInfo e = new EmployeeInfo();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
OUTPUT:
Enter the percentage of the increment
39
Salary after incrememt: 125100.0
import java.util.Scanner;
class MyPoint {
int x, y;
MyPoint() {
x = 0;
y = 0;
}
void setXY() {
Scanner s = new Scanner(System.in);
System.out.print("Enter x coordinate: ");
x = s.nextInt();
System.out.print("Enter y coordinate: ");
y = s.nextInt();
}
String pointToString() {
return String.format("(%d, %d)", x, y);
}
double calcDistance() {
return Math.sqrt(x * x + y * y); // Distance from origin (0,0)
}
class Program4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MyPoint p1 = new MyPoint();
MyPoint p2 = new MyPoint();
MyPoint p3 = new MyPoint();
OUTPUT:
Enter the coordinates for P1:
Enter x coordinate: 1
Enter y coordinate: 2
Enter the coordinates for P2:
Enter x coordinate: 3
Enter y coordinate: 4
Enter the coordinates for P3:
Enter x coordinate: -1
Enter y coordinate: -1
Distance between P1 (1, 2) and O(0,0) is: 2.24
Distance between P1 (1, 2) and P2 (3, 4) is: 2.83
Distance between P1 (1, 2) and P3 (-1, -1) is: 3.61
5. Develop a Java program to create a class named shape. Create three sub
classes namely: circle, triangle and square, each class has two member
functions named draw () and erase (). Demonstrate polymorphism concepts by
developing suitable methods, defining member data and main program.
class ThreeShapes{
c.draw();
c.erase();
t.draw();
t.erase();
s.draw();
s.erase();
}
}
class Shape {
void draw(){
System.out.println("Drawing the shape");
}
void erase(){
System.out.println("Erasing the shape");
void erase(){
System.out.println("Erasing the Circle");
}
}
void erase(){
System.out.println("Erasing the Traingle");
}
}
void erase(){
System.out.println("Erasing the Square");
}
}
OUTPUT:
6. Develop a Java program to create an abstract class Shape with abstract methods
calculateArea() and calculatePerimeter(). Create subclasses Circle and Triangle
that extend the Shape class and implement the respective methods to calculate
the area and perimeter of each shape.
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
@Override
public double calculateArea() {
double s = (a+b+c)/2;
return Math.sqrt( s*(s-a)*(s-b)*(s-c));
}
@Override
public double calculatePerimeter() {
return a+b+c;
}
}
OUTPUT:
Circle Area: 78.53981633974483
Circle Perimeter: 31.41592653589793
interface Resizable {
class Program7{
public static void main(String[] args) {
Rectangle r = new Rectangle();
System.out.println("Original Dimensions:");
System.out.println("Width= "+r.width);
System.out.println("Height= "+r.height);
r.resizeWidth(30);
r.resizeHeight(40);
System.out.println("Resized Dimensions:");
System.out.println("Width= "+r.width);
System.out.println("Height= "+r.height);
}
}
OUTPUT:
OOP with Java Lab (BCS306A) Page
14 of 22
Original Dimensions:
Width= 10
Height= 20
Resized Dimensions:
Width= 30
Height= 40
8. Develop a Java program to create an outer class with a function display. Create
another class inside the outer class named inner with a function called display
and call the two functions in the main class.
class Outer {
void display(){
System.out.println("Hey User, I am an Outer Class ");
}
class Inner{
void display(){
System.out.println("Hey User, I am an Inner Class");
}
}
}
class Program8 {
public static void main(String[] args) {
Outer oo = new Outer();
Outer.Inner io = oo.new Inner();
oo.display();
io.display();
}
}
OUTPUT:
Hey User, I am an Outer Class
Hey User, I am an Inner Class
9. Develop a Java program to raise a custom exception (user defined exception) for
DivisionByZero using try, catch, throw and finally.
import java.util.Scanner;
class Program9 {
try{
if(b==0){
throw new A("Devide by Zero Its an Exception!");
}
c= a/b;
System.out.println("The Result is: "+c);
}
catch(Exception e){
System.out.println(e);
}
finally{
System.out.println("Finally block will be Executed.");
}
}
OUTPUT 1:
Enter Value for Nominator a
4
Enter Value for Nominator b
2
The Result is: 2
Finally block will be Executed.
OUTPUT 2:
9. Dvelop a JAVA program to create a package named mypack and import &
implement it in a suitable class.
//create the main program in a different file outside the mypack folder:
OUTPUT:
Hello I am MyPackageClass from mypack package!
Result of adding numbers: 9
11. Write a program to illustrate creation of threads using runnable class. (start method
start each of the newly created thread. Inside the run method there is sleep() for suspend
the thread for 500 milliseconds).
class ThreadRunnable {
public static void main(String args[]) {
RunnableThread rt1 = new RunnableThread("T1");
rt1.start();
RunnableThread rt2 = new RunnableThread("T2");
rt2.start();
RunnableThread rt3 = new RunnableThread("T3");
rt3.start();
}
}
OUTPUT:
T1 Created and is Running
T2 Created and is Running
T3 Created and is Running