1
1
2. Write a program to display the month of a year. Month of the year should be held in an
array.
import java.util.Calendar;
public class dateDemo {
public static void main(String[] args) {
Calendar calendar=Calendar.getInstance();
String[] month={"January","February","March","April","May",
"June","July","August","September","October","November","December"};
System.out.println("Current Month = "+month[calendar.get(Calendar.MONTH)]);
}
}
Output:
5. Write a java program to add two integer and two float numbers. When no arguments are
supplied, give a default value to calculate the sum. Use function overloading.
public class MethodOverloadingDemo {
int addition() {
return(10 + 10);
}
int addition(int x, int y) {
return(x + y);
}
float addition(float a, float b) {
return(a + b);
}
public static void main(String args[]) {
MethodOverloadingDemo a = new MethodOverloadingDemo();
System.out.println("By using Default Values, Sum is : " + a.addition());
System.out.println("Sum of Two Integer Values(10 and 20) is : " + a.addition(10,20));
System.out.println("Sum of Two Float Values(10.5 and 20.5) is : "+a.addition(10.2f,20.3f));
}
}
Output:
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
operation.
class AddSub {
int n1,n2;
public AddSub(int x, int y) {
n1=x;
n2=y;
}
public int add() {
return(n1+n2);
}
public int sub() {
return(n1-n2);
}
}
class MulDiv extends AddSub {
public MulDiv(int x, int y) {
super(x, y);
}
public int mul() {
return(n1*n2);
}
public int div() {
return(n1/n2);
}
}
public class ArithmeticOperations{
public static void main(String args[]) {
MulDiv obj = new MulDiv(20,10);
System.out.println("Sum of 20 and 10 = "+obj.add());
System.out.println("Difference of 20 and 10 = "+obj.sub());
System.out.println("Product of 20 and 10 = "+obj.mul());
System.out.println("Result of division 20 / 10 = "+obj.div());
}
}
Output:
7. Write a program with class variable for all instances of a class. Use static variables
declaration. Observe the changes that occur in the object’s member variable values.
class Student {
static String collegeName = "PES College";
int rollNo;
String name;
Student(int rollno, String name) {
this.rollNo = rollno;
this.name = name;
}
void display() {
System.out.println(collegeName+" "+rollNo+" "+name);
}
}
public class StaticDemo {
public static void main(String args[]) {
System.out.println("Objects Sharing the Static Variable – College Name \n");
Student s1 = new Student(1001,"Srikanth");
Student s2 = new Student(1002,"Indumathi");
s1.display();
s2.display();
System.out.println("\n Static Value Changed by One of the Object \n");
s1.collegeName = "Jain College";
s1.display();
s2.display();
}
}
Output:
8. Write a program to create a student class with following attributes: Enrollment_id, Name,
Marks of 3 subjects and total marks obtained by the student. If the students gets less than
50, than their total marks must be declared as zero.
import java.util.*;
class Student {
Scanner sc = new Scanner(System.in);
String Enrollment_id;
String Name;
int sub1,sub2,sub3,total;
Student(){
readStudentInfo();
}
public void readStudentInfo() {
System.out.println("Enter Student Details");
System.out.println("Enrollment No: ");
Enrollment_id = sc.next();
System.out.println("Name: ");
Name = sc.next();
System.out.println("Enter marks of 3 subjects: ");
sub1 = sc.nextInt();
sub2 = sc.nextInt();
sub3 = sc.nextInt();
if(sub1 >= 50 && sub2 >= 50 && sub3 >= 50)
total = sub1+sub2+sub3;
else
total = 0;
}
public void displayInfo() {
System.out.println(Enrollment_id+"\t\t"+Name+"\t"+total);
}
}
public class StudentInfo {
public static void main(String[] args) {
Student s[] = new Student[3];
for(int i=0; i<3; i++) {
s[i]=new Student();
}
System.out.println("\t\tStudent Details");
System.out.println("EnrollmentNO\tName\tTotal");
for(int i=0; i<3; i++) {
s[i].displayInfo();
}
}
}
Output:
9. Define a class first year with class_name, class_teacher, number of students in the class
and student’s names should be in array. Also write a method called beststudent() which
process a first year object and return the student with highest total mark.
import java.util.*;
class FirstYear {
String Classname;
String classteacher;
int stdcount;
int stdmarks[] = new int[50];
String stdnames[] = new String[50];
Scanner sc = new Scanner(System.in);
public FirstYear() {
getinfo();
}
public void getinfo() {
System.out.println("Please Enter the Class Name:");
Classname = sc.nextLine();
System.out.println("Please Enter the Class Teacher Name:");
classteacher = sc.nextLine();
System.out.println("Please Enter the Total Number of Students of the Class:");
stdcount = Integer.parseInt(sc.nextLine());
System.out.println("Please Enter the Name of all the Students of the Class:");
for(int i=0; i<stdcount; i++)
stdnames[i] = sc.nextLine();
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();
}
public void beststudent() {
int best = 0,k=-1;
for(int i=0; i<stdcount; i++) {
if(stdmarks[i]>best) {
best = stdmarks[i];
k=i;
}
}
System.out.println("The best student is "+stdnames[k]);
}
}
public class Student {
public static void main(String args[]) {
FirstYear fy = new FirstYear();
fy.beststudent();
}
}
Output:
10. Write a program to define a class employee with the name and date of appointment.
Create ten employee object as an array and sort them as per their date of appointment i.e,
print them as per their seniority.
import java.util.Date;
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+"\t Appointment date: \t" +
appdate.getDate()+"/"+appdate.getMonth()+"/"+appdate.getYear());
}
}
public class Empdate{
public static void main(String as[]){
employee emp[] = new employee[10];
emp[0] = new employee("Neeraja K",new Date(1999,05,22));
emp[1] = new employee("Kuldeep M",new Date(2000,01,12));
emp[2] = new employee("Roja D",new Date(2009,04,12));
emp[3] = new employee("Rana K",new Date(2005,02,19));
emp[4] = new employee("Jyothi",new Date(2010,01,01));
emp[5] = new employee("Srikanth",new Date(1999,01,01));
emp[6] = new employee("Rajesh",new Date(2020,05,19));
emp[7] = new employee("Asha",new Date(2022,04,22));
emp[8] = new employee("Ammu",new Date(2000,01,25));
emp[9] = new employee("Gourav",new Date(2002,9,9));
System.out.println("List of Employees");
for(int i=0;i<emp.length;i++)
emp[i].display();
for(int i=0;i<emp.length;i++){
for(int j=i;j<emp.length;j++){
if(emp[i].appdate.after(emp[j].appdate)){
employee t = emp[i];
emp[i] = emp[j];
emp[j] = t;
}
}
}
System.out.println("\nList of Employees Seniority wise");
for(int i=0;i<emp.length;i++)
emp[i].display();
}
}
Output:
11. Create a folder named student, inside student create another folder called fulltime, again
inside fulltime folder create another folder named bca.
NOTE->Create a folder named student, inside student create another folder called fulltime,
again inside fulltime folder create another folder named bca
package student.fulltime.bca;
import java.util.Scanner;
public class BCAStudent {
String name, sex;
int age;
Scanner sc = new Scanner(System.in);
public void getdata() {
System.out.println("Student Name: ");
name = sc.nextLine();
System.out.println("Student Sex: ");
sex = sc.nextLine();
System.out.println("Student Age: ");
age = sc.nextInt();
}
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);
}
}
NOTE->Now save the following codes as PackageDemo.java outside of the student folder
and compile it. Now execute the PackageDemo file to get output.
import student.fulltime.bca.BCAStudent;
public class PackageDemo {
public static void main(String args[]) {
BCAStudent std = new BCAStudent();
std.getdata();
std.display();
}
}
Output:
12. Write a small program to catch Negative Array Size Exception. This exception is caused
when the array is initialized to a negative value.
public class NegativeArraySizeExceptionDemo {
public static void main(String[] args) {
try {
int[] array=new int[-10];
} catch (NegativeArraySizeException obj) {
obj.printStackTrace();
}
System.out.println("Exception Catch and Continuing Execution...");
}
}
Output:
13. Write a program to handle Null Pointer Exception and use the “finally” method to display
a message to the user
public class NullPointerExceptionDemo {
public static void main(String[] args) {
String city = null;
try{
if(city.equals("Banglore"))
System.out.print("Equal");
else
System.out.print("Not Equal");
}catch (NullPointerException e) {
System.out.println("Null Pointer Exception Caught");
} finally {
System.out.println("This is Finally Block after Catching NullPointerException");
}
}
}
Output:
14. Write a program which create and displays a message on the window
import java.awt.*;
public class FrameDemo {
FrameDemo() {
Frame fm = new Frame();
fm.setTitle("My first Frame");
Label lb = new Label("Welcome to GUI Programming");
fm.add(lb);
fm.setSize(300,300);
fm.setVisible(true);
}
public static void main(String args[]) {
FrameDemo ta = new FrameDemo();
}
}
Output:
4. Write a program to draw several shapes in the created window
import java.awt.*;
class Drawings extends Canvas{
public void paint(Graphics g){
g.drawRect(50,75,100,50);
g.fillRect(200,75,50,50);
g.drawRoundRect(50,150,100,50,15,15);
g.fillRoundRect(175,150,100,50,15,15);
g.drawOval(50,275,100,50);
g.fillOval(175,275,100,50);
g.drawArc(20,350,100,50,25,75);
g.drawArc(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(300,450);
f.setVisible(true);
}
}
Output:
17. Write a program to create frame with two buttons father and mother. When we click
each button their name, age and their designation must appear
import java.awt.*;
import java.awt.event.*;
public class ButtonClickActionEvents {
public static void main(String[] args) {
Frame f = new Frame("Button Event");
Label l = new Label("DETAILS OF THE PARENTS");
l.setFont(new Font("Calibri",Font.BOLD,16));
Label nl = new Label();
Label dl = new Label();
Label al = new Label();
l.setBounds(20,20,500,50);
nl.setBounds(20,110,500,30);
dl.setBounds(20,150,500,30);
al.setBounds(20,190,500,30);
Button mb = new Button("Mother");
mb.setBounds(20,70,50,30);
mb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nl.setText("Name:"+" "+ "Aishwarya");
dl.setText("Designation:"+" "+ "Professor");
al.setText("Age:"+" "+ "42");
}
});
Button fb = new Button("Father");
fb.setBounds(80,70,50,30);
fb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nl.setText("Name:"+" "+ "Ram");
dl.setText("Designation:"+" "+ "Manager");
al.setText("Age:"+" "+ "44");
}
});
f.add(mb);
f.add(fb);
f.add(l);
f.add(nl);
f.add(dl);
f.add(al);
f.setSize(250,250);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
18. Create a frame which displays your personal details with respective button click.
import java.awt.*;
import java.awt.event.*;
public class PersonalDetails {
public static void main(String[] args) {
Frame f = new Frame("Button Example");
Label l = new Label("WELCOME TO MY PAGE");
l.setFont(new Font("Calibri",Font.BOLD,16));
Label fnl = new Label();
Label mnl = new Label();
Label lnl = new Label();
Label rl = new Label();
Label al = new Label();
l.setBounds(250,30,600,50);
fnl.setBounds(20,120,600,30);
mnl.setBounds(20,160,600,30);
lnl.setBounds(20,200,600,30);
rl.setBounds(20,240,600,30);
al.setBounds(20,280,600,30);
Button mb = new Button("CLICK HERE FOR MY PERSONAL DETAILS");
mb.setFont(new Font("Calibri",Font.BOLD,14));
mb.setBounds(210,70,320,30);
mb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fnl.setText("Full Name: Aishwarya Rao");
mnl.setText("Father Name: Ranjit Mother Name: Vijetha Age: 19");
lnl.setText("Roll No: BNU35628 College Name: Jain Degree College");
rl.setText("Nationality: Indian Contact No: 9876543210");
al.setText("Address: 7th Cross, Indira Nagar, Bangalore");
}
});
f.add(mb);
f.add(l);
f.add(fnl);
f.add(mnl);
f.add(lnl);
f.add(rl);
f.add(al);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
20.Write a program to move different shapes according to the arrow key pressed.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="Arrowkeys" width = 400 height = 400>
</applet>
*/
public class Arrowkeys extends Applet implements KeyListener{
int x1 = 100,y1 = 50,x2 = 250,y2 = 200;
public void init(){
addKeyListener(this);
}
public void keyPressed(KeyEvent ke){
showStatus("KeyDown");
int key = ke.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT : x1 = x1 - 10; x2 = x2 - 10;
break;
case KeyEvent.VK_RIGHT : x1 = x1 + 10; x2 = x2 + 10;
break;
case KeyEvent.VK_UP : y1 = y1 - 10; y2 = y2 - 10;
break;
case KeyEvent.VK_DOWN : y1 = y1 + 10; y2 = y2 + 10;
break;
}
repaint();
}
public void keyReleased(KeyEvent ke){
}
public void keyTyped(KeyEvent ke){
repaint();
}
public void paint(Graphics g){
g.drawLine(x1,y1,x2,y2);
g.drawRect(x1,y1+160,100,50);
g.drawOval(x1,y1+235,100,50);
}
}
Output:
21. Write a program to create a window when we pressed ‘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.awt.*;
import java.awt.event.*;
public class Keysdemo extends Frame implements KeyListener{
Label lbl;
Keysdemo(){
addKeyListener(this);
requestFocus();
lbl = new Label();
lbl.setBounds(100,100,200,40);
lbl.setFont(new Font("Calibri",Font.BOLD,16));
add(lbl);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e){
if(e.getKeyChar()=='M' || e.getKeyChar()=='m')
lbl.setText("Good Morning");
if(e.getKeyChar()=='A' || e.getKeyChar()=='a')
lbl.setText("Good Afternoon");
if(e.getKeyChar()=='E' || e.getKeyChar()=='e')
lbl.setText("Good Evening");
if(e.getKeyChar()=='N' || e.getKeyChar()=='n')
lbl.setText("Good Night");
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public static void main(String[] args){
new Keysdemo();
}
}
Output:
s
22.Demonstrate the various mouse handling events using suitable example
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MouseListenerExample implements MouseListener {
Label lbl1,lbl2;
Frame fr;
String s;
MouseListenerExample() {
fr = new Frame("Java Mouse Listener Example");
lbl1 = new Label("Demo for the Mouse Event",Label.CENTER);
lbl2 = new Label();
fr.setLayout(new FlowLayout());
fr.add(lbl1);
fr.add(lbl2);
fr.addMouseListener(this);
fr.setSize(250,250);
fr.setVisible(true);
}
public void mouseClicked(MouseEvent ev) {
lbl2.setText("Mouse Button Clicked");
fr.setVisible(true);
}
public void mouseEntered(MouseEvent ev) {
lbl2.setText("Mouse has entered the area of window");
fr.setVisible(true);
}
public void mouseExited(MouseEvent ev) {
lbl2.setText("Mouse has left the area of window");
fr.setVisible(true);
}
public void mousePressed(MouseEvent ev ) {
lbl2.setText("Mouse button is being pressed");
fr.setVisible(true);
}
public void mouseReleased(MouseEvent ev) {
lbl2.setText("Mouse Released");
fr.setVisible(true);
}
public static void main(String[] args) {
new MouseListenerExample();
}
}
Output: