BHAGWAN MAHAVEER COLLEGE OF
ENGINEERING AND MANAGEMENT
Java Programming
File
BATCH 2018-2022
Submitted By: Submitted To:
Aman Rawat Ms. Usha Dixit
50455202718
CSE, 5th Sem
Sno Title DATE SIGN
1 WAP to Implement Method Overloading 03/09/2020
(Compile time Polymorphism)
2 WAP to Implement Constructor 10/09/2020
Overloading
3 WAP to Implement concept of 17/09/2020
Inheritance:
a) with using super keyword
b) Without using super keyword
4 WAP to implement the concept Interface 24/09/2020
5 WAP to Implement Exception Handling 01/10/2020
using try, throw, finally, catch & finalized
keyword
6 WAP to implement the concept of Multi- 08/10/2020
Threading using:
a) Runnable Interface
b) Thread Class
7 Write an applet that allow free hands 15/10/2020
drawing and WAP to implement event
handling that modifies the applet
program for diamond pattern in the
following manner. The size of diamond
given in a text field and provides 3 choice
list;
a. For selecting background color
b. For selecting foreground color
c. For selecting symbol for drawing
diamond
WAP to play an audio clip using applet all
of its functions
8 WAP to create user input using AWT 22/10/2020
tools.
9 WAP to create a file that can store 05/11/2020
information about five products
10 WAP to implement JDBC connectivity 12/11/2020
Experiment No: 01
Aim: WAP to Implement Method Overloading (Compile time
Polymorphism)
class Overload //Java program to implement method overloading in java
{
void demo (int a) // First Method
{
System.out.println ("a: " + a);
}
void demo (int a, int b) // Second Method
{
System.out.println ("a and b: " + a + "," + b);
}
double demo(double a) // Third Method
{
System.out.println("double a: " + a);
return a*a;
}
}
class MethodOverloading
{
public static void main (String args[]) // main function
{
Overload O1 = new Overload();
double result;
O1 .demo(10);
O1 .demo(10, 20);
result = O1 .demo(5.5);
System.out.println("O/P : " + result);
}
}
OUTPUT
Experiment No: 02
Aim: WAP to Implement Constructor Overloading.
class Student //Java program to overload constructors in java
{
int id;
String name;
int age;
Student(int i,String n) //creating two arg constructor
{
id = i;
name = n;
}
Student(int i,String n,int a) //creating three arg constructor
{
id = i;
name = n;
age=a;
}
void display() //display student-id,name,age
{
System.out.println(id+" "+name+" "+age);
}
public static void main(String args[]) //main function
{
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan",25);
s1.display();
s2.display();
}
}
OUTPUT
Experiment No: 03
Aim: WAP to Implement concept of Inheritance:
a) with using super keyword
class Base //Java program to implement inheritance with super in java
{
void show()
{
System.out.println("BASE");
}
}
class Child extends Base // single inheritance
{
void show()
{
System.out.println("CHILD");
super.show(); // super keyword
public static void main(String args[]) // main function
{
Child C1 = new Child();
C1.show();
}
}
OUTPUT
b) Without using super keyword
class Base1
//Java program to implement inheritance without super in java
{
int x;
int y;
void show()
{
System.out.println(x);
System.out.println(y);
}
}
class Child1 extends Base1 // single inheritance
{
void get(int x, int y)
{
this.x = x;
this.y = y;
}
public static void main(String args[]) // main function
{
Child1 c1 = new Child1();
c1.get(10,20);
c1.show();
}
}
OUTPUT
Experiment No: 04
Aim: WAP to implement the concept Interface
interface Printable //interface 1
{
void print();
}
interface Showable //interface 2
{
void show();
}
class Child implements Printable,Showable
{
public void print()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome");
}
public static void main(String args[]) //main function
{
Child C1 = new Child();
C1.print();
C1.show(); }}
OUTPUT
Experiment No: 05
Aim: WAP to Implement Exception Handling using try, throw, finally,
catch & finalized keyword
public class ExcepHandl
//Java program to implement Exception handling using try, catch, finally in java
{
public void finalize()
{
System.out.println("finalize called");
}
public static void main(String args[]) //main function
{
try
{
throw new ArithmeticException("InValid");
//ArithmeticException
//int data=25/0;
//System.out.println(data);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
finally
{
System.out.println("finally block is always executed");
}
System.out.println("rest of the code..."); //rest code of the program
ExcepHandl f1=new ExcepHandl();
f1=null;
System.gc();
}
}
OUTPUT
Experiment No: 06
Aim: WAP to implement the concept of Multi- Threading using:
c) Runnable Interface
class Multi1 implements Runnable //Java program to implement concept of multi-threading using
Runnable interface in java
{
public void run()
{
System.out.println("thread is running...through Runnable interface");
}
public static void main(String args[]) //main function
{
Multi1 m1=new Multi1();
Thread t1 =new Thread(m1);
t1.start();
}
}
OUTPUT
d) Thread Class
class Multi extends Thread //Java program to implement concept of multi-threading
using Thread class in java
{
public void run()
{
System.out.println("thread is running...through Thread class");
}
public static void main(String args[]) // main function
{
Multi t1=new Multi();
t1.start();
}
}
OUTPUT
Experiment No: 07
Aim: Write an applet that allow free hands drawing and WAP to
implement event handling that modifies the applet program for diamond
pattern in the following manner. The size of diamond given in a text field
and provides 3 choice list;
d. For selecting background color
e. For selecting foreground color
f. For selecting symbol for drawing diamond
WAP to play an audio clip using applet all of its functions.
package applet;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
/*<applet code="AppletProgram.class" height="600" width="600"></applet>*/
public class AppletProgram extends Applet {
String ch;
protected int lastX = 0, lastY = 0;
Button forG, bagG;
int f = 0, b = 0, s = 0;
AudioClip audio;
Color color1[] = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY,
Color.GRAY, Color.GREEN, Color.LIGHT_GRAY,
Color.MAGENTA,
Color.ORANGE, Color.PINK, Color.RED, Color.WHITE,
Color.YELLOW };
Color ColorB, ColorF;
Choice choice, choice1, choice2;
Label lblSelectSymbol,label,label_1;
public void init() {
setBackground(Color.CYAN);
setForeground(Color.blue);
addMouseListener(new PositionRecorder());
addMouseMotionListener(new LineDrawer());
setSize(600, 600);
setLayout(null);
audio = getAudioClip(getCodeBase(), "applet/sound/bomb.wav");
ch = "!";
lblSelectSymbol = new Label("Select Symbol");
lblSelectSymbol.setBounds(18, 29, 84, 14);
add(lblSelectSymbol);
choice = new Choice();
choice.setBounds(125, 23, 94, 20);
add(choice);
label = new Label("Background Color");
label.setBounds(18, 49, 101, 22);
add(label);
choice1 = new Choice();
choice1.setBounds(125, 49, 94, 20);
add(choice1);
label_1 = new Label("Foreground Color");
label_1.setBounds(18, 78, 101, 22);
add(label_1);
choice2 = new Choice();
choice2.setBounds(125, 80, 94, 20);
add(choice2);
Button button = new Button("ReDraw");
button.setBounds(18, 122, 101, 22);
add(button);
choice.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent event) {
// TODO Auto-generated method stub
//ch = c[Integer.parseInt(event.getItem().toString())];
try {
ch = event.getItem().toString();
}catch(NumberFormatException e)
{ System.out.println(e.getMessage());
}
}
});
choice1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent event) {
// TODO Auto-generated method stub
ColorB = color1[choice1.getSelectedIndex()];
}
});
choice2.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent event) {
// TODO Auto-generated method stub
ColorF = color1[choice2.getSelectedIndex()];
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
repaint();
lblSelectSymbol.setBackground(ColorB);
label.setBackground(ColorB);
label_1.setBackground(ColorB);
lblSelectSymbol.setForeground(ColorF);
label.setForeground(ColorF);
label_1.setForeground(ColorF);
setBackground(ColorB);
setForeground(ColorF);
}
});
Button button_1 = new Button("Play Audio Clip");
button_1.setBounds(127, 122, 94, 22);
add(button_1);
button_1.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent arg0)
{
audio.play();
}
});
choice.add("!");
choice.add("@");
choice.add("#");
choice.add("$");
choice.add("%");
choice.add("^");
choice.add("&");
choice.add("&");
choice.add("*");
choice.add("(");
choice.add(")");
choice1.add("Black");
choice1.add("Blue");
choice1.add("Cyan");
choice1.add("Dark Gray");
choice1.add("Gray");
choice1.add("Green");
choice1.add("Light Gray");
choice1.add("Magenta");
choice1.add("Orange");
choice1.add("Pink");
choice1.add("Red");
choice1.add("White");
choice1.add("Yellow");
choice2.add("Black");
choice2.add("Blue");
choice2.add("Cyan");
choice2.add("Dark Gray");
choice2.add("Gray");
choice2.add("Green");
choice2.add("Light Gray");
choice2.add("Magenta");
choice2.add("Orange");
choice2.add("Pink");
choice2.add("Red");
choice2.add("White");
choice2.add("Yellow");
}
protected void record(int x, int y)
{ lastX = x;
lastY = y;
}
public void paint(Graphics g) {
for (int i = 0; i <= 50; i = i + 10)
{ g.drawString(ch, 200 + i, 200 +
i); g.drawString(ch, 200 - i, 200 +
i);
}
for (int i = 0; i <= 50; i = i + 10)
{ g.drawString(ch, 250 - i, 250 +
i);
}
for (int i = 0; i <= 50; i = i + 10)
{ g.drawString(ch, 150 + i, 250 +
i);
}
}
// Record position that mouse entered window or
// where user pressed mouse button.
private class PositionRecorder extends MouseAdapter
{ public void mouseEntered(MouseEvent event)
{
requestFocus(); // Plan ahead for typing
record(event.getX(), event.getY());
}
public void mousePressed(MouseEvent event) {
record(event.getX(), event.getY());
}
}
// As user drags mouse, connect subsequent positions
// with short line segments.
private class LineDrawer extends MouseMotionAdapter
{ public void mouseDragged(MouseEvent event) {
int x = event.getX();
int y = event.getY();
Graphics g = getGraphics();
g.drawLine(lastX, lastY, x, y);
record(x, y);
}
}
}
OUTPUT
Experiment No: 08
Aim: WAP to create user input using AWT tools.
package userinput;
import java.awt.Button;
import java.awt.EventQueue;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class UserInput extends JFrame
{ private JPanel contentPane;
private TextArea textArea;
private TextField nameField,enoField,serialnoField,branchField,yearField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable()
{ public void run() {
try {
UserInput frame = new UserInput();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public UserInput() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 348);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
Label label = new Label("Name :");
label.setBounds(5, 5, 62, 22);
contentPane.add(label);
Label label_1 = new Label("E. No. :");
label_1.setBounds(5, 33, 62, 22);
contentPane.add(label_1);
Label label_2 = new Label("Serial No. :");
label_2.setBounds(5, 61, 62, 22);
contentPane.add(label_2);
Label label_3 = new Label("Branch :");
label_3.setBounds(5, 92, 62, 22);
contentPane.add(label_3);
Label label_4 = new Label("Year :");
label_4.setBounds(5, 130, 62, 22);
contentPane.add(label_4);
nameField = new TextField();
nameField.setBounds(73, 5, 351, 22);
contentPane.add(nameField);
enoField = new TextField();
enoField.setBounds(73, 33, 351, 22);
contentPane.add(enoField);
serialnoField = new TextField();
serialnoField.setBounds(73, 61, 351, 22);
contentPane.add(serialnoField);
branchField = new TextField();
branchField.setBounds(73, 92, 351, 22);
contentPane.add(branchField);
yearField = new TextField();
yearField.setBounds(73, 130, 351, 22);
contentPane.add(yearField);
Button submitbutton = new Button("Submit");
submitbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{ submitActionPreformed(event);
}
});
submitbutton.setBounds(5, 166, 70, 22);
contentPane.add(submitbutton);
textArea = new TextArea();
textArea.setBounds(10, 194, 414, 106);
contentPane.add(textArea);
}
private void submitActionPreformed(ActionEvent event) {
// TODO Auto-generated method stub
String name,eno,serialno,branch,year;
name = "Name :" + nameField.getText();
eno = "E.No. :" + enoField.getText();
serialno = "Serial No. :" + serialnoField.getText();
branch = "Branch :" + branchField.getText();
year = "Year :" + yearField.getText();
String finaltext = name + '\n' +
eno + '\n' +
serialno+ '\n' +
branch+ '\n' +
year+ '\n';
textArea.setText("");
textArea.setText(finaltext);
}
}
Experiment No: 09
Aim: WAP to create a file that can store information about five products.
package savetofile;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class ProductDetails extends JFrame {
private JLabel Productno;
private JPanel contentPane;
private JTextField nameField;
private JTextField idfield;
private JTextField pricefield;
private JTextField discountField;
private JTextField DatesoldField;
String FinalDetails = "";
int count = 0;
JButton btnSubmit;
public static void main(String[] args)
{ EventQueue.invokeLater(new Runnable() {
public void run()
{ try {
ProductDetails frame = new ProductDetails();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}}});}
public ProductDetails()
{ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Product Details");
lblNewLabel.setBounds(152, 11, 94, 14);
contentPane.add(lblNewLabel);
Productno = new JLabel("Product 1");
Productno.setBounds(10, 43, 61, 14);
contentPane.add(Productno);
JLabel lblProductName = new JLabel("Product Name :");
lblProductName.setBounds(10, 68, 94, 14);
contentPane.add(lblProductName);
JLabel lblId = new JLabel("ID :");
lblId.setBounds(10, 93, 46, 14);
contentPane.add(lblId);
JLabel lblPrice = new JLabel("Price(Rs) :");
lblPrice.setBounds(10, 118, 46, 14);
contentPane.add(lblPrice);
JLabel lblDiscount = new JLabel("Discount(%) :");
lblDiscount.setBounds(10, 143, 69, 14);
contentPane.add(lblDiscount);
JLabel lblDateSold = new JLabel("Date Sold :");
lblDateSold.setBounds(10, 168, 61, 14);
contentPane.add(lblDateSold);
nameField = new JTextField();
nameField.setBounds(108, 65, 316, 20);
contentPane.add(nameField);
nameField.setColumns(10);
idfield = new JTextField();
idfield.setBounds(108, 90, 316, 20);
contentPane.add(idfield);
idfield.setColumns(10);
pricefield = new JTextField();
pricefield.setBounds(108, 115, 316, 20);
contentPane.add(pricefield);
pricefield.setColumns(10);
discountField = new JTextField();
discountField.setBounds(108, 140, 316, 20);
contentPane.add(discountField);
discountField.setColumns(10);
DatesoldField = new JTextField();
DatesoldField.setBounds(108, 165, 316, 20);
contentPane.add(DatesoldField);
DatesoldField.setColumns(10);
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{ submitActionPerformed(event);
}
});
btnSubmit.setBounds(10, 209, 109, 42);
contentPane.add(btnSubmit);
}
protected void submitActionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
if(count == 4) {
btnSubmit.setText("Save");
JFileChooser fc = new JFileChooser();
int k = fc.showSaveDialog(ProductDetails.this);
if (k == JFileChooser.APPROVE_OPTION) {
try {
FileWriter fw = new
FileWriter(fc.getSelectedFile().getPath() + ".txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(FinalDetails);
bw.close();
} catch (IOException e)
{ e.printStackTrace();
}
JOptionPane.showMessageDialog(null,
fc.getSelectedFile().getName() + " is Saved", "Success",
JOptionPane.INFORMATION_MESSAGE);
Productno.setText("Product 1");
count = 0;
FinalDetails = "";
btnSubmit.setText("Submit");
}
} else {
String name,id,price,discount,datesold;
String temp;
name = "Name : " + nameField.getText();
id = "ID : " + idfield.getText();
price = "Price : " + pricefield.getText();
discount = "Discount : " + discountField.getText();
datesold = "Date Sold : " + DatesoldField.getText();
temp = "Porduct " + String.valueOf(count + 1) + '\n' +
name + '\n' +
id + '\n' +
price + '\n' +
discount + '\n' +
datesold + '\n' + '\n';
FinalDetails+= temp;
count ++;
nameField.setText("");
idfield.setText("");
pricefield.setText("");
discountField.setText("");
DatesoldField.setText("");
Productno.setText("Product " +String.valueOf(count + 1));
}
}
}
OUTPUT
Experiment No: 10
Aim: WAP to implement JDBC connectivity.
importjava.sql.*;
importjava.util.*;
class Main
{
public static void main(String a[])
{
//Creating the connection
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "system";
String pass = "12345";
//Entering the data
Scanner k = new Scanner(System.in);
System.out.println("enter name");
String name = k.next();
System.out.println("enter roll
no"); int roll = k.nextInt();
System.out.println("enter class");
String cls = k.next();
//Inserting data using SQL query
String sql = "insert into student1
values('"+name+"',"+roll+",'"+cls+"')";
Connection con=null;
try
{
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
//Reference to connection interface
con = DriverManager.getConnection(url,user,pass);
Statement st = con.createStatement();
int m = st.executeUpdate(sql);
if (m == 1)
System.out.println("inserted successfully : "+sql);
else
System.out.println("insertion failed");
con.close();
}
catch(Exception ex)
{
System.err.println(ex);
}
}
}
OUTPUT