assignment report
assignment report
package exampleprgms;
//1DA22CS029 C DHANUSH B SECTION
public class Example1 {
package exampleprgms;
//1DA22CS029 C DHANUSH B SECTION
//2)main function residing outside the class
public class Student {
int id;
String name;
}
class Student1{
public static void main(String[] args) {
Student s1= new Student();
s1.id=10;
s1.name="abc";
System.out.println("Student name: "+s1.name);
System.out.println("Student id: "+s1.id);
}
OUTPUT:
1) 2)
3. Example program for main function residing within the class
package exampleprgms;
//1DA22CS029 C DHANUSH B SECTION
// main function residing within the class
public class Prgrm {
int id;
String name;
OUTPUT:
String Example Programs
4. Program to create String using different methods in java
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class StringExample1 {
//creation of string in java
public static void main(String[] args) {
String s1="java";//creating string by Java string literal
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch);//converting char array to string
String s3=new String("example");//creating Java string by new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class StringExample2 {
//Test immutable strings
public static void main(String[] args) {
String s="Java";
s.concat(" Program");//concat() method appends the string at the end
System.out.println("Immutable string:");
System.out.println(s);//will Java because strings are immutable objects
s=s.concat(" Programm");
System.out.println("String overwritten:");
System.out.println(s);
}
}
OUTPUT:
4) 5)
6. Program to take user input
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
import java.util.Scanner;
//6)user input program
public class StringExample3 {
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class LengthofString {
//7)to find length of string
public static void main(String[] args) {
char[] ch = {'j','a','v','a'};
String s = new String(ch);
System.out.println(s);
System.out.println(s.length());
String s1="Java Program";
System.out.println(s1);
System.out.println(s1.length());
}
}
OUTPUT:
6) 7)
8. Program to compare two strings using equals() method:
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class StringEqualsmethod {
//8)string comparison using equals() method
public static void main(String[] args) {
String s1="Sachin";
String s2="Sachin";
String s3=new String("sachin");
String s4="Saurav";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//false
System.out.println(s1.equalsIgnoreCase(s3));//true
System.out.println(s1.equals(s4));//false
}
}
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class StringequalsOperator {
//9)string comparison using ==
public static void main(String[] args) {
String s1="Java";
String s2="Java";
String s3=new String("Java");
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
}
}
OUTPUT:
8) 9)
10. Program to compare string using compareTo( ) method
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
class String_compareTo {
//10)string comparison using compareTo() method
public static void main(String[] args) {
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
System.out.println(s1.compareTo(s2));//0
System.out.println(s1.compareTo(s3));//1(because s1>s3)
System.out.println(s3.compareTo(s1));//-1(because s3 < s1 )
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class StartswithEndswith {
//11) Comparison using startsWith() endsWith() method
public static void main(String[] args) {
String str = "Java Program";
System.out.println(str.startsWith("Java"));
System.out.println(str.endsWith("Program"));
System.out.println(str.startsWith("java"));
OUTPUT:
10) 11)
12. String concatenation using ‘+’ operator
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class StringConcatenationByOperator {
//12)string concatenation using "+" operator
public static void main(String[] args) {
String s="Sachin"+" Tendulkar";
System.out.println(s);//Sachin Tendulkar
String s1=50+30+"Sachin"+40+40;
System.out.println(s1);//80Sachin4040
}
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class StringConcatMethod {
//13)concatenation using concat() method
public static void main(String[] args) {
String s1="String ";
String s2="Concatenation using different methods";
String s3=s1.concat(s2);
System.out.println(s3);
}
OUTPUT:
12 ) 13)
14. String concatenation using format method( )
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class ConcatUsingFormat {
//14)String concatenation using format() method
public static void main(String[] args) {
String s1 = new String("Format "); //String 1
String s2 = new String(" method concatenation"); //String 2
String s = String.format("%s%s",s1,s2); //String 3 to store the result
System.out.println(s.toString()); //Displays result
OUTPUT:
15. String concatenation using join method
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class ConcatUsingJoin {
//15)String concatenation using String join() method
public static void main(String[] args) {
String s1 = new String("Joining of"); //String 1
String s2 = new String(" Strings"); //String 2
String s = String.join("",s1,s2); //String 3 to store the result
System.out.println(s.toString()); //Displays result
}
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class JoinMethod {
//16)string concatenation using String.join() method
public static void main(String[] args) {
String s=String.join("","Romeo"," Juliet");
System.out.println(s);
}
OUTPUT:
17. Program to perform concatenation using StringBuilder
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class ConcatByStrBuilder {
//17)String concatenation using StringBuilder class
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder("first "); //String 1
StringBuilder s2 = new StringBuilder(" to last"); //String 2
StringBuilder s = s1.append(s2); //String 3 to store the result
System.out.println(s.toString()); //Displays result
OUTPUT:
18. Program to show string class methods to change case
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class CaseMethods {
//18)java String class methods to change the case
public static void main(String[] args) {
String s="Java";
System.out.println(s.toUpperCase());//JAVA
System.out.println(s.toLowerCase());//java
System.out.println(s);//Java(no change in original)
OUTPUT:
19. Program to show string class method replace( )
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class ReplaceMethod {
//19)String operations String replace() method
public static void main(String ar[]) {
String s1="Java is a programming language. Java is a platform.";
System.out.println(s1);//statement before replace()
String replaceString=s1.replace("Java","Python");
//replaces all occurrences of "Java" to "Kava"
System.out.println(replaceString);
}
}
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class StringCharAtMethod {
// 20)String charAt() method
public static void main(String ar[]) {
String s="JAVA";
System.out.println(s.charAt(0));//J
System.out.println(s.charAt(3));//A
}
}
OUTPUT:
21. Program to show substring method
package stringexamples;
//1DA22CS029 C DHANUSH B SECTION
public class Substring {
//21)Java substring method
public static void main(String[] args) {
String s="ChristianoRonaldo";
System.out.println("Original String: " + s);
System.out.println("Substring starting from index 10: "
+s.substring(10));//Ronaldo
System.out.println("Substring starting from index 0 to 10:
"+s.substring(0,10));//Christiano
OUTPUT:
StringBuffer example programs
package stringbuffer;
//1DA22CS029 C DHANUSH B SECTION
public class StringBufferAppend {
//22.example of StringBuffer using append() method
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
OUTPUT:
23. StringBuffer insert method
package stringbuffer;
//1DA22CS029 C DHANUSH B SECTION
public class StringBufferInsert {
//23)StringBuffer insert() Method
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.insert(1,"Java");//now original string is changed
System.out.println(sb);//prints HJavaello
}
}
package stringbuffer;
//1DA22CS029 C DHANUSH B SECTION
public class StringBufferReplace {
//24)StringBuffer replace() Method
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("Dr.AIT");
sb.replace(2,4," Ambedkar ");
System.out.println(sb);
}
}
OUTPUT:
25. StringBuffer delete method
package stringbuffer;
//1DA22CS029 C DHANUSH B SECTION
public class StringBufferDelete {
package stringbuffer;
//1DA22CS029 C DHANUSH B SECTION
public class StringBufferReverse {
//26)StringBuffer reverse() Method
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("Mississippi");
sb.reverse();
System.out.println(sb);
}
}
OUTPUT:
27. Program to show the StringBuffer capacity
package stringbuffer;
//1DA22CS029 C DHANUSH B SECTION
public class StringBufferCapacity {
//27)StringBuffer capacity() Method
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
}
}
package stringbuffer;
//1DA22CS029 C DHANUSH B SECTION
public class StringMethods {
//29)intern() method
public static void main(String[] args) {
String s = new String("Prgm");
String s2 = s.intern();
String s3 = "java";
System.out.println(s2);
System.out.println(s3);
}
}
OUTPUT:
29. Program to demonstrate ensureCapacity method in StringBuffer
package stringbuffer;
//1DA22CS029 C DHANUSH B SECTION
public class StringBufferEnsureCapacity {
//28)StringBuffer ensureCapacity() method
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//now (34*2)+2
System.out.println(sb.capacity());//now 70
OUTPUT:
30. Program to demonstrate toString( ) method
package stringbuffer;
//1DA22CS029 C DHANUSH B SECTION
public class ToString {
//30)Example of Java toString() method
int rollno;
String name;
String city;
ToString(int rollno, String name, String city){
this.rollno=rollno;
this.name=name;
this.city=city;
}
OUTPUT:
StringBuilder Example programs
31. StringBuilder insert( ) method
package stringbuilder;
//1DA22CS029 C DHANUSH B SECTION
public class StringBuilderInsert {
//31) StringBuilder insert ()
public static void main(String[] args) {
StringBuilder sb=new StringBuilder("Hello ");
sb.insert(1,"Java");//now original string is changed
System.out.println(sb);//prints HJavaello
}
}
package stringbuilder;
//1DA22CS029 C DHANUSH B SECTION
public class StringBuilderReplace {
//32)StringBuilder replace () method
public static void main(String[] args) {
StringBuilder sb=new StringBuilder("Dr .AIT");
sb.replace(3,5,"Ambedkar ");
System.out.println(sb);
}
}
OUTPUT:
33. StringBuilder delete( ) method
package stringbuilder;
//1DA22CS029 C DHANUSH B SECTION
public class StringBuilderDelete {
//33)StringBuffer delete() method
public static void main(String[] args) {
StringBuilder sb=new StringBuilder("Dr Ambedkar IT");
System.out.println("Before deletion: "+sb);
sb.delete(4,12);
System.out.println("After deletion: "+sb);
}
}
package stringbuilder;
//1DA22CS029 C DHANUSH B SECTION
public class StringBuilderReverse {
//34)StringBuilder reverse() method
public static void main(String[] args) {
StringBuilder sb=new StringBuilder("Mississippi");
sb.reverse();
System.out.println(sb);
}
OUTPUT:
35. StringBuilder capacity( )
package stringbuilder;
//1DA22CS029 C DHANUSH B SECTION
public class StringBuilderCapacity {
//35)StringBuilder capacity()
public static void main(String[] args) {
StringBuilder sb=new StringBuilder();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
}
OUTPUT:
36. StringBuilder ensureCapacity( )
package stringbuilder;
//1DA22CS029 C DHANUSH B SECTION
public class StringBuilderEnsureCapacity {
//36)StringBuilder ensureCapacity()
public static void main(String[] args) {
StringBuilder sb=new StringBuilder();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//now (34*2)+2
System.out.println(sb.capacity());//now 70
}
}
OUTPUT:
Remote method Invocation
Design and implement a simple Client Server Application using RMI.
AddServerIntf.java
package remotemethodinvocation.RMI;
import java.rmi.*;
AddServerImpl.java
package remotemethodinvocation.RMI;
import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf {
private static final long serialVersionUID = 1L;
AddServer.java
package remotemethodinvocation.RMI;
import java.rmi.*;
public class AddServer {
public static void main(String[] args) {
try{
AddServerImpl server = new AddServerImpl();
Naming.rebind("registerme",server);
System.out.println("Server is running...");
} catch (Exception e) {
System.out.println(e);
}
}
}
AddClient.java
package remotemethodinvocation.RMI;
import java.rmi.*;
public class AddClient {
public static void main(String[] args) {
try{
AddServerIntf client = (AddServerIntf)Naming.lookup("registerme");
System.out.println("First number is :" + args[0]);
int x = Integer.parseInt(args[0]);
System.out.println("Second number is :" + args[1]);
int y = Integer.parseInt(args[1]);
System.out.println("Sum =" + client.add(x,y));
} catch (Exception e){
System.out.println(e);
}
}
}
OUTPUT:
Design and implement Client Server communication using socket
Programming
Client.java
try {
// Validate the arguments
if (args.length < 2) {
System.out.println("Usage: java Client <server_ip> <port>");
return;
}
} catch (Exception e) {
System.out.println("Error during communication with the server: " + e.getMessage());
} finally {
// Close the client socket if it was opened
try {
if (client != null) {
client.close();
}
} catch (IOException e) {
System.out.println("Failed to close the client socket: " + e.getMessage());
}
}
}
}
Server.java
OUTPUT:
Swing Example Programs
OUTPUT:
2. Event handling using swings
package swingDemo;
//1DA22CS029 C DHANUSH B SECTION
//Handle an event in a Swing program.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class EventDemo {
JLabel jlab;
EventDemo() {
//Create a new JFrame container.
JFrame jfrm = new JFrame("An Event Example");
//Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
//Give the frame an initial size.
jfrm.setSize(250,250);
//Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Make two buttons.
JButton jbtnAlpha = new JButton("Alpha");
JButton jbtnBeta = new JButton("Beta");
//Add action listener for Alpha.
jbtnAlpha.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jlab.setText("Alpha was pressed.");
}
});
// Add action listener for Beta.
jbtnBeta.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jlab.setText("Beta was pressed.");
}
});
// Add the buttons to the content pane.
jfrm.add(jbtnAlpha);
jfrm.add(jbtnBeta);
// Create a text-based label.
jlab = new JLabel("Press a button.");
// Add the label to the content pane.
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
public static void main(String args[]) {
// Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new EventDemo();
}
});
}
}
OUTPUT:
package swingDemo;
//1DA22CS029 C DHANUSH B SECTION
//A simple Swing-based applet
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
This HTML can be used to launch the applet:
<object code="MySwingApplet" width=220 height=90>
</object>
*/
public class MySwingApplet extends JApplet {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton jbtnAlpha;
JButton jbtnBeta;
JLabel jlab;
//Initialize the applet.
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
makeGUI(); // initialize the GUI
}
});
} catch(Exception exc) {
System.out.println("Can't create because of "+ exc);
}
}
//This applet does not need to override start(), stop(),
//or destroy().
//Set up and initialize the GUI.
private void makeGUI() {
//Set the applet to use flow layout.
setLayout(new FlowLayout());
//Make two buttons.
jbtnAlpha = new JButton("Alpha");
jbtnBeta = new JButton("Beta");
//Add action listener for Alpha.
jbtnAlpha.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Alpha was pressed.");
}
});
//Add action listener for Beta.
jbtnBeta.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Beta was pressed.");
}
});
//Add the buttons to the content pane.
add(jbtnAlpha);
add(jbtnBeta);
//Create a text-based label.
jlab = new JLabel("Press a button.");
//Add the label to the content pane.
add(jlab);
}
}
OUTPUT: