//print square root
package jaava;
import javax.swing.*;
public class JAAVA{
public static void main(String[] args)
{
String s = JOptionPane.showInputDialog("Enter Number: ");
int number = Integer.parseInt(s);
int square = number * number;
System.out.println("Number: " + number + "\nSquare: "+ square);
JOptionPane.showMessageDialog(null,"Number: "+number+"\nSquare: " +
square);
}
}
// java class
package jaava;
import javax.swing.*;
class Display
{
public Display() {
JOptionPane.showMessageDialog(null, "Hello, world!");
}
}
public class JAAVA{
public static void main(String[] args)
{
System.out.println("hello"+"\t"+"world");
new Display();
}
}
//Wrapper class
package firstprogram;
import javax.swing.*;
public class FirstProgram{
public static void main(String[] args)
{
Integer num = new Integer(4);
int pnum = num.intValue();
JOptionPane.showMessageDialog(null, pnum);
}
}
//Lecture 3
package firstprogram;
import javax.swing.*;
public class FirstProgram{
public static void main(String[] args)
{
int num = 12;
String n = String.valueOf(num);
JOptionPane.showMessageDialog(null, num);
}
}
//Lecture 4: class with sette getter
package firstprogram;
import javax.swing.*;
class Student{
int rollno;
String name;
void setR()
{
rollno = 100;
}
void setN()
{
name = "shakila";
}
void getR()
{
System.out.println("Roll no: " + rollno);
}
void getN()
{
System.out.println("Name : " + name);
}
}
public class FirstProgram{
public static void main(String[] args)
{
Student s = new Student();
s.setR();
s.setN();
s.getR();
s.getN();
}
}
// commant to run this
cd C:\Users\Administrator.FREELANCERS\Desktop\FirstProgram
javac -d bin src\firstprogram\FirstProgram.java
java -cp bin firstprogram.FirstProgram
// Inheritence lecture 5
package firstprogram;
import javax.swing.*;
class Student{
int rollno;
String name;
Student()
{
rollno = 0;
name = "";
}
void setS()
{
name = "shakila";
rollno = 100;
}
void getS()
{
System.out.println("Roll no: " + rollno);
System.out.println("Name : " + name);
}
}
class Department extends Student
{
int deptId;
String debtName;
Department()
{
deptId = 0;
debtName = "";
}
void setD()
{
deptId = 1455;
debtName = "Computer Science";
}
void getD()
{
System.out.println("Debt ID : " + deptId);
System.out.println("Dept Name : " + debtName);
}
}
public class FirstProgram{
public static void main(String[] args)
{
Department D = new Department();
D.setS();
D.setD();
D.getS();
D.getD();
}
}
// polimorphism in java
package firstprogram;
import javax.swing.*;
class Student{
int rollno;
String name;
Student()
{
rollno = 0;
name = "";
}
void set()
{
name = "shakila";
rollno = 100;
}
void get()
{
System.out.println("Roll no: " + rollno);
System.out.println("Name : " + name);
}
}
class Department extends Student
{
int deptId;
String debtName;
Department()
{
deptId = 0;
debtName = "";
}
@Override
void set()
{
super.set();
deptId = 1455;
debtName = "Computer Science";
}
@Override
void get()
{
super.get();
System.out.println("Debt ID : " + deptId);
System.out.println("Dept Name : " + debtName);
}
}
public class FirstProgram{
public static void main(String[] args)
{
Student s = new Department(); // object ka reference parent class ka aure
creation child class li
s.set();
s.get();
}
}
// Using ArrayList
package firstprogram;
import javax.swing.*;
import java.util.*;
class Student{
int id;
String name;
Student(int i, String n )
{
id = i;
name = n;
}
void print()
{
System.out.println(id + " " + name);
}
}
public class FirstProgram{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
Student s1 = new Student(12, "shakila");
Student s2 = new Student(13, "shagufta");
al.add(s1);
al.add(s2);
System.out.println(al.isEmpty());
System.out.println(al.size());
Student s = (Student)al.get(0);
s.print();
s = (Student)al.get(1);
s.print();
al.remove(0); //s1 removed
al.remove(s2); //s2 removed
System.out.println(al.isEmpty());
System.out.println(al.size());
System.out.println();
}
}
// Using HashMap
package firstprogram;
import javax.swing.*;
import java.util.*;
class Student{
int id;
String name;
Student(int i, String n )
{
id = i;
name = n;
}
void print()
{
System.out.println(id + " " + name);
}
}
public class FirstProgram{
public static void main(String[] args)
{
HashMap h = new HashMap();
Student s1 = new Student(03234633, "asma\n");
Student s2 = new Student(98765, "Shakeela");
h.put("One",s1);
h.put("Two",s2);
System.out.println(h.isEmpty());
System.out.println(h.size());
Student s = (Student)h.get("Two");
s.print();
s = (Student)h.get("One");
s.print();
h.remove("One");
}
}
// Unchecked Exception
package firstprogram;
public class FirstProgram{
public static void main(String[] args)
{
//Unchecked Exception
try{
int a = 10;
int b = 0;
int div = a/b;
System.out.println(div);
}
catch(Exception e)
{
System.out.println("Error: " + e);
}
}
}
//checked Exception
package firstprogram;
import java.io.*;
public class FirstProgram{
public static void main(String[] args)
{
//checked Exception
try{
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
System.out.println("Line: " + line);
}
catch(IOException e)
{
System.out.println("Error: " + e);
}
}
}
// Two or more Catch blocks
package firstprogram;
import java.io.*;
public class FirstProgram
{
public static void main(String[] args)
{
try{
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
System.out.println("Line: " + line);
int i = Integer.parseInt(line);
}
catch(NumberFormatException ne)
{
System.out.println("Error: " + ne);
}
catch(FileNotFoundException fe)
{
System.out.println("Error: " + fe);
}
catch(IOException e)
{
System.out.println("Error: " + e);
}
finally{
System.out.println("Always run");
}
}
}
//Print Stack track
// Two or more Catch blocks
package firstprogram;
import java.io.*;
public class FirstProgram
{
public static void main(String[] args)
{
try{
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
System.out.println("Line: " + line);
int i = Integer.parseInt(line);
}
catch(IOException e)
{
e.printStackTrace();
}
finally{
System.out.println("Always run");
}
}
}
//Reading from File
package firstprogram;
import java.io.*;
public class FirstProgram
{
public static void main(String[] args)
{
FileReader fr = null;
BufferedReader br = null;
try{
fr = new FileReader("input.txt");
br = new BufferedReader(fr);
String line = br.readLine();
while(line!=null)
{
System.out.println(line);
line = br.readLine();
}
br.close();
fr.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
//Writing to File
package firstprogram;
import java.io.*;
public class FirstProgram
{
public static void main(String[] args)
{
FileWriter fw = null;
PrintWriter pw = null;
try{
fw = new FileWriter("vu.txt");
pw = new PrintWriter(fw);
String s1 = "Shakila";
String s2 = "Naveed";
pw.println(s1);
pw.println(s2);
pw.close();
fw.close();
System.out.println("Record Saved");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
//Abstract classes (Khiyali plao)
// abstract class always parent class
package firstprogram;
abstract class Person {
abstract void takeInput();
abstract void displayOutput();
}
class Student extends Person {
int id;
String name;
@Override
public void takeInput() {
id = 100;
name = "Shakila";
}
@Override
public void displayOutput() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
}
}
public class FirstProgram
{
public static void main(String[] args)
{
Person p = new Student();
p.takeInput();
p.displayOutput();
}
}
//Lecture 18
package javadifficult;
import java.awt.*;
import javax.swing.*;
class myPanel extends JPanel
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawRect(20, 20, 20, 20);
g2.setColor(Color.blue);
g2.fillOval(50, 50, 20, 20);
g2.drawString("Hello world", 120, 50);
}
}
public class JavaDifficult {
public static void main(String[] args)
{
JFrame F = new JFrame();
myPanel P = new myPanel();
F.setTitle("MyPainting");
F.setBounds(100, 100, 300, 300);
F.getContentPane().setLayout(new BorderLayout());
F.getContentPane().add(P);
F.setVisible(true);
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//Interface classes (Khiyali plao)
package firstprogram;
interface Person {
void takeInput();
void displayOutput();
}
class Student implements Person
{
int id;
String name;
@Override
public void takeInput() {
id = 100;
name = "Shakila";
}
@Override
public void displayOutput() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
}
}
public class FirstProgram
{
public static void main(String[] args)
{
Person p = new Student();
p.takeInput();
p.displayOutput();
}
}
// GUI
package firstprogram;
import java.awt.*;
import javax.swing.*;
public class FirstProgram{
public static void main(String[] args)
{
JFrame myFrame = new JFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("My First GUI Project");
myFrame.setSize(250, 250);
myFrame.setLocation(100, 200);
Container C = myFrame.getContentPane();
C.setLayout(new FlowLayout());
JTextField TF = new JTextField("Enter Your Name: ");
JButton B = new JButton("Click here");
TF.setSize(100,20);
B.setSize(75, 30);
C.add(TF);
C.add(B);
myFrame.setVisible(true);
}
}
// Event handling : ActionEvent
package firstprogram;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class FirstProgram implements ActionListener
{
void init()
{
JFrame myFrame = new JFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("My GUI Application");
myFrame.setSize(500, 300);
myFrame.setLocation(100, 100);
Container C = myFrame.getContentPane();
C.setLayout(new FlowLayout());
JButton B = new JButton("Click here");
B.setSize(200, 50);
B.setLocation(150, 150);
B.addActionListener(this);
C.add(B);
myFrame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "Button Clicked");
}
public static void main(String[] args)
{
FirstProgram Test = new FirstProgram();
Test.init();
}
}
// Event handling : MouseMotionEvent
package firstprogram;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class FirstProgram implements MouseMotionListener
{
JFrame myFrame;
JLabel L;
void init()
{
myFrame = new JFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("My Title my marzi");
myFrame.setVisible(true);
myFrame.setLocation(100, 100);
myFrame.setSize(500, 400);
Container C = myFrame.getContentPane();
C.setLayout(new BorderLayout());
myFrame.addMouseMotionListener(this);
L = new JLabel("Virtual University");
C.add(L, BorderLayout.PAGE_START);
}
@Override
public void mouseDragged(MouseEvent me)
{
int x = me.getX();
int y = me.getY();
L.setText("Mouse X-axis: " + x + "Mouse Y-axis: " + y);
}
@Override
public void mouseMoved(MouseEvent me)
{
int x = me.getX();
int y = me.getY();
L.setText("Mouse X-axis: " + x + "Mouse Y-axis: " + y);
}
public static void main(String[] args)
{
FirstProgram Test = new FirstProgram();
Test.init();
}
}
// Event handling : Windaw Adapter plus closing window
package firstprogram;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class FirstProgram extends WindowAdapter
{
JFrame myFrame;
void init()
{
myFrame = new JFrame();
myFrame.setTitle("My GUI Application");
myFrame.setLocation(100, 100);
myFrame.setSize(500, 400);
myFrame.setVisible(true);
myFrame.addWindowListener(this);
}
@Override
public void windowClosing(WindowEvent e)
{
JOptionPane.showMessageDialog(null, "Good bye");
System.exit(0);
}
public static void main(String[] args)
{
FirstProgram Test = new FirstProgram();
Test.init();
}
}
// Handling window event with inner classes
package firstprogram;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class FirstProgram extends WindowAdapter {
JFrame myFrame;
JLabel L;
myClass1 t1;
myClass2 t2;
void init() {
myFrame = new JFrame();
myFrame.setTitle("My GUI Application");
myFrame.setLocation(100, 100);
myFrame.setSize(500, 400);
t1 = new myClass1();
t2 = new myClass2();
myFrame.addWindowListener(t1);
myFrame.addMouseMotionListener(t2); // Corrected
Container C = myFrame.getContentPane();
C.setLayout(new BorderLayout());
L = new JLabel("Shakila Naveed");
L.setSize(100, 200);
C.add(L, BorderLayout.PAGE_START);
myFrame.setVisible(true);
}
private class myClass1 extends WindowAdapter {
@Override
public void windowClosing(WindowEvent w) {
JOptionPane.showMessageDialog(null, "Good Bye");
System.exit(0);
}
}
private class myClass2 extends MouseMotionAdapter {
@Override
public void mouseMoved(MouseEvent m) {
int x = m.getX();
int y = m.getY();
L.setText("X-axis: " + x + " Y-axis: " + y);
}
}
public static void main(String[] args) {
FirstProgram Test = new FirstProgram();
Test.init();
}
}
// Handling window event with inner classes using anonimous class
package firstprogram;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class FirstProgram {
JFrame myFrame;
JLabel L;
void init() {
myFrame = new JFrame();
myFrame.setTitle("My GUI Application");
myFrame.setLocation(100, 100);
myFrame.setSize(500, 400);
Container C = myFrame.getContentPane();
C.setLayout(new BorderLayout());
L = new JLabel("Shakila Naveed");
L.setSize(100, 200);
C.add(L, BorderLayout.PAGE_START);
// Add window listener using anonymous inner class
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent w) {
JOptionPane.showMessageDialog(null, "Good Bye");
System.exit(0);
}
});
// Add mouse motion listener using anonymous inner class
myFrame.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent m) {
int x = m.getX();
int y = m.getY();
L.setText("X-axis: " + x + " Y-axis: " + y);
}
});
myFrame.setVisible(true);
}
public static void main(String[] args) {
FirstProgram Test = new FirstProgram();
Test.init();
}
}
package javadifficult;
import java.sql.*;
public class JavaDifficult {
public static void main(String[] args) {
try {
// Use double backslashes in the path for Windows
String url = "jdbc:ucanaccess://D:\\ssc\\shakeela downloads\\MidTerm
Prepration\\PersonInfo.accdb";
Connection con = DriverManager.getConnection(url);
Statement st = con.createStatement();
String qry = "SELECT * FROM Person";
ResultSet rs = st.executeQuery(qry);
while (rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("name");
String address = rs.getString("address");
String phone = rs.getString("PhoneNum");
System.out.println("ID: " + id + " Name: " + name + " Address: " +
address + " Phone: " + phone);
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//Executing SQL DML Statements
package javadifficult;
import java.sql.*;
public class JavaDifficult {
public static void main(String[] args) {
try{
String url = "jdbc:ucanaccess://D:\\ssc\\shakeela downloads\\MidTerm
Prepration\\PersonInfo.accdb";
Connection con = DriverManager.getConnection(url);
Statement st = con.createStatement();
String qry = "INSERT INTO Person (name, address, phoneNum)
VALUES('khizar shahal', 'pakistan', '877665454343')";
int num = st.executeUpdate(qry);
System.out.println(num + "Rows Affected");
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//Updatable resultset(): Use of previous ( ), next( ) & various getters methods
package javadifficult;
import java.sql.*;
public class JavaDifficult {
public static void main(String[] args) {
try{
String url = "jdbc:ucanaccess://D:\\ssc\\shakeela downloads\\MidTerm
Prepration\\PersonInfo.accdb";
Connection con = DriverManager.getConnection(url);
String qry = "SELECT * FROM Person";
PreparedStatement ps = con.prepareStatement(qry,
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = ps.executeQuery();
rs.next();
rs.next();
System.out.println("Move Forward");
String name = rs.getString("name");
System.out.println("Name: " + name);
rs.previous();
name = rs.getString("name");
System.out.println("Name: " + name);
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//Lecture 16: Update methos
package javadifficult;
import java.sql.*;
public class JavaDifficult {
public static void main(String[] args) {
try{
String url = "jdbc:ucanaccess://D:\\ssc\\shakeela downloads\\MidTerm
Prepration\\PersonInfo.accdb";
Connection con = DriverManager.getConnection(url);
String qry = "SELECT * FROM Person";
PreparedStatement ps = con.prepareStatement(qry,
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = ps.executeQuery();
rs.next();
String name = rs.getString("name");
System.out.println("Name: " + name);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//Lecture 16: Absolute method
package javadifficult;
import java.sql.*;
public class JavaDifficult {
public static void main(String[] args) {
try{
String url = "jdbc:ucanaccess://D:\\ssc\\shakeela downloads\\MidTerm
Prepration\\PersonInfo.accdb";
Connection con = DriverManager.getConnection(url);
String qry = "SELECT * FROM Person";
PreparedStatement ps = con.prepareStatement(qry,
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = ps.executeQuery();
rs.absolute(3);
String name = rs.getString("name");
System.out.println("Name: " + name);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//Lecture 16: Absolute method
package javadifficult;
import java.sql.*;
public class JavaDifficult {
public static void main(String[] args) {
try{
String url = "jdbc:ucanaccess://D:\\ssc\\shakeela downloads\\MidTerm
Prepration\\PersonInfo.accdb";
Connection con = DriverManager.getConnection(url);
String qry = "SELECT * FROM Person";
PreparedStatement ps = con.prepareStatement(qry,
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = ps.executeQuery()
;
rs.moveToInsertRow(); // lASTS
rs.updateString("name", "Arooj Fatime");
rs.updateString("address", "mngalsingspace");
rs.updateString("phoneNum", "66545457");
rs.insertRow();
System.out.println("Row inserted");
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}