












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This Java Programming Lab Report summarizes our hands-on experiences in the course, focusing on key concepts, coding challenges, and Java's relevance in modern software development. We used a standard development environment and documented each experiment thoroughly, including code snippets and problem-solving insights. The report also analyzes code performance and encourages comparisons between different approaches. We discuss the implications of our findings and reflect on our learning journey, considering limitations. In conclusion, we emphasize the importance of Java skills in software development and suggest areas for future work while maintaining proper referencing throughout the report.
Typology: Lab Reports
1 / 20
This page cannot be seen from the preview
Don't miss anything!













2) Write a program to demonstrate various arithmetic calculations using packages.
package arithmetic; public class MyMath { public intadd(intx,int y) { System.out.println (“\t”+(x+y)); } public intsub(intx,int y) { System.out.println (“\t”+(x-y)); } public intmul(intx,int y) { System.out.println (“\t”+(x*y)); } public double div(intx,int y) { System.out.println (“\t”+(x/y)); } public intmod(intx,int y) { System.out.println (“\t”+(x%y)); } }
import arithmetic.*; class Test { public static void main(String as[]) { MyMath m=new MyMath(); m.add(13.42); m.sub(16,5); m.mul(8,4); m.div(24,9); m.mod(19,6); } }
3) Write a program to demonstrate client-server environment using multithreading. import java.net.; import java.io.;
public class GreetingClient {
public static void main(String [] args) { String serverName = args[0]; int port = Integer.parseInt(args[1]); try { System.out.println("Connecting to " + serverName + " on port " + port); Socket client = new Socket(serverName, port);
System.out.println("Just connected to " + client.getRemoteSocketAddress()); OutputStreamoutToServer = client.getOutputStream(); DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello from " + client.getLocalSocketAddress()); InputStreaminFromServer = client.getInputStream(); DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF()); client.close(); } catch (IOException e) { e.printStackTrace(); } } }
// File Name GreetingServer.java import java.net.; import java.io.;
public class GreetingServer extends Thread { private ServerSocketserverSocket;
public GreetingServer(int port) throws IOException { serverSocket = new ServerSocket(port); serverSocket.setSoTimeout(10000); }
public void run() { while(true) { try { System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "..."); Socket server = serverSocket.accept();
System.out.println("Just connected to " + server.getRemoteSocketAddress()); DataInputStream in = new DataInputStream(server.getInputStream());
4) Write a program to demonstrate mutual exclusion using thread synchronization.
class PrintDemo { public void printCount() { try { for(inti = 5; i> 0; i--) { System.out.println("Counter --- " +i ); } } catch (Exception e) { System.out.println("Thread interrupted."); } } } class ThreadDemo extends Thread { private Thread t; private String threadName; PrintDemo PD; ThreadDemo( String name, PrintDemopd) { threadName = name; PD = pd; } public void run() { PD.printCount(); System.out.println("Thread " + threadName + " exiting."); } public void start () { System.out.println("Starting " + threadName ); if (t == null) { t = new Thread (this, threadName); t.start (); } } } public class TestThread { public static void main(String args[]) { PrintDemo PD = new PrintDemo(); ThreadDemo T1 = new ThreadDemo( "Thread - 1 ", PD ); ThreadDemo T2 = new ThreadDemo( "Thread - 2 ", PD ); T1.start(); T2.start(); // wait for threads to end try { T1.join(); T2.join(); } catch ( Exception e) { System.out.println("Interrupted"); } } }
5) Write a program to demonstrate Linked list class.
import java.util.*; class LinkedListDemo { public static void main(String args[]) { LinkedListll = new LinkedList(); ll.add("F"); ll.add("B"); ll.add("D"); ll.add("E"); ll.add("C"); ll.addLast("Z"); ll.addFirst("A"); ll.add(1, "A2"); System.out.println("Original contents of ll: " + ll); ll.remove("F"); ll.remove(2); System.out.println("Contents of ll after deletion: "
7) Write a program to demonstrate Enumeration and Comparator interfaces. import java.util.Enumeration; import java.util.Vector;
class EnumerationTest { public static void main(String arg[]) { Enumeration
// Assigns vector elements to enumeration days = dayNames.elements(); while (days.hasMoreElements()) { System.out.println(days.nextElement()); } } }
8) Write a program to create a registration form with different controls, menus and demonstrate event handling. import java.awt.; import java.applet.; import java.awt.event.*; public class student extends Frame implements ActionListener {String msg; Button b1=new Button("save"); Label l11=new Label("Student details",Label.CENTER); Label l1=new Label("Name:",Label.LEFT); Label l2=new Label("age:",Label.LEFT); Label l3=new Label("Sex(M/F):",Label.LEFT); Label l4=new Label("Address:",Label.LEFT); Label l5=new Label("Course:",Label.LEFT); Label l6=new Label("Semester:",Label.LEFT); Label l7=new Label("",Label.RIGHT); TextField t1=new TextField(); Choice c1=new Choice(); CheckboxGroupcbg=new CheckboxGroup(); Checkbox ck1=new Checkbox("Male",false,cbg); Checkbox ck2=new Checkbox("Female",false,cbg); TextArea t2=new TextArea("",180,90,TextArea.SCROLLBARS_VERTICAL_ONLY); Choice course=new Choice(); Choice sem=new Choice(); Choice age=new Choice(); public student() {addWindowListener(new myWindowAdapter()); setBackground(Color.cyan); setForeground(Color.black); setLayout(null); add(l11); add(l1); add(l2); add(l3); add(l4); add(l5); add(l6); add(l7); add(t1); add(t2); add(ck1); add(ck2); add(course); add(sem); add(age); add(b1); b1.addActionListener(this); add(b1); course.add("BSc c.s"); course.add("BSc maths");
9) Write a program to copy data from one file to another file. import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;
public class CopyExample { public static void main(String[] args) { FileInputStream instream = null; FileOutputStreamoutstream = null;
try{ File infile =new File("C:\MyInputFile.txt"); File outfile =new File("C:\MyOutputFile.txt");
instream = new FileInputStream(infile); outstream = new FileOutputStream(outfile);
byte[] buffer = new byte[1024];
int length; /*copying the contents from input stream to
//Closing the input/output file streams instream.close(); outstream.close();
System.out.println("File copied successfully!!");
}catch(IOExceptionioe){ ioe.printStackTrace(); } } }
10) Write a program to merge contents of two files and display output on console. import java.io.*; public class FileMerge { public static void main(String[] args) throws IOException { PrintWriter pw = new PrintWriter("file3.txt"); BufferedReader br1 = new BufferedReader(new FileReader("file1.txt")); BufferedReader br2 = new BufferedReader(new FileReader("file2.txt")); String line1 = br1.readLine(); String line2 = br2.readLine(); // loop to copy lines of // file1.txt and file2.txt // to file3.txt alternatively while (line1 != null || line2 !=null) { if(line1 != null) { pw.println(line1); line1 = br1.readLine(); }
if(line2 != null) { pw.println(line2); line2 = br2.readLine(); } } pw.flush(); // closing resources br1.close(); br2.close(); pw.close();
System.out.println("Merged file1.txt and file2.txt alternatively into file3.txt"); BufferedReader in = new BufferedReader(new FileReader("file3.txt")); String line = in.readLine(); while(line != null) { System.out.println(line); line = in.readLine(); } in.close(); } }
12) Write a program to retrieve web page using URL class. import java.net.; import java.io.;
public class URLDemo {
public static void main(String [] args) { try { URL url = new URL("https://www.amrood.com/index.htm?language=en#j2se");
System.out.println("URL is " + url.toString()); System.out.println("protocol is " + url.getProtocol()); System.out.println("authority is " + url.getAuthority()); System.out.println("file name is " + url.getFile()); System.out.println("host is " + url.getHost()); System.out.println("path is " + url.getPath()); System.out.println("port is " + url.getPort()); System.out.println("default port is " + url.getDefaultPort()); System.out.println("query is " + url.getQuery()); System.out.println("ref is " + url.getRef()); } catch (IOException e) { e.printStackTrace(); } } }
13) Write a program to load and display image and perform gray scale. import java.awt.*; import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO; import javax.swing.JFrame;
public class GrayScale {
BufferedImage image; int width; int height;
public GrayScale() {
try { File input = new File("digital_image_processing.jpg"); image = ImageIO.read(input); width = image.getWidth(); height = image.getHeight();
for(inti=0; i<height; i++){
for(int j=0; j<width; j++){
Color c = new Color(image.getRGB(j, i)); int red = (int)(c.getRed() * 0.299); int green = (int)(c.getGreen() * 0.587); int blue = (int)(c.getBlue() *0.114); Color newColor = new Color(red+green+blue,
red+green+blue,red+green+blue);
image.setRGB(j,i,newColor.getRGB()); } }
File ouptut = new File("grayscale.jpg"); ImageIO.write(image, "jpg", ouptut);
} catch (Exception e) {} }
static public void main(String args[]) throws Exception { GrayScaleobj = new GrayScale(); } }