20ISL47A OOPS With JAVA Lab Manual
20ISL47A OOPS With JAVA Lab Manual
20ISL47A OOPS With JAVA Lab Manual
Semester : IV 2022
Mrs. Lohitha
Mrs. PriyaN
New Horizon College of Engineering
VISION
MISSION
● To strengthen the theoretical, practical and ethical dimensions of the learning process
by fostering a culture of research and innovation among faculty members and
students.
● To encourage long-term interaction between the academia and industry through their
involvement in the design of curriculum and its hands-on implementation.
● To strengthen and mould students in professional, ethical, social and environmental
dimensions by encouraging participation in co-curricular and extracurricular
activities.
MISSION
● To strengthen the theoretical, practical and ethical dimensions of the learning process
by continuous learning and establishing a culture of research and innovation among
faculty members and students, in the field of Information Science and Engineering
● To build long-term interaction between the academia and Information Technology
industry, through their involvement in the design of curriculum and its hands-on
implementation.
● To strengthen and mould students in professional, ethical, social and environmental
dimensions by encouraging participation in co-curricular and extracurricular
activities.
PEO1: Excel as Information Science Engineers with ability to solve wide range of
computational problems in IT industry, Government or other work environments.
PEO2: Pursue higher studies with profound knowledge enriched with academia and industrial
skill sets.
PEO3: Exhibit adaptive skills to develop computing systems using modern tools and
technologies in multidisciplinary areas to meet technical and managerial challenges, which
meet societal requirements.
PEO4: Possess the ability to collaborate as a team member and leader with professional ethics
to make a positive impact on society.
PSO1: The ability to understand, analyze and develop computer programs in the areas related
to Algorithms, System Software, Web Design, Big Data Analytics, Machine Learning,
Internet of Things, Data Science and Networking for efficient design of computer based
systems of varying complexity.
PSO2: The ability to apply standard practices and strategies in software project development
using innovative ideas and open ended programming environment with skills in teams and
professional ethics to deliver a quality product for business success.
SPECIFIC
OBJECT ORIENTED PROGRAMMING WITH JAVA LABORATORY
Course Outcomes: At the end of the Course, the Student will be able to:
CO1 Model the real world applications using Object Oriented Programming concepts.
CO3 Analyze the importance of exception handling and learn the importance of string handling
CO5 Develop applications using collections framework for managing user defined types
CO6 Solve the real world problems using Object Oriented concepts and collection framework in
Java.
CO/
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
PO
CO1 3 2 3 1 3 2 2 2 - 1 - 3
CO2 3 3 3 2 3 2 2 2 - 1 - 3
CO3 2 2 3 3 2 2 2 2 - 1 - 3
CO4 2 2 3 3 2 2 2 2 - 1 - 3
Experiment
Experiment
No.
PART-A
1 Design and Implement a Java program to print the sum of the elements of the
array withthe given below condition. If the array has 6 and 7 in
succeedingorders, ignore 6 and 7 and the numbers between them for the
calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22
[i.e. 10+3+9]
Eg2) Array Elements - 7,1,2,3,6
O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10
Design and Implement a Java program that displays a menu with options 1. Add
2. Sub, Based on the options chosen, read 2 numbers and perform the relevant
operation. After performing the operation, the program should ask the user if he
wants to continue. If the user presses y or Y, then the program should continue
2
displaying the menu else the program should terminate.
[ Note: Use Scanner class, you can take help from the trainer
regarding the same ]
Develop a Java program Write a program to check if the program has received
command line arguments or not. If the program has not received the values then
print "No Values", else print all the values in a single line separated by ,
(comma).
Eg1) java Example
4
O/P: No values
Eg2) java Example Mumbai Bangalore
O/P: Mumbai, Bangalore
[Note: You can use length property of an array to check its length
Design and Develop a simple Java program to find the longest substring without
repeating characters in a given String. Accept the String through Command Line
5 argument.
Given a string and a non-empty word string, return a string made of each char
just before and just after every appearance of the word in the string. Ignore cases
where there is no char before or after the word, and a char may be included twice
if it is between two words.
If inputs are "abcXY123XYijk" and "XY", output should be "c13i".
6
If inputs are "XY123XY" and "XY", output should be "13".
PART-B
7 Design a class that can be used by a health care professional to keep track of a
patient’s vital statistics. Here’s what the class should do:
Construct a class called Patient
Store a String name for the patient
Store weight and height for patient as doubles
Construct a new patient using these values
Write a method called BMI which returns the patient’s BMI as a
double. BMI can be calculated as BMI = ( Weight in Pounds / ( Height in inches
x Height in inches ) ) x 703
Next, construct a class called “Patients” and create a main method. Create a
Patient object and assign some height and weight to that object. Display the BMI
of that patient.
Create class of SalesPersons as a thread that will display fives sales persons
name. Create a class as Days as other Thread that has array of seven days. Call
the instance of SalesPersons in Days and start both the Threads. Suspend
10
SalesPersons on Sunday and resume on Wednesday. Use Thread handling Apis
to perform the same.
Design and Develop a editor like MS-Word using Java AWT Programming.
12
● One experiment from part A & One experiment from part B to be given
● Examination will be conducted for 50 marks and scaled down to 25 marks
● Marks Distribution : Procedure write-up – 20%
Conduction – 60%
Viva – Voce – 20%
● Change of the experiment is allowed only once and procedure write-up marks will be
considered as ‘0’
CIE - Continuous Internal Evaluation (25 Marks)
Tests
Bloom’s Category
(25 Marks )
Remember
Understand 5
Apply 15
Analyze 5
Evaluate
Create
Design and Implement a Java program to print the sum of the elements of the array with the
given below condition. If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the
numbers between them for the calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22
[i.e. 10+3+9]
O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10
import java.util.Scanner;
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array:");
n = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();}
int sixPos=-1;
int sevenPos=-1;
int sum=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==6)
{sixPos=i;
break;}}
for(int i=0;i<arr.length;i++)
{
if(arr[i]==7) sevenPos=i;
}
if(sevenPos==-1) sixPos=-1;
for(int i=0;i<arr.length;i++)
{
if(sixPos!=-1 && i>=sixPos && i<=sevenPos) continue;
sum+=arr[i];
}
System.out.println(sum);
}
}
Output:
Test 1 :
Test 2 :
Design and Implement a Java program that displays a menu with options 1. Add 2. Sub,
Based on the options chosen, read 2 numbers and perform the relevant operation. After
performing the operation, the program should ask the user if he wants to continue. If the user
presses y or Y, then the program should continue displaying the menu else the program
should terminate.
[ Note: Use Scanner class, you can take help from the trainer
regarding the same ]
package fourd;
import java.util.*;
class calculator
{
public static void main(String a[])
{
int num1=0,num2=0,option,ex;
Scanner sc1 = new Scanner(System.in);
do
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your choice from the following menu:");
System.out.println("1.Addition 2.Subtraction 3.Exit");
option = sc.nextInt();
if(option!=3){
System.out.println("Enter the first number");
num1=sc.nextInt();
System.out.println("Enter the second number");
num2=sc.nextInt();
}
else
break;
switch(option)
{
case 1:System.out.println("Addition of "+num1+" and
"+num2+" is "+(num1+num2));
break;
case 2:System.out.println("Subtraction of "+num1+" and
"+num2+" is "+(num1-num2));
break;
case 3: break;
}
}
Output:
Design and implement an algorithm to accept an array of 5 positive integers. The algorithm
must then find the smallest positive integer in the array which cannot be formed from the sum
of 2 numbers in the array.
package javapgms;
return res;
}
OUTPUT:
2
4
5
10
Program 4
Develop a Java program Write a program to check if the program has received command line
arguments or not. If the program has not received the values then print "No Values", else
print all the values in a single line separated by ,(comma).
Eg1) java Example
O/P: No values
Eg2) java Example Mumbai Bangalore
O/P: Mumbai, Bangalore
[Note: You can use length property of an array to check its length
package programs;
}
}
}
No Values
With arguments
Mumbai, Bangalore
Program 5.
Design and Develop a simple Java program to find the longest substring without repeating
characters in a given String. Accept the String through Command Line argument.
package javapgms;
import java.util.LinkedHashMap;
public class longestsubstr {
//Initialization
int longestSubstringLength = 0;
//If ch is not present in charPosMap, adding ch into charPosMap along with its position
if(!charPosMap.containsKey(ch))
{
charPosMap.put(ch, i);
}
//If ch is already present in charPosMap, reposioning the cursor i to the position of ch and
clearing the charPosMap
else
{
i = charPosMap.get(ch);
charPosMap.clear();
}
//Updating longestSubstring and longestSubstringLength
if(charPosMap.size() > longestSubstringLength)
{
longestSubstringLength = charPosMap.size();
longestSubstring = charPosMap.keySet().toString();
}
}
System.out.println("==========================");
longestSubstring("thelongestsubstring");
}
}
OUTPUT:
Given a string and a non-empty word string, return a string made of each char just before and
just after every appearance of the word in the string. Ignore cases where there is no char
before or after the word, and a char may be included twice if it is between two words.
If inputs are "abcXY123XYijk" and "XY", output should be "c13i".
If inputs are "XY123XY" and "XY", output should be "13".
package javapgms;
package com.nhce.program1a;
public class Patient
{
String patient_name;
double weight;
double height;
Patients.java
package com.nhce.program1a;
public class Patients {
Program 8.
1. A static method called powerInt(int num1,int num2) that accepts two integers and returns
num1 to the power of num2 (num1 power num2).
2. A static method called powerDouble(double num1,int num2) that accepts one double and
one integer and returns num1 to the power of num2 (num1 power num2).
3. Call your method from another class without instantiating the class (i.e. call it like
Calculator.powerInt(12,10) since your methods are defined to be static).
Hint: Use Math.pow(double,double) to calculate the power.
package lab8;
import java.lang.Math;
class calp{
// driver code
public static void main(String args[])
{
int w;
w= powerInt(2,3);
System.out.println("CASE - 1 :");
System.out.println("static method called that accepts two integers ");
System.out.println(w);
double w1;
w1 = powerdouble(2.2,3);
System.out.println("CASE-2 :");
System.out.println("static method called that accepts one double and one
integer ");
System.out.println(w1);
System.out.println("CASE-3 :");
System.out.println("static method called from other class");
System.out.println(calp.powInt_withinotherclass(4,3));
}
}
Output
CASE - 1 :
CASE-2 :
static method called that accepts one double and one integer
10.648000000000003
CASE-3 :
static method called from other class
64
Program 9.
Write a Program to take care of Number Format Exception if user enters values other than
integer for calculating average marks of 2 students. The name of the students and marks in 3
subjects are taken from the user while executing the program. In the same Program write
your own Exception classes to take care of Negative values and values out of range (i.e.
other than in the range of 0-100). Include finally to output the statement “Program
terminated”.
InputExceptionHandle.java
package com.nhce.program2a;
import java.util.Scanner;
avg_marks=(sum1+sum2)/6;
System.out.println("The average marks of "+ student1_name + " and " + student2_name + "
is " + avg_marks);
} msg
catch(NumberFormatException e)
{ System.out.println("Entered input is not a valid format for an integer" + e); }
catch(MarksValidationException e)
{ System.out.println(e.toString()); }
finally
{
in.close();
System.out.println("Program terminated!!!");
}
}
}
MarksValidationException.java
package com.nhce.program2a;
MarksValidationException(String msg){
this.msg=msg;
}
Output
Enter Student1 Name:
vandana
Enter Student1 mark1 :
Fdgdghd
Entered input is not a valid format for an integer
-12
Enter Student1 mark2 :
105
Marks entered are negative or outside the range (0-100)
Program terminated!!!
Program 10
Create class of SalesPersons as a thread that will display fives sales persons name. Create a
class as Days as other Thread that has array of seven days. Call the instance of SalesPersons
in Days and start both the Threads. Suspend SalesPersons on Sunday and resume on the day
Wednesday.
Days.java
package com.nhce.program3a;
sales.start();
for(int i=0;i<7;i++)
{
if(weekDays[i].equals("Sunday"))
if(weekDays[i].equals("Wednesday"))
{
sales.resume();
System.out.println("resuming sales thread");
}
}
SalesPersons.java
package com.nhce.program3a;
public class SalesPersons extends Thread {
Output
Starting Days Thread
suspended sales thread
resuming sales thread
Staring SalesPerson Thread
SalesPerson1 : Hari
SalesPerson2 : Ram
SalesPerson3 : John
SalesPerson4 : Tom
SalesPerson5 : Joseph
Program 11
Create a Student Attendance Management System using aHashMap Collection type. Perform
the following operations:
Add the key-value pair.
Retrieve the value associated with a given key
Check whether a particular key/value exist.
replace a value associated with a given key in the HashMap
import java.util.*;
import java.lang.*;
class Xyz
{
String name;
String clg ;
String branch ;
double per;
}
class Lab11
{
public static void main(String arg[])
{
HashMap<Integer,Xyz> hm=new HashMap<Integer, Xyz>();
Output:
Design and Develop a editor like MS-Word using Java AWT Programming.
package programs;
import java.awt.*;
public class MenuExample {
MenuExample(){
Frame f= new Frame("MS-Word like menu");
MenuBar mb=new MenuBar();
Menu menu=new Menu("File");
MenuItem i1=new MenuItem("New");
MenuItem i2=new MenuItem("Open");
MenuItem i3=new MenuItem("Save");
MenuItem i4=new MenuItem("Save as");
MenuItem i5=new MenuItem("Print");
MenuItem i6=new MenuItem("Close");
menu.add(i1);
menu.add(i2);
menu.add(i3);
menu.add(i4);
menu.add(i5);
menu.add(i6);
Menu menu1=new Menu("Home");
MenuItem i7=new MenuItem("clipboard");
MenuItem i8=new MenuItem("Font");
MenuItem i9=new MenuItem("paragraph");
MenuItem i10=new MenuItem("Styles");
menu1.add(i7);
menu1.add(i8);
menu1.add(i9);
menu1.add(i10);
Menu menu2=new Menu("Insert");
MenuItem i11=new MenuItem("Pages");
MenuItem i12=new MenuItem("Tables");
MenuItem i13=new MenuItem("Illustrations");
MenuItem i14=new MenuItem("Add-ins");
MenuItem i15=new MenuItem("Media");
MenuItem i16=new MenuItem("Links");
MenuItem i17=new MenuItem("Comments");
MenuItem i18=new MenuItem("Header & Footer");
MenuItem i19=new MenuItem("Text");
MenuItem i20=new MenuItem("Symbols");
menu2.add(i11);
menu2.add(i12);
menu2.add(i13);
menu2.add(i14);
menu2.add(i15);
menu2.add(i16);
menu2.add(i17);
menu2.add(i18);
menu2.add(i19);
menu2.add(i20);
Menu menu3=new Menu("Design");
MenuItem i21=new MenuItem("Document Formatting");
MenuItem i22=new MenuItem("Page Background");
menu3.add(i21);
menu3.add(i22);
Menu menu4=new Menu("Layout");
MenuItem m1=new MenuItem("Page setup");
MenuItem m2=new MenuItem("Paragraph");
MenuItem m3=new MenuItem("Arrange");
menu4.add(m1);
menu4.add(m2);
menu4.add(m3);
Menu menu5=new Menu("References");
MenuItem n6=new MenuItem("Table of Contents");
MenuItem n1=new MenuItem("Foot Notes");
MenuItem n2=new MenuItem("Citations & Bibliography");
MenuItem n3=new MenuItem("Captions");
MenuItem n4=new MenuItem("Index");
MenuItem n5=new MenuItem("Table of Authorities");
menu5.add(n6);
menu5.add(n1);
menu5.add(n2);
menu5.add(n3);
menu5.add(n4);
menu5.add(n5);
Menu menu6=new Menu("Mailings");
MenuItem n7=new MenuItem("Create");
MenuItem n8=new MenuItem("Start mail Merge");
MenuItem n9=new MenuItem("Write & Insert Fields");
MenuItem n10=new MenuItem("Preview results");
MenuItem n11=new MenuItem("Finish");
menu6.add(n7);
menu6.add(n8);
menu6.add(n9);
menu6.add(n10);
menu6.add(n11);
Menu menu7=new Menu("Review");
MenuItem p1=new MenuItem("Proofing");
MenuItem p2=new MenuItem("Insights");
MenuItem p3=new MenuItem("Language");
MenuItem p4=new MenuItem("Comments");
MenuItem p5=new MenuItem("Tracking");
MenuItem p6=new MenuItem("Changes");
MenuItem p7=new MenuItem("Compare");
MenuItem p8=new MenuItem("Protect");
menu7.add(p1);
menu7.add(p2);
menu7.add(p3);
menu7.add(p4);
menu7.add(p5);
menu7.add(p6);
menu7.add(p7);
menu7.add(p8);
Menu menu8=new Menu("view");
MenuItem p9=new MenuItem("views");
MenuItem p10=new MenuItem("Show");
MenuItem p11=new MenuItem("zoom");
MenuItem p12=new MenuItem("window");
MenuItem p13=new MenuItem("macros");
menu8.add(p9);
menu8.add(p10);
menu8.add(p11);
menu8.add(p12);
menu8.add(p13);
mb.add(menu);
mb.add(menu1);
mb.add(menu2);
mb.add(menu3);
mb.add(menu4);
mb.add(menu5);
mb.add(menu6);
mb.add(menu7);
mb.add(menu8);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample();
}
}
Output: