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

Sahil

The document discusses string handling in Java. It explains that strings are immutable objects backed by character arrays. It provides examples of string handling functions like charAt(), length(), toUpperCase(), etc. The source code demonstrates: 1. Checking the capacity of a StringBuffer object 2. Reversing a user-input string and converting it to uppercase 3. Appending the reversed-uppercase string to another string entered by the user.

Uploaded by

Vn196622
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)
47 views

Sahil

The document discusses string handling in Java. It explains that strings are immutable objects backed by character arrays. It provides examples of string handling functions like charAt(), length(), toUpperCase(), etc. The source code demonstrates: 1. Checking the capacity of a StringBuffer object 2. Reversing a user-input string and converting it to uppercase 3. Appending the reversed-uppercase string to another string entered by the user.

Uploaded by

Vn196622
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/ 10

Java Programming Lab

NAME : SAHIL DATE:05/08/2022


Program No:01 USN : 4SN21MC039
Program Statement : Write a java program to demonstrate constructor
overloading and method overloading.

Constructor Overloading :
The constructor overloading can be defined as the concept of having more than one

constructor with different parameters so that every constructor can perform a different
task.
• Java Constructor overloading is a technique in which a class can have any number of
constructors that differ in parameter list.
Example :

class Main {
private String name;

// constructor
Main() {
System.out.println("Constructor Called:");
name = "Programiz";
}

public static void main(String[] args) {

// constructor is invoked while


// creating an object of the Main class
Main obj = new Main();
System.out.println("The name is " + obj.name);
}
}

Method Overloading :
• In Java, two or more methods may have the same name if they differ in parameters
(different number of parameters, different types of parameters, or both). These methods
are called overloaded methods and this feature is called method overloading.
• Method overloading is one of the ways through which java supports
polymorphism. Polymorphism is a concept of object oriented programming that deal
with multiple forms.

Example :

class MethodOverloading {
private static void display(int a){
System.out.println("Arguments: " + a);
}
Java Programming Lab

private static void display(int a, int b){


System.out.println("Arguments: " + a + " and " + b);
}

public static void main(String[] args) {


display(1);
display(1, 4);
}
}
SOURCE CODE

import java.io.*;

import java.lang.*;

import java.util.Scanner;

class ConstOver

private static int a;

private static int b;

private static int c;

private static String s1;

int x=0,y=0,z=0;

String str="";

Scanner in=new Scanner(System.in);

ConstOver()

System.out.println("This is empty constructor");

ConstOver(int a,int b)

System.out.println("\nEnter two numbers:");

a=in.nextInt();

b=in.nextInt();
Java Programming Lab

x=a;

y=b;

ConstOver(int a,int b,int c)

System.out.println("\nEnter three numbers:");

int a1=in.nextInt();

int b1=in.nextInt();

int c1=in.nextInt();

x=a1;

y=b1;

z=c1;

System.out.println("Inputs are: "+x+" and "+y+" and "+z);

void display()

System.out.println("The inputs are: "+x+" and "+y);

void display(int a)

System.out.println("Enter a number:");

int a1=in.nextInt();

x=a1;

System.out.println("The input square is:"+x*x);

void display(String s)

