Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
148 views

JAVA Lab

The document describes a Java programming lab assignment to create classes for an educational institution's employee database. It includes classes for Typist, Teacher, and Office staff that extend an abstract StaffCodeName class. Methods are defined to set and retrieve data from the classes like name, speed, subject taught, publications, and grade. The hierarchy shows StaffCodeName as the parent class with Typist, Teacher and Office as subclasses, each requiring specific additional attributes as per the figure in the document.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

JAVA Lab

The document describes a Java programming lab assignment to create classes for an educational institution's employee database. It includes classes for Typist, Teacher, and Office staff that extend an abstract StaffCodeName class. Methods are defined to set and retrieve data from the classes like name, speed, subject taught, publications, and grade. The hierarchy shows StaffCodeName as the parent class with Typist, Teacher and Office as subclasses, each requiring specific additional attributes as per the figure in the document.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Java Programming Lab

PRACTICAL 1
1. Write a JAVA Program to make a calculator using switch case. You can have 5
operations +,-,/,*,%.

Source Code:

import java.util.Scanner;
class q1 {
public static void main(String[] args) {
char operator;
Double n1, n2, result;
Scanner input = new Scanner(System.in);
System.out.println("Choose an operator: +, -, *, or /");
operator = input.next().charAt(0);
System.out.println("Enter first number");
n1 = input.nextDouble();
System.out.println("Enter second number");
n2 = input.nextDouble();
switch (operator) {
case '+':
result = n1 + n2;
System.out.println(n1 + " + " + n2 + " = " + result);
break;
case '-':
result = n1 - n2;
System.out.println(n1 + " - " + n2 + " = " + result);
break;
case '*':
result = n1 * n2;
System.out.println(n1 + " * " + n2 + " = " + result);
break;
case '/':
result = n1 / n2;
System.out.println(n1 + " / " + n2 + " = " + result);
break;
default:
System.out.println("Invalid operator!");
break;
}
input.close();
}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

2. Write a JAVA Program to print Floyd's Triangle.

Source Code:

import java.util.Scanner;
public class Floyd {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of row to print");
int rows = sc.nextInt();
int floydNumber=1;
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(floydNumber+" ");
floydNumber = floydNumber + 1;
}
System.out.println();
}
}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

PRACTICAL 2
3. Given a list of marks ranging from 0 to 100. Write a JAVA Program to compute and
print the number of students who have obtained marks
a. in the range of 81-100
b. in the range of 61-80
c. in the range of 41-60
d. in the range of 0-40.
The program should use a minimum number of if statements.

Source Code:

