0% found this document useful (0 votes)
26 views35 pages

Ajava Lab File (Priyathik Raj)

Uploaded by

Sumit Yadav
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)
26 views35 pages

Ajava Lab File (Priyathik Raj)

Uploaded by

Sumit Yadav
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/ 35

ADVANCE JAVA LAB FILE

SUBMITTED BY: Priyathik Raj SUBMITTED TO: Dr.Ranjana Jain


CLASS: CSE: 5B
ROLLNO: 2K22CSUL01004
DEPARTMENT: B. TECH CSE

1
INDEX

Lab1 Inheritance in Java

Lab2 Awt &Swing

Lab3 Database Connectivity

Lab4 Java Collection FrameWork

Lab5 Generics

2
LAB-1

CO: CO1
BT: BT3
Objectives
• To identify the data types required.
• To use the loops and conditional statement.
• To use arrays
• To implement inheritance in java.
• To define and use interfaces.
• Demonstrate exception handling mechanism in java

Q1) Write a program that prompts the user to input days, hours, minutes, and seconds it took a
mail to reach a destination. The output is total time in seconds for the mail to reach its
destination.

Java Code:
package Lab1;
import java.util.*;
import java.io.*;

public class seconds {


public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter days");
int day=sc.nextInt();
System.out.println("Enter Hours:");
int hours=sc.nextInt();
System.out.println("Enter minutes");
int min=sc.nextInt();

3
System.out.println("Enter Seconds");
int sec=sc.nextInt();
int timeinsec=day*24*60*60+hours*60*60+min*60+sec;
System.out.println("Time in seconds:"+timeinsec);
}
}

Output:

Q2) According to the grading policy, final grade is determined by the average of four test scores.
Create a program to read student’s first name, last name, and four test scores. The program
outputs the student’s last name, four test scores, and the average of test scores of four different
students. Find the best student also and display the details of best student.

Java Code:

import java.util.*;
public class grade {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String namearr[]=new String[4];
String lnamearr[]=new String[4];
int avgarr[]=new int[4];
int bestaverage=0;
int bestind=0;
for(int i=0;i<4;i++) {
System.out.println("Enter first name of "+(i+1)+" student");
namearr[i]=sc.nextLine();
System.out.println("Enter last name of "+(i+1)+" student");
lnamearr[i]=sc.nextLine();
int sum=0;

4
for(int j=0;j<4;j++) {
System.out.println("Enter marks of "+(j+1) +" Subject");
sum=sum+sc.nextInt();
}
int average =sum/4;
avgarr[i]=average;
System.out.println("Average of your marks is ="+average);
if(average>bestaverage) {
bestaverage=average;
bestind=i;
}
}
System.out.println("Details of best student is");
System.out.println("Name: "+namearr[bestind]);
System.out.println("Last Name: "+lnamearr[bestind]);
System.out.println("Average: "+bestaverage);
}

}
Output-:

5
Q3) Write a program to generate Fibonacci series upto the limit entered by the user.

Java Code:

package Lab1;
import java.util.*;
import java.io.*;
public class fibbonaci {
public static void main(String args[])
{
int n1=0,n2=1,n3,i;
Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");


int count =sc.nextInt();
System.out.print(n1+" "+n2);

for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}

}
Output-:

6
Q4) Write a java program to create n objects of the Student class. Assign roll numbers in the
ascending order. Accept name and percentage from the user for each object. Display details of all
the students.
Java Code:
import java.util.*;
import java.io.*;

public class student {

String name;
int roll;
int percentage;
student(String name, int roll, int precentage){
this.name=name;
this.roll=roll;
this.percentage=precentage;

void display() {
System.out.println("Details are :\n Name:"+this.name+"\t roll :"+this.roll+"\t percentage is
:"+this.percentage);
}

public static void main(String args[])throws IOException {

Scanner sc=new Scanner(System.in);


System.out.println("Enter the number of Students : ");
InputStreamReader isr = new InputStreamReader(System.in);

7
BufferedReader br = new BufferedReader(isr);
int n=sc.nextInt();

for(int i=0;i<n;i++) {
System.out.println("Enter the name of student : ");
String name=br.readLine();
System.out.println("Enter the percentage of " +(i+1)+"student : ");
int percent=sc.nextInt();
student st=new student(name, i+1, percent);
st.display();

}
}
}

Output-:

Q5) Define a class Employee having private members - id, name, department, salary. Create a
subclass called "Manager" with private member bonus. Define methods accept and display in
both the classes. Create n objects of the Manager class and display the details of the manager
having the maximum total salary (salary+bonus)
Java Code:

8
class employee{
int Empid;
String name;
int salary;
String dept;

class manager extends employee{


int bonus;
int msal=bonus+this.salary;

void display() {

System.out.println("Details of the employee are");


System.out.println("Name:"+this.name);
System.out.println("salary(including bonus) :"+(bonus+this.salary));
System.out.println("Department:"+this.dept);
System.out.println("Empid:"+this.Empid);

public class EmpDet {


public static void main(String args[]) {
manager man=new manager();
man.Empid=101;
man.dept="Project";
man.name="Shinchan";
man.bonus=10000;
man.salary=40000;
man.display();
}

Output-:

9
LAB-2
Awt & Swing
CO: CO1
BT: BT3

Objectives
• To implement AWT & Swing basic components.
• To implement event handling in Java
• To validate the fields of a form
• To implement Advanced Swing in Java

Q1) Design a form to swap two numbers in AWT.


import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

10
import javax.swing.JList;
import javax.swing.JComboBox;

public class SWAP extends JFrame {

private JPanel contentPane;


private JTextField tf1;
private JTextField tf2;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SWAP frame = new SWAP();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public SWAP() {
setTitle("swap");
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 label1 = new JLabel("number 1");


label1.setFont(new Font("Tahoma", Font.PLAIN, 18));
label1.setBounds(10, 11, 98, 33);
contentPane.add(label1);

tf1 = new JTextField();

11
tf1.setFont(new Font("Tahoma", Font.PLAIN, 18));
tf1.setBounds(133, 20, 130, 20);
contentPane.add(tf1);
tf1.setColumns(10);

JLabel lblNewLabel = new JLabel("number2");


lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
lblNewLabel.setBounds(10, 55, 98, 33);
contentPane.add(lblNewLabel);

tf2 = new JTextField();


tf2.setFont(new Font("Tahoma", Font.PLAIN, 18));
tf2.setBounds(133, 64, 130, 20);
contentPane.add(tf2);
tf2.setColumns(10);

JButton btnNewButton = new JButton("Swap");


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String one=tf1.getText();
tf1.setText(tf2.getText());
tf2.setText(one);
}
});
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnNewButton.setBounds(10, 198, 173, 33);
contentPane.add(btnNewButton);

JButton btnNewButton_1 = new JButton("Cancel");


btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf1.setText(" ");
tf2.setText(" ");

}
});
btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnNewButton_1.setBounds(230, 198, 147, 33);
contentPane.add(btnNewButton_1);
}
}
Output-:

12
Q 2). Design a Login Form using AWT that takes username and password as an input from user
and pops up a message box as login successful if password matches with username otherwise
display login fail as a message in message box.

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Check extends JFrame {

private JPanel contentPane;


private JTextField tf1;
private JTextField tf2;
private JTextField tf3;

public static void main(String[] args) {


EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Check frame = new Check();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

13
});
}

public Check() {
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(" User Name");


lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
lblNewLabel.setBounds(10, 11, 109, 33);
contentPane.add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel("PassWord");


lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 18));
lblNewLabel_1.setBounds(10, 71, 109, 26);
contentPane.add(lblNewLabel_1);

