JAVA LAB
JAVA LAB
PART A
class Largest
{
public static void main(String...args)
{
int x=51;
int y=5;
if(x>y)
{
System.out.println("Largest is x = "+x);
}
else
{
System.out.println("Largest is y = "+y);
}
}
}
********************OUTPUT********************
********************OUTPUT********************
class Functionoverloading
{
public void add(int x, int y)
{
int sum;
sum=x+y;
System.out.println(sum);
}
public void add(double a, double b)
{
double total;
total=a+b;
System.out.println(total);
}
}
class Mainfunover
{
public static void main(String args[])
{
Functionoverloading obj = new Functionoverloading();
obj.add(10,20);
obj.add(10.40,10.30);
}
}
********************OUTPUT********************
class Operations
{
public static void main(String args[])
{
MulDiv obj = new MulDiv(); //creating object for derived class
obj.add();
obj.sub();
obj.mul();
obj.div();
}
}
********************OUTPUT********************
class StaticDemo
{
static int count=0; //static variable declaration
public void increment()
{
count++;
}
public static void main(String args[])
{
StaticDemo obj1=new StaticDemo ();
StaticDemo obj2=new StaticDemo ();
obj1.increment();
obj2.increment();
System.out.println("Obj1: count is="+obj1.count);
System.out.println("Obj2: count is="+obj2.count);
}
}
********************OUTPUT********************
import java.util.Scanner;
import java.lang.Math;
public class Coc
{
public static void main(String[] args)
{
double circumference, radius, area;
Scanner sc=new Scanner (System.in);
System.out.print("Enter the radius of the circle: ");
radius=sc.nextDouble();
circumference= Math.PI*2*radius;
area=Math.PI*radius*radius;
System.out.println("Area Of a Circle is "+area);
System.out.println("The circumference of the circle is: "+circumference);
}
}
********************OUTPUT********************
import java.util.*;
class Prime
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no");
int n = sc.nextInt();
int i=1,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
{
System.out.println(n+" is a PRIME no");
}
else
{
System.out.println(n+" is a NOT a prime no");
}
}
}
********************OUTPUT********************
import java.util.Scanner;
class Student
{
int rollno;
String sname;
int s1,s2,s3;
int total;
void set()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter student details: roll no, name, s1, s2, s3");
rollno = sc.nextInt();
sname = sc.next();
s1 = sc.nextInt();
s2 = sc.nextInt();
s3 = sc.nextInt();
if (s1 >= 50 && s2 >= 50 && s3 >= 50 )
total = s1+s2+s3;
}
void disp()
{
System.out.println(rollno+ "\t" +sname+ "\t" +s1+ "\t" +s2+ "\t" +s3+ "\t" +
total);
}
********************OUTPUT********************
Enter student details: rollno, name, s1, s2, s3
1 Ram 56 78 89
Enter student details: rollno, name, s1, s2, s3
2 Raja 34 56 45
Enter student details: rollno, name, s1, s2, s3
3 Raashi 67 78 89
class Fclass
{
String nameofclass, nameofstaff;
int noofstds;
Fclass(String noc, String nos, int numstd)
{
nameofclass=noc;
nameofstaff=nos;
noofstds=numstd;
}
void display()
{
System.out.print(nameofclass+"\t"+nameofstaff+"\t"+noofstds);
}
}
class Firstyear
{
public static void main(String [] args)
{
Fclass std[]=new Fclass[3];
std[0]=new Fclass("\n BCA","MANJUNATH BALLULI\t",139);
std[1]=new Fclass("\n BBA","PRASHANTH BABU\t\t",110);
std[2]=new Fclass("\n BCom ","GURU PRASAD BABU\t",100);
System.out.print("Course \t Staff \t\t\t Number of Students ");
for(int i=0;i<3;i++)
std[i].display();
}
}
********************OUTPUT********************
import java.util.Scanner;
public class Firstyear
{
public String cname;
public String sname;
public int rollno;
public int m1, m2, m3, total;
********************OUTPUT********************
Enter no of students: 3
Enter student details: name, roll no, m1, m2, m3:
Ram 1 35 45 60
Enter student details: name, roll no, m1, m2, m3:
Ranjini 2 67 35 60
Enter student details: name, roll no, m1, m2, m3:
Raashi 3 45 56 45
import java.util.*;
class Employee
{
String name;
Date appdate;
public Employee(String nm, Date apdt)
{
name=nm;
appdate=apdt;
}
public void display()
{
System.out.println("Employee Name:"+name+" Appointment date:"+
appdate.getDate()+"/" +appdate.getMonth()+"/"+appdate.getYear());
}
}
class Empsort
{
public static void main(String args[])
{
Employee emp[]=new Employee[10]; //array of object creation
emp[0]=new Employee("Shaha PD",new Date(1999,05,22));
emp[1]=new Employee("Patil AS",new Date(2000,01,12));
emp[2]=new Employee("Phadake PV",new Date(2009,04,25));
emp[3]=new Employee("Shinde SS",new Date(2005,02,19));
emp[4]=new Employee("Shrivastav RT",new Date(2010,02,01));
emp[5]=new Employee("Ramanjineya",new Date(2013,03,01));
emp[6]=new Employee("Jennifar",new Date(2014,04,01));
emp[7]=new Employee("Anjum",new Date(2022,05,01));
emp[8]=new Employee("Samanth",new Date(2012,06,01));
emp[9]=new Employee("Channa Basava",new Date(2011,07,01));
System.out.println("\n List of employees\n");
for(int i=0;i<emp.length;i++)
emp[i].display();
********************OUTPUT********************
package student.fulltime.BCA;
import student.fulltime.BCA.Studentpkg;
********************OUTPUT********************
Enter the name: Ram
Enter the Gender : Male
Enter the Age : 19
Student Information
Name : Ram
Gender : Male
Age : 19
PART B
********************OUTPUT********************
import java.io.*;
class Exceptionfinally
{
public static void main (String args[])
{
String ptr = null;
try
{
if (ptr.equals("gfg"))
System.out.println("Same");
else
System.out.println("Not Same");
}
catch(NullPointerException e)
{
System.out.println("Null Pointer Exception Caught");
}
finally
{
System.out.println("finally block is always executed");
}
}
}
********************OUTPUT********************
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Message implements ActionListener
{
//Function to create the original frame
public static void main(String args[])
{
//Create a frame
JFrame frame = new JFrame("Original Frame");
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create an object
Message obj = new Message();
********************OUTPUT********************
import java.awt.*;
import java.applet.*;
public class chessboard extends Applet
{
public void paint(Graphics g)
{
int row, column, x, y;
//for every row on the board
for (row = 0; row < 8; row++ )
{
//for every column on the board
for (column = 0; column < 8; column++)
{
//Coordinates
x = column * 20;
y = row * 20;
//square is red if row and col are either both even or both odd.
if ( (row % 2) == (column % 2) )
g.setColor(Color.red);
else
g.setColor(Color.black);
g.fillRect(x, y, 20, 20);
}
}
}
}
********************OUTPUT********************
********************OUTPUT********************
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString("Name : Vindhya Rani",150,150);
g.drawString("Age : 20",200,200);
g.drawString("Course : BCA",250,250);
g.drawString("Percentage : 85",300,300);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
********************OUTPUT********************
import java.applet.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
// init method
public void init()
{
addKeyListener( this );
}
// @Override
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if (code == KeyEvent.VK_LEFT) {
x -= inc;
if(x<getWidth())
x=20;
repaint();
}
else if (code == KeyEvent.VK_RIGHT)
{
x += inc;
if(x>getWidth())
x=getWidth()-100;
repaint();
}
else if (code == KeyEvent.VK_DOWN)
{
y += inc;
if(y>getHeight())
y=getHeight()-100;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Key extends Applet implements KeyListener
{
static String str="Hello";
public void init()
{
setBackground(Color.pink);
Font f= new Font("arial",Font.BOLD, 20);
setFont(f);
addKeyListener(this);
}
//@Override
public void keyTyped(KeyEvent e)
{
char key = e.getKeyChar();
switch(key)
{
case 'M' :
case 'm' : str="GOOD MORNING";repaint(); break;
case 'A' :
case 'a' : str="GOOD AFTERNOON";repaint(); break;
case 'E' :
case 'e' : str="GOOD EVENING";repaint(); break;
********************OUTPUT********************
import java.awt.*;
import java.awt.event.*;
********************OUTPUT********************
import java.awt.*;
class MenuExample
{
MenuExample()
{
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
Menu submenu=new Menu("Sub Menu");
MenuItem i1=new MenuItem("Item 1");
MenuItem i2=new MenuItem("Item 2");
MenuItem i3=new MenuItem("Item 3");
MenuItem i4=new MenuItem("Item 4");
MenuItem i5=new MenuItem("Item 5");
menu.add(i1);
menu.add(i2);
menu.add(i3);
submenu.add(i4);
submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
********************OUTPUT********************