Java Practical File IPU
Java Practical File IPU
Java Programming
File
BATCH 2018-2022
{
void demo (int a) // First Method
{
System.out.println ("a: " + a);
}
{
System.out.println ("a and b: " + a + "," + b);
}
{
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);
{
int id;
String name;
int age;
{
id = i;
name = n;
}
{
id = i;
name = n;
age=a;
}
{
System.out.println(id+" "+name+" "+age);
}
{
Student s1 = new Student(111,"Karan");
{
void show()
{
System.out.println("BASE");
}
}
{
void show()
{
System.out.println("CHILD");
super.show(); // super keyword
{
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);
}
}
{
void get(int x, int y)
{
this.x = x;
this.y = y;
}
{
Child1 c1 = new Child1();
c1.get(10,20);
c1.show();
}
}
OUTPUT
Experiment No: 04
Aim: WAP to implement the concept Interface
{
void print();
}
{
void show();
}
{
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
//Java program to implement Exception handling using try, catch, finally in java
{
public void finalize()
{
System.out.println("finalize called");
}
{
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
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
{
public void run()
{
System.out.println("thread is running...through Runnable interface");
}
{
Multi1 m1=new Multi1();
Thread t1 =new Thread(m1);
t1.start();
}
}
OUTPUT
d) Thread Class
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;
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;
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_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");
}
}
// Record position that mouse entered window or
// where user pressed mouse button.
record(event.getX(), event.getY());
}
record(event.getX(), event.getY());
}
}
// As user drags mouse, connect subsequent positions
// with short line segments.
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();
setContentPane(contentPane);
contentPane.setLayout(null);
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);
String name,eno,serialno,branch,year;
name = "Name :" + nameField.getText();
eno = "E.No. :" + enoField.getText();
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();
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";
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