We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
Date Of Experiment
Date Of Submission
PRACTICAL NO.: 11
TITLE : Write a Java Programme to count the total no of character in the string?
THEORY : The Java String class length() method finds the length of a string. The length of the
Java string is the same as the Unicode code units of the string. The String class internally uses a
char{] array to store the characters. The length variable of the array is used to find the total number
of elements present in the array. Since the Java String class uses this char{] array internally;
therefore, the length variable can not be exposed t0 the outside world. Hence, the Java developers
created the length() method, the exposes the value of the length variable. One can also think of the
length) method as the getter() method, that provides a value of the class field 10 the user. The
internal implementation clearly depicts that the length() method returns the value of then the
length variable.
CODE (With comments) :
public class CountCharacter
{
public static void main(string[] args) {
String string = “The best of bot worlds";
int count = @;
/ (Counts each character except space
for(int i = @; i< string.length(); i++) {
if(string.charat(i) != * *)
count++};
t
//Displays the total number of characters present in the given string
System.out.println("Total number of characters in a string: " + count);
}
OUTPUT (Screenshot) :
java -cp /tmp/POFOG}YQSs CountCharacter
Total number of characters in a string: 19
Computer Programing - II ([T3CO15)
Department of Information Technology, uu
Medicaps University, IndoreDate Of Experiment
Date Of Submission
PRACTICAL NO.: 12
TITLE : Write a Java Program to count no of vowels and constant using charAt?
THEORY : The charAt() method returns the character at the specified index in a string. The index
of the first character is 0, the second character is 1, and so on.
Let's see Java program related to string in which we will use charAt() method that perform some
operation on the give string.
CODE (With comments) :
import java.util.Scanner;
public class Main{
public static void main(String[] args){
int vow=8,con=6;
String temp;
Scanner sc =new Scanner(System.in);
System.out.print("Enter the string
‘temp=sc.nextLine();
int len=temp.length();
System.out.print(temp) ;
String str=temp.toLowerCase();
for(int i=@;iclensi++){
if(str.charat(i
str.charat(i)
vows+;
}
else if(str.charat (i
}
elsef
con++3
}
}
System.out.print("No. of Vowels = "+vow);
system.out.print("\nNo. of Constant = "+con);
te’ || str.charat(i,
wt
* || str charat( 4:
|| str.charat(i)
mt
t
OUTPUT (Screenshot) :
‘Computer Programing - I! (|T3CO15)
Department of Information Technology, 2
Medicaps University, IndoreDate Of Experiment
‘see Date Of Submission
PRACTICALNO.: 13
TITLE : Write a Java Program to convert given string toLowerCase/toUpperCase,
THEORY : toLowerCase() method - This Java string method converts every character of the
particular string into the lower case by using the rules of the default locale.
Note: This method is locale sensitive. Therefore it can show unexpected results if used for strings
which are intended to be interpreted separately.
toUpperCase () - method The toUppercase() method is used to convert all the characters in a given
string to upper case.
CODE (With comments) :
import java.util.Scannerj
public class Main{
public static void main(string[] args){
int vow=2,con=0;
String temp;
Scanner sc =new Scanner(System. in) ;
system.out.print("Enter the string =
‘temp=sc.nextLine();
System .out.print(temp) ;
String lower=temp.toLowerCase();
String upper=temp.toUpperCase();
system. out.print("\nOriginal String = "+temp);
system.out.print("\nLower case string = "+lower);
System.out.print("\nUpper case string = "+upper);
}
OUTPUT (Screenshot) :
‘Computer Programing - I! (|T3CO15)
Department of Information Technology, B
Medicaps University, IndoreDate Of Experiment
Date Of Submission
PRACTICAL NO.: 14
TITLE : Write a Java Program to demonstrate single level inheritance.
THEORY : Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviours of a parent object. It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing
classes. When you inherit from an existing class, you can reuse methods and fields of the parent class.
Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
The inheritance in which a single derived class is inherited from a single base class is known as the
Single Inheritance.
CODE (With comments) :
import java.util.Scanner;
class Employee{
float salary=40000;
t
class Programmer extends Employee{
int bonus=10000;
t
public class Main
{
public static void main(String[] args) {
Progranmer pl=new Progranmer() ;
system.out.print("Salary of Employee = “spl. salary);
system.out.print("\nSalary + Bonus = "+(p1.salary+p1.bonus));
}
}
OUTPUT (Screenshot) :
‘Computer Programing - I! (|T3CO15)
Department of Information Technology, 14
Medicaps University, IndoreDate Of Experiment
Date Of Submission
PRACTICAL NO.: 15
TITLE : Write a Java Program to demonstrate multilevel inheritance,
THEORY : The multi-level inheritance includes the involvement of at least two or more than two classes.
One class inherits the features from a parent class and the newly created sub-class becomes the base
class for another new class.
As the name suggests, in the multi-level inheritance the involvement of multiple base classes is there.
Inthe multilevel inheritance in java, the inherited features are also from the multiple base classes as
the newly derived class from the parent class becomes the base class for another newly derived class.
CODE (With comments) :
import java.util.Scanner;
class Employee{
float salary-56000;
t
class Programmer extends Employee{
int bonus=20000;
}
class Manager extends Progranmer{
int extra=3000;
}
public class Main
{
public static void main(String[] args) {
Manager ml=new Nanager();
system.out.print("Salary of Employee = "smi. salary);
system.out.print("\nSalary + Bonus = "+(m1.salary+m1.bonus));
system.out.print("\nTotal Salary = "+(m1.salary+mt.bonus+ml..extra));
}
t
OUTPUT (Screenshot) :
‘Computer Programing - I! (|T3CO15)
Department of Information Technology, 15
Medicaps University, IndoreDate Of Experiment
Date Of Submission
PRACTICAL NO.: 16
TITLE : Write a Java Program to Demonstrate Interface in java?
THEORY : An Interface in Java programming language is defined as an abstract type used to specify
the behaviour of a class. An interface in Java is a blueprint of a class. A Java interface contains static
constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the
Java interface, not the method body. It is used to achieve abstraction and multiple inheritance in Java
In other words, you can say that interfaces can have abstract methods and variables. It cannot have a
method body. Java Interface also represents the IS-A relationship.
Like a class, an interface can have methods and variables, but the methods declared in an interface are
by default abstract (only method signature, no body).
Interfaces specify what a class must do and not how. It is the blueprint of the class.
An Interface is about capabilities like a Player may be an interface and any class implementing Player
must be able to (or must implement) moved). So it specifies a set of methods that the class has to implement
Ia class implements an interface and does not provide method bodies for all functions specified in the
interface, then the class must be declared abstract.
CODE (With comments) :
interface Drawable{
void draw();
}
class triangle implements Drawable{
public void draw(){System.out.printin("drawing triangle");}
t
class sphere implements Drawable{
public void draw(){System.out.println( "drawing sphere");}
t
public class Main{
public static void main(String args[]){
Drawable denew sphere();
d.draw();
Drawable d2=new triangle();
d2.draw();
t
OUTPUT (Screenshot) :
Computer Programing - II ([T3CO15)
Department of Information Technology, 16
Medicaps University, IndoreDate Of Experiment
ei Date Of Submission
PRACTICAL NO.: 17
TITLE : Write a Java Program to demonstrate Exceptional Handling in java,
THEORY : Exception Handling in Java is one of the effective means to handle the runtime errors so
that the regular flow of the application can be preserved. Java Exception Handling is a mechanism to
hhandle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException,
te
Exception is an unwanted or unexpected event, which oceurs during the execution of a program, ie. at
run time, that disrupts the normal flow of the program's instructions. Exceptions can be caught and
hhandled by the program, When an exception occurs within a method, it creates an object. This object
is called the exception object. It contains information about the exception, such as the name and description
of the exception and the state of the program when the exception occurred.
CODE (With comments)
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int a,b,c;
Scanner sc=new Scanner(System. in) ;
System.out.print("Enter the first no. =
a=sc.nextInt();
System.out.print("Enter the second no. =
b=sc.nextInt();
try{
c=a/b;
System.out.print("The Division = "+c);
}
catch(Arithmeticexception ae){
System.out.print("A no. cannot be devided by @ ");
}
}
}
OUTPUT (Screenshot) :
Pty
Sete)
‘Computer Programing - I! (|T3CO15)
Department of Information Technology, v
Medicaps University, Indore