20ISL47A OOPS With JAVA Lab Manual

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

OBJECT ORIENTED PROGRAMMING WITH

JAVA LABORATORY MANUAL


Course Code : 20ISL47A

Semester : IV 2022

Department of Information Science and Engineering

Prepared By Approved By Authorized By


Mrs. Karthiyayini J

Dr.Kalaivani D Mrs. Vandana C P Dr.R J Anandhi

Mrs. Lohitha

Mrs. PriyaN
New Horizon College of Engineering
VISION

To emerge as an institute of eminence in the fields of engineering, technology and


management in serving the industry and the nation by empowering students with a high
degree of technical, managerial and practical competence.

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.

Department of Information Science & Engineering


VISION

To emerge as a Department of eminence in Information Science and Engineering in serving


the Information Technology industry and the nation by empowering students with a high
degree of technical and practical competence.

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.

PROGRAM EDUCATIONAL OBJECTIVES

The Information Science and Engineering Graduates will be able to

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.

PROGRAM SPECIFIC OUTCOMES (PSOs)

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 Code : 20ISL47A Credits : 1.5


L:T:P:S : 0:0:1.5:0 CIE Marks : 25
Exam Hours : 3 SEE Marks : 25

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.

CO2 Identify the importance of inheritance and interface concepts

CO3 Analyze the importance of exception handling and learn the importance of string handling

CO4 Apply the concept of Multithreading in concurrent programming

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.

Mapping of Course Outcomes to Program Outcomes:

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 ]

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
3
be formed from the sum of 2 numbers in the array.

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".

If inputs are "XY1XY" and "XY", output should be "11".


Develop a Java program for the same.

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 a class in Java called “Calculator” which contains the following:


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
8
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.

Develop 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.
9
 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”.

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.

Create a Student Attendance Management System using aHashMap Collection


type. Perform the following operations:
Add the key-value pair.
11. 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

Design and Develop a editor like MS-Word using Java AWT Programming.
12

For SEE Examination:

● 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

SEE – Semester End Examination (25 Marks)

Bloom’s Taxonomy Marks


Remember
Understand 5
Apply 15
Analyze 5
Evaluate
Create
Program 1

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]

Eg2) Array Elements - 7,1,2,3,6

O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10

import java.util.Scanner;

public class Lab1 {

public static void main(String args[])


{
//checking testcases
//int[] arr= {1,2,3,4,6,8,9,7,2};
// int[] arr= {1,2,3,6,4,2,8,7,4};
//int[] arr= {1,2,7,1,2,3,6,8};
//int[] arr= {1,2,3};

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 :

Enter the size of array:


6
Enter the elements of array:
3
4
5
6
8
9
The Result considering all the conditions is = 35

Test 2 :

Enter the size of array:


4
Enter the elements of array:
1
2
3
4
The Result considering all the conditions is = 10
Test 3:

Enter the size of array:


7
Enter the elements of array:
1
2
3
6
4
3
7
The Result considering all the conditions is = 6
Test 4:

Enter the size of array:


7
Enter the elements of array:
6
6
6
7
7
7
7
The Result considering all the conditions is = 0
Program 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;

default: System.out.println("Invalid choice");


}
System.out.println("Do you want to continue? Press y or Y to continue
or n to exit ");
String input = sc1.nextLine();
if (input.equals("n")) {
break;
}
}while(true);

}
}

Output:

Enter your choice from the following menu:


1.Addition 2.Subtraction 3.Exit
1
Enter the first number
2
Enter the second number
3
Addition of 2 and 3 is 5
Do you want to continue?press Y or y to continue, press No to quit
y
Enter your choice from the following menu:
1.Addition 2.Subtraction 3.Exit
2
Enter the first number
7
Enter the second number
2
Subtraction of 7 and 2 is 5
Do you want to continue?press Y or y to continue, press No to quit
n
Program 3.

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;

public class pgm3 {


int findSmallest(int arr[], int n)
{
int res = 1; // Initialize result

// Traverse the array and increment 'res' if arr[i] is


// smaller than or equal to 'res'.
for (int i = 0; i < n && arr[i] <= res; i++)
res = res + arr[i];

return res;
}

public static void main(String[] args) {


// TODO Auto-generated method stub
pgm3 small = new pgm3();
int arr1[] = {1, 3, 4, 5};
int n1 = arr1.length;
System.out.println(small.findSmallest(arr1, n1));

int arr2[] = {1, 2, 6, 10, 11, 15};


int n2 = arr2.length;
System.out.println(small.findSmallest(arr2, n2));

int arr3[] = {1, 1, 1, 1};


int n3 = arr3.length;
System.out.println(small.findSmallest(arr3, n3));

int arr4[] = {1, 1, 3, 4};


int n4 = arr4.length;
System.out.println(small.findSmallest(arr4, n4));
}
}

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;