{
Java Programming Lab

System.out.println("Enter a string:");

String s1=in.nextLine();

System.out.println("The input string is:"+s1);

public static void main(String args[])

ConstOver c1=new ConstOver();

ConstOver c2=new ConstOver(a,b);

ConstOver c3=new ConstOver(a,b,c);

c2.display();

c1.display(s1);

c3.display(a);

OUTPUT :
Documents/2semjava$ javac ConstOver.java
Documents/2semjava$ java ConstOver
This is empty constructor

Enter two numbers:


58

Enter three numbers:


695
Inputs are: 6 and 9 and 5
The inputs are: 5 and 8
Enter a string:
sahil
The input string is:sahil
Enter a number:
4
The input square is:16
JavaProgrammingLab

NAME : SAHIL DATE:05/08/2022


Program No:02 USN : 4SN21MC039
Program Statement: Write a java program to implement Innerclass and
demonstrate its access protection.

Inner class(Nested class) :


 Nested class is a member of its enclosing class. Non-static nested classes (inner classes)
have access to other members of the enclosing class, even if they are declared private. Static
nested classes do not have access to other members of the enclosing class. As a member of
the OuterClass, a nested class can be declared private, public, protected, or package private. 

Syntax :

class OuterClass {
...
class NestedClass {
...
}
}

Access Protection :

The protected access modifier is accessible within package and outside the
package but through inheritance only. The protected access modifier can be applied on the
data member, method and constructor. It can’t be applied on the class. It provides more
accessibility than the default modifer.

SOURCE CODE
import java.io.*;
import java.lang.*;
class InnerClass
{
int x=0,y=0,z=0;
String str="";
void impi()
{

Inner in=new Inner();

Department of MCA Page 5


Java Programming Lab

in.call();
in.display();
System.out.println("The inner method is called\n");
}
class Inner
{
int a,b,c;
protected void call()
{
DataInputStream in=new DataInputStream(System.in);
try{
System.out.println("Enter the value of a:\n");
a=Integer.parseInt(in.readLine());
System.out.println("Enter the value of b:\n");
b=Integer.parseInt(in.readLine());
catch(Exception e)
{}
c=a*b-50;
}
void display()
{
System.out.println("The solution is "+c);
}
}
public static void main(String args[])
{
InnerClass inc=new InnerClass();
inc.impi();
}

Department of MCA Page 6


Java Programming Lab

Output:
Documents/2semjava$ java InnerClass

Enter the value of a:


5

Enter the value of b:


6
The solution is: 7
The inner class method is called

Department of MCA Page 7


Java Programming Lab
NAME : SAHIL DATE:05/08/2022
Program No:03 USN : 4SN21MC039
Program Statement: Write a program in Java for String handling which
performs the following:
a Checks the capacity of String Buffer objects.
b Reverses the contents of a string given on console and converts the
resultant string in upper case.
c Reads a string from console and appends it to the resultant string of (ii).

String in Java :

Strings in Java are Objects that are backed internally by a char array. Since
arrays are immutable(cannot grow), Strings are immutable as well. Whenever a
change to a String is made, an entirely new String is created. 

Syntax :
<String_Type> <string_variable> = "<sequence_of_string>";

Example:
String str = "hey";

String Handling Functions :

 charAt(): It returns a character at a specified position.


 equals(): It compares the two given strings and returns a Boolean, that
is, True or False.
 concat(): Appends one string to the end of another.
 length(): Returns the length of a specified string.
 toLowerCase(): Converts the string to lowercase letters.
 toUpperCase(): Converts the string to uppercase letters.
 indexOf(): Returns the first found position of a character.
 substring(): Extracts the substring based on index values, passed as an argument.

SOURCE CODE :
import java.io.*;
class StringHandler
{

Department of MCA Page 8


Java Programming Lab
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1,s2,s3,s4,s5;
int i,l;
s2="";
System.out.println("\nEnter the string:\t\t\t");
System.out.println("\n======================\t");
s1=br.readLine();
System.out.println("\nEntered the string is:\t"+s1);
System.out.println("\nLength of the string is:\t"+s1.length());
StringBuffer sb=new StringBuffer(s1); System.out.println("\
nCapacity of String buffer:\t"+sb.capacity()); l=s1.length();
if(l==0)
System.out.println("\n String is empty cannot be reversed");
else
{
for(i=l-1;i>=0;i--)
{

s2=s2+s1.charAt(i);
}
System.out.println("\nThe reversed String is:\t"+s2);
s3=s2.toUpperCase();
System.out.println("\nThe uppercase of reverse string is:\t"+s3);
System.out.println("\nEnter a new string:\t");
System.out.println("\n===========================\
t");s4=br.readLine();

System.out.println("\nThe entered new string is:\t"+s4);


StringBuffer sb1=new StringBuffer(s4);

Department of MCA Page 9


Java Programming Lab
s5=sb1.append(s3).toString();
System.out.println("\n The append string is:\t"+s5);
}
}
}

Output:

Documents/2semjava$ java StringHandler

Enter the string:


==========================
sahil

Entered string is: sahil

Length of the string is: 7

Capacity of string buffer: 23

The reversed string is: lihas

Upper case of reverse string is: LIHAS

Enter a new string:

=====================================
pinto

The Entered new string is: a

The Appended String is:a LIHAS

Department of MCA Page 10

You might also like