We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 13
Program 1. Write a simple java application, to print the message, “Welcome to java”
Class first
{Public static void main(String args[])
{System.out.printin(“Welcome to Java”);
x
Program 2. Write a program to display the month of a year. Months of the year should
be held in an array.
Import java.util.Calendar;
Class demo
{Public static void main(String[] args)
{ Calendar cal = Calendar.getinstance();
String[] month = new String[] {“January”, “February”, “March”, “April”, “May”,
“June”,“July”, “August”,”September”, “October”, “November”, “December” };
‘System.out.printin(“Current Month =“ + month[cal.get(Calendar.MONTH)]);
n
Program 3. Write a program to demonstrate a division by zero exception
Class zeroexe
{Public static void main (String args{])
{Int a=15,b=0, result =0;
Try { Result = a/b;
System.out.printin(“The result is” +result);
+
Catch (ArithmeticException e)
{ System.out.printin (“Can't be divided by Zero“ +e);
HhProgram 4. Write a program to create a user defined exception say Pay Out of Bounds.
import java.util;
class OutOfBoundException extends Exception
{ OutOfBoundException(String msg)
{ System.out.printin("Out of Bounds Except
yn
class userdefinedex
{public static void main(String args[]) throws OutOfBoundException
{System.out.printin("enter age of the person");
‘Scanner sc = new Scanner(System.in);
int age = sc.nextint();
if(age<18 || age>100)
{ throw new OutOfBoundException("Person is not eligible for voting");
+
else
{System.out.printin("Person is eli
nt
ible for voting");Program 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.
class sum
fint a,b;
sum()
=10; b=5;
}
void add()
{int o=a+b;
System.out.printin("Sum is "+c);
+
void add(int a,int b)
{int c=a+b;
System.out.printin("sum is
+
void add(double a, double b)
{double c=a+b;
‘System.out.printin("sum is "+c);
Hn
class calculate
{public static void main(String args[])
{sum S1 = new sum();
St.add();
S1.add(20,40);
S1.add(6.3,7.8);
nProgram 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.
Class addsub
{Int a,b;
Addsub()
{ A=50; b=10;
+
Void add()
{Int c=atb; »
‘System.out.printin(“addition of ”+a+ “and” +b+ “is” +c); Class arithmetic
} {Public static void main(String args 1)
{Mul
md=new muldiv(;
Void sub()
‘System.out.printin( “ARITHMETIC OPERATIONS”);
md.
1d();
System.out.printin(“subtract of” +a* “and” +b* “is” +c); ma.sub();
y md.muttiQ;
Class muldiv extends addsub ma.dtls
»
{Void multi()
{Int c=a*b;
System.out.printin("“muttiplication of “ +a+ “and” +b+ “is” +c);
+
System.out.printin(“‘division of” +a+"and “+b:
c);Program 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.
Class student
{Static String college=” beu College”;
Int rno;
String name;
Student(int mno,String name)
{ This.rno=rno;
This.name=name;
+
Void display()
{System.out.printin(rnot”“+name+” “+college);
a
Public class StaticDemo
{Public static void main(String args[])
{System.out.printin(“objects sharing the static varible-College name\n”’
‘Student s1= new student(111,”beu”);
Student s2= new student(112,”college”);
S1.display();
S2.display();
‘System.out.printin(“\nStatoc value changed by one of the object \n”);
S1.college=” bu College (Autonomous)”;
S1.display();
S2.display();
yProgram 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 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’;
Class student
{Scanner sc= new Scanner(System.in);
String rolino;
Totat=mt+m2tm3;
String name;
Int m1,m2,m3,total;
Student() }
{Readdata(); Public void display()
{System.out.printin(rolinot\t\t”+namet"\t\t” total);
»
Public class Studentinfo
}
Public void readdata()
{System.out.printin(“enter student details’
{Public static void main(S!
{
Rollno=se.next(); Student s{J= new student{3];
‘System.out.printin(“enter rolinumber”
For(int
System.out.printin(“enter Name”); iit)
{Slilznew student);
}
System.out.printin("\t Student Details\n");
System.out.printin(“enter marks in 3 subjects’
Mi=sc.nextint(); System.out.printin( “Roll NO \tName\t Tota
M2=sc.nextint();
M3=sc.nextint();
If(m1>=50 && m2>= 50 && m3>= 50)Program 10. Define a class called first year with above attri
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 det
first-year object and find the best student of this class
Import java.util;
Class Firstyear
{String classname;
String Classteacher;
Int studentcount;
Int marks[]=new int[50};
String stdnames[]=new String[50];
‘Scanner sc = new Scanner(System.in);
Public Firstyear()
{Getdata();
y
Public void getdata()
{System.out.printin(“enter class name’
Classname = sc.nextLine();
‘System.out.printin(“enter the class teacher name”)
Classname = sc.nextLine();
‘System.out.printin(“Please enter the total number of students in the class’
‘Studentcount = Integer.parseint(sc.nextLine());
‘System.out.printin(“Please enter the names of all students of the class:”
For(int i=0;ibest)
{Best =marksti;
ket;
»
‘System.out.printin(“the best countis.."+stdnamesfk]};
»
Public class Student
{Public static vold main(String arge{))
{Firstyear fy=new Firstyear();
Fy.beststudent();
»Program 12. Write a small program to catch Negative Array Size Exception. This,
exception is caused when the array is initialized to negative value
Class negativearray
{Pul
static void main(String[] args)
{ Int{] array = new int[-5];
‘System.out.printin(“Array length: “ + array.length);
Ht
Program 13. Write a program to handle Null Pointer Exception and use the “finally”
method to display a message to the user.
Class nullpointer
{ Public static void main(String args[])
{ String name = null;
Try
{If(name.equals(“Sree”))
System.out-println(“Same”);
Else
‘System.out.printin(“not same”);
}
Catch (NullPointerException e)
{ System.out. printin(“Null Pointer Exception caught”);
}
Finally
{System.out.printin(“‘thi
yy
inally block after catching NullPointerException”);Program 14, Wri
Import java.awt.
Public class FrameDemo
{FrameDemo()
{ Frame fm = new Frame();
Fm.setTitle(“My First Frame’
@ program which create and displays a message on the window.
Label tb= new Label(“Welcome to GUI Programming”);
Fm.add(lb);
Fm.setSize(300,300);
Fm.setVisible(true);
+
Public static void main(String args{])
{ FrameDemo f1 = new FrameDemoj();
Program 15. Write a program to draw several shapes in the created window
Import java.awt-;
Public class Drawings extends Canvas
{ Public void paint(Graphics g)
{g.drawRect(50,75,100,50);
&4fillRect(200,75,100,50);
g.drawRoundRect(50,150,100,500,15, 15);
g.drawOval(175,275, 100,50);
g.drawAro(20,350,100,50,25,75);
g,fillArc(175,350,100,50,25,75);
+
Public static void main(String args[])
{Drawings m = new Drawings();
Frame f = new Frame("Shapes”);
f.add(m);
f.setSize(500,501
f.setVisible(true);
ttProgram 17. Wri
a program to create menu bar and pull-down menu.
Import java.awt.
Public class MenuDemo
{MenuDemo()
{Frame fr = new Frame(“Menu Demo”);
MenuBar mb = new MenuBar();
Menu fileMenu = new Menu(“File”);
Menu editMenu = new Menu(“Edit”);
Menu viewMenu = new Menu(“View”);
Mb.add{(fileMenu);
Mb.add(editMenu);
Mb.add(viewMenu);
Menultem a1 = new Menultem("New”)
Menultem a2 = new Menultem("Open”);
Menultem a3 = new Menultem("Save”);
Menultem b1 = new Menultem(“Copy”);
Menultem b2 = new Menultem(“Find”);
Menultem c1 = new Menultem(‘‘Show”);
fileMenu.add(a1);
fileMenu.add(a2); fr.setLayout{null};
fileMenu.add(a3); frsetVisible(true);
editMenu.add(b1); }
editMenu.add(b2); Public static void main(String args{])
viewMenu.add(c1); { new MenuDemo();
fr.setMenuBar (mb); }
frsetSize(300, 300);Prpgram 18. Wri
a program using mous
Import java.awt.
Import java.awt.event;
fener event for mouse operatic
Public class MouseListenerExample extends Frame implements MouseListener
{ Label label;
Public MouseListenerExample() {
addMouseListener(this);
label = new Label();
label.setBounds(20, 50, 100, 20);
add(labet);
setSize(300, 300);
setLayout(null);
setVisible(true);
+
Public void mouseClicked(MouseEvent e)
{ Label.setText(“Mouse Clicked”);
+
Public void mouseEntered(MouseEvent e)
{ Label.setText(“Mouse Entered”)
}
Public void mouseExited(MouseEvent e)
{ Label.setText(“Mouse Exited”);
+
Public void mousePressed(MouseEvent e)
{ Label.setText(“Mouse Pressed”)
+
Public void mouseReleased(MouseEvent e)
{ Label.setText(“Mouse Released”);
}
Public static void main(String[] args)
{New MouseListenerExample();
aProgram 19. Wri
a program using keylistener event to perform keyboard actions.
Import java.awt.
Import java.awt.event.*;
Public class KeyListenerExample extends Frame implements KeyListener
{Label |;
TextArea area;
KeyListenerExample()
{ L=newLabel();
LsetBounds (20, 50, 100, 20);
area = new TextArea();
area.setBounds (20, 80, 300, 300);
area.addKeyListener(this);
add(l);
add(area);
setSize (400, 400);
setLayout (null);
setVisible (true); }
Public void keyPressed (KeyEvent e)
{ LsetText (“Key Pressed”); }
Public void keyReleased (KeyEvent e)
{ LsetText (“Key Released”); }
Public void keyTyped (KeyEvent e)
{ LsetText (“Key Typed”); }
Public static void main(Stringf] args)
{ New Keyl
ye
jenerExample();Program 20. Write a program using packages and to implement the sum of 2 numbers
by creating a new packages.
Package mypack;
Public class Add
{ Public void addition()
{Int a=100;
Int b = 200;
System.out.printin( “The sum is :” +(a+b));
2a
Import mypack.Add;
Public class Myclass
{ Public static void main(String args[])
{Add a= new Add();
a.addition();
+
}// save it as Myclass.java