Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

CS506-finalterm code

The document contains code examples from various lectures covering Java programming concepts such as animation, applets, client-server communication, serialization, threading, and file handling. It includes implementations of graphical user interfaces, socket programming, and multithreading techniques. Each lecture provides practical coding examples to demonstrate the discussed concepts.

Uploaded by

hunyroomi4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CS506-finalterm code

The document contains code examples from various lectures covering Java programming concepts such as animation, applets, client-server communication, serialization, threading, and file handling. It includes implementations of graphical user interfaces, socket programming, and multithreading techniques. Each lecture provides practical coding examples to demonstrate the discussed concepts.

Uploaded by

hunyroomi4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lecture # 19

// Animation Code
package lecture_19;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class myPanel extends JPanel
{
int mx = 20, my = 20;
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.blue);
g2.fillRect(mx, my, 30, 30);
// g2.fillOval(mx, my, 20, 20); //for ball animation
}
}
public class Lecture_19 {
static JFrame F = new JFrame();
static myPanel p = new myPanel();
public static void main(String[] args) {
F.setTitle("My Painting");
F.setBounds(150, 150, 300, 300);
F.getContentPane().setLayout(new BorderLayout ());
F.getContentPane().add(p);
p.addMouseMotionListener(new Event());
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
F.setVisible(true);
}
static class Event extends MouseMotionAdapter
{
@Override
public void mouseDragged(MouseEvent me)
{
p.mx = me.getX();
p.my = me.getY();
p.repaint();
}
}
}

Lecture # 20
Create Simple Applet
import javax.swing.*;
import java.awt.*;
public class MyApplet extends JApplet {
@Override
public void Paint(Graphics g)
{
g.drawString(“Hello World!” 30, 30);
}
}
// Init method in applet
@Override
Public void init()
{
JOptionPane.showMessageDialog(null, “ init method ”);
}
// start method in applet
@Override
Public void start()
{
JOptionPane.showMessageDialog(null, “ start method ”);
}
// Paint method in applet
@Override
Public void paint()
{
g.drawString(“Hello World! “, 30, 30);
}
// Stop method in applet
@Override
Public void stop()
{
JOptionPane.showMessageDialog(null, “ stop method ”);
}
// Destroy method in applet
@Override
Public void destroy()
{
JOptionPane.showMessageDialog(null, “ destroy method ”);
}

