Java Programs
Java Programs
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();
}
}
// 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();
// 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);
}
}
}
//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();
}
}
}
@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);
}
}
@Override
public void takeInput() {
id = 100;
name = "Shakila";
}
@Override
public void displayOutput() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
}
}
// 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);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
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());
myFrame.setVisible(true);
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
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());
myFrame.setVisible(true);
}
package javadifficult;
import java.sql.*;
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}