java prictical for read and zerox
java prictical for read and zerox
import java.util.Random;
public class RandomNumbers
{
public static void main(String[] args)
{
Random random = new Random();
int num = random.nextInt(50) + 1;
System.out.println("Random number: " + num);
if (num < 20) System.out.println("Less than 20");
else System.out.println("20 or more");
}
}
Out put :
Random number: 12
Less than 20
5. Write a program to do string manipulation using
character array and perform the following string
operations
a) String length b) Finding a character at a particular
position
c) concatenating two strings
Length: 5
Character at index 2: l
Concatenation: Hello World
6. Writing a program to perform a following a string
operation using string class:
a) String concatenation
b) to extract Substring from the given string
Java is fun
Programming
7) write a program to perform the following the string
operations using string buffer class:
a) Length of a string
b) Reverse a string
c) Delete a substring from the given String
Length: 5
Reverse: olleH
Delete: oeH
8. write a java program that implements a multi-threading
application that had three threads, first thread generates
random integer every I second and if the value is even,
second thread computes the square of the number and
prints. If the value is odd, the third thread will print the
value of the cube of the number.
import java.util.Random;
class RandomThread extends Thread {
public static volatile int num;
public void run() {
Random r = new Random();
while (true) {
num = r.nextInt(50) + 1;
System.out.println("Generated: " + num);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class SquareThread extends Thread {
public void run() {
while (true) {
synchronized (RandomThread.class) {
if (RandomThread.num % 2 == 0) {
System.out.println("Square: " + (RandomThread.num *
RandomThread.num));
}
}
}
}
}
t1.start();
t2.start();
}
}
Output
1
2
3
4
5
6
7
8
90
91
92
93
94
95
96
97
98
99
100
10. Write a program to demonstrate the use of following
exceptions.
a) Arithmetic Exception
b) Number Format Exception
c) Array Index Out of Bound Exception
d) Negative Array Size Exception
Try
{
int[] arr = new int[5];
System.out.println(arr[10]);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index Out Of Bounds Exception
occurred");
}
Try
{
int[] arr = new int[-5];
}
catch (NegativeArraySizeException e)
{
System.out.println("Negative Array Size Exception
occurred");
}
}
}
Output:
Java exception
Arithmetic exception occurred
Number format exception occurred
Array index out of boundaries Exception occurred
Negative array size exceptions occurred
11. Write a Java program that reads on file name from the
user, then displays information about whether the file
exists, whether the file is readable, whether the file is
writable, the type of file and the length of the file in
bytes?
import java.io.File;
import java.util.Scanner;
public class FileInfo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter file name: ");
String fileName = sc.nextLine();
File file = new File(fileName);
if (file.exists())
{
System.out.println("File exists");
System.out.println("Readable: " + file.canRead());
System.out.println("Writable: " + file.canWrite());
System.out.println("File type: " + (file.isDirectory() ?
"Directory" : "File"));
System.out.println("File size: " + file.length() + " bytes");
} else
{
System.out.println("File does not exist");
}
}
}
Output:
Enter the file name :
Sample.txt
Readable: true
Writable: true
File type: File
File Size: 74 bites
12. Write a program to accept a text and change its size
and font. Include bold italic options. Use frames and
controls.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class TextStyleShort
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Text Styler");
JTextField textField = new JTextField("Sample Text");
JCheckBox bold = new JCheckBox("Bold"), italic = new
JCheckBox("Italic");
JButton apply = new JButton("Apply");
frame.setLayout(new GridLayout(3, 1));
frame.add(textField);
frame.add(bold);
frame.add(italic);
frame.add(apply);
apply.addActionListener(e -> textField.setFont(new
Font("Arial", (bold.isSelected() ? Font.BOLD : 0) +
(italic.isSelected() ? Font.ITALIC : 0), 20)));
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Output:
13. Write a Java program that handles all mouse events and
shows the event name at the center of the window when a
mouse event is fired. (Use adapter classes).
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public MouseEventsExample()
{
label = new JLabel("", SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 24));
add(label);
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e) {
label.setText("Mouse Clicked"); }
public void mouseEntered(MouseEvent e) {
label.setText("Mouse Entered"); }
public void mouseExited(MouseEvent e) {
label.setText("Mouse Exited"); }
});
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
String[] buttons = {
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"C", "0", "=", "/"
};
frame.setVisible(true);
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;