import java.util.*;
public class q3
{
public static ArrayList<Integer> marksList = new ArrayList<Integer>();
public static void main(String[] args)
{
marks();
int first=0;
int second=0;
int third=0;
int fourth=0;
for(int mark : marksList)
{
if(mark>=81 && mark<=100)
{
first++;
}
else if(mark>=61 && mark<=80)
{
second++;
}
else if(mark>=41 && mark<=60)
{
third++;
}
else if(mark <=40)
{
fourth++;
}
}
System.out.println("No of student in the range 81 to 100:"+first);
System.out.println("No of student in the range 61 to 80:"+second);
System.out.println("No of student in the range 41 to 60:"+third);
System.out.println("No of student in the range 0 to 40:"+fourth);
Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K
Java Programming Lab

public static void marks()


{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Marks of " + (marksList.size() + 1) + " Student.");
int marks = sc.nextInt();
marksList.add(marks);
System.out.print("Do you want to take marks of another Student(Y/N)");
sc = new Scanner(System.in);
char c = sc.nextLine().toUpperCase().toCharArray()[0];
if (c == 'Y')
{
marks();
}
}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

4. Write a JAVA Program to implement various string functions.

Source Code:

import java.util.*;
public class StringFunction {
public static void main(String[] args)
{
String first="",second="";
Scanner c=new Scanner(System.in);
System.out.println("String Functions");
System.out.println();
System.out.print("Enter the first Sting: ");
first=c.nextLine();
System.out.print("Enter the second Sting: ");
second=c.nextLine();
System.out.println("The strings are: "+first+" , "+second);
System.out.println("The length of the first string is :"+first.length());
System.out.println("The length of the second string is :"+second.length());
System.out.println("The concatenation of first and second string is :"+first.concat("
"+second));
System.out.println("The first character of " +first+" is: "+first.charAt(0));
System.out.println("The uppercase of " +first+" is: "+first.toUpperCase());
System.out.println("The lowercase of " +first+" is: "+first.toLowerCase());
System.out.print("Enter the occurance of a character in "+first+" : ");
String str=c.next();
char b=str.charAt(0);
System.out.println("The "+c+" occurs at position " + first.indexOf(b)+ " in " + first);
System.out.println("The substring of "+first+" starting from index 3 and ending at 6 is: " +
first.substring(3,7));
System.out.println("Replacing 'a' with 'o' in "+first+" is: "+first.replace('a','o'));
boolean check=first.equals(second);
if(!check)
System.out.println(first + " and " + second + " are not same.");
else
System.out.println(first + " and " + second + " are same.");
}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

PRACTICAL 3
5. Write a JAVA Program to compute power of 2 using for loop.

Source Code:

import java.util.Scanner;
public class Power {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int base = 2;
int exponent;
System.out.println("Enter the value of exponent.");
exponent = sc.nextInt();
long result = 1;
for (; exponent != 0; --exponent) {
result *= base;
}

System.out.println("Answer = " + result);

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

6. An educational institution wishes to maintain a database of its employees. The


database is divided into a number of classes whose hierarchical relationships are
shown in below figure. The figure also shows the minimum information required
for each class. Specify all the classes and define methods to create the database and
retrieve individual information as and when required.

Source Code:

import java.util.Scanner;

class staffCodeName{
private
String name;
public
Scanner sc = new Scanner(System.in);
void setname()
{
System.out.println("enter the name of user");
name = sc.nextLine();
}
void getname()
{
System.out.println("the name of staff memeber is : "+name);
}
}

class typist extends staffCodeName


{
int speed;
void setspeed()
{
System.out.println("enter the typist speed");
speed = sc.nextInt();
}
Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K
Java Programming Lab

void getspeed()
{
System.out.println("the speed of typist is : "+speed);
}
}

class teacher extends staffCodeName


{
String subject, publication;
void setteacher()
{
System.out.println("enter the name of subject");
subject= sc.nextLine();
System.out.println("enter the name of publucation");
publication = sc.nextLine();
}
void getteacher()
{
System.out.println("the name of subject is "+subject);
System.out.println("the name of publication is "+publication);
}
}

class office extends staffCodeName{


char grade;
void setgrade()
{
System.out.println("enter the grade");
grade = sc.next().charAt(0);
}
void getgrade()
{
System.out.println("the grade is "+ grade);
}
}

class regular extends typist


{
int salary;
void setsalary()
{
System.out.println("enter the salary on regular basis ");
salary = sc.nextInt();
}
void getsalary()
{
Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K
Java Programming Lab

System.out.println("the salary is :"+ salary);


}
}

class casual extends typist


{
int salary;
void setsalary()
{
System.out.println("enter the salalry in daily wages ");
salary = sc.nextInt();
}
void getsalary()
{
System.out.println("the salary on daily wagis is :"+ salary);
}
}

public class Practice


{
public static void main(String args[])
{
regular reg = new regular();
reg.setname();
reg.setsalary();
reg.setspeed();
reg.getname();
reg.getsalary();
reg.getspeed();

}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

PRACTICAL 4
7. Design a class to represent a bank account. Include the following members-

Data Members-
*Name of the Depositor
*Type of account
*Account Number
*Balance amount in the account

Methods-
*To assign initial values.
*To deposit an amount
*To withdraw an amount after checking balance.
*To display the name and balance.

Source code:

import java.io.*;
import java.util.*;
import java.util.Scanner;
import java.util.Random;

class Bank
{
public String nameOfDepositor;
public int accNumber;
public String accType;
public double balanceAmount;

public void assignValues(String nameOfDepositor, String accType, double balanceAmount)


{
this.nameOfDepositor=nameOfDepositor;
this.accType=accType;

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

this.balanceAmount=balanceAmount;

Random random = new Random();


this.accNumber=random.nextInt(1000000);
System.out.println("Your new account number is: "+accNumber);
}
public void depositAmount(double amount)
{
if(accNumber==0)
System.out.println("!You don't have bank account to deposit\nNote:please assign values to create
an account");
else
{
balanceAmount+=amount;
System.out.println("Amount deposited successfully...");
}
}
public void withdrawAmount(double amount)
{
if(accNumber==0)
System.out.println("!You don't have bank account to credit\nNote:please assign values to create
an account");
else if(balanceAmount>amount)
{
balanceAmount-=amount;
System.out.println("Amount credited successfully...");
}
else
System.out.println("! Insufficient balance");
}
public void displayDetails()
{
Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K
Java Programming Lab

if(accNumber==0)
System.out.println("!You don't have bank account\nNote:please assign values to create an
account");
else
{
System.out.println("Name of the Depositor: "+nameOfDepositor);
System.out.println("Balance amount in the account: "+balanceAmount);
}
}
public void getInput()
{

System.out.println("1. Open account");


System.out.println("2. Deposit amount");
System.out.println("3. Withdraw amount");
System.out.println("4. Account details");
System.out.println("5. Exit");
System.out.print("Please choose from above: ");
}
}

class Main
{
public static void main(String[] s) throws IOException
{
Bank newAccount=new Bank();

Scanner scan=new Scanner(System.in);


boolean process=true;
int continueState=0;

while(continueState==0)
Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K
Java Programming Lab

{
newAccount.getInput();
int currentProcess=scan.nextInt();

if(currentProcess==1)
{
System.out.print("Enter your name: ");
String nameOfDepositor=scan.next();
System.out.print("Enter your account type: ");
String accType=scan.next();
System.out.print("Enter your opening balance: ");
double balanceAmount=scan.nextDouble();
newAccount.assignValues(nameOfDepositor, accType, balanceAmount);
}
else if(currentProcess==2)
{
System.out.print("Enter amount to deposit: ");
newAccount.depositAmount(scan.nextDouble());
}
else if(currentProcess==3)
{
System.out.print("Enter amount to withdraw: ");
newAccount.withdrawAmount(scan.nextDouble());
}
else if(currentProcess==4)
{
newAccount.displayDetails();
}
else if(currentProcess==5)
{
continueState=1;

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

System.out.println("THANK YOU");
}

System.out.print ("press 0 to continue... ");


continueState=scan.nextInt();
}
}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

8. Write a JAVA Program to implement method overloading in function calc().

Source Code:

public class Overloading


{
public static void main(String []args)
{
Overloading a=new Overloading();
System.out.println(a.calc(25));
System.out.println(a.calc("26","24"));
System.out.println(a.calc(45,8888888888.0));
System.out.println(a.calc(25,48));
}
public int calc(int a)
{
System.out.println("calc(int a) is called");
return a+1;
}
public int calc(String a,String b)
{
System.out.println("calc(String a, String b) is called");
return Integer.parseInt(a)+Integer.parseInt(b);
}
public Double calc(int a,double b)
{
System.out.println("calc(int a,Double b) is called");
return a+b;
}
public Float calc(int a,float b)
{
System.out.println("calc(int a,Float b) is called");
return a+b;
}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

PRACTICAL 5
9. Create a class “Student” having following instance variables and methods.
Instance variables: ID, Name, Branch, city and university

While creating constructors with one, two, three, four and five arguments reuse the
constructors by construction chaining.

Source Code:

import java.util.Scanner;
class Student
{
private int id;
private String name,branch,city,university;

public Student(int id,String name,String city,String branch)


{
this.id=id;
this.name=name;
this.branch=branch;
this.city=city;
this.university="Graphic Era Hill University";
}
public Student(int id,String name,String city,String branch,String university)
{
this(id,name,city,branch);
this.university=university;
}
public int getid()
{
return this.id;
}
public String getname()
{
return this.name;
}
public String getcity()
{
return this.city;
}
public String getbranch()
{
return this.branch;
}
public String getunivesity()
{
Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K
Java Programming Lab

return this.university;
}
}
public class Student_Overloading
{
public static void main(String[] args)
{
Scanner sc1=new Scanner(System.in);
Scanner sc2=new Scanner(System.in);
Scanner sc3=new Scanner(System.in);
Scanner sc4=new Scanner(System.in);
Scanner sc5=new Scanner(System.in);
Scanner sc6=new Scanner(System.in);

System.out.println("Enter the Student Id :");


int id= sc1.nextInt();
System.out.println("Enter the Student Name :");
String name=sc2.nextLine();
System.out.println("Enter the Student City :");
String city=sc3.nextLine();
System.out.println("Enter the Student Branch :");
String branch=sc4.nextLine();
while(true)
{
System.out.println("Does you belong to Graphic Era Hill University [Y/N]]:");
String answer=sc5.nextLine();

if(answer.equalsIgnoreCase("Y"))
{
Student s=new Student(id, name, city, branch);
System.out.println(" Id :"+s.getid());
System.out.println(" Student Name :"+s.getname());
System.out.println(" Student City :"+s.getcity());
System.out.println(" Student Branch :"+s.getbranch());
break;
}
else if(answer.equalsIgnoreCase("N"))
{
System.out.println("Enter the College Name :");
String c_name=sc6.nextLine();
Student s=new Student(id, name, city, branch, c_name);
System.out.println(" Student Id :"+s.getid());
System.out.println(" Student Name :"+s.getname());
System.out.println(" Student City :"+s.getcity());
System.out.println(" Student Branch :"+s.getbranch());
System.out.println(" Student University :"+s.getunivesity());
break;
}
Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K
Java Programming Lab

else
{
System.out.println("<<<<<.WRONG INPUT.>>>>>");
}
}
}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

10.Create a class “Shape” having area() method to calculate area. Overload the area()
method for shapes like triangle, rectangle and circle.
Source Code:

import java.io.*;
class Calculate {
static final double PI = Math.PI;
void Area(float a)
{
float A = a * a;
System.out.println("Area of the Square: " + A);
}
void Area(double a)
{
double A = PI * a * a;
System.out.println("Area of the Circle: " + A);
}
void Area(int a, int b)
{
int A = a * b;
System.out.println("Area of the Rectangle: " + A);
}
}

class Shape {

public static void main(String[] args)


{

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

Calculate obj = new Calculate();


obj.Area(10.5);
obj.Area(3);
obj.Area(5, 4);
}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

PRACTICAL 6

11. Create two dimensional integer array and insert, search and traverse this array.
Note: Use Scanner class to insert data.

Source Code:

import java.util.Scanner;
public class Carr {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int a,b;
System.out.println("Enter the size of array.");
a=sc.nextInt();
b=sc.nextInt();
int arr[][] = new int[a][b];
System.out.println("Enter Elements in array.");
int i,j,k;
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
arr[i][j] = sc.nextInt();
}
}
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
System.out.print(arr[i][j] + " ");

}
System.out.println();
}
System.out.println("Enter a integer value to search.");
k=sc.nextInt();
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
if(k==arr[i][j])
{
System.out.println("The element is found at index:"+i +j);
return;
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

}
}
System.out.println("Element not found.");
}
}

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

12.A cloth showroom has announced the following seasonal discounts on purchase of
items:

Discount
Purchase Mill Handloom
Amount Cloth Items
1-100 --- 5%
101-200 5% 7.50%
210-300 7.50% 10%
Above 300 10% 15%

Write a program using switch and if statements to compute the net amount to
be paid by a customer.
Source Code:

import java.util.*;

public class Discount

public static void main(String[] args)

Scanner scan=new Scanner(System.in);

System.out.println("Enter the item type(M/H)");

char c=scan.nextLine().toUpperCase().toCharArray()[0];

System.out.print("Enter the cost:");

double cost=scan.nextDouble();

double dis=discount(c,cost);

double netAmt=cost-dis;

System.out.println("The net paid amount="+netAmt);

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

public static double discount(char typ, double cost)

double dis = 0;

double rate = 0;

switch (typ)

case 'M':

if (cost > 100 && cost <= 200)

rate = 5;

else if (cost > 200 && cost <= 300)

rate = 7.5;

else if (cost > 300)

rate = 10;

break;

case 'H':

if (cost > 0 && cost <= 100)

rate = 5;

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

else if (cost > 100 && cost <= 200)

rate = 7.5;

else if (cost > 200 && cost <= 300)

rate = 10;

else if (cost > 300)

rate = 15;

break;

dis = cost * rate / 100;

return dis;

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:

Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

PRACTICAL 7
13.Create a Java program to perform survey on four different model of Maruti (Maruti
-K10, Zen-Astelo, Wagnor, Maruti- SX4) owned by person living in four metro
cities(Delhi, Mumbai, Chennai & Kolkatta). Display tabulated report like format
given below:

Maruti-K10 Zen-Astelo Wagnor Maruti-SX4


Delhi
Mumbai
Chennai
Kolkatta

Calculate numbers of cars of different model in each metro city.

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

14.Write a Java program to find the duplicate characters in the given paragraph
using either String or StringBuffer class methods.

Sample Output:

The given String is : Graphic Era


After removing duplicates characters the new string is : GraphicE

Source Code:

import java.util.*;
public class Duplicate
{
public static String unique(String s)
{
String str = new String();
int len = s.length();
for (int i = 0; i < len; i++)
{
char c = s.charAt(i);
if (str.indexOf(c) < 0)
{
str += c;
}
}
System.out.println("String after Removing Duplicate :");
return str;
}
public static void main(String[] args)
{
String s =" ";
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String :");
s=sc.nextLine();
System.out.println(unique(s));
}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

PRACTICAL 8
15. Write a Java program implementing students of marks using Inheritance.

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

16.Write a Java program to count the occurrences of a given string in another given
string.
Sample Output:
aa' has occured 3 times in 'abcd abc aabc baa abcaa'
Source Code:

import java.util.Arrays;
import java.util.Scanner;
public class string {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String :");
String m_string =sc.nextLine();
System.out.println("Enter the sub String to Search :");
String s_string = sc.nextLine();
int countV1 = count_sub_str_in_main_str(m_string, s_string);
System.out.println(s_string + "' has occured " + countV1 + " times in '" + m_string + "'");
}
public static int count_sub_str_in_main_str(String m_string, String s_string) {
if (m_string == null || s_string == null) {
throw new IllegalArgumentException("The given strings cannot be null");
}
if (m_string.isEmpty() || s_string.isEmpty()) {
return 0;
}
int position = 0;
int ctr = 0;
int n = s_string.length();
while ((position = m_string.indexOf(s_string, position)) != -1) {
position = position + n;
ctr++;

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

}
return ctr;
}
}

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

17. Define an exception called “NoMatchException” that is thrown when a string is not
equal to “India”. Write a program in Java that uses this exception.

Source Code:

import java.util.Scanner;
class NoMatchException extends Exception{
String s1;
NoMatchException(String s2)
{
this.s1=s2;
}

public String toString()


{
return s1;
}

public static void main(String[] args) {


String s3;
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a string");
s3=sc.nextLine();

try {
if(!"india".equalsIgnoreCase(s3))
throw new NoMatchException("NoMatchException caught!!!");

else {
System.out.println("String matched!!!");
}
}

catch(NoMatchException e) {
System.out.println(e);
}
}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

18. Apply following functions on StringBuffer object "HELLO"


(i) Append "Java"
(ii) Insert "Java" at index 1
(iii) Replace with "Java" with characters between index 1 to 2
(iv) Delete characters between index 1 and 2
(v) Reverse the string "HELLO"

Source Code:

class stringbuffer{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
//APPEND
sb.append(" Java");
System.out.println(sb);
//INSERT AT INDEX 1
sb.insert(1," Java");
System.out.println(sb);
//REPLACE AT INDEX
sb.replace(1,3," Java");
System.out.println(sb);
//DELETE CHAR BETWEEN INDEX
sb.delete(1,3);
System.out.println(sb);
//REVERSE OF STRING
sb.reverse();
System.out.println(sb);
}
}

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K


Java Programming Lab

19. Create a jagged array having three rows. Where 1st row contains 3 columns, 2nd row contains 4
columns and 3rd row contains 2 columns. Insert and traverse it.

Source Code:

public class jagged {


public static void main(String[] args)
{
int arr[][]=new int[3][];
arr[0]=new int[]{1,2,3};
arr[1]=new int[]{4,5,6,7};
arr[2]=new int[]{8,9};
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[i].length;j++)
System.out.print(arr[i][j]+"");
System.out.println(" ");
}
}
}

OUTPUT:
Screenshot

Shashank Garg/BTECH CSE/20011190/SEM-IV/SECTION-K

You might also like