JButton btnNewButton = new JButton("Verify User");


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name=tf1.getText();
String PassWord=tf2.getText();
int counter=0;
String nameO="Pokemon";
String pass="123";
if(name.equals(nameO)&& PassWord.equals(pass)) {
tf3.setText("Congratulation Your UserId and Password are correct");
}
else if(name!="Pokemon"||PassWord!="123"){
tf3.setText(" Your UserId and Password are Incorrect");
}
}
});

btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 18));


btnNewButton.setBounds(10, 205, 416, 47);
contentPane.add(btnNewButton);
tf1 = new JTextField();
tf1.setFont(new Font("Tahoma", Font.PLAIN, 18));
tf1.setBounds(177, 17, 221, 27);

14
contentPane.add(tf1);
tf1.setColumns(10);
tf2 = new JTextField();
tf2.setFont(new Font("Tahoma", Font.PLAIN, 18));
tf2.setBounds(177, 71, 221, 26);
contentPane.add(tf2);
tf2.setColumns(10);
tf3 = new JTextField();
tf3.setFont(new Font("Tahoma", Font.PLAIN, 16));
tf3.setEditable(false);
tf3.setBounds(10, 147, 416, 47);
contentPane.add(tf3);
tf3.setColumns(10);
}

}
Output-:

Q3) In the form given below on the click of the Add subject Button, every detail filled by user
should be displayed in a message Box and every field should be filled.

15
Java Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.JButton;

public class Subject extends JFrame{

private JTextField tf1=new JTextField("Enter Your Details");

private JLabel l1=new JLabel("Subject Name");


private JTextField tf2=new JTextField(20);
private JLabel l2=new JLabel("Subject Code");
private JTextField tf3=new JTextField(20);
private JLabel l3=new JLabel("Branch");
private JComboBox cb1=new JComboBox();
private JLabel l4=new JLabel("Semster");
private JComboBox cb2=new JComboBox();
private JButton b1=new JButton("Add Subject");
Subject(){
tf1.setEditable(false);
tf1.setBounds(50, 50, 100, 20);
l1.setBounds(50, 100, 50, 20 );

16
cb1.addItem("Civil");
cb1.addItem("CSE");
cb1.addItem("Mechanical");

for(int i=1;i<=8;i++) {
cb2.addItem(i);
}

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Your Subject is "+tf2.getText()+"\nYour Subject
Code is"+tf3.getText()+"\n Your Branch:"+cb1.getSelectedItem()+"\nYour
Semester:"+cb2.getSelectedItem());
}
});
this.setLayout(new FlowLayout());
this.getContentPane().add(tf1);
this.getContentPane().add(l1);
this.getContentPane().add(tf2);
this.getContentPane().add(l2);
this.getContentPane().add(tf3);
this.getContentPane().add(l3);
this.getContentPane().add(cb1);
this.getContentPane().add(l4);
this.getContentPane().add(cb2);
this.getContentPane().add(b1);

public static void main(String args[]) {


JFrame f1=new Subject();
f1.setDefaultCloseOperation(f1.EXIT_ON_CLOSE);
f1.setSize(400, 300);
f1.setTitle("Lab 3");
f1.setVisible(true);

Output-:

17
Q4) Create a student registration form using swings.

Java Code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTree;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

18
public class Lab5_ extends JFrame {

private JPanel contentPane;


private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
private JTextField tf4;
private JTextField tf5;
private JTextField tf6;

public static void main(String[] args) {


EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Lab5_ frame = new Lab5_();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public Lab5_() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel lblNewLabel = new JLabel("STUDENT REGISTRATION FORM ");


lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNewLabel.setBounds(88, 11, 349, 34);
contentPane.add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel("Name");


lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_1.setBounds(10, 82, 130, 28);
contentPane.add(lblNewLabel_1);

JLabel lblNewLabel_2 = new JLabel("Age");


lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_2.setBounds(10, 121, 136, 28);

19
contentPane.add(lblNewLabel_2);

JLabel lblNewLabel_3 = new JLabel("Gender");


lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_3.setBounds(10, 160, 130, 28);
contentPane.add(lblNewLabel_3);

JLabel lblNewLabel_4 = new JLabel("Date Of Birth");


lblNewLabel_4.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_4.setBounds(10, 199, 130, 28);
contentPane.add(lblNewLabel_4);

JLabel lblNewLabel_5 = new JLabel("Address");


lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_5.setBounds(10, 238, 130, 28);
contentPane.add(lblNewLabel_5);

JLabel lblNewLabel_6 = new JLabel("Roll Number");


lblNewLabel_6.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_6.setBounds(10, 277, 130, 28);
contentPane.add(lblNewLabel_6);

JLabel lblNewLabel_7 = new JLabel("Brach");


lblNewLabel_7.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_7.setBounds(10, 316, 130, 28);
contentPane.add(lblNewLabel_7);

JLabel lblNewLabel_8 = new JLabel("Semester");


lblNewLabel_8.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_8.setBounds(10, 355, 130, 28);
contentPane.add(lblNewLabel_8);

JTree tree = new JTree();


tree.setBounds(120, 246, -2, 110);
contentPane.add(tree);

JLabel lblNewLabel_9 = new JLabel("Contact\r\n");


lblNewLabel_9.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_9.setBounds(10, 394, 108, 28);
contentPane.add(lblNewLabel_9);

JLabel lblNewLabel_10 = new JLabel("Email\r\n");


lblNewLabel_10.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_10.setBounds(10, 438, 108, 28);

20
contentPane.add(lblNewLabel_10);

tf1 = new JTextField();


tf1.setBounds(215, 85, 204, 26);
contentPane.add(tf1);
tf1.setColumns(10);

tf2 = new JTextField();


tf2.setBounds(225, 127, 194, 28);
contentPane.add(tf2);
tf2.setColumns(10);

JRadioButton rb1 = new JRadioButton("Male");


rb1.setFont(new Font("Tahoma", Font.PLAIN, 16));
rb1.setBounds(193, 165, 111, 23);
contentPane.add(rb1);

JRadioButton rb2 = new JRadioButton("Female");


rb2.setFont(new Font("Tahoma", Font.PLAIN, 16));
rb2.setBounds(338, 165, 111, 23);
contentPane.add(rb2);

JList list = new JList();


list.setBounds(275, 246, 1, 1);
contentPane.add(list);

JComboBox cb1 = new JComboBox();


cb1.setBounds(215, 204, 30, 22);
contentPane.add(cb1);

for(int i=1;i<31;i++) {
cb1.addItem(i);

JComboBox cb2 = new JComboBox();


cb2.setBounds(345, 204, 30, 22);
contentPane.add(cb2);
for(int i=1;i<=12;i++) {
cb2.addItem(i);
}

JComboBox cb3 = new JComboBox();


cb3.setBounds(467, 204, 30, 22);

21
contentPane.add(cb3);
for(int i=2000;i<2051;i++) {
cb3.addItem(i);

JLabel lblNewLabel_11 = new JLabel("Date");


lblNewLabel_11.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_11.setBounds(168, 208, 49, 19);
contentPane.add(lblNewLabel_11);

JLabel lblNewLabel_12 = new JLabel("Month\r\n");


lblNewLabel_12.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_12.setBounds(275, 202, 60, 23);
contentPane.add(lblNewLabel_12);

JLabel lblNewLabel_13 = new JLabel("Year");


lblNewLabel_13.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_13.setBounds(408, 208, 49, 19);
contentPane.add(lblNewLabel_13);

tf3 = new JTextField();


tf3.setBounds(180, 244, 239, 28);
contentPane.add(tf3);
tf3.setColumns(10);

tf4 = new JTextField();


tf4.setBounds(190, 283, 229, 22);
contentPane.add(tf4);
tf4.setColumns(10);
String[] Course={"CSE", "Mechanical", "Electrical"};
JComboBox cb4 = new JComboBox(Course);
cb4.setBounds(215, 321, 89, 22);
contentPane.add(cb4);
JComboBox cb5 = new JComboBox();
cb5.setBounds(215, 360, 89, 22);
contentPane.add(cb5);
for(int i=0;i<8;i++) {
cb5.addItem(i+1);
}

tf5 = new JTextField();


tf5.setBounds(180, 400, 239, 34);
contentPane.add(tf5);

22
tf5.setColumns(10);

tf6 = new JTextField();


tf6.setBounds(180, 444, 239, 28);
contentPane.add(tf6);
tf6.setColumns(10);
ButtonGroup bg1=new ButtonGroup();

bg1.add(rb1);
bg1.add(rb2);
JButton btnNewButton = new JButton("Submit Details\r\n");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

String Name=tf1.getText();
String age=tf2.getText();
String Gender=(rb1.isSelected())?"Male":"Female";
int BirthDay=(int)cb1.getSelectedItem();
int BMonth=(int)cb2.getSelectedItem();
int Byear=(int)cb3.getSelectedItem();
String Adress=tf3.getText();
String RollNo=tf4.getText();
String Brach=cb4.getSelectedItem().toString();
int Sem=(int)cb5.getSelectedItem();
String Contact=tf5.getText();
String Email=tf6.getText();

String Message="Hello "+Name+"!"+"\nYour age is :"+age+"\n Your


Gender:"+Gender+"\nYour Date of Birth:"+BirthDay+"-"+BMonth+"-"+Byear+"\n Your Adress
is:"+Adress+""
+ "\nyour Roll No is "+RollNo+"\nYour Brach is:"+Brach+"\n Contact
:"+Contact+"\nEmail:"+Email;
JOptionPane.showMessageDialog(null, Message);

}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 18));
btnNewButton.setBounds(215, 512, 234, 23);
contentPane.add(btnNewButton);
}
}

