Java Lab programs
Laboratory Manual
JAVA PROGRAMMING LABORATORY
(II B. Tech- II SEMESTER)
(VCE-R22)
Course Code-A8609
COURSE OVERVIEW:
This course provides Object Oriented Programming concepts using Java. The course focuseson different aspect of core Java Environment suitable to write efficient, maintainable, andportable code. It also ignites Object Oriented thinking and explores with the evolution ofJava and its basics.It provides strong foundation on Inheritance, Packages and Interfacesand also illustrates Exception Handling and Multithreaded mechanisms.It also providesCollection framework for manipulating data. This course also focuses on file handling usingJavaAPI.
CoursePre/co-requisites
A8505-DataStructures
A8508-PythonProgrammingLaboratory
COURSE OBJECTIVE
The course enables the learner to apply Object-oriented Programming concepts to develop Console components as per needs and specifications.
COURSE OUTCOMES (COs)
After the completion of the course, the student will be able to:
CO# | Course Outcomes |
A8609.1 | Make use of various constructs to write a console application. |
A8609.2 | Use principles of OOP to develop real time applications. |
A8609.3 | Identifytheneedofexceptionhandlingtodealwithruntimeerrors. |
A8609.4 | BuildapplicationsforparallelprocessingusingMultithreading. |
A8609.5 | ChooseCollectionframeworkandI/Otomanipulateandstoredata. |
Bloom’s Level of the Course Outcomes:
CO# | Bloom’s Level | |||||
Remember (L1) | Understand (L2) | Apply (L3) | Analyze (L4) | Evaluate (L5) | Create (L6) | |
A8609.1 |
| ✔ |
|
|
|
|
A8609.2 |
|
| ✔ |
|
|
|
A8609.3 |
|
|
| ✔ |
|
|
A8609.4 |
|
|
| ✔ |
|
|
A8609.5 |
|
| ✔ |
|
| |
COURSE CONTENT
THEORY
Contents |
Introduction to OOP: OOP principles, Java Buzzwords, Implementing Java program, JVM, Data Types, Variables, Type conversions and Casting, Operators, Control statements, Arrays. Class, Objects, Methods and Constructors- Classes, Objects, Methods, Constructors, this keyword, static keyword, Overloading Methods and Constructors, Argument passing, Exploring String class.
Inheritance, Packages and Interfaces: Inheritance Basics, using super, Inheritance types and its program, Method Overriding, Dynamic Method Dispatch, Abstract classes, final keyword. Packages and Interfaces-Defining a Package, Finding Packages and Classpath, Access Protection, Importing Packages, Defining and Implementing interfaces, Extending interfaces. Exception Handling : Exception Handling Fundamentals, Exception Types, using try catch, throw throws and finally keywords, Built-in Exceptions, Creating own exception sub- classes. Multithreading: Multithreading-Lifecycleofathread,Threadclassmethods, creating threads, thread priorities, Synchronizing threads, Interthread Communi-cation. Collections and I/O: Collections and I/O :Collections - Introduction to Collection Framework, CollectionsHierarchy, ArrayList, LinkedList, HashSet, TreeSet. The Date and StringTokenizer. I/O – Basics,readingandwritingconsoleinputandoutput,PrintWriterclass,operationsoffiles –reading,writingandcopyingfiles. |
PRACTICE:
SNo | Title of the Experiment | Tools and Techniques | Expected Skills/Ability |
1 | Implement Control statements and Arrays Write a Java Program to read three integers and find the greatest among them. | Open source Java Tool kit: JDK 8 and above versions,
Open Source IDE: Eclipse / NetBeans
OS: Windows / Ubuntu
| Apply OOP principles to solve problems |
2 | Program to check a number is Amstrong or not. | ||
3 | Write a Java Program to check a number is prime or not. | ||
4 | Implement OOP First principle - Encapsulation a. Define a class Rectangle with data member’s length and width. Write methods to find perimeter and area of a rectangle. (class and object) | ||
5 | Create a class Account with data members name, acno and balance. Use appropriate methods to perform Various operations like deposit, withdraw, balnceCheck. | ||
6 | Implement Constructor overloading Create a class Student with appropriate data and methods using constructor. | ||
7 | Implement OOP Second principle – Polymorphism and Arrays a. Create overloaded methods to find area of rectangle, triangle and square. | ||
8 | Implement Java String Class. (a)To sort given list of elements in ascending order. (b)Check a string is palindrome or not. | ||
9 | Implement OOP Third principle – Inheritance. a. Declare a class called Employee having employee _id and employee_name as members. Extend class Employee to have a subclass called Salary having designation and monthly_salary as members. Define following: • Required constructor • A method to find and display all details of employees drawing salary more than Rs.20000/- • main () method to create an array. | ||
10 | a) Write a Java program that create an abstract base class Shape with two members base and height, a member function for initialization and a function to compute shapeArea(). b) Derive two specific classes Triangle and Rectangle which override the function shapeArea(). Write a driver classes (main) to display the area of the triangle and the rectangle.(Use super keyword). | ||
11 | Implement Packages. a. Create a Package Measure, in which store a class named Convertor that contains methods to convert mm to cm, cm to m and m to km. b. Define a class Need_Convertor that imports the Convertor class, now store Need_Convertor outside the package Measure. Perform path settings accordingly. | Develop user defined packages | |
12 | Implement Interfaces. Write a Java program that implements an interface Student which has two methods displayGrade() and attendance(). Implement two classes PG_Student and UG_Student with necessary inputs of data. | Develop Interfaces | |
13 | Implement Exception Handling . (a) Write a Java Program to handle Arithmetic Exception and Number Format Exception (b) Write a Java Program to handle custom exceptions.
| Analyze exceptions and multithreading and handle them | |
14 | Develop applications on Multithreaded Programming and thread synchronization. a. Create a multithreaded java program by creating a subclass of Thread and then creating, initializing, and staring two Thread objects from your class. The threads will execute concurrently and display “Java is object oriented” in console window. | ||
15 | Implement Collection Frameworks to retrieve data. a. Use an ArrayList to manage Employee objects for insertion, display and remove.
b. Use HashSet methods to perform operations on collection of data. | Develop Collection frameworks |
Text Book:
1. Herbert Schildt, Java: The Complete Reference, 11th Edition, Tata McGraw-Hill Education, 2019.
Reference Books:
1. Y. Daniel Liang, Introduction to Java Programming-Comprehensive Version, 10th Edition, Pearson Education, 2018.
2. Kathy Sierra, Bert Bates , OCA Java SE 8 Programmer, 1st Edition, McGraw-Hill Education, 2017.
Journals/Magazines
1. https://www.oracle.com/technetwork/java/javamagazine/index.html
2. https://www.javaworld.com/
Using Java Scanner Class to read input
Scanner is a class in “java.util” package used for obtaining the input of the primitive types like int, double, and strings etc. It is the easiest way to read input in a Java program
The Java Scanner class breaks the input into tokens using a delimiter that is whitespace by default. It provides many methods to read and parse various primitive values.
Commonly used methods of Scanner class
Method | Description |
public String next() | It returns the next token (string) from the scanner. |
public String nextLine() | It moves the scanner position to the next line and returns the value as a string. |
public byte nextByte() | It scans the next token as a byte. |
public short nextShort() | It scans the next token as a short value. |
public int nextInt() | It scans the next token as an int value. |
public long nextLong() | It scans the next token as a long value. |
public float nextFloat() | It scans the next token as a float value. |
public double nextDouble() | It scans the next token as a double value. |
Create your Folder : Week1
Goto Week1--> Right Click--> Open Terminal--> Give below Command and Type the Java Code
Week1> gedit DataRead.java
// Example to read and display data
import java.util.*;
class DataRead{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the roll number");
int roll= sc.nextInt();
System.out.println("Enter the name");
String name= sc.next();
System.out.println("Enter the fee");
double fee= sc.nextDouble();
System.out.println("Roll no:"+roll);
System.out.println("NAme is:"+name);
System.out.println("Fee:"+fee);
//System.out.println("Rollno:"+rollno +" name:"+name + " //fee:"+fee);
}
}
To save the file
1) Press Ctrl S
2) Close the Editor (You will be redirected to Terminal)
3) Week1>javac DataRead.java //For Compilation
4) Week1>java DataRead //For Execution
WEEK-1
Objective: After the completion of the practice session, the student will be able to implement Control statements
1. Write a java program to read three integers and find the greatest among them.
Create your Folder : java eee
Goto java eee --> Right Click--> Open Terminal--> Give below Command and Type the Java Code
java eee> gedit Greatest.java
import java.util.*;
class Greatest{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the 3 numbers");
int n1= sc.nextInt();
int n2= sc.nextInt();
int n3= sc.nextInt();
if(n1>=n2 && n1>=n3)
System.out.println(n1+" is the greatest number");
else if(n2>=n1 && n2>=n3)
System.out.println(n2+" is the greatest number");
else
System.out.println(n3+" is the greatest number");
}
}
Output:
To save the file
1) Press Ctrl S
2) Close the Editor (You will be redirected to Terminal)
3) java eee>javac Greatest.java //For Compilation
4) java eee>java Greatest //For Execution
WEEK-2
2. Program to check a Number is Amstrong or Not
import java.util.*;
class AmstrongDemo{
public static void main(String[] args) {
int n, temp, digits=0, last=0, sum=0;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number: ");
n=sc.nextInt();
temp=n;
while(temp>0) {
temp = temp/10;
digits++;
}
temp = n;
while(temp>0) {
last = temp % 10;
sum += (Math.pow(last, digits));
temp = temp/10;
}
if(n==sum)
System.out.println("The number "+n+" -> is a Armstrong ");
else
System.out.println("The number "+n+" -> is Not Armstrong ");
}
}
Output:
WEEK-3
3. WriteaJavaProgramtocheckanumberisprimeornot.
import java.util.*;
class PrimeNum{
public static void main(String args[]){
int i,m=0,flag=0;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
m=n/2;
if(n==0||n==1){
System.out.println(n+" is not prime number");
}
else{
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println(n+" is not prime number");
flag=1;
break;
}
}
if(flag==0){
System.out.println(n+" is prime number");
}
}//end of else
}
}
Output:
WEEK-4
Objective: After the completion of the practice session, the student will be able toimplement OOP First principle – Encapsulation.
4. Define a class Rectangle with data member’s length and width. Write methods to find perimeter and area of a rectangle. (class and object)
import java.util.*;
class Rectangle
{
double len,wid;
public void setData(double d1 , double d2)
{
len=d1; wid=d2;
}
public double perimeter( )
{
double res= 2*(len+wid);
return res;
}
public double area( )
{
double res=(len*wid);
return res;
}
public void display( )
{
System.out.println("The Rectangle dimensions are " +len +", " + wid);
}
} //close Rectangle
class RectangleDemo
{
public static void main(String args[])
{
Rectangle r = new Rectangle( );
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length & width");
r.length= sc.nextDouble( );
r.width = sc.nextDouble( );
r.display();
System.out.println("The perimeter of the Rectangle is "
+r.perimeter());
System.out.println("The area of the Rectangle is " +r.area());
sc.close();
}
}
Output:
WEEK-5
5. Create a class Account with data members name, acno and balance. Use appropriate methods to perform various operations like deposit,withdraw, balnceCheck.
class Account
{
int acno;
String name;
double balance;
public void setData(int x , String y , double z){
acno=x;
name=y;
balance=z;
}
public double getBal( ) {
return (balance);
}
public void deposit( double amt) {
balance = balance + amt;
}
public void withdraw( double amt) {
balance = balance -amt;
}
public void display() {
System.out.println("The account details are");
System.out.println("Account No ->" +acno);
System.out.println("Account Name ->" +name);
System.out.println("Account Balance ->" + balance);
}
}
public class AccDemo
{
public static void main(String[] args)
{
Account ac1 = new Account();
ac1.setData(1102 , "Ramesh A" , 345678.75);
ac1.display();
System.out.println("The balance is " + ac1.getBal());
ac1.deposit(10000);
System.out.println("The balance after deposit " + ac1.getBal());
ac1.withdraw(10000);
System.out.println("The balance after withdraw " + ac1.getBal());
}
}
Output:
WEEK-6
6. Create a class Student with appropriate data and methods using constructor.
class Student
{
int rollno;
String name, dept;
double avg;
Student() { }
Student(int x) {
rollno = x;
}
Student(int x , String y) {
this(x);
name=y;
}
Student(int x , String y , double z ) {
this(x,y);
avg=z;
}
Student(int x , String y , double z , String p ) {
this(x,y,z);
dept=p;
}
public void display() {
System.out.println("The Student Data is");
System.out.println(rollno + " , " +name +"," +avg + " ," +dept );
}
}
public class StudentDemo
{
public static void main(String args[])
{
Student s1 = new Student(501, "Ramesh" );
s1.avg = 87.5;
s1.dept = "CSE";
s1.display();
Student s2 = new Student(502, "Suresh" , 78.5 , "ECE");
s2.display();
}
}
Output:
WEEK-7
7. Create overloaded methods to find area of rectangle, triangle and square.
class Shape
{
public int area(int a)
{
System.out.print("Square Area is ");
return(a*a);
}
public void area(double r)
{
System.out.println("Area of Circle is "+(3.142 *r*r));
}
public void area(int a , int b) {
System.out.println("The area of Rectangle is " + (a*b));
}
public double area(double a , double b) {
System.out.print("Area of Triangle is ");
return(0.5*a*b);
}
}
class OverloadingArea {
public static void main(String args[]) {
Shape s = new Shape();
s.area(3,4); //Rectangle
System.out.println(s.area(5)); //Square
s.area(3.5); //Circle
System.out.println(s.area(4.5,8.5)); //Triangle
}
}
WEEK-8
8. (a)To sort given list of elements in ascending order.
import java.util.*;
public class SortDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of Elements");
int n = sc.nextInt();
int s[] = new int[n];
System.out.println(" Enter the Elements");
for(int i=0; i< n;i++)
s[i] = sc.nextInt();
System.out.println(" The Elements before sorting are");
for(int i=0; i< s.length;i++)
System.out.print(s[i] + "");
for(int i= 0; i < s.length; i++)
{
for(int j = i + 1; j < s.length; j++)
{
if( s[i] >s[j])
{
int tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
}
System.out.println(" \n The Elements After Sorting are");
for(int i=0; i< s.length;i++)
System.out.print(s[i] + " “);
} }
Output:

(b) Check a string is palindrome or not.
import java.util.*;
public class Palindrome
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String");
String s = sc.next();
int len = s.length();
int i , j=len-1;
boolean flag=true;
for(i=0 ; i < len/2 ; i++)
{
if(s.charAt(i) != s.charAt(j))
{
flag=false;
}
j--;
}
if(flag==true)
System.out.println("The string " +s + " is Palindrome");
else
System.out.println("The string " +s + " is Not a Palindrome");
}
}
Output:
WEEK-9
Objective: After the completion of the practice session, the student will be able to implement OOP Third principle – Inheritance.
a) Declare a class called Employee having employee _id and employee_name as members. Extend class Employee to have a subclass called Salary having designation and monthly_salary as members. Define following:
• Required constructor
• A method to find and display all details of employees drawing salary more than Rs.20000/-
import java.util.*;
class Employee
{
int empid;
String empname;
Employee(int no)
{
empid = no;
}
Employee(int no , String name)
{
this(no);
empname = name;
}
}
class Salary extends Employee
{
String designation;
double sal;
Salary(int no , String name , String des , double salary)
{
super(no,name);
designation = des;
sal = salary;
}
void display()
{
System.out.println(empid + " , " + empname + "," +designation + " , " +sal);
}
}
public class EmpInheritDemo
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of employees");
int n = sc.nextInt();
Salary s[] = new Salary[n];
for(int i =0 ; i <n ; i++)
{
System.out.println("Enter the Employee details of " +(i+1));
int id = sc.nextInt();
String name = sc.next();
String desg = sc.next();
double sal = sc.nextDouble();
s[i] = new Salary(id,name,desg,sal);
}
System.out.println("The Employee details having > 20000 are");
for(int i=0;i<n ; i++)
{
if(s[i].sal >20000)
s[i].display();
}
}
}
Output:
WEEK-10
10. (a) Write a Java program that create an abstract base class Shape with two members base and height, a member function for initialization and a function to compute shapeArea().
(b) Derive two specific classes Triangle and Rectangle which override the function shapeArea(). Write a driver classes (main) to display the area of the triangle and the rectangle.(Use super keyword).
abstract class Shape
{
double base;
double hgt;
Shape(double b , double h)
{
base = b;
hgt = h;
}
abstract double shapeArea();
public void display()
{
System.out.println("dimenstions are " + base + " ," +hgt);
}
}
//10.b.
class Triangle extends Shape
{
Triangle(double b , double h)
{
super(b,h);
}
double shapeArea()
{
return(0.5*base*hgt);
}
}
class Rectangl extends Shape
{
Rectangl (double b , double h)
{
super(b,h);
}
double shapeArea()
{
return(base*hgt);
}
}
class AreaDemo
{
public static void main(String args[])
{
Shape t = new Triangle(34.5,89.75); //upcasting
t.display();
System.out.println("The area of triangle is "+t.shapeArea());
Shape r = new Rectangl(34.5 , 75); //upcasting
r.display();
System.out.println("The area of Rectangle is "+r.shapeArea());
}
}
Output:
WEEK-11
11(a) Create a Package Measure; in which store a class named Convertor that contains methods to convert mm to cm, cm to m and m to km.
(b) Define a class Need_Convertor that imports the Convertor class, now store Need_Convertor outside the package Measure. Perform path settings accordingly.
package Measure;
public class Converter
{
public float mmtocm(float mm)
{
float cm=(mm/10);
return cm;
}
public float cmtom(float cm)
{
float m=(cm/100);
return m;
}
public float mtokm(float m)
{
float km=(m/1000);
return km;
}
}
import Measure.Converter;
public class NeedConverter
{
public static void main(String args[])
{
Converter c=new Converter();
System.out.println(" mm to cm is "+c.mmtocm(100));
System.out.println(" cm to m is "+c.cmtom(1000));
System.out.println(" m to km is "+c.mtokm(3000));
}
}
Output:
WEEK-12
Write a Java program that implements an interface Student which has two methods displayGrade() and attendance(). Implement two classes PG_Student and UG_Student with necessary inputs of data.
interface Student
{
void displayGrade();
void attendence();
}
class PGStudent implements Student
{
String name;
int rollno;
String grade;
double att;
PGStudent(String n , int r , String g , double a)
{
name = n;
rollno=r;
grade=g;
att = a;
}
public void display()
{
System.out.println("The PG Student data is ");
System.out.println(" Name : " +name +" : "+ "Rollno " + rollno);
}
public void displayGrade()
{
System.out.println("The Grade of PG Student is " +grade);
}
public void attendence()
{
System.out.println("The attendence of PG Student is " +att);
}
}
class UGStudent implements Student
{
String name;
int rollno;
String grade;
double att;
UGStudent(String n , int r , String g , double a)
{
name = n;
rollno=r;
grade=g;
att = a;
}
public void display()
{
System.out.println("The UG Student data is ");
System.out.println(" Name : " +name +" : "+ "Rollno " + rollno);
}
public void displayGrade()
{
System.out.println("The Grade of UG Student is :" +grade);
}
public void attendence()
{
System.out.println("The attendence of UG Student is : " +att);
}
}
public class InterfaceDemo
{
public static void main(String args[])
{
PGStudent s = new PGStudent("Harsha", 1101, "A",78.5);
s.display();
s.attendence();
s.displayGrade();
UGStudent u = new UGStudent("Varsha",5101,"B",68.5);
u.display();
u.attendence();
u.displayGrade();
}
}
Output:
WEEK-13
a) Write a java program to handle ArithmeticException and NumberFormatException.
import java.util.*;
public class ExcepDemo
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number num1");
String s1 = sc.next(); //”20” or aaa
System.out.println("Enter the number2");
String s2=sc.next(); //”2” or 0
try
{
int num1 = Integer.parseInt(s1);
int num2 = Integer.parseInt(s2);
System.out.println("num1 is " +num1);
System.out.println("num2 is " +num2);
if(num2 ==0)
throw new ArithmeticException ("Division Error");
int res = num1/num2;
System.out.println("The result is " +res);
}
catch(NumberFormatException e)
{
System.out.println("The numbers must be numeric data");
System.out.println("Exception " +e);
}
catch(ArithmeticException e)
{
System.out.println("num2 must not be zero");
System.out.println("Exception" +e);
}
finally
{
System.out.println("Finally block is executed");
}
System.out.println("Remaining statements");
}
}
Output:
b) Write a java program to handle custom exceptions. (or)
In the CustomExceptionTest class, the age is expected to be a positive number. It would throw the user defined exception NegativeAgeException if the age is assigned a negative number.
class NegativeAgeException extends Exception
{
NegativeAgeException(String s)
{
super(s);
}
public String toString()
{
return "Age Exception";
}
}
class ExcepDemo2
{
static void validate(int age) throws NegativeAgeException
{
if(age<0)
throw new NegativeAgeException(" not valid "+age);
else
System.out.println(" welcome to the world " +age);
}
public static void main(String args[])
{
try{
validate(23);
validate(17)
validate(-12);
validate(25); // Not Executed
}
catch(Exception m)
{
System.out.println("Exception occured: No Negative Age");
System.out.println("--- " + m + "---");
}
finally {
System.out.println("Finally block executed");
}
System.out.println("rest of the code...");
}
}
Output:
WEEK-14
Objective: After the completion of the practice session, the student will be able to develop applications on Multithreaded Programming and thread synchronization.
Create a multithreaded java program by creating a subclass of Thread and then creating, initializing, and staring two Thread objects from your class. The threads will execute concurrently and display “Java is object oriented” in console window.
class NewThread extends Thread
{
NewThread(String name)
{
super(name);
//start();
}
public void run()
{
try
{
for(int i=1;i<=4;i++ )
{
System.out.println("Java is object oriented" + getName());
sleep(1000);
}
}
catch (InterruptedException ie)
{
System.out.println("Child Thread - Exception caught");
}
}
}
public class ThreadDemo
{
public static void main(String args[])
{
NewThread t1 = new NewThread("First");
NewThread t2 = new NewThread("Second");
t1.start();
t2.start();
System.out.println("Main Program");
}
}
Output:
WEEK-15
Objective: After the completion of the practice session, the student will be able toimplement Collection Frameworks to retrieve data.
a) Use an ArrayList class to perform operations on collection of data. (or)
Use an ArrayList to manage Employee objects for insertion, display and remove.
import java.util.*;
class Employee
{
int eid;
String ename;
double sal;
public Employee(int x, String y, double z)
{
eid=x;
ename=y;
sal = z;
}
}
public class EmpArrayList{
public static void main(String[] args){
ArrayList<Employee> list =new ArrayList<Employee>();
//Creating Employees
Employ e1=new Employee(101,"A.Harsha",75000.50);
Employee e2=new Employee(102,"B.Varsha",85000.50);
Employee e3=new Employee(103,"C.Sirisha",95000.50); Employee e4=new Employee(104,"D.Sandeep",195000.50);
//Adding Employees to list
list.add(e1);
list.add(e2);
list.add(e3);
list.add(e4);
//Displaying Number of Employees
System.out.println("\n The number of employees is ->" +list.size());
//Displaying Details of Employees
System.out.println("\n The employess data is \n");
for(Employee e:list)
{
System.out.println(e.eid+":"+e.ename+":"+e.sal);
System.out.println();
}
//Deleting an Employee
list.remove(2);
System.out.println("\n After removing number of employees are ->" + list.size());
//Displaying Details of Employees
System.out.println("\n The employess data after removing is \n");
for(Employee e:list)
{
System.out.println(e.eid+":"+e.ename+":"+e.sal);
System.out.println();
}
}
}
Output:
b) Use HashSet methods to perform operations on collection of data.
import java.util.*;
class HashDemo
{
public static void main(String args[])
{
//Creating HashSet
HashSet<String> set=new HashSet<String>();
//Adding Elements to HashSet - ignores duplicates
set.add("hyderabad");
set.add("hyderabad");
set.add("bangalore");
set.add("chennai");
set.add("kolkata");
set.add("kolkata");
set.add("pune");
Iterator<String> i=set.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
//Removing specific element from HashSet
set.remove("hyderabad");
//Displaying set
System.out.println("\n The list after remove ->" +set);
//adding another set
HashSet<String> set1=new HashSet<String>();
set1.add("Panjab");
set1.add("Delhi");
set.addAll(set1);
System.out.println("\n Updated List is ->"+set);
//removing new set from list
set.removeAll(set1);
System.out.println("\n Updated List is ->"+set);
//Removing all the elements available in the set
set.clear();
System.out.println("\n After clear the set is ->"+set);
}
}
Output:
ONLINE RESOURCES
OBJECTIVE
To help students on acquiring more practice on the course using various online resources
DESCRIPTION
These open tools form an excellent practice platform for the students, which he can explore anytime from anywhere. The links of the websites providing OOP tutorials are given. Students are needed to explore the websites.
LINKS
1. https://www.geeksforgeeks.org/java/
2. https://www.tutorialspoint.com/java/java_tutorial.pdf
3. https://www.pdfdrive.com/java-tutorial-e19074086.html
4. https://www.tutorialspoint.com/java/index.htm
5. https://www.tutorialspoint.com/java/index.htm
Comments
Post a Comment