Lecture # 21
Client.java file code
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Client {
public static void main(String args[])
{
try{

while(true)
{
// create socket
Socket s = new Socket("localhost", 2222);
// input Stream
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

// Output Stream
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);

// Take name from user


String name = JOptionPane.showInputDialog("Enter your
name");

// send name to server


pw.println(name);

// get name from server


name = br.readLine();

// print name on screen


JOptionPane.showMessageDialog(null, name);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Server.java file code


import java.net.*;
import java.io.*;
public class Server {
public static void main(String args[])
{
try{
// create socket server
ServerSocket ss = new ServerSocket(2222);
System.out.println("Server started..");

while(true)
{
// port accept or not
Socket s = ss.accept();
System.out.println("Connection Request Received");

// Input Stream
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// Output Stream
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);

// Reading name send byb user


String name = br.readLine();

// Appending hello with user name


String msg = "Hello!" + name;

//Sent back to client


pw.println(msg);

// close communication
s.close();

}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Lecture # 22
// serialization with file
package lecture_22;
import java.io.*;
class PersonInfo implements Serializable
{
int id;
String name;
String address;
transient String phone;
PersonInfo(int id, String name, String address, String phone)
{
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
}
void PersonInfoDisplay()
{
System.out.println("Id: " + id);
System.out.println("Name: " + name);
System.out.println("Address: " + address);
System.out.println("Phone: " + phone);
}
}
public class Lecture_22 {
public static void main(String[] args) {
PersonInfo p = new PersonInfo(210407911, "Maryam", "Wah
Cantt", "03175184101");

try {
// send data into file
FileOutputStream fos = new FileOutputStream("Person.dat");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(p);
// close connection
os.close();
fos.close();

// take data from file


FileInputStream fis = new FileInputStream("Person.dat");
ObjectInputStream is = new ObjectInputStream(fis);
p = (PersonInfo) is.readObject();
p.PersonInfoDisplay();
// close connection

is.close();
fis.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Lecture # 23
// code example using Interface
package lecture_23;
class Worker implements Runnable
{
String job;
Worker(String j)
{
job = j;
}
@Override
public void run()
{
for(int i=1; i<=5; i++)
{
System.out.println(job + " = " + i);
}
}
}
public class Lecture_23 {
public static void main(String[] args) {
Worker w1 = new Worker("First: ");
Worker w2 = new Worker("Second: ");
Worker w3 = new Worker("Third: ");
Thread t1 = new Thread(w1);
Thread t2 = new Thread(w2);
Thread t3 = new Thread(w3);
t1.start();
t2.start();
t3.start();
}
}

// code example using Inheritence


package lecture_23.b;
class Worker extends Thread
{
String job;
Worker(String j)
{
job = j;
}
@Override
public void run()
{
for(int i=1; i<=5; i++)
{
System.out.println(job + " = " + i);
}
}
}
public class Lecture_23B {
public static void main(String[] args) {
Worker w1 = new Worker("First: ");
Worker w2 = new Worker("Second: ");
Worker w3 = new Worker("Third: ");
w1.start();
w2.start();
w3.start();
}
}
// code example thread priorities
package lecture_23.c;
class Worker implements Runnable
{
String job;
Worker(String j)
{
job = j;
}
@Override
public void run()
{
for(int i=1; i<=5; i++)
{
System.out.println(job + " = " + i);
}
}
}
public class Lecture_23C {
public static void main(String[] args) {
Worker w1 = new Worker("First: ");
Worker w2 = new Worker("Second: ");
Worker w3 = new Worker("Third: ");
Thread t1 = new Thread(w1);
Thread t2 = new Thread(w2);
Thread t3 = new Thread(w3);
t1.setPriority(Thread.MAX_PRIORITY); // high priority
t2.setPriority(Thread.MIN_PRIORITY); // low priority
t3.setPriority(Thread.NORM_PRIORITY); // normal priority
t1.start();
t2.start();
t3.start();
}
}

Lecture # 24
package lecture_24;
class Worker implements Runnable {
String job;

Worker(String j) {
job = j;
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(job + " = " + i);
}
}
}

public class Lecture_24 {


public static void main(String[] args) {
Worker first = new Worker("First Job");
Worker second = new Worker("Second Job");

Thread t1 = new Thread(first);


Thread t2 = new Thread(second);

// Debugging outputs
System.out.println("Starting threads...");
t1.start();
t2.start();

try {
t1.join();
t2.join();
} catch (Exception e) {
e.printStackTrace();
}

System.out.println("Both threads have finished.");


}
}

File Handling Using Multiple Threads


import java.io.*;
public class File implements Runnable{
String filename, line;
public File(String f){
fileName = f;
}
@Override
public void run ( ){
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
line = br.readLine();
while(line != null) {
System.out.println(line);
line = br.readLine();
}
fr.close();
br.close();
}
catch (Exception e){
System.out.println(e);
}
}
}
public class Test {
public static void main (String args[]){
File first = new File("first.txt");
File second = new File("second.txt");
Thread t1 = new Thread(first);
Thread t2 = new Thread(second);
t1.start();
t2.start();
}
}
Demonstrating sleep ( ) usage
public class Worker implements Runnable {
String job ;
Worker (String j ){
job = j;
}
@Override
public void run ( ) {
for(int i=1; i<= 10; i++) {
try {
Thread.sleep(100);
}catch (Exception ex){
System.out.println(ex);
}
System.out.println(job + " = " + i);
}
}
}
public class SleepEx {
public static void main (String args[ ]){
Worker first = new Worker (“first job”);
Worker second = new Worker (“second job”);
Thread t1 = new Thread (first );
Thread t2 = new Thread (second);
t1.start();
t2.start();
}
}
Yield Method of Thread
Class Worker implements Runnable {
String job;
Worker (String j)
{
job = j;
}
@Override
Public void run()
{
for(int i=1; i<=5; i++)
{
Thread.yield();
System.out.println(job + “ ” + i);
}
}
}
Public class lecture_24 {
Public static void main(String[] args) {
Worker first = new Worker(“First job”);
Worker second = new Worker(“Second job”);
Thread t1 = new Thread(first);
Thread t2 = new Thread(second);
t1.start();
t2.start();
}
}

Lecture 28

You might also like