Manav 22CSU111 Final Lab Manual FOCP-II 2nd Sem
Manav 22CSU111 Final Lab Manual FOCP-II 2nd Sem
Lab Manual
Department of Computer Science and
Engineering
The NorthCap University, Gurugram
FOCP II Lab Manual(CSL108)
2021-22
FOCP II
Lab Manual
CSL 108
Session 2021-22
2
FOCP II Lab Manual(CSL108)
2021-22
Published by:
© Copyright Reserved
3
FOCP II Lab Manual(CSL108)
2021-22
PREFACE
The aim of this lab manual is to help students understand real life
problem using programming skills.
By the end of this practical, the students will gain the foundational skills
a software engineer needs, to solve real-world problems, from
designing algorithms to testing and debugging; and will be able to
apply these concepts to build their own interactive Java applications.
4
FOCP II Lab Manual(CSL108)
2021-22
CONTENTS
Page
S.N. Details No.
1 Introduction 6
2 Lab Requirement 6
3 General Instructions 6
4 List of Experiments 8
5 Rubrics 9
5
FOCP II Lab Manual(CSL108)
2021-22
1. INTRODUCTION
2. LAB REQUIREMENTS
● Mac or PC
3. GENERAL INSTRUCTIONS
6
FOCP II Lab Manual(CSL108)
2021-22
● Students must turn up in time and contact concerned faculty for the
experiment they are supposed to perform.
● Students will not be allowed to enter late in the lab.
● Students will not leave the class till the period is over.
● Damaging lab equipment or removing any component from the lab may
invite penalties and strict disciplinary action.
3.2 Attendance
● Students should not attend a different lab group/section other than the one
assigned at the beginning of the session.
● On account of illness or some family problems, if a student misses his/her
lab classes, he/she may be assigned a different group to make up the
losses in consultation with the concerned faculty / lab instructor. Or he/she
may work in the lab during spare/extra hours to complete the experiment.
No attendance will be granted for such case.
7
FOCP II Lab Manual(CSL108)
2021-22
● Students should come to the lab thoroughly prepared on the practicals they are
assigned to perform on that day. Brief introduction to each experiment with
information about self-study reference is provided on LMS.
● Students must bring the lab report during each practical class with written records
of the last experiments performed complete in all respect.
● Each student is required to write a complete report of the practical he has
performed and bring to lab class for evaluation in the next working lab. Sufficient
space in work book is provided for independent writing of theory, observation,
calculation and conclusion.
● Students should follow the Zero tolerance policy for copying / plagiarism. Zero
marks will be awarded if found copied. If caught further, it will lead to disciplinary
action.
● Refer Annexure 1 for Lab Report Format
4. LIST OF EXPERIMENTS
8
FOCP II Lab Manual(CSL108)
2021-22
3 Programs on Arrays
5 Programs on Inheritance
6 Programs on Interface
7 Programs on Packages
5. RUBRICS
Marks Distribution
9
FOCP II Lab Manual(CSL108)
2021-22
6. Annexure 1
FOCP II
CSL108
Project Report
10
FOCP II Lab Manual(CSL108)
2021-22
Roll No.:
Semester:
Group:
Session 2021-22
Table of Contents
S.No Page
No.
1. Project Title:
2. Description of Project:
Problem Statement
3. Problem Analysis
11
FOCP II Lab Manual(CSL108)
2021-22
4. Design
6. Output (Screenshots)
1 Programs on Data
Types
2 Programs on
Control Statements
12
FOCP II Lab Manual(CSL108)
2021-22
3 Programs on Arrays
4 Programs on
Classes & Objects
5 Programs on
Inheritance
6 Programs on
Packages
7 Programs on
Interfaces
8 Programs on
Exception Handling
9 Programs on File
handling
Index
PRACTICAL NO. 1
Date:
Faculty Signature:
13
FOCP II Lab Manual(CSL108)
2021-22
Objective(s):
Outcome:
Problem Statement:
Q1. Write a program that takes two integers (values to be given within the program) and
displays the output of the following operators: addition, subtraction, multiplication, division and
modulus.
Definition of Done:
DoD 1: Assign two numbers to two variables.
DoD2: Use separate variables to store the results.
Q2. Write a Java program to convert minutes into the number of years, months and days.
Definition Of Done :
DoD 1: Ask the user to input the minutes
DoD 2: Display years and months and days in the sequence yy-mm-dd.
Question Bank:
Ans. Java is considered to be dynamic since it is designed to adapt to an evolving environment. Java
programs can carry an extensive amount of run-time information that can be used to verify and resolve
14
FOCP II Lab Manual(CSL108)
2021-22
Ans. JVM is the main component of Java architecture, and it is the part of the JRE. JVM is
responsible for allocating the necessary memory needed by the Java program. Java is called
platform independent because of Java Virtual Machine. As different computers with the
different operating system have their JVM, when we submit a .class file to any operating system,
JVM interprets the bytecode into machine level language.
Ans.
1. VS Code
2. IntelliJ IDEA
boolean A data type that can hold True and False values only
15
FOCP II Lab Manual(CSL108)
2021-22
Class variables also known as static variables are declared with the static keyword in a class,
but outside a method, constructor or a block. There would only be one copy of each class
variable per class, regardless of how many objects are created from it.
Instance variables are declared in a class, but outside a method. When space is allocated for an object in
the heap, a slot for each instance variable value is created. Instance variables hold values that must be
referenced by more than one method, constructor or block, or essential parts of an object's state that must
be present throughout the class.
Flipped practicals
16
FOCP II Lab Manual(CSL108)
2021-22
import java.util.*;
17
FOCP II Lab Manual(CSL108)
2021-22
in.close();
}
import java.util.*;
sc.close();
18
FOCP II Lab Manual(CSL108)
2021-22
19
FOCP II Lab Manual(CSL108)
2021-22
PRACTICAL NO. 2
Student Name and Roll Number: Manav Goel 22CSU111
Date:
Faculty Signature:
Objective
To familiarize the students with control statements in JAVA
Program Outcome
Through this practical, students will learn about the control statements
Problem Statement
1. Write a Java program to solve quadratic equations (use if, else if and else).
Definition of Done:
DoD 1: The program asks the values of coefficients of a quadratic equation.
DoD 2: The program should display the roots or an appropriate message.
2. Write a Java program that asks the user to provide a single character from the alphabet.
Print Vowel or Consonant, depending on the user input. If the user input is not a letter
(between a and z or A and Z), or is a string of length > 1, print an error message.
Definition of Done:
DoD 1: The program asks an input from the user.
20
FOCP II Lab Manual(CSL108)
2021-22
DoD 2: A single character input is taken from the user or an error message is
generated.
DoD 3: The program should print “Vowel” if the entered character is a vowel and
“Consonant” if the entered character is a consonant.
Background Study:
Java compiler executes the java code from top to bottom. The statements are executed according
to the order in which they appear. However, Java provides statements that can be used to control
the flow of java code. Such statements are called control flow statements.
Java provides three types of control flow statements.
Decision Making statements
Loop statements
Jump statements
Question Bank
1. The most basic control flow statement supported by the Java programming language is the
__if-then__ statement.
2. The __Switch__ statement allows for any number of possible execution paths.
3. The __do-while__ statement is similar to the while statement, but evaluates its expression at
the __bottom__ of the loop.
class Main {
21
FOCP II Lab Manual(CSL108)
2021-22
The code will be executed infinite times as each time the value of I is incremented by 1 and goes
upto infinity.
class Main {
The code will print Hello World as the while loop keeps on executing for infinite time.
6. Which looping process checks the test condition at the end of the loop?
Ans. For loop.
9. Which looping process is best used when the number of iterations is known?
Ans. For loop.
Flipped Practicals
1. Consider the following code snippet.
if (aNumber >= 0)
if (aNumber == 0)
System.out.println("first string");
else System.out.println("second string");
System.out.println("third string");
22
FOCP II Lab Manual(CSL108)
2021-22
import java.util.*;
System.out.println("Enter a");
int a = sc.nextInt();
System.out.println("Enter b");
int b = sc.nextInt();
System.out.println("Enter c");
int c = sc.nextInt();
float d = b*b-4*a*c;
if(d<0){
System.out.println("imaginary soln");
23
FOCP II Lab Manual(CSL108)
2021-22
}
else if(d==0){
System.out.println("roots are equal");
System.out.println("x1 & x2 = "+(float)-b/(2*a));
}
else if(d>0){
System.out.println("unique soln");
System.out.println("x1 = "+(float)(-b+(b*b-
4*a*c))/2*a);
System.out.println("x2 = "+(float)(-b-(b*b-
4*a*c))/2*a);
}
sc.close();
Q2. Write a Java program that asks the user to provide a single character
from the alphabet. Print Vowel or Consonant, depending on the user input. If
the user input is not a letter.
import java.util.*;
if(b==true){
System.out.println("error");
}else{
char ch = sc.next().charAt(0);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='O'||ch=='U'||ch=='I'){
System.out.println(ch+" :its a vowel");
}else{
System.out.println(ch+" :its a consonant");
}
}
24
FOCP II Lab Manual(CSL108)
2021-22
sc.close();
**
***
****
*****
******
25
FOCP II Lab Manual(CSL108)
2021-22
for(int j=1;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
26
FOCP II Lab Manual(CSL108)
2021-22
PRACTICAL NO. 3
Student Name and Roll Number: Manav Goel 22CSU111
Date:
Faculty Signature:
Objective
To familiarize the students with array in JAVA.
Program Outcome
The students will learn the concept of arrays in Java.
Problem Statement
1. Write a Java program to find the maximum and minimum value of an array.
Definition of Done:
27
FOCP II Lab Manual(CSL108)
2021-22
DoD 1: The program should ask the user to enter the elements of the array.
DoD 2: The program should display the maximum and minimum elements of the array.
2. Write a Java program to find the index of an array element in an array of size 10. The
program should not use any function other than main ( ) functions.
Definition of Done:
DoD 1: The program should ask the user to enter the elements of the array.
DoD 2: The program should ask the user to enter a number to search.
DoD 3: The program should display the elements of the array entered.
DoD 4: The program should display the index of the number if the item is present or
display -1 of the elements is not present.
Definition of Done
DoD 1: The program should ask the user to enter the elements of the array.
DoD 2: Even elements will be stored in EvenArray[] and odd elements will be stored in
oddArray[].
4. Write a Java program to read numbers in an integer array of size 5 and display the
following (using functions for each functionality):
Definition of Done
28
FOCP II Lab Manual(CSL108)
2021-22
DoD 1: The program should ask the user to enter the elements of the array.
DoD 2: The program should display a menu with the above choices and ask the user to
choose one of the choices.
Background Study:
Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure
where we store similar elements. We can store only a fixed set of elements in a Java
array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.
Question Bank:
Ans. NO
2. Can you change the size of the array once you define it?
Ans. NO
Ans. No difference.
29
FOCP II Lab Manual(CSL108)
2021-22
Flipped Practicals
1.Which of these is an incorrect array declaration?
a) int arr[] = new int[5]
b) int [] arr = new int[5]
c) int arr[] = new int[5]
d) int arr[] = int [5] new
Ans-(d)
30
FOCP II Lab Manual(CSL108)
2021-22
Q1.Write a Java program to find the maximum and minimum value of an array.
package Arrays;
import java.util.*;
public class MAXMIN {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i;
System.out.println("enter length of array");
int n = sc.nextInt();
System.out.println("enter elements of array");
int arr[] = new int[n];
for( i=0;i<n;i++){
arr[i] = sc.nextInt();
}
int max = arr[0];
int min = arr[0];
for( i=0;i<n;i++){
if(max<arr[i]){
max=arr[i];
}if(min>arr[i]){
min=arr[i];
}
}
System.out.println("max is "+max);
31
FOCP II Lab Manual(CSL108)
2021-22
System.out.println("min is "+min);
sc.close();
}
}
for(i=0;i<n1;i++){
System.out.println(arr[i]);
}
else if(i==n1-1) {
System.out.println("number is not present in
the entered array");
}
}
sc.close();
32
FOCP II Lab Manual(CSL108)
2021-22
Q3. Write a Java Program to count even and odd numbers in an array.
package Arrays;
import java.util.Scanner;
public class hh {
private static Scanner sc;
public static void main(String[] args)
{
int Size, i;
int even = 0, odd = 0;
sc = new Scanner(System.in);
33
FOCP II Lab Manual(CSL108)
2021-22
}
for (i = 0; i < 5; i++) {
alrsum = alrsum + arr[i];
i++;
}
System.out.println("Sum of elements = " + sum);
System.out.println("Sum of alt elements = " + alrsum);
}
34
FOCP II Lab Manual(CSL108)
2021-22
35
FOCP II Lab Manual(CSL108)
2021-22
PRACTICAL NO: 4
Student Name and Roll Number: Manav Goel 22CSU111
Date:
Faculty Signature:
Objective
To familiarize the students with classes and objects.
Program Outcome
The students will learn the concept of classes and objects.
Problem Statement
1.Create a class named 'Student' with String variable 'name' and integer variable 'roll_no'.
Assign the value of roll_no as '2' and that of name as "John" by creating an object of the
class Student.
2. Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5
36
FOCP II Lab Manual(CSL108)
2021-22
units by creating a class named 'Triangle' without any parameter in its constructor.
3. Write a program to print the area of a rectangle by creating a class named 'Area' taking
the values of its length and breadth as parameters of its constructor and having a method
named 'returnArea' which returns the area of the rectangle. Length and breadth of the
rectangle are entered through the keyboard.
4. Print the sum, difference and product of two complex numbers by creating a class
named 'Complex' with separate methods for each operation whose real and imaginary
parts are entered by the user.
5. Write a program to calculate the distance between two points (x1, y1) and (x2, y2). All
numbers and return values should be of type double.
Definition of Done:
DoD 1: Two java files to be defined. One for class definitions and another for the
application
DoD 2: A class point is defined with two float variables for x1 and x2 and the following
functionality:
ii. Get and set methods are defined for all the instance variables.
iii. Distance function is defined to calculate the distance between two points.
Write this program with a static method definition for calculating the distance between two
points.
Background Study
37
FOCP II Lab Manual(CSL108)
2021-22
Question Bank
CLASSES OBJECTS
Class is a blueprint or template from which Object is an instance of a class.
objects are created
Class is declared once. Object is created many times as per
requirement.
Ans. Constructor chaining is the process of calling one constructor from another
constructor with respect to current object. One of the main use of constructor chaining is
to avoid duplicate codes while having multiple constructor (by means of constructor
overloading) and make code more readable
Ans. A constructor that has no parameter is known as the No-argument or Zero argument
constructor. If we don’t define a constructor in a class, then the compiler creates a
constructor(with no arguments) for the class. And if we write a constructor with arguments
or no arguments then the compiler does not create a default constructor.
Ans. If we add a return type to a constructor, then it will become a method of the class.
38
FOCP II Lab Manual(CSL108)
2021-22
This is the way java runtime distinguish between a normal method and a constructor.
Ans. The main purpose of using a private constructor is to restrict object creation. We
also use private constructors to implement the singleton design pattern.
Ans. Yes, we can write a method in Java with method name same as class name. But it is not
suggestable because of the following reasons −
Using upper case letter at the starting of the name of the method violates the coding
conventions of Java for writing a methods name.
There is a chance of assuming such methods with constructors of the class.
Flipped Practicals
39
FOCP II Lab Manual(CSL108)
2021-22
Ans. A) false
Q1. Create a class named 'Student' with String variable 'name' and integer variable
'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating
an object of the class Student.
class student{
String name;
int roll;
student(String name,int roll){
this.name = name;
40
FOCP II Lab Manual(CSL108)
2021-22
this.roll = roll;
}
}
public class basic{
public static void main(String[] args) {
student s1 = new student("John",1);
System.out.println(s1.name);
System.out.println(s1.roll);
}
}
Q2. Write a program to print the area and perimeter of a triangle having sides of 3,
4 and 5 units by creating a class named 'Triangle' without any parameter in its
constructor.
import java.util.*;
float s1 = sc.nextFloat();
float s2 = sc.nextFloat();
float s3 = sc.nextFloat();
t1.perimeter(s1,s2,s3);
float b = sc.nextFloat();
float h = sc.nextFloat();
t1.area(b,h);
}
}
class triangle{
float s1;
float s2;
float s3;
41
FOCP II Lab Manual(CSL108)
2021-22
float b;
float h;
Q3. Write a program to print the area of a rectangle by creating a class named
'Area' taking the values of its length and breadth as parameters of its constructor
and having a method named 'returnArea' which returns the area of the rectangle.
Length and breadth of the rectangle are entered through the keyboard.
import java.util.*;
class Area{
int l1;
int b1;
int l2;
int b2;
}
public int rectangle1(int l1,int b1){
return l1*b1;
}
public int rectangle2(int l2,int b2){
return l2*b2;
42
FOCP II Lab Manual(CSL108)
2021-22
System.out.println("area of rectangle1:
"+rectangle.rectangle1(l1,b1));
System.out.println("area of rectangle2:
"+rectangle.rectangle2(l2,b2));
Q4. Print the sum, difference and product of two complex numbers by
creating a class named 'Complex' with separate methods for each
operation whose real and imaginary parts are entered by the user.
import java.util.*;
class Complex {
Complex()
{
43
FOCP II Lab Manual(CSL108)
2021-22
return temp;
}
return temp;
}
void printComplexNumber()
{
System.out.println("Complex number: "
+ real + " + "
+ imaginary + "i");
}
}
44
FOCP II Lab Manual(CSL108)
2021-22
C1.printComplexNumber();
C2.printComplexNumber();
C3 = C3.addComp(C1, C2);
System.out.print("Sum of ");
C3.printComplexNumber();
C3 = C3.subtractComp(C1, C2);
System.out.print("Difference of ");
C3.printComplexNumber();
}
}
Q5. Write a program to calculate the distance between two points (x1,
y1) and (x2, y2). All numbers and return values should be of type
double.
import java.util.*;
import java.lang.Math;
class distance{
double x1,x2,y1,y2;
45
FOCP II Lab Manual(CSL108)
2021-22
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
46
FOCP II Lab Manual(CSL108)
2021-22
47
FOCP II Lab Manual(CSL108)
2021-22
PRACTICAL NO: 5
Student Name and Roll Number:
Semester /Section:
Date:
Faculty Signature:
Objective
To familiarize the students with the concept of Inheritance.
48
FOCP II Lab Manual(CSL108)
2021-22
Program Outcome
The students will learn the concept of inheritance.
Program Statement
1. Create a class named 'Shape' with a method to print "This is This is shape". Then
create two other classes named 'Rectangle', 'Circle' inheriting the Shape class, both
having a method to print "This is rectangular shape" and "This is circular shape"
respectively. Create a subclass 'Square' of 'Rectangle' having a method to print
"Square is a rectangle". Now call the method of 'Shape' and 'Rectangle' class by the
object of 'Square' class.
Class Vehicle:
Vehicle Class will contain a display() function, which will say "This is a Vehicle".
Class Car:
Car Class will derive the Vehicle Class and overwrite its display() function. it will say
"This is a Car".
Class Bike:
Bike Class will derive the Vehicle Class and overwrite its display() function. it will say "This
is a Bike".
Write an application that reads an Integer N, which will denote the number of tyres in the
vehicle. You have to create an object of the appropriate class according to the value of N
and use it display() function.
Definition of Done:
49
FOCP II Lab Manual(CSL108)
2021-22
DoD 2: Switch statement is used for identifying the appropriate class for which the object is
to be invoked.
3. Define a class Box with the following instance variables: width, height and depth, all
of type float. Create a new class BoxWeigth that extends Box to include weight as
an instance variable. Write an application that tests the functionalities of both these
classes.
Definition of Done:
DoD 1: Three java files to be defined. One for each class definition: Box,
BoxWeight and BoxWeightDemo.
DoD 2: Box and BoxWeight should have three types of constructors defined: clone
of an object, all dimensions specified as arguments, no argument.
DoD 3: Super is used to call base class constructors in derived class
DoD 4: Get and set functions defined as applicable in Box and BoxWeight classes.
DoD 5: Function to display volume in Box class and weight in BoxWeigth class
Background Study
Inheritance can be defined as the process where one class acquires the properties
(methods and fields) of another. With the use of inheritance, the information is made
manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child
class) and the class whose properties are inherited is known as superclass (base class,
parent class).
extends Keyword
extends is the keyword used to inherit the properties of a class. Following is the syntax of
extends keyword.
Syntax
class Super {
.....
.....
50
FOCP II Lab Manual(CSL108)
2021-22
}
class Sub extends Super {
.....
.....
}
Question Bank
class B extends A
{
{
System.out.println(2);
}
}
class C extends B
{
{
System.out.println(3);
}
}
51
FOCP II Lab Manual(CSL108)
2021-22
class A
{
public A()
{
System.out.println("Class A Constructor");
}
}
class B extends A
{
public B()
{
System.out.println("Class B Constructor");
}
}
class C extends B
{
public C()
{
System.out.println("Class C Constructor");
}
}
52
FOCP II Lab Manual(CSL108)
2021-22
53
FOCP II Lab Manual(CSL108)
2021-22
PRACTICAL NO. 6
Student Name and Roll Number:
Semester /Section:
Date:
Faculty Signature:
Objective
To familiarize the students with the concept of Interfaces in Java.
Program Outcome
54
FOCP II Lab Manual(CSL108)
2021-22
The students will be able to understand where and how interfaces are implemented.
Problem Statement
1. 1. a) Write a program in java to check if a class can extends another class and/ can
implement one and more than one interface.
2. Define the interface / class hierarchy as detailed in the following class diagram
Definition of Done:
DOD 1: The class definitions are defined as per the class diagram.
DOD 2: Each class definition is stored in its own .java file.
DOD 3: Base class constructors are invoked using super keyword
DOD 4: Function overriding is applied wherever applicable.
3. We have to calculate the area of a rectangle, a square and a circle. Create an abstract
class 'Shape' with three abstract methods namely 'RectangleArea' taking two parameters,
'SquareArea' and 'CircleArea' taking one parameter each. The parameters of
'RectangleArea' are its length and breadth, that of 'SquareArea' is its side and that of
'CircleArea' is its radius. Now create another class 'Area' containing all the three methods
'RectangleArea', 'SquareArea' and 'CircleArea' for printing the area of rectangle, square
and circle respectively. Create an object of class 'Area' and call all the three methods.
Background Study
55
FOCP II Lab Manual(CSL108)
2021-22
Flipped Practicals
56
FOCP II Lab Manual(CSL108)
2021-22
2. Which of the following package stores all the standard java classes?
a) lang
b) java
c) util
d) java.packages
interface A { }
class C { }
class D extends C { }
class B extends D implements A { }
public class Test extends Thread{
public static void main(String[] args){
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}
a) b is an instance of A.
b) b is an instance of C.
57
FOCP II Lab Manual(CSL108)
2021-22
Question Bank
58
FOCP II Lab Manual(CSL108)
2021-22
Experiment No: 7
Student Name and Roll Number:
Semester /Section:
Date:
Faculty Signature:
Objective
To familiarize the students with package in Java.
Program Outcome
The students will learn the concept of package in Java. They will be able to understand
Problem Statement
1. Create a Java package called exercises. Inside the exercises package, create
another package (subpackage) called java. Create a Java class called PackageDemo
inside the java package. Insert a display() method inside the PackageDemo class.
Inside the method, insert this statement:System.out.println("PackageDemo
59
FOCP II Lab Manual(CSL108)
2021-22
2. Write a java program outside the above defined package that imports the
package PackageDemo and calls its display() function.
Background Study
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.
Flipped Practicals
package pkg;
class display
{
int x;
void show()
{
if (x > 1)
System.out.print(x + " ");
}
}
class packages
{
public static void main(String args[])
{
display[] arr=new display[3];
60
FOCP II Lab Manual(CSL108)
2021-22
for(int i=0;i<3;i++)
arr[i]=new display();
arr[0].x = 0;
arr[1].x = 1;
arr[2].x = 2;
for (int i = 0; i < 3; ++i)
arr[i].show();
}
}
Qu Question Bank
5. Java compiler stores the .class files in the path specified in CLASSPATH environmental
variable. True/False ?
6. User-defined package can also be imported just like the standard packages
True/False ?
7. A _______ is used to separate the hierarchy of the class while declaring an Import
statement.
8. All standard classes of Java are included within a package called _____.
61
FOCP II Lab Manual(CSL108)
2021-22
62
FOCP II Lab Manual(CSL108)
2021-22
PRACTICAL NO.8
Student Name and Roll Number:
Semester /Section:
Date:
Faculty Signature:
63
Objective
To familiarize the students with exception in java.
Program Outcome
The students will learn the concept of exceptions in Java.
1. Write a program that takes 5 integer command line arguments. Create a user
defined Exception named CheckArgumentException to check the number of
arguments passed through command line. If the number of arguments is less than
five, throw the CheckArgumentException, else print the addition of all five numbers.
Definition of Done:
Background Study
An exception (or exceptional event) is a problem that arises during the execution of a
program. When an Exception occurs the normal flow of the program is disrupted and the
program/Application terminates abnormally, which is not recommended, therefore, these
exceptions are to be handled.
An exception can occur for many different reasons. Following are some scenarios where
an exception occurs.
A user has entered an invalid data.
A file that needs to be opened cannot be found.
A network connection has been lost in the middle of communications or the JVM
has run out of memory.
QuestionQuestion Bank
65
FOCP II Lab Manual(CSL108)
2021-22
PRACTICAL NO. 9
Student Name and Roll Number:
Semester /Section:
Date:
Faculty Signature:
Objective
To familiarize the students with File handling.
66
FOCP II Lab Manual(CSL108)
2021-22
Program Outcome
The students will learn the concept of files in Java.
Problem Statement
3.W 3. Write a program to perform simple read and write operation into file.
4. Write a program that writes an Serial No.(int), First Name (String), CGPA (float) and
Grade(char) into a text file using bufferedWriter and displays the contents using
bufferedReader.
Background Study
File handling is an important part of any application. Java has several methods for creating,
reading, updating, and deleting files.
The File class from the java.io package, allows us to work with files.
To use the File class, create an object of the class, and specify the filename or directory name:
Example
67
FOCP II Lab Manual(CSL108)
2021-22
1. Which of these exception is thrown in cases when the file specified for writing is not found?
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputException
3. Which of these values is returned by read() method is end of file (EOF) is encountered?
a) 0
b) 1
c) -1
d) Null
Flipped Practicals
1. import java.io.*;
2. class filesinputoutput
3. {
4. public static void main(String args[])
5. {
6. InputStream obj = new FileInputStream("inputoutput.java");
7. System.out.print(obj.available());
8. }
9. }
68
FOCP II Lab Manual(CSL108)
2021-22
a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file
1. import java.io.*;
2. public class filesinputoutput
3. {
4. public static void main(String[] args)
5. {
6. String obj = "abc";
7. byte b[] = obj.getBytes();
8. ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
9. for (int i = 0; i < 2; ++ i)
10. {
11. int c;
12. while((c = obj1.read()) != -1)
13. {
14. if(i == 0)
15. {
16. System.out.print(Character.toUpperCase((char)c));
17. obj2.write(1);
18. }
19. }
20. System.out.print(obj2);
21. }
22. }
23. }
a) AaBaCa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa
69
FOCP II Lab Manual(CSL108)
2021-22
Algorithm/Flowchart/Code/Sample Outputs
70