Java Lab Manual
Java Lab Manual
Java Lab Manual
www.brindavancollege.com
II Semester
CA-C9P
ACADEMIC
YEAR 2023 –
2024
LABORATORY MANUAL
PREPARED BY
Department of BCA
II Semester
CA-C9P
ACADEMIC
YEAR 2023 –
2024
BATCH :
PREPARED BY
LABORATORY CERTIFICATE
This is to certify that Mr. /Ms.
satisfactorily completed the course of experiments in Machine Learning Laboratory lab code CA-
C29L prescribed by the Bengaluru City University of this Institute for the academic year 2023–
2024.
MARKS
Date:
MACHINE LEARNING LAB CA-C29L
PREFACE
Welcome to the fascinating world of machine learning! In this ever-evolving digital era, the
importance of machine learning cannot be overstated. Machine learning is a subset of artificial
intelligence in the field of computer science that often uses statistical techniques to give
computers the ability to "learn" (i.e., progressively improve performance on a specific task) with
data, without being explicitly programmed.
Course Outcome
1. Learn the basics of machine learning, understanding its uses, challenges, and various
applications.
2. Build practical data skills, covering data collection, analysis, visualization, and preparation.
3. Become skilled in using classification and regression algorithms, including selecting, training,
and evaluating models.
4. Dive into advanced clustering and specialized applications, using methods like K- Means,
DBSCAN, and others.
I acknowledge Asst. Prof. Nikita Singh for his valuable guidance and suggestions as per Revised
Blooms Taxonomy in preparing the lab manual.
2. Write a program to display the month of a year. Months of the year should be
held in an array.
4. Write a program to create a user defined exception say Pay Out of Bounds.
5. Write a java program to add two integers and two float numbers. When no
arguments are supplied, give a default value to calculate the sum. Use function
overloading.
7. Write a program with class variable that is available for all instances of a class.
Use static variable declaration. Observe the changes that occur in the object’s
member variable values.
10. Define a class called first year with above attributes and define a suitable
constructor. Also write a method called best Student () which process a first-
year object and return the student with the highest total mark. In the main
method define a first-year object and find the best student of this class
11. Write a Java program to define a class called employee with the name and date
of appointment. Create ten employee objects as an array and sort them as per
their date of appointment. ie, print them as per their seniority.
13. Write a small program to catch Negative Array Size Exception. This exception
is caused when the array is initialized to negative values.
14. Write a program to handle Null Pointer Exception and use the “finally” method
to display a message to the user.
15. Write a program which create and displays a message on the window
18. Write a program which creates a frame with two buttons father and mother.
When we click the father button the name of the father, his age and designation
must appear. When we click mother similar details of mother also appear.
19. Create a frame which displays your personal details with respect to a button
click
20. Create a simple applet which reveals the personal information of yours.
21. Write a program to move different shapes according to the arrow key pressed.
22. Write a java Program to create a window when we press M or m the window
displays Good Morning, A or a the window displays Good After Noon E or e
the window displays Good Evening, N or n the window displays Good Night
23. Demonstrate the various mouse handling events using suitable example.
package p1;
Output:
welcome to java
2.Write a program to display the month of a year. Months of the year should be held in an
array.
package p1;
class mnth
{
public static void main(String [] args)
{
int n=11 ;
int index;
int days[]= {31,28,31,30,31,30,31,31,30,31,30,31};
String [] month
={"Jan","Feb","March","April","May","June","July","August","Sep","Oc
t","Nov","Dec "};
for( index=0;index<n;index++)
{
System.out.println(month[index]+"has"+days[index]+"days");
}
System.out.println(month[index]);
System.out.println("Thank you");
}
}
Output:
Janhas31days
Febhas28days
Marchhas31days
Aprilhas30days
Mayhas31days
Junehas30days
Julyhas31days
Augusthas31days
Sephas30days
Octhas31days
Novhas30days
Dec
Thank you
package p1;
}
catch(ArithmeticException e)
{
System.out.println("division by zero is Arithmetic
exception");
}
int y=a/2;
System.out.println("the value of y if a is not divisible
by zero is"+y);
Output:
division by zero is Arithmetic exception
the value of y if a is not divisible by zero is5
4.Write a program to create a user defined exception say Pay Out of Bounds.
package p9;
import java.util.Scanner;
Output:
enter the employee salary
55000
Pay out of bounds exceptionsalary not in the valid range
5.Write a java program to add two integers and two float numbers. When no arguments are
supplied, give a default value to calculate the sum. Use function overloading.
package p9;
}
}
Output:
by using default values sum is20
sum of two integer values (10 and 20) is30
sum of two float values (10.2 and 20.5 and 40.8) is71.6
6.Write a program to perform mathematical operations. Create a class called AddSub with
methods to add and subtract. Create another class called MulDiv that extends from AddSub
class to use the member data of the super class. MulDiv should have methods to multiply and
divide A main function should access the methods and perform the mathematical operations.
package p9;
class addsub
{
int num1;
int num2;
int add()
{
return num1+num2;
}
int sub()
{
return num1-num2;
}
}
Output:
Addition =70
Subtraction =30
Multiply =80
Division =5.0
7.Write a program with class variable that is available for all instances of a class. Use static
variable declaration. Observe the changes that occur in the object’s member variable values.
package p1;
Output:
9
11
9
12
8.Write a java program to create a student class with following attributes: Enrollment_id:
Name, Mark of sub1, Mark of sub2, mark of sub3, Total Marks. Total of the three marks
must be calculated only when the student passes in all three subjects. The pass mark for each
subject is 50. If a candidate fails in any one of the subjects his total mark must be declaredas
zero. Using this condition write a constructor for this class. Write separate functions for
accepting and displaying student details. In the main method create an array of three student
objects and display the details.
package p1;
import java.util.*;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
String name;
int Enrollment_id;
try{
BufferedReader reader =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter student Name : ");
System.out.println("Enter enrollment no : ");
Scanner sc = new Scanner(System.in);
name=reader.readLine();
Enrollment_id=sc.nextInt();
System.out.print("Marks of 3 subject:");
marks[i] = sc.nextInt();
if(marks[i]<50)
flag=1;
}
studen1 s1=new studen1(name,marks[0],marks[1],marks[2]);
if(flag==1){
total=0;
System.out.println("student is Failed in one of subject total mark
is Reduced to Zero : "+total);
}
else
{
System.out.println("student Optained total marks : "+total);
}
}catch(Exception e){
}
}
}
class studen1{
Output:
Enter student Name :
Enter enrollment no :
priya
1001
Marks of 3 subject:Enter marks out of 100:
10
20
20
student is Failed in one of subject total mark is Reduced to Zero : 0
Enter student Name :
Enter enrollment no :
priya
1001
Marks of 3 subject:Enter marks out of 100:
78
89
90
student Optained total marks : 257
9.In a college first year class are having the following attributesName of the class (BCA,
BCom, BSc), Name of the staff No of the students in the class, Array of students in the class
package p1;
import java.io.*;
import java.lang.*;
import java.util.*;
class Student
{
int total;
String name,department;
public Student(int total,String name,String department)
{
this.total=total;
this.name=name;
this.department=department;
}
class Program5new
{
public static void main (String[]args)
{
ar.add(new Student(340,"Anil","BCA"));
ar.add(new Student(450,"Mayank","BCA"));
ar.add(new Student(360,"Anshul","BCA"));
ArrayList< Student>arbsc=new ArrayList< Student>();
arbsc.add(new Student(230,"Solanki","BSC"));
arbsc.add(new Student(411,"Aggarwal","BSC"));
arbsc.add(new Student(311,"Aggarwal","BSC"));
System.out.println("Unsorted");
for(int i=0;i< ar.size();i++)
System.out.println(ar.get(i));
Collections.sort(ar,new Beststudent());
Collections.sort(arbsc,new Beststudent());
Collections.sort(arbcom,new Beststudent());
System.out.println("Best student is:");
System.out.println("High Score:");
System.out.println(" "+ar.get(0));
System.out.println(" "+arbsc.get(0));
System.out.println(" "+arbcom.get(0));
Collections.sort(ar,new Sortbyname());
System.out.println("\nSorted by name");
for(int i=0;i< ar.size();i++)
System.out.println(ar.get(i));
}
}
Output:
Unsorted
340 Anil BCA
450 Mayank BCA
360 Anshul BCA
230 Solanki BSC
411 Aggarwal BSC
311 Aggarwal BSC
220 Solanki BCOM
431 Aggarwal BCOM
141 nav BCOM
Best student is:
High Score:
450 Mayank BCA
411 Aggarwal BSC
431 Aggarwal BCOM
Sorted by name
340 Anil BCA
360 Anshul BCA
450 Mayank BCA
10.Define a class called first year with above attributes and define a suitable constructor. Also
write a method called best Student () which process a first-year object and return the student
with the highest total mark. In the main method define a first-year object and find the best
student of this class
package p1;
import java.util.*;
import java.util.Scanner;
class FirstYear
{
String classname;
String classteacher;
int stdcount;
int stdmarks[]=new int[50];
Scanner sc=new Scanner(System.in);
String[] stdnames=new String[50];
public FirstYear()
{
classteacher=sc.next();
System.out.println("please enter the total number of students in the
class");
stdcount=sc.nextInt();
System.out.println("please enter the names of all the students of the
class");
for(int i=0;i<stdcount;i++)
stdnames[i]=sc.next();
System.out.println("please enter the marks of all the students of the
class");
for(int i=0;i<stdcount;i++)
stdmarks[i]=sc.nextInt();
}
System.out.println("The best student is " +stdnames[k]);
}
}
public class stu1
{
Output:
please enter the class name
bca
please enter the class teacher name
priya
please enter the total number of students in the class
2
please enter the names of all the students of the class
rahul
vikas
please enter the marks of all the students of the class
345
678
The best student is vikas
11.Write a Java program to define a class called employee with the name and date of
appointment. Create ten employee objects as an array and sort them as per their date of
appointment. ie, print them as per their seniority.
package p1;
import java.util.*;
class employee
String name;
Date appdate;
name=nm;
appdate=apdt;
class program6
{
public static void main(String as[])
System.out.println("List of employees");
emp[i].display();
if(emp[i].appdate.after(emp[j].appdate))
employee t=emp[i];
emp[i]=emp[j];
emp[j]=t;
emp[i].display();
Output:
List of employees
employee name:shaha PD appoinment date:22/5/1999
employee name:Patil AS appoinment date:12/1/2000
employee name:Phadake PV appoinment date:25/4/2009
employee name:Shinde SS appoinment date:19/2/2005
employee name:Shrivastav RT appoinment date:1/1/2010
List of employees seniority wise
employee name:Shrivastav RT appoinment date:1/1/2010
employee name:Phadake PV appoinment date:25/4/2009
employee name:Shinde SS appoinment date:19/2/2005
employee name:Patil AS appoinment date:12/1/2000
employee name:shaha PD appoinment date:22/5/1999
package studentFulltimeBCA;
import java.util.Scanner;
import p1.BCAStudent;
}
public void display()
{
System.out.println("student details are");
System.out.println("student name"+name);
System.out.println("student sex"+sex);
System.out.println("student age"+age);
}
Output:
student name
yashu
student sex
female
student age
22
student details are
student nameyashu
student sexfemale
student age22
13Write a small program to catch Negative Array Size Exception. This exception is caused
when the array is initialized to negative values.
package studentFulltimeBCA;
class programb1
}
}
Output:
14.Write a program to handle Null Pointer Exception and use the “finally” method to display
a message to the user.
package p1;
String str="abc";
System.out.println(str.length());
String str1=null;
if (str1!=null)
{
System.out.println(str1.length());
}
}
catch(NullPointerException e)
{
System.out.println(e);
}
finally
{
System.out.println("Null pointer exception is caught
in catch block");
}
}
}
Output:
3
Null pointer exception is caught in catch block
package p1;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
//Create a frame
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create an object
frame.add(button);
button.addActionListener(obj);
frame.setVisible(true);
sub_frame.setSize(200,200);
c.setBackground(Color.RED);
win.add(label);
win.add(c);
win.show();
sub_frame.add(c);
sub_frame.setVisible(true);
Output:
g.fillRect(145,40,100,50);
g.drawRect(265,40,50,50);
g.drawRoundRect(25,125,100,50,15,15);
g.fillRoundRect(145,125,100,50,15,15);
g.drawOval(25,205,100,50);
g.fillOval(145,205,100,50);
g.drawOval(265,205,50,50);
g.draw3DRect(25,280,100,50,true);
g.draw3DRect(145,280,100,50,false);
g.drawArc(25,345,100,50,25,75);
g.drawArc(145,345,100,50,125,75);
}
}
Output:
import java.awt.*;
import java.applet.*;
}
}
}
Output:
18.Write a program which creates a frame with two buttons father and mother. When we
click the father button the name of the father, his age and designation must appear. When we
click mother similar details of mother also appear.
package p1;
import java.awt.event.*;
import javax.swing.*;
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText(" FATHER AGE= 40 \n designation = Manager.");
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText(" Mother AGE= 35 \n designation = teacher.");
}
});
f.add(tf);
f.add(b);
f.add(b2);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
19.Create a frame which displays your personal details with respect to a button click
21.Write a program to move different shapes according to the arrow key pressed.
22.Write a java Program to create a window when we press M or m the window displays
Good Morning, A or a the window displays Good After Noon E or e the window displays
Good Evening, N or n the window displays Good Night
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class MouseApplet extends Applet implements MouseListener
{
String msg="Initial Message";
public void init()
{
addMouseListener(this);
}
public void mouseClicked(MouseEvent me)
{
msg = "Mouse Clicked";
repaint();
}
public void mousePressed(MouseEvent me)
{
msg = "Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent me)
{
msg = "Mouse Released";
repaint();
}
public void mouseEntered(MouseEvent me)
{
msg = "Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
msg = "Mouse Exited";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,20,20);
}
}
Output:
package p1;
import javax.swing.*;
class MenuExample
{
JMenu menu, submenu;
JMenuItem i1, i2, i3, i4, i5;
MenuExample(){
JFrame f= new JFrame("Menu and MenuItem Example");
JMenuBar mb=new JMenuBar();
menu=new JMenu("Menu");
submenu=new JMenu("Sub Menu");
i1=new JMenuItem("Item 1");
i2=new JMenuItem("Item 2");
i3=new JMenuItem("Item 3");
i4=new JMenuItem("Item 4");
i5=new JMenuItem("Item 5");
menu.add(i1); menu.add(i2); menu.add(i3);
submenu.add(i4); submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setJMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample();
}}
Output: