0% found this document useful (0 votes)
0 views38 pages

JAVA LAB

The document contains a series of Java programming exercises from Shree Medha Degree College, Ballari, covering various concepts such as conditional statements, loops, function overloading, inheritance, and class design. Each exercise includes code snippets that demonstrate specific functionalities, such as calculating factorials, performing mathematical operations, and managing student records. The document serves as a practical guide for students to understand and implement basic programming principles in Java.

Uploaded by

rohith11290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views38 pages

JAVA LAB

The document contains a series of Java programming exercises from Shree Medha Degree College, Ballari, covering various concepts such as conditional statements, loops, function overloading, inheritance, and class design. Each exercise includes code snippets that demonstrate specific functionalities, such as calculating factorials, performing mathematical operations, and managing student records. The document serves as a practical guide for students to understand and implement basic programming principles in Java.

Uploaded by

rohith11290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

SHREE MEDHA DEGREE COLLEGE, BALLARI

PART A

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/*1. Program to assign two integer values to X and Y. Using the ‘if’ statement the
output of the program should display a message whether X is greater than Y.*/

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********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/*2. Program to list the factorial of the numbers 1 to 10. To calculate the factorial
value, use while loop.*/

public class Factorial


{
public static void main(String[] args)
{
// declare variables
int count=1;
long fact = 1;

// Find factorial from 1 to 10


System.out.println("Number \t\t\t Factorial");
while(count <=10)
{
fact *= count;
System.out.println(count+”\t\t\t\t”+ fact);
count++;
}
}
}

********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/*3. 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.*/

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********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 4. Program to perform mathematical operations. Create a class called AddSub
with methods to add and subtract. Create another class called MulDiv that extends
from Add, Sub 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.*/

//Base or Parent class


class AddSub
{
int x=20;
int y=15;
public void add()
{
int sum;
sum=x+y;
System.out.println(sum);
}
public void sub()
{
int Minus;
Minus=x-y;
System.out.println(Minus);
}
}

//Single inheritance & derived class or child or sub class


class MulDiv extends AddSub
{
public void mul()
{
int product;
product=x*y;
System.out.println(product);
}

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
public void div()
{
int Rem;
Rem=x/y;
System.out.println(Rem);
}
}

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********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 5. 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.*/

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********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 6a. Program to find the area and circumference of the circle by accepting the
radius from the user.*/

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********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 6b. Program to accept a number and find whether the number is Prime or not.*/

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********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/*7. Program to create a student class with following attributes. Enrollment No:
Name, Mark of sub1, Mark of sub2, mark ofsub3, 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 declared as 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.*/

import java.util.Scanner;
class Student
{
int rollno;
String sname;
int s1,s2,s3;
int total;

Student() //No parameter constructor


{ total = 0; }

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);
}

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
public static void main(String[] args)
{
Student s[ ] = new Student[3];
for (int i=0;i<3;i++)
{
s[i] = new Student();
s[i].set();
}
System.out.println(“RollNo \t Name \t M1 \t M2 \t M3 \t Total \n”);
for (int i=0;i<3;i++)
s[i].disp();
}
}

********************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

RollNo Name M1 M2 M3 Total


1 Ram 56 78 89 223
2 Raja 34 56 45 0
3 Raashi 67 78 89 234

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 8. In a college first year class are having the following attributes Name of the class
(BCA, BCom, BSc), Name of the staff No of the students in the class, Array of
students in the class. */

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********************

Course Staff Number of Students


BCA MANJUNATH BALLULI 139
BBA PRASHANTH BABU 110
BCom GURU PRASAD BABU 100

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 9. Define a class called first year with above attributes and define a suitable
constructor. Also write a method called best Student () which process 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. */

import java.util.Scanner;
public class Firstyear
{
public String cname;
public String sname;
public int rollno;
public int m1, m2, m3, total;

public Firstyear() // Constructor without any parameters..


{
cname = "BCA"; m1 = m2 = m3 = total = 0;
}

public Firstyear(String cname) // Constructor overloading..


{
this.cname = cname;
m1 = m2 = m3 = total = 0;
}

public void setStudent()


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter student details: name, roll no, m1, m2, m3:");
sname = sc.next();
rollno = sc.nextInt();
m1 = sc.nextInt();
m2 = sc.nextInt();
m3 = sc.nextInt();
total = m1 + m2 + m3;
}

public void display()


{
System.out.println(cname + "\t" + rollno + "\t" + sname + "\t" + m1 + "\t" + m2 +
"\t" + m3 + "\t" + total);
}

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
public static void main(String[] args)
{
Firstyear s[] = new Firstyear[60];
int max = 0;
String beststudent = "";
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no of students:");
n = sc.nextInt();
for (int i = 0; i < n; i++)
{
s[i] = new Firstyear();
s[i].setStudent();
if (s[i].total > max)
{
max = s[i].total;
beststudent = s[i].sname;
}
}
System.out.println("Course\tRollNo\tName\tM1\tM2\tM3\tTotal");
for (int i = 0; i < n; i++)
{
s[i].display();
}
System.out.println("\nBest Student:" + beststudent);
System.out.println("Marks:" + max);
}
}

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI

********************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

Course RollNo Name M1 M2 M3 Total


BCA 1 Ram 35 45 60 140
BCA 2 Ranjini 67 35 60 162
BCA 3 Raashi 45 56 45 146

Best Student: Rajini


Marks:162

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 10. 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. i.e., print them as per their seniority.*/

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();

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
//sorting data
for(int i=0;i<emp.length;i++)
{
for(int j=0;j<emp.length;j++)
{
if(emp[i].appdate.before(emp[j].appdate))
{
Employee t=emp[i];
emp[i]=emp[j];
emp[j]=t;
}
}
}

System.out.println("\nList of employees seniority wise\n");


for(int i=0;i<emp.length;i++)
emp[i].display();
}
}

********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 11. Create a package ‘student. Full time BCA‘ in your current working directory
a. Create a default class student in the above package with the following
attributes: Name, age, sex.
b. Have methods for storing as well as displaying*/

package student.fulltime.BCA;//creating new package


import java.util.Scanner;

public class Studentpkg


{
public String name,gender;
public int age;
public void accept()
{
System.out.println("Enter the name ");
Scanner scan=new Scanner(System.in);
name=scan.nextLine();
System.out.println("Enter the gender ");
gender=scan.nextLine();
System.out.println("Enter the age ");
Scanner scan1=new Scanner(System.in);
age=scan1.nextInt();
}
public void display()
{
System.out.println("\nStudent Information\n") ;
System.out.println("Name:"+name+"\n"+"Gender"+gender+"\n"+"A
ge"+age+"\n");
}
}

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
//b. Have methods for storing as well as displaying

package student.fulltime.BCA;
import student.fulltime.BCA.Studentpkg;

public class Studentp


{
public static void main(String[] args)
{
Studentpkg s1=new Studentpkg();
s1.accept();
s1.display();
}
}

********************OUTPUT********************
Enter the name: Ram
Enter the Gender : Male
Enter the Age : 19

Student Information

Name : Ram
Gender : Male
Age : 19

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI

PART B

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 1. Program to catch NegativeArraySizeException. This exception is caused when
the array is initialized to negative values.*/

public class NegativeArraySizeExceptionExample


{
public static void main(String[] args)
{
try
{
int[] array = new int[-5];
}
catch (NegativeArraySizeException nase)
{
nase.printStackTrace();
//handle the exception
}
System.out.println("Continuing execution...");
}
}

********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 2. Program to handle NullPointerException and use the “finally” method to
display a message to the user.*/

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********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
// 3. Program which create and displays a message on the window

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();

//Create a button to view message


JButton b1 = new JButton("View Message");
frame.add(b1);
b1.addActionListener(obj);

//View the frame


frame.setVisible(true);
}
//Function to create a new frame with message
public void actionPerformed(ActionEvent e)
{
//Create a new frame
JFrame sub_frame = new JFrame("Sub Frame");
sub_frame.setSize(200,200);
//Display the message
JLabel lbl = new JLabel("!!! Hello !!!");
sub_frame.add(lbl);
//View the new frame
sub_frame.setVisible(true);
}
}

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI

********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
// 4. Program to draw several shapes in the created window.

import java.awt.*; // Importing awt package


import java.applet.*; // Importing applet package

public class Shapes extends Applet


{
public void paint(Graphics g)
{
g.setFont(new Font("Cambria", Font.BOLD,15));
g.drawString("Different Shapes", 15, 15);
g.drawLine(10,20,50,60);
g.drawRect(10,70,40,40);
g.setColor(Color.RED);
g.fillOval(60,20,30,90);
g.fillArc(60,135,80,40,180,180);
g.fillRoundRect(20,120,60,30,5,5);
}
}

/* <applet code="Shapes" width=200 height=200>


</applet>
*/
********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
// 5. Program to create an applet and draw gridlines.

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********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 6. 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.*/
import java.awt.*;
import java.awt.event.*;
public class Fathermother extends Frame implements ActionListener
{
Button b1, b2; Label lbl;
Fathermother()
{
b1 = new Button("Father");
b1.setBounds(50,40,170,40);
add(b1);
b1.addActionListener(this);
b2 = new Button("Mother");
b2.setBounds(200,40,170,40);
add(b2);
lbl = new Label();
lbl.setBounds(50,80,400,200);
add(lbl);
b2.addActionListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1)
lbl.setText("Name : Chiranjeevi Age : 66 Designation : Manager");
else
lbl.setText("Name : Kousalya Age : 56 Designation : Asst. Manager");
}
public static void main(String args[])
{
Fathermother a=new Fathermother();
}
}

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI

********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 7. Create a frame which displays your personal details with respect to a button
click. */
import java.awt.*;
import java.awt.event.*;
class Personal extends Frame implements ActionListener
{
Button b1;
Label lbl;
Personal()
{
b1 = new Button("Display");
lbl= new Label();
b1.setBounds(30,90,90,30);
lbl.setBounds(250,250,250,120);
lbl.setText("");
setSize(400,350);
setVisible(true);
add(b1);
b1.addActionListener(this);
add(lbl);
}

public void actionPerformed(ActionEvent e)


{
lbl.setText("Name : Raja, Age : 20, Course : BCA II sem");
}

public static void main(String args[])


{
Personal p=new Personal();
}
}
********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 8. Create a simple applet which reveals the personal information of yours.*/

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********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 9. Program to move different shapes according to the arrow key pressed. */

import java.applet.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class MoveShapes extends Applet implements KeyListener


{
private int x = 20, y = 20;
private int width = 100, height = 100;
private int inc = 5;
private Color myColor = Color.red;

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

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
repaint();
}
else if(code==KeyEvent.VK_UP)
{
y -= inc;
if(y<getHeight())
y=20;
repaint();
}
}

public void paint(Graphics g)


{
g.drawString("Click to activate",7,20);
g.setColor(Color.RED);
g.fillRect(x,y,width,height);
g.setColor(Color.yellow);
g.fillOval(x+100, y+100, width, height);
}
// @Override
public void keyTyped(KeyEvent e)
{
}
// @Override
public void keyReleased(KeyEvent e)
{
}
}
********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 10. Program to create a window when we press M or m the window displays Good
Morning, A or the window displays Good Afternoon, 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 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);
}

public void paint(Graphics g)


{
g.drawString("Click here to activate", 100, 100);
g.drawString("Type M/m---> To Good Morning", 100, 200);
g.drawString("Type A/a---> To Good Afternoon", 100, 300);
g.drawString("Type E/e---> To Good Evening", 100, 400);
g.drawString("Type N/n---> To Good Night", 100, 500);
g.setColor(Color.blue);
g.drawString(str, 600, 200);
}

//@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;

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
case 'N' :
case 'n' : str="GOOD NIGHT";repaint(); break;
default: str="Invalid input";
}
}
//@Override
public void keyPressed(KeyEvent e)
{
}
//@Override
public void keyReleased(KeyEvent e)
{
}
}

********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
/* 11. Demonstrate the various mouse handling events using suitable example.*/

import java.awt.*;
import java.awt.event.*;

public class MainM extends Frame implements MouseListener


{
Label lbl;
MainM()
{
super("AWT Frame");
lbl = new Label();
lbl.setBounds(25, 60, 250, 30);
lbl.setAlignment(Label.CENTER);
add(lbl);
setSize(300, 300);
setLayout(null);
setVisible(true);
this.addMouseListener(this);
}

public void windowClosing(WindowEvent e)


{
dispose();
}

public static void main(String[] args)


{
new MainM();
}

public void mouseClicked(MouseEvent e)


{
lbl.setText("Mouse Clicked");
}

public void mousePressed(MouseEvent e)


{
}

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
public void mouseReleased(MouseEvent e)
{
}

public void mouseEntered(MouseEvent e)


{
lbl.setText("Mouse Entered");
}

public void mouseExited(MouseEvent e)


{
lbl.setText("Mouse Exited");
}
}

********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI
// 12. Program to create menu bar and pull-down menus.

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);
}

public static void main(String args[])


{
new MenuExample();
}
}

DEPT. OF COMPUTER SCIENCE Page No. _____


SHREE MEDHA DEGREE COLLEGE, BALLARI

********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No. _____

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy