0% found this document useful (0 votes)
3 views5 pages

Part-B-Java Lab Programs-1 To 5

The document contains multiple Java programs demonstrating various concepts, including string manipulation, exception handling, user-defined exceptions, GUI creation, and drawing shapes. Each part provides a complete Java class with methods to showcase specific functionalities, such as handling built-in exceptions and creating a message window using Swing. Additionally, it explains the differences between the 'java' and 'javax' packages in Java.

Uploaded by

kappekariya
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)
3 views5 pages

Part-B-Java Lab Programs-1 To 5

The document contains multiple Java programs demonstrating various concepts, including string manipulation, exception handling, user-defined exceptions, GUI creation, and drawing shapes. Each part provides a complete Java class with methods to showcase specific functionalities, such as handling built-in exceptions and creating a message window using Swing. Additionally, it explains the differences between the 'java' and 'javax' packages in Java.

Uploaded by

kappekariya
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
You are on page 1/ 5

/*Part-B-1.

Write a Java Program to demonstrate String Methods


used for manipulating strings like accessing, inserting, modifying and
appending*/
//File Name: StringDemo.java
import java.lang.String;
public class StringDemo
{
public static void main(String[] args)
{
String str = "Java is Good,";

char ch = str.charAt(8);
System.out.println("Character at index 8: " + ch);

StringBuffer sb = new StringBuffer(str);


sb.insert(8, "very ");
System.out.println("After insertion: " + sb.toString());

String str1 = sb.toString().replace("Good", "Best");


System.out.println("After modification: " + str1);

str1 = str1.concat(" Enjoy coding!");


System.out.println("After appending: " + str1);
}
}
/*Part-B-2. Java program to for the creation of Java Bulit-on exceptions*/
//File Name : ExceptionDemo.java
public class ExceptionDemo
{
public static void main(String[] args)
{
// 1. ArithmeticException
try {
int result = 10 / 0; // Division by zero
} catch (ArithmeticException e) {
System.out.println("Caught ArithmeticException: " + e.getMessage());
}

// 2. ArrayIndexOutOfBoundsException
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]); // Invalid index
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught ArrayIndexOutOfBoundsException: " +
e.getMessage());
}

// 3. NullPointerException
try {
String text = null;
System.out.println(text.length()); // Calling method on null
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException: " + e.getMessage());
}

// 4. NumberFormatException
try {
String invalidNumber = "abc";
int num = Integer.parseInt(invalidNumber); // Invalid parsing
} catch (NumberFormatException e) {
System.out.println("Caught NumberFormatException: " +
e.getMessage());
}
System.out.println("Program continues after handling exceptions.");
}
}
// Part-B-3.Write a Java Program for creation of User Defined Exceptions.
//FileName:UserException.java
import java.io.*;
import java.util.*;
class InvalidAgeException extends Exception
{
public InvalidAgeException(String message)
{
super(message);
}
}
// Class to demonstrate user-defined exception
public class UserException
{

// Method to check age


static void validateAge(int age) throws InvalidAgeException
{
if (age < 18)
{
throw new InvalidAgeException("Age must be 18 or above to
proceed.");
} else {
System.out.println("Age is valid. You may proceed.");
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the your age");
int age=sc.nextInt();
try {
validateAge(age); // This will trigger the exception
} catch (InvalidAgeException e) {
System.out.println("Caught Exception: " + e.getMessage());
}
}
}
/*Part-B-4. Java Program which creates and displays a message on the
window*/
//File Name: MessageWindow.java
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MessageWindow {


public static void main(String[] args) {
// Create a new frame
JFrame frame = new JFrame("Message Window");

// Create a label to display the message


JLabel label = new JLabel("Hello, welcome to Java Swings!",
JLabel.CENTER);

// Set frame properties


frame.add(label);
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

javax is a package namespace in Java that contains extended functionalities beyond the
core java package.

Initially, javax was used for Java extensions, but over time, many important libraries
remained under javax instead of being moved to java.

Key Differences Between java and javax:

• java Package: Contains core Java classes like java.lang, java.util, java.io, etc.

• javax Package: Includes additional libraries such as javax.swing (GUI components),


javax.servlet (web applications), and javax.tools (compiler tools).
//Part-B-5.Java Program to draw several shapes in the created window*/
//FileName: DrawShapes.java
import javax.swing.*;
import java.awt.*;
class ShapePanel extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// Set the color and draw a rectangle
g.setColor(Color.RED);
g.fillRect(50, 50, 100, 60);
// Set the color and draw an oval
g.setColor(Color.BLUE);
g.fillOval(200, 50, 100, 60);
// Set the color and draw a line
g.setColor(Color.GREEN);
g.drawLine(50, 150, 300, 150);
// Set the color and draw a triangle
g.setColor(Color.ORANGE);
int[] xPoints = {150, 180, 120};
int[] yPoints = {200, 250, 250};
g.fillPolygon(xPoints, yPoints, 3);
}
}

public class DrawShapes


{
public static void main(String[] args)
{
// Create a frame
JFrame frame = new JFrame("Shapes Window");
// Set up the panel with custom drawings
ShapePanel panel = new ShapePanel();
frame.add(panel);
// Set frame properties
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy