0% found this document useful (0 votes)
29 views

Practical No 8 to 14 Java

Uploaded by

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

Practical No 8 to 14 Java

Uploaded by

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

BCA - 4th (A) Roll No.

1321057

Practical No. 8
Aim: Write a program to demonstrate the use of stringtokenizer class.
Source Code:
import java.util.StringTokenizer;

public class stringtokenizer{

public static void main(String[] args) {

String text = "Hello, how are you doing today?";

StringTokenizer tokenizer = new StringTokenizer(text);

while (tokenizer.hasMoreTokens()) {

String token = tokenizer.nextToken();

System.out.println(token);

System.out.println("Hii, I am Badal");

String csvText = "badal,Singh,20";

StringTokenizer csvTokenizer = new StringTokenizer(csvText, ",");

while (csvTokenizer.hasMoreTokens()) {

String token = csvTokenizer.nextToken();

System.out.println(token);

}
BCA - 4th (A) Roll No. 1321057

Output:
BCA - 4th (A) Roll No. 1321057

Practical No. 9
Aim: Write a program to implements applet in java.
Source Code:
 HelloWorld.java
import java.applet.Applet;
import java.awt.Graphics;

// HelloWorld class extends Applet


public class HelloWorld extends
Applet
{
// Overriding paint() method
@Override
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);}}

 HelloWorld.html
<html>
<head>
<title>Applet in java</title>
</head>
<body>
<applet code="HelloWorld" width=200 height=60>
</applet>
</body>
</html>

Output:
BCA - 4th (A) Roll No. 1321057

Practical No. 10
Aim: Write a program to apply the concept of exception handling in classes.
Source Code:
import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

class ExceptionDemo {

public static void main(String[] args) {

try {

Scanner scanner = new Scanner(new File("nonexistentfile.txt"));

} catch (FileNotFoundException e) {

System.out.println("File not found: " + e.getMessage());

try {

int num1 =

10; int num2

= 0;

int result = num1 / num2;

} catch (ArithmeticException e) {

System.out.println("Cannot divide by zero");

try {

throw new CustomException("Custom exception thrown");

} catch (CustomException e) {
BCA - 4th (A) Roll No. 1321057

System.out.println(e.getMessage());

try {

int num = Integer.parseInt("abc");

} catch (NumberFormatException e) {

System.out.println("Invalid number format");

try {

int[] arr = new

int[5]; arr[10] = 10;

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Array index out of bounds");

} finally {

System.out.println("Finally block executed");

class CustomException extends Exception {

public CustomException(String message) {

super(message);

}
BCA - 4th (A) Roll No. 1321057

Output:
BCA - 4th (A) Roll No. 1321057

Practical No. 11
Aim: Write a program to create a frame, button, text box, and label.
Source Code:
import javax.swing.*;

import java.awt.*;

class registration {

public static void main(String[] args) {

JFrame frame = new JFrame("Frame with Fields");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 200);

JLabel firstNameLabel = new JLabel("First

Name:"); firstNameLabel.setBounds(20, 20, 80,

30);

JLabel lastNameLabel = new JLabel("Last

Name:"); lastNameLabel.setBounds(20, 60, 80,

30);

JTextField firstNameField = new JTextField();

firstNameField.setBounds(100, 20, 150, 30);

JTextField lastNameField = new

JTextField(); lastNameField.setBounds(100,

60, 150, 30);

JButton submitButton = new JButton("Submit");

submitButton.setBounds(100, 100, 80, 30);

frame.add(firstNameLabel);

frame.add(firstNameField);
BCA - 4th (A) Roll No. 1321057

frame.add(lastNameLabel);

frame.add(lastNameField);

frame.add(submitButton);

frame.setLayout(null);

frame.setVisible(true);

Output:
BCA - 4th (A) Roll No. 1321057

Practical No. 12
Aim: Write a program to create a button. When the button is click , an
appropriate message should be displayed in the dialog box.

Source Code:
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

class actionlistener {

public static void main(String[] args) {

JFrame frame = new JFrame("Frame with Fields");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 200);

JLabel firstNameLabel = new JLabel("First

Name:"); firstNameLabel.setBounds(20, 20, 80,

30);

JLabel lastNameLabel = new JLabel("Last

Name:"); lastNameLabel.setBounds(20, 60, 80,

30);

JTextField firstNameField = new JTextField();

firstNameField.setBounds(100, 20, 150, 30);

JTextField lastNameField = new JTextField();

lastNameField.setBounds(100, 60, 150, 30);

JButton submitButton = new JButton("Submit");

submitButton.setBounds(100, 100, 80, 30);


BCA - 4th (A) Roll No. 1321057

submitButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String firstName = firstNameField.getText();

String lastName = lastNameField.getText();

JOptionPane.showMessageDialog(null, "Hello, " + firstName + " " + lastName + "!");

});

frame.add(firstNameLabel);

frame.add(firstNameField);

frame.add(lastNameLabel);

frame.add(lastNameField);

frame.add(submitButton);

frame.setLayout(null);

frame.setVisible(true);

}
BCA - 4th (A) Roll No. 1321057

Output:
BCA - 4th (A) Roll No. 1321057

class copy{ // Copy Constructor


int a;

String b;

copy()

{ a=1

1;

b="badal";

System.out.println(a+""+b);

copy(copy ref) {

a=ref.a;

b=ref.b;

System.out.print(a+" "+b);

class copcons {

public static void main(String args[]) {

copy obj=new copy();

copy d=new copy(obj);

} }

Output:
BCA - 4th (A) Roll No. 1321057

//Default Constructor
class boy{

int a;

Float b;

boy() {

a=10;

void display() {

System.out.print(a+" "+b);

class decons{

public static void main(String args[]) {

boy obj=new boy();

obj.display();

Output:

You might also like