Ajava Lab File (Priyathik Raj)
Ajava Lab File (Priyathik Raj)
1
INDEX
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.*;
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);
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.*;
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);
}
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;
void 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
10
import javax.swing.JList;
import javax.swing.JComboBox;
/**
* 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);
11
tf1.setFont(new Font("Tahoma", Font.PLAIN, 18));
tf1.setBounds(133, 20, 130, 20);
contentPane.add(tf1);
tf1.setColumns(10);
}
});
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;
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);
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;
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);
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 {
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);
19
contentPane.add(lblNewLabel_2);
20
contentPane.add(lblNewLabel_10);
for(int i=1;i<31;i++) {
cb1.addItem(i);
21
contentPane.add(cb3);
for(int i=2000;i<2051;i++) {
cb3.addItem(i);
22
tf5.setColumns(10);
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();
}
});
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
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;
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);
26
lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_5.setBounds(10, 238, 130, 28);
contentPane.add(lblNewLabel_5);
27
rb1.setFont(new Font("Tahoma", Font.PLAIN, 16));
rb1.setBounds(193, 165, 111, 23);
contentPane.add(rb1);
for(int i=1;i<31;i++) {
cb1.addItem(i);
}
28
contentPane.add(lblNewLabel_12);
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
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
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