public class arguments {


public static void main (String[] args)
{
//PROGRAM FINDS WHETHER THERE IS A COMMAND LINE AGRGUMENT
OR NOT AND SEPERATES BY COMMA
if (args.length ==0)
{
System.out.println("No Values");
}
else{
for(String i : args) //for each value in args store in i
System.out.print(i+" , ");

}
}
}

To give command line arguments in eclipse

1. Click on Run -> Run Configurations.


2. Click on Arguments tab.
3. In Program Arguments section , Enter your arguments.
4. Click Apply.
Output

Without giving arguments

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 {

static void longestSubstring(String inputString)


{
//Convert inputString to charArray

char[] charArray = inputString.toCharArray();

//Initialization

String longestSubstring = null;

int longestSubstringLength = 0;

//Creating LinkedHashMap with characters as keys and their position as values.

LinkedHashMap<Character, Integer> charPosMap = new LinkedHashMap<Character,


Integer>();

//Iterating through charArray

for (int i = 0; i < charArray.length; i++)


{
char ch = charArray[i];

//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("Input String : "+inputString);

System.out.println("The longest substring : "+longestSubstring);

System.out.println("The longest Substring Length : "+longestSubstringLength);


}

public static void main(String[] args)


{
longestSubstring("javaconceptoftheday");

System.out.println("==========================");

longestSubstring("thelongestsubstring");
}
}

OUTPUT:

Input String : javaconceptoftheday


The longest substring : [o, f, t, h, e, d, a, y]
The longest Substring Length : 8
==========================
Input String : thelongestsubstring
The longest substring : [u, b, s, t, r, i, n, g]
The longest Substring Length : 8
Program 6.

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;

public class pgm5 {


public static void main(String[] args) {
// TODO Auto-generated method stub
String Test="xvxXY123XYabcXY";
String word="XY";
String result= new String();
String pattern[]= Test.split(word); //1st string:xvx
// 2nd string:123
//3rd string:abc splitting the input string based on word pattern
for(int i=0;i<pattern.length;i++)
{
int length=pattern[i].length(); // number of strings returned after split operation

if(i==0) // first string


{
if (!(pattern[i]).equals("")) //if first string is empty, case when input string starts with word
string
{
String strend=pattern[i].substring(length-1,length); //if not, take the last character of the
string , eg abcXY123 , abc ->a xvx & XY take x
result=strend;
}
}
else if(i==pattern.length-1 && !(Test.endsWith(word))) // last string but not ending with
XY , eg abcXYhj, except cases like abcXY abcXY & XY take c
{ String strbegin= pattern[i].substring(0,1); // take the first character
result=result.concat(strbegin);
break; }
else
{
String strend=pattern[i].substring(length-1,length); //all other case take first and last
character of the string
String strbegin= pattern[i].substring(0,1);
result=result.concat(strbegin).concat(strend);
}
}
System.out.print("Final output "+ result);
}
}
Output
Final output x13ac
Program 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:
1. Construct a class called Patient
2. Store a String name for the patient
3. Store weight and height for patient as doubles
4. Construct a new patient using these values
5. 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
6. 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.
Patient.java

package com.nhce.program1a;
public class Patient
{
String patient_name;
double weight;
double height;

Patient(String name, double wt, double ht)


{
patient_name=name;
weight=wt;
height=ht;
}
double BMI()
{
return (weight/(height*height))*703;
}
}

Patients.java

package com.nhce.program1a;
public class Patients {

public static void main(String[] args) {

Patient patient_1 = new Patient("Hari", 78.4, 51);

System.out.println("BMI of Patient: "+ patient_1.patient_name+" is "+patient_1.BMI());


Patient patient_2 = new Patient("Ram", 79.4, 67);
System.out.println("BMI of Patient: "+ patient_2.patient_name+" is "+patient_2.BMI());
}
}
Output
BMI of Patient: Hari is 21.190003844675125
BMI of Patient: Ram is 12.434439741590555

Program 8.

Create a class in Java called “Calculator” which contains the following:

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{

static int powInt_withinotherclass(int a,int b)


{
double a1 = a;
double a2= b;
double x;
x= Math.pow(a1,a2);
int x1 =(int)x;
return x1;

public class calpower {

static int powerInt(int a,int b)


{
double a1 = a;
double a2= b;
double x;
x= Math.pow(a1,a2);
int x1 =(int)x;
return x1;

static double powerdouble(double a,int b )


{
//double a1 = a;
double a2= b;
double x;
x= Math.pow(a,a2);
//int x1 =(int)x;
return x;

// 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 :

static method called that accepts two integers


8

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;

public class InputExceptionHandle {


public static void main(String[] args) {
String student1_name,student2_name;
int student1_marks[]= new int[3];
int student2_marks[]= new int[3];
int sum1=0,sum2=0;
float avg_marks;
Scanner in = new Scanner(System.in);
try{
System.out.println("Enter Student1 Name: ");
student1_name = in.nextLine();
for(int i=0;i<3;i++)
{
System.out.println("Enter Student1 mark" + (i+1) +" :");
student1_marks[i]=Integer.parseInt(in.next());
if(student1_marks[i]<0 || student1_marks[i]>100)
throw new MarksValidationException("Marks entered are negative or outside the range (0-
100)");
sum1=sum1 + student1_marks[i];
}
System.out.println("Enter Student2 Name: ");
student2_name = in.next();
for(int i=0;i<3;i++)
{
System.out.println("Enter Student1 mark" + (i+1) +" :");
student2_marks[i]=Integer.parseInt(in.next());
if(student2_marks[i]<0 || student2_marks[i]>100)
msg
throw new MarksValidationException("Marks entered are negative or outside the range (0-
100)");
sum2=sum2 + student2_marks[i];
}

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;

public class MarksValidationException extends Exception {


String msg;

MarksValidationException(String msg){
this.msg=msg;
}

public String toString()


{
return(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;

public class Days implements Runnable {

static String[] weekDays = new String[]


{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
public void run() {
SalesPersons sales = new SalesPersons();

sales.start();

for(int i=0;i<7;i++)
{
if(weekDays[i].equals("Sunday"))

{ System.out.println("suspended sales thread");


sales.suspend();

if(weekDays[i].equals("Wednesday"))
{
sales.resume();
System.out.println("resuming sales thread");
}
}

public static void main(String args[])


{

Days days= new Days();


Thread DaysThread = new Thread(days);
System.out.println("Starting Days Thread");
DaysThread.start();
}
}

SalesPersons.java
package com.nhce.program3a;
public class SalesPersons extends Thread {

String salesperson[] = new String[] { "Hari","Ram","John","Tom","Joseph"};

public void run()


{
System.out.println("Staring SalesPerson Thread");
for(int i=0;i<5;i++)
System.out.println("SalesPerson" + (i+1) +" : " + salesperson[i]);
try {
sleep(1000);
} catch (InterruptedException e) {
//e.printStackTrace();
}
}
}

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;

Xyz(String s1,String s2,String s3,double p)


{
name=s1;
clg=s2;
branch=s3;
per=p;
}

}
class Lab11
{
public static void main(String arg[])
{
HashMap<Integer,Xyz> hm=new HashMap<Integer, Xyz>();

Xyz x1=new Xyz("goutham","SNIST","cse",68.5);


Xyz x2=new Xyz("kanakesh","SNIST","ecm",70);
Xyz x3=new Xyz("rahul","SNIST","ecm",75);
Xyz x4=new Xyz("vasista","SNIST","ECM",80);
Xyz x5=new Xyz("narayana","SNIST","EEE",85);
hm.put(1,x1);
hm.put(2,x2);
hm.put(3,x3);
hm.put(4,x4);
hm.put(5,x5);
Iterator trav=hm.entrySet().iterator();
while(trav.hasNext())
{
Map.Entry record=(Map.Entry)trav.next(); //will give next (Key, Value) pair
Xyz j=(Xyz)record.getValue();
System.out.println(record.getKey()+" "+j.name+" "+j.clg+" "+j.branch+" "+j.per);
}
}
}

Output:

1 goutham SNIST cse 68.5


2 kanakesh SNIST ecm 70.0
3 rahul SNIST ecm 75.0
4 vasista SNIST ECM 80.0
5 narayana SNIST EEE 85.0
Program 12

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:

You might also like