Output-:

23
LAB-3
Database Connectivity
CO: CO2
BT: BT3

Objectives:
• To implement database connectivity using JDBC API

Q1.) WAP to perform the following using JDBC:


Search a student using student name and display the details of that particular student

24
Java Code:
import java.sql.*;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTree;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Lab5_ extends JFrame {

private JPanel contentPane;


private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
private JTextField tf4;
private JTextField tf5;
private JTextField tf6;

public static void main(String[] args) throws ClassNotFoundException, SQLException{


EventQueue.invokeLater(new Runnable() {

25
public void run() {
try {
Lab5_ frame = new Lab5_();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Lab5_() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel lblNewLabel = new JLabel("STUDENT REGISTRATION FORM ");


lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNewLabel.setBounds(88, 11, 349, 34);
contentPane.add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel("Name");


lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_1.setBounds(10, 82, 130, 28);
contentPane.add(lblNewLabel_1);

JLabel lblNewLabel_2 = new JLabel("Age");


lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_2.setBounds(10, 121, 136, 28);
contentPane.add(lblNewLabel_2);

JLabel lblNewLabel_3 = new JLabel("Gender");


lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_3.setBounds(10, 160, 130, 28);
contentPane.add(lblNewLabel_3);

JLabel lblNewLabel_4 = new JLabel("Date Of Birth");


lblNewLabel_4.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_4.setBounds(10, 199, 130, 28);
contentPane.add(lblNewLabel_4);

JLabel lblNewLabel_5 = new JLabel("Address");

26
lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_5.setBounds(10, 238, 130, 28);
contentPane.add(lblNewLabel_5);

JLabel lblNewLabel_6 = new JLabel("Roll Number");


lblNewLabel_6.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_6.setBounds(10, 277, 130, 28);
contentPane.add(lblNewLabel_6);

JLabel lblNewLabel_7 = new JLabel("Brach");


lblNewLabel_7.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_7.setBounds(10, 316, 130, 28);
contentPane.add(lblNewLabel_7);

JLabel lblNewLabel_8 = new JLabel("Semester");


lblNewLabel_8.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_8.setBounds(10, 355, 130, 28);
contentPane.add(lblNewLabel_8);

JTree tree = new JTree();


tree.setBounds(120, 246, -2, 110);
contentPane.add(tree);

JLabel lblNewLabel_9 = new JLabel("Contact\r\n");


lblNewLabel_9.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_9.setBounds(10, 394, 108, 28);
contentPane.add(lblNewLabel_9);

JLabel lblNewLabel_10 = new JLabel("Email\r\n");


lblNewLabel_10.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_10.setBounds(10, 438, 108, 28);
contentPane.add(lblNewLabel_10);

tf1 = new JTextField();


tf1.setBounds(215, 85, 204, 26);
contentPane.add(tf1);
tf1.setColumns(10);

tf2 = new JTextField();


tf2.setBounds(225, 127, 194, 28);
contentPane.add(tf2);
tf2.setColumns(10);

JRadioButton rb1 = new JRadioButton("Male");

27
rb1.setFont(new Font("Tahoma", Font.PLAIN, 16));
rb1.setBounds(193, 165, 111, 23);
contentPane.add(rb1);

JRadioButton rb2 = new JRadioButton("Female");


rb2.setFont(new Font("Tahoma", Font.PLAIN, 16));
rb2.setBounds(338, 165, 111, 23);
contentPane.add(rb2);

JList list = new JList();


list.setBounds(275, 246, 1, 1);
contentPane.add(list);

JComboBox cb1 = new JComboBox();


cb1.setBounds(215, 204, 30, 22);
contentPane.add(cb1);

for(int i=1;i<31;i++) {
cb1.addItem(i);
}

JComboBox cb2 = new JComboBox();


cb2.setBounds(345, 204, 30, 22);
contentPane.add(cb2);
for(int i=1;i<=12;i++) {
cb2.addItem(i);
}

JComboBox cb3 = new JComboBox();


cb3.setBounds(467, 204, 30, 22);
contentPane.add(cb3);
for(int i=2000;i<2051;i++) {
cb3.addItem(i);

JLabel lblNewLabel_11 = new JLabel("Date");


lblNewLabel_11.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_11.setBounds(168, 208, 49, 19);
contentPane.add(lblNewLabel_11);

JLabel lblNewLabel_12 = new JLabel("Month\r\n");


lblNewLabel_12.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_12.setBounds(275, 202, 60, 23);

28
contentPane.add(lblNewLabel_12);

JLabel lblNewLabel_13 = new JLabel("Year");


lblNewLabel_13.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_13.setBounds(408, 208, 49, 19);
contentPane.add(lblNewLabel_13);

tf3 = new JTextField();


tf3.setBounds(180, 244, 239, 28);
contentPane.add(tf3);
tf3.setColumns(10);

tf4 = new JTextField();


tf4.setBounds(190, 283, 229, 22);
contentPane.add(tf4);
tf4.setColumns(10);
String[] Course={"CSE", "Mechanical", "Electrical"};
JComboBox cb4 = new JComboBox(Course);
cb4.setBounds(215, 321, 89, 22);
contentPane.add(cb4);
JComboBox cb5 = new JComboBox();
cb5.setBounds(215, 360, 89, 22);
contentPane.add(cb5);
for(int i=0;i<8;i++) {
cb5.addItem(i+1);
}

tf5 = new JTextField();


tf5.setBounds(180, 400, 239, 34);
contentPane.add(tf5);
tf5.setColumns(10);
tf6 = new JTextField();
tf6.setBounds(180, 444, 239, 28);
contentPane.add(tf6);
tf6.setColumns(10);
ButtonGroup bg1=new ButtonGroup();
bg1.add(rb1);
bg1.add(rb2);
JButton btnNewButton = new JButton("Submit Details\r\n");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Name=tf1.getText();
String age=tf2.getText();
String Gender=(rb1.isSelected())?"Male":"Female";

29
int BirthDay=(int)cb1.getSelectedItem();
int BMonth=(int)cb2.getSelectedItem();
int Byear=(int)cb3.getSelectedItem();
String Adress=tf3.getText();
String RollNo=tf4.getText();
String Branch=cb4.getSelectedItem().toString();
int Sem=(int)cb5.getSelectedItem();
String Contact=tf5.getText();
String Email=tf6.getText();
try(Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/semester5", "root",
"7827975878"))
{
Statement statemnt1 = conn.createStatement();
statemnt1.executeUpdate( "insert into ajava_lab5 (name, age, gender, Birthday,
birthMonth, birthYear, adress , roll_no, branch, semester, contact, email)values('"
+Name+"','"+age +"' ,'"+Gender+"' ,'"+BirthDay+"'
,'"+BMonth+"','"+Byear+"','"+Adress+"','"+RollNo+"','"+Branch+"','"+Sem+"',
'"+Contact+"','"+Email+"')");
}
catch (SQLException e1) {
e1.printStackTrace();
}
String Message="Hello "+Name+"!"+"\nYour age is :"+age+"\n Your
Gender:"+Gender+"\nYour Date of Birth:"+BirthDay+"-"+BMonth+"-"+Byear+"\n Your Adress
is:"+Adress+""
+ "\nyour Roll No is "+RollNo+"\nYour Brach is:"+Branch+"\n Contact
:"+Contact+"\nEmail:"+Email;
JOptionPane.showMessageDialog(null, Message);
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 18));
btnNewButton.setBounds(215, 512, 234, 23);
contentPane.add(btnNewButton);
}
}

Output-:

30
LAB-4
Java Collections Framework
CO: CO2

BT: BT3

Objectives:
• To implement Link List operations using Collection framework

Q1.) Implementation of Linked List


import java.io.*;
// Java program to implement
// a Singly Linked List
public class LinkedList {
Node head;
static class Node {
int data;
Node next;
// Constructor
Node(int d)

31
{
data = d;
next = null;
}}
public static LinkedList insert(LinkedList list, int data){
Node new_node = new Node(data);
if (list.head == null) {
list.head = new_node;
}
else {
Node last = list.head;
while (last.next != null) {
last = last.next;
}
// Insert the new_node at last node
last.next = new_node;)}
return list;
}
public static void printList(LinkedList list)
{
Node currNode = list.head;
System.out.print("LinkedList: ")
while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data + " ");
currNode = currNode.next;
}}
public static void main(String[] args){

32
LinkedList list = new LinkedList();
// Insert the values
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);
printList(list);
}
}
Output-:

LAB-5
Generics
CO: CO2
BT: BT3
Objectives:
• To implement the concept of generics in Java

Q1.) WAP to perform following concepts related to generics:


1. Parametrized Arraylist
Java Code:
import java.util.*;
public class arrayList {

33
public static void main(String args[]) {
ArrayList<Integer> al=new ArrayList<>();
boolean b=al.add(15);
System.out.println("Return type of arrayList add method:"+b);
al.add(25);
al.remove(1);
for(int i=0;i<100;i=i+10) {
al.add(i);
}
int a=al.remove(5);
System.out.println("arrayList remove method:"+a);
System.out.print("Using To String method:"+al.toString());
}
}

Output-:

B. Generic class
class Gener<T, U>
{
T obj1;
U obj2;
Gener(T obj1, U obj2)
{
this.obj1 = obj1;
this.obj2 = obj2;
}
public void print()
{
System.out.println(obj1);
System.out.println(obj2);
}
}

34
class Main
{
public static void main (String[] args)
{
Gener <String, Integer> obj = new Gener<String, Integer>("Perman ", 100);
Gener<String, Boolean>obj1=new Gener<>("Ninja Hattori ", true);
Gener<Integer, Integer>obj2 =new Gener<>(10, 20);
System.out.println("Object 1:");
obj.print();
System.out.println("Object 2:");
obj1.print();
System.out.println("Object 3:");
obj2.print();
}
}

Output-:

35

You might also like