[Go to site: main page, start]

0% found this document useful (0 votes)
6 views4 pages

Java Practical Programs-3

The document contains various Java programming examples demonstrating fundamental concepts such as finding the largest element in an array, reversing a string, checking for palindromes, creating AWT frames and buttons, handling sockets for client-server communication, file handling, multithreading, JDBC connection, and exception handling. Each example is presented as a separate class with a main method. The code snippets illustrate basic operations and functionalities in Java.

Uploaded by

shsbdhabsnbd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Java Practical Programs-3

The document contains various Java programming examples demonstrating fundamental concepts such as finding the largest element in an array, reversing a string, checking for palindromes, creating AWT frames and buttons, handling sockets for client-server communication, file handling, multithreading, JDBC connection, and exception handling. Each example is presented as a separate class with a main method. The code snippets illustrate basic operations and functionalities in Java.

Uploaded by

shsbdhabsnbd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Array – Largest Element


class ArrayLargest {
public static void main(String[] args) {
int a[] = {10, 45, 20, 5};
int max = a[0];
for(int i = 1; i < [Link]; i++) {
if(a[i] > max)
max = a[i];
}
[Link]("Largest element = " + max);
}
}

2. String – Reverse
class StringReverse {
public static void main(String[] args) {
String s = "JAVA";
String rev = "";
for(int i = [Link]()-1; i >= 0; i--) {
rev = rev + [Link](i);
}
[Link]("Reversed String = " + rev);
}
}

3. String – Palindrome
class StringPalindrome {
public static void main(String[] args) {
String s = "MADAM";
String rev = "";
for(int i = [Link]()-1; i >= 0; i--)
rev += [Link](i);

if([Link](rev))
[Link]("Palindrome String");
else
[Link]("Not Palindrome");
}
}

4. AWT – Simple Frame


import [Link].*;

class MyFrame {
MyFrame() {
Frame f = new Frame("My AWT Frame");
[Link](300, 200);
[Link](true);
}
public static void main(String[] args) {
new MyFrame();
}
}

5. AWT – Button Event


import [Link].*;
import [Link].*;

class ButtonEvent extends Frame implements ActionListener {


Button b;
ButtonEvent() {
b = new Button("Click Me");
[Link](this);
add(b);
setSize(300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked");
}
public static void main(String[] args) {
new ButtonEvent();
}
}

6. Applet – Hello Applet


import [Link].*;
import [Link].*;

public class HelloApplet extends Applet {


public void paint(Graphics g) {
[Link]("Hello Applet", 50, 50);
}
}

7. Applet – Button Applet


import [Link].*;
import [Link].*;

public class ButtonApplet extends Applet {


Button b;
public void init() {
b = new Button("Click");
add(b);
}
}

8. Socket – Client
import [Link].*;
import [Link].*;

class MyClient {
public static void main(String args[]) throws Exception {
Socket s = new Socket("localhost", 3333);
DataOutputStream dout = new DataOutputStream([Link]());
[Link]("Hello Server");
[Link]();
[Link]();
[Link]();
}
}
9. Socket – Server
import [Link].*;
import [Link].*;

class MyServer {
public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(3333);
Socket s = [Link]();
DataInputStream din = new DataInputStream([Link]());
String str = [Link]();
[Link]("Message = " + str);
[Link]();
}
}

10. File Handling – Write


import [Link].*;

class FileWrite {
public static void main(String[] args) throws Exception {
FileOutputStream fout = new FileOutputStream("[Link]");
String s = "Java File Handling";
byte b[] = [Link]();
[Link](b);
[Link]();
[Link]("Data written successfully");
}
}

11. File Handling – Read


import [Link].*;

class FileRead {
public static void main(String[] args) throws Exception {
FileInputStream fin = new FileInputStream("[Link]");
int i;
while((i = [Link]()) != -1) {
[Link]((char)i);
}
[Link]();
}
}

12. Multithreading – Thread


class MyThread extends Thread {
public void run() {
[Link]("Thread is running");
}
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}

13. Multithreading – Runnable


class MyRunnable implements Runnable {
public void run() {
[Link]("Runnable thread running");
}
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
[Link]();
}
}

14. JDBC – Connection


import [Link].*;

class JdbcDemo {
public static void main(String[] args) {
try {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/test","root","password");
[Link]("Connection successful");
[Link]();
} catch(Exception e) {
[Link](e);
}
}
}

15. Exception Handling


class MultipleCatch {
public static void main(String[] args) {
try {
int a = 10/0;
} catch(ArithmeticException e) {
[Link]("Arithmetic Exception");
} catch(Exception e) {
[Link]("General Exception");
}
}
}

You might also like