0% found this document useful (0 votes)
82 views37 pages

Ex 5.2:-To Implement A Program To Accept A String From The Console and Count Number of Vowels, Consonant, Digits, Tabs and Blank Spaces in A String

The document describes a Java program that implements a vector to store different object types. The vector stores 5 integer objects, 3 string objects, 2 character objects and 2 float objects. The program then performs various operations on the vector such as adding an object at the end, removing the second object, searching for an object, and displaying the first/last elements and full list of objects.

Uploaded by

Rohan Pandire
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views37 pages

Ex 5.2:-To Implement A Program To Accept A String From The Console and Count Number of Vowels, Consonant, Digits, Tabs and Blank Spaces in A String

The document describes a Java program that implements a vector to store different object types. The vector stores 5 integer objects, 3 string objects, 2 character objects and 2 float objects. The program then performs various operations on the vector such as adding an object at the end, removing the second object, searching for an object, and displaying the first/last elements and full list of objects.

Uploaded by

Rohan Pandire
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Ex 5.

2:- To implement a program to accept a string from the console and count number of
vowels, consonant, digits, tabs and blank spaces in a string.

import java.io.*;
import java.util.*;
class filter
{
String x,y;
int count1=0,count2=0,count3=0,count4=0,count5=0;
filter(String z)
{
x=z;
}

void counter()
{
int l=x.length();
for(int i=0;i<l;i++)
{
char b=x.charAt(i);
if(b=='a'||b=='e'||b=='i'||b=='o'||b=='u')
count1++;
else if(b=='0'||b=='1'||b=='2'||b=='3'||b=='4'||b=='5'||b=='6'||b=='7'||b=='8'||b=='9')
count2++;
else if(b=='\t')
count3++;
else if(b=='\b')
count4++;
else
count5++;
}
System.out.println("lenght : "+l);
System.out.println("Vowels : "+count1+" consonant : "+count5+" digits : "+count2+" tabs :
"+count3+" blank : "+count4);
}
}
class ex52
{
public static void main(String args[])throws IOException
{
System.out.println("\n Name:- Chaan \n");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter String :- ");
String s=in.readLine();
filter f=new filter(s);
f.counter();}}
Ex 5.2:- To implement a program to accept a string from the console and count number of
vowels, consonant, digits, tabs and blank spaces in a string.

import java.io.*;
import java.util.*;
class filter
{
String x,y;
int count1=0,count2=0,count3=0,count4=0,count5=0;
filter(String z)
{
x=z;
}

void counter()
{
int l=x.length();
for(int i=0;i<l;i++)
{
char b=x.charAt(i);
if(b=='a'||b=='e'||b=='i'||b=='o'||b=='u')
count1++;
else if(b=='0'||b=='1'||b=='2'||b=='3'||b=='4'||b=='5'||b=='6'||b=='7'||b=='8'||b=='9')
count2++;
else if(b=='\t')
count3++;
else if(b=='\b')
count4++;
else
count5++;
}
System.out.println("lenght : "+l);
System.out.println("Vowels : "+count1+" consonant : "+count5+" digits : "+count2+" tabs :
"+count3+" blank : "+count4);
}
}
class ex52
{
public static void main(String args[])throws IOException
{
System.out.println("\n Name:- Roshni \n ");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter String :- ");
String s=in.readLine();
filter f=new filter(s);
f.counter();}}
Ex 6.2:- Implement a program to accomplish the following task using String/StringBuffer
class.
1-Accept password from user
2-Check if password is correct then display “GOOD”
Else display “Incorrect Password”
3-Append the password with the string “welcome to java”
4-Display the password in reverse order.
5-Replace the character ‘i’ in password with “*” character.

import java.io.*;
import java.lang.*;

class test
{
String s1=new String();
String s2;
test(String s)
{
s1=s;
}
void task1()
{

if(s1.equals("ab!cd"))
{
s2=s1.concat(" Welcome To java ");
System.out.println("GOOD");
System.out.println(s2);

}
else
System.out.println("Incorrect Password");
s2=s1.replaceAll("!","*");
System.out.println("Replacing :- "+s2);

int length = s1.length();


StringBuilder reverse = new StringBuilder();
for(int i = length; i > 0; --i)
{
char result = s1.charAt(i-1);
reverse.append(result);
}
System.out.println("reverse : "+reverse);

}}
class ex62
{
public static void main(String args[])throws IOException
{
System.out.println("\n Name:- Chaan \n");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String x;
System.out.println("Enter a password : ");
x=in.readLine();
test t=new test(x);
t.task1();
}
}
Ex 6.2:- Implement a program to accomplish the following task using String/StringBuffer
class.
1-Accept password from user
2-Check if password is correct then display “GOOD”
Else display “Incorrect Password”
3-Append the password with the string “welcome to java”
4-Display the password in reverse order.
5-Replace the character ‘i’ in password with “*” character.

import java.io.*;
import java.lang.*;

class test
{
String s1=new String();
String s2;
test(String s)
{
s1=s;
}
void task1()
{

if(s1.equals("ab!cd"))
{
s2=s1.concat(" Welcome To java ");
System.out.println("GOOD");
System.out.println(s2);

}
else
System.out.println("Incorrect Password");
s2=s1.replaceAll("!","*");
System.out.println("Replacing :- "+s2);

int length = s1.length();


StringBuilder reverse = new StringBuilder();
for(int i = length; i > 0; --i)
{
char result = s1.charAt(i-1);
reverse.append(result);
}
System.out.println("reverse : "+reverse);
}}

class ex62
{
public static void main(String args[])throws IOException
{
System.out.println("\n Name:- Roshni \n");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String x;
System.out.println("Enter a password : ");
x=in.readLine();
test t=new test(x);
t.task1();
}
}
Ex 7.3:- Write a program to accept 10 number in an array and display the number in
ascending order (use Bubble sort)

import java.io.*;

class sorting
{
int a[]=new int[10];
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
void getdata() throws IOException
{
System.out.print("Enter any 10 numbers : ");
for(int i=0;i<10;i++)
{
a[i]=Integer.parseInt(in.readLine());
}
}
void putdata()
{
for(int i=0;i<10;i++)
{
for(int j=i+1;j<10;j++)
{
if(a[i]>a[j])
{int temp;
temp=a[j];
a[j]=a[i];
a[i]=temp;}
}
}
System.out.print("Sorted output :- ");
for(int i=0;i<10;i++)
{System.out.print(" "+a[i]); }
}
}
class ex73
{
public static void main(String args[]) throws IOException
{
System.out.print("\n Name : Chaan \n\n ");
sorting s=new sorting();
s.getdata();
s.putdata();
}
Ex 7.3:- Write a program to accept 10 number in an array and display the number in
ascending order (use Bubble sort)

import java.io.*;

class sorting
{
int a[]=new int[10];
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
void getdata() throws IOException
{
System.out.print("Enter any 10 numbers : ");
for(int i=0;i<10;i++)
{
a[i]=Integer.parseInt(in.readLine());
}
}
void putdata()
{
for(int i=0;i<10;i++)
{
for(int j=i+1;j<10;j++)
{if(a[i]>a[j])
{int temp;
temp=a[j];
a[j]=a[i];
a[i]=temp; } }
}
System.out.print("Sorted output :- ");
for(int i=0;i<10;i++)
{System.out.print(" "+a[i]); }
}
}

class ex73
{
public static void main(String args[]) throws IOException
{
System.out.print("\n Name : Roshni \n\n ");
sorting s=new sorting();
s.getdata();
s.putdata();
}}
Ex 8.2:- Program to implement a vector that creates and stores five integer objects, three
String objects, two character objects, two float objects such that to accomplish the
following
1. to add object at the end of vector
2. to remove 2nd object from the vector
3. to search for a particular object in vector
4. to display first and last elements in vector
5. to display the list of objects in vector

import java.util.*;

class ex82
{
public static void main(String args[])
{
System.out.println("\n Name :- Chaan \n");
Vector v=new Vector();
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
v.addElement(new Integer(5));
v.addElement("abc");
v.addElement("def");
v.addElement("ghi");
v.addElement('x');
v.addElement('y');
v.addElement(new Float(1.1));
v.addElement(new Float(2.2));
System.out.println("\n As per Question all 12 elements are added ");
System.out.println("\n Initial Size : "+v.size());
v.insertElementAt(new Integer(6),12);
System.out.println("\n 1.After adding object at the end of vector : "+v.size());
v.removeElementAt(1);
System.out.println("\n 2.After removing 2nd object from vector : "+v.size());
System.out.println("\n 3.Searching vector at 5th position : "+v.elementAt(4));
System.out.println("\n 4.first element in vector : "+v.firstElement()+" last element in vector :
"+v.lastElement());
System.out.println("\n 5.list of objects in vector : "+v.subList(0,12));
}

}
Ex 8.2:- Program to implement a vector that creates and stores five integer objects, three
String objects, two character objects, two float objects such that to accomplish the
following
1. to add object at the end of vector
2. to remove 2nd object from the vector
3. to search for a particular object in vector
4. to display first and last elements in vector
5. to display the list of objects in vector

import java.util.*;

class ex82
{
public static void main(String args[])
{
System.out.println("\n Name :- Roshni \n");
Vector v=new Vector();
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
v.addElement(new Integer(5));
v.addElement("abc");
v.addElement("def");
v.addElement("ghi");
v.addElement('x');
v.addElement('y');
v.addElement(new Float(1.1));
v.addElement(new Float(2.2));
System.out.println("\n As per Question all 12 elements are added ");
System.out.println("\n Initial Size : "+v.size());
v.insertElementAt(new Integer(6),12);
System.out.println("\n 1.After adding object at the end of vector : "+v.size());
v.removeElementAt(1);
System.out.println("\n 2.After removing 2nd object from vector : "+v.size());
System.out.println("\n 3.Searching vector at 5th position : "+v.elementAt(4));
System.out.println("\n 4.first element in vector : "+v.firstElement()+" last element in vector :
"+v.lastElement());
System.out.println("\n 5.list of objects in vector : "+v.subList(0,12));
}

}
Ex 9.2:- Program to implement the following Multi Level inheritance:
Class:Account{cust_name,acc_no}Class:Saving_Acc{min_bal,saving_bal}
Class:Acct_Details{deposits,withdrawals}

import java.lang.*;
import java.io.*;
class Account
{
String cust_name;
int acc_no;
Account(String cn,int an)
{
cust_name=cn;
acc_no=an;
}
void disp1()
{
System.out.println("=======Account Details============");
System.out.println("Customer Name :- "+cust_name);
System.out.println("Account number :- "+acc_no);
}
}

class Saving_Acc extends Account


{
static float min_bal=500;
float saving_bal;

Saving_Acc(String cn1,int an1,float sb)


{
super(cn1,an1);
saving_bal=sb;
}
void disp2()
{
super.disp1();
System.out.println("Saving Balance :- "+saving_bal);
}
}
class Acct_Details extends Saving_Acc
{
float deposits;
float withdrawals;
int x;
String c;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

Acct_Details(String cn,int an,float sb)


{
super(cn,an,sb);
}
void trans()throws IOException
{
do
{
System.out.println("1. Deposits \t 2. Withdrawals");
System.out.println("Enter Choice :- ");
x=Integer.parseInt(in.readLine());
switch(x)
{
case 1:
System.out.println("Enter amount to Deposits : ");
deposits=Integer.parseInt(in.readLine());
saving_bal=saving_bal+deposits;
System.out.println("Current Saving Balance : "+saving_bal);
break;
case 2:
System.out.println("Enter amount to Withdrawals : ");
withdrawals=Integer.parseInt(in.readLine());
float z=saving_bal-withdrawals;
if(z>min_bal)
{
saving_bal=saving_bal-withdrawals;
}
else
{
System.out.println("Insufficient Balance");
}
System.out.println("Current Saving Balance : "+saving_bal);
break;
default:
System.out.println("Invalid Entry");
break;
}
System.out.println("Do you want to continue (y/n) : ");
c=in.readLine();
}while(c.equalsIgnoreCase("y")||c.equalsIgnoreCase("Y"));
}
void disp3()
{
super.disp2();
}
}

class ex92
{
public static void main(String args[]) throws IOException
{
System.out.println("\n Name :- Chaan \n");
Acct_Details a=new Acct_Details("abc",123,1000);
a.disp3();
a.trans();
}
}
Ex 9.2:- Program to implement the following Multi Level inheritance:
Class:Account{cust_name,acc_no}Class:Saving_Acc{min_bal,saving_bal}
Class:Acct_Details{deposits,withdrawals}

import java.lang.*;
import java.io.*;
class Account
{
String cust_name;
int acc_no;
Account(String cn,int an)
{
cust_name=cn;
acc_no=an;
}
void disp1()
{
System.out.println("=======Account Details============");
System.out.println("Customer Name :- "+cust_name);
System.out.println("Account number :- "+acc_no);
}
}

class Saving_Acc extends Account


{
static float min_bal=500;
float saving_bal;

Saving_Acc(String cn1,int an1,float sb)


{
super(cn1,an1);
saving_bal=sb;
}
void disp2()
{
super.disp1();
System.out.println("Saving Balance :- "+saving_bal);
}
}
class Acct_Details extends Saving_Acc
{
float deposits;
float withdrawals;
int x;
String c;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

Acct_Details(String cn,int an,float sb)


{
super(cn,an,sb);
}
void trans()throws IOException
{
do
{
System.out.println("1. Deposits \t 2. Withdrawals");
System.out.println("Enter Choice :- ");
x=Integer.parseInt(in.readLine());
switch(x)
{
case 1:
System.out.println("Enter amount to Deposits : ");
deposits=Integer.parseInt(in.readLine());
saving_bal=saving_bal+deposits;
System.out.println("Current Saving Balance : "+saving_bal);
break;
case 2:
System.out.println("Enter amount to Withdrawals : ");
withdrawals=Integer.parseInt(in.readLine());
float z=saving_bal-withdrawals;
if(z>min_bal)
{
saving_bal=saving_bal-withdrawals;
}
else
{
System.out.println("Insufficient Balance");
}
System.out.println("Current Saving Balance : "+saving_bal);
break;
default:
System.out.println("Invalid Entry");
break;
}
System.out.println("Do you want to continue (y/n) : ");
c=in.readLine();
}while(c.equalsIgnoreCase("y")||c.equalsIgnoreCase("Y"));
}
void disp3()
{
super.disp2();
}
}

class ex92
{
public static void main(String args[]) throws IOException
{
System.out.println("\n Name :- Roshni \n");
Acct_Details a=new Acct_Details("abc",123,1000);
a.disp3();
a.trans();
}
}
Ex 10.1:- Program to implement the following Multiple Inheritance:
Interface: Exam{Percen_cal()} Class:Student{name,roll_no,mark1,mark2}

Class: Result {display()}

import java.lang.*;

interface Exam
{
void percent_cal();
}

class Student
{
String name;
int roll_no,mark1,mark2;
void getdata(String n,int r,int m1,int m2)
{
name=n;
roll_no=r;
mark1=m1;
mark2=m2;
}
void putdata()
{
System.out.println("Name: "+name);
System.out.println("Roll No: "+roll_no);
System.out.println("Marks Sub1: "+mark1+"\t Sub2: "+mark2);
}
}

class Result extends Student implements Exam


{
float tot,avg;
void display()
{
putdata();
percent_cal();
}
public void percent_cal()
{
tot=mark1+mark2;
avg=tot/2;
System.out.println("Percentage : "+avg);}}
class ex101
{
public static void main(String args[])
{
System.out.println("\n Name : Chaan \n");
Result x=new Result();
x.getdata("ABC",98,89,86);
x.display();
}
}
Ex 10.1:- Program to implement the following Multiple Inheritance:
Interface: Exam{Percen_cal()} Class:Student{name,roll_no,mark1,mark2}

Class: Result {display()}

import java.lang.*;

interface Exam
{
void percent_cal();
}

class Student
{
String name;
int roll_no,mark1,mark2;
void getdata(String n,int r,int m1,int m2)
{
name=n;
roll_no=r;
mark1=m1;
mark2=m2;
}
void putdata()
{
System.out.println("Name: "+name);
System.out.println("Roll No: "+roll_no);
System.out.println("Marks Sub1: "+mark1+"\t Sub2: "+mark2);
}
}

class Result extends Student implements Exam


{
float tot,avg;
void display()
{
putdata();
percent_cal();
}
public void percent_cal()
{
tot=mark1+mark2;
avg=tot/2;
System.out.println("Percentage : "+avg);}}
class ex101
{
public static void main(String args[])
{
System.out.println("\n Name : Roshni \n");
Result x=new Result();
x.getdata("ABC",98,89,86);
x.display();
}
}
Ex 11.1:- Program to implement Method Overriding for following inheritance: (Assume
data)
Abstract Class: Shape {dim1,dim2,abstract area()}

Class: Rectangle {getd(),area()} Class: Triangle {getd(),area()}

abstract class Shape


{
float dim1,dim2;
Shape(float d1,float d2)
{
dim1=d1;
dim2=d2;
}
void disp()
{
System.out.println("Dimension 1 : "+dim1);
System.out.println("Dimension 1 : "+dim2);
}
abstract void area();
}
class Rectangle extends Shape
{
Rectangle(float d1,float d2)
{
super(d1,d2);
}

void area()
{
float a=dim1*dim2;
System.out.println("Area of Rectangle :- "+a);
}
}
class Triangle extends Shape
{
Triangle(float d1,float d2)
{
super(d1,d2);
}
void area()
{
float a=(dim1*dim2)/2;
System.out.println("Area of Triangle :- "+a);
}
}
class ex111
{
public static void main(String args[])
{
System.out.println("\n Name : Chaan \n");
Rectangle r=new Rectangle(5,5);
Triangle t=new Triangle(10,10);
r.area();
t.area();
}
}
Ex 11.1:- Program to implement Method Overriding for following inheritance: (Assume
data)
Abstract Class: Shape {dim1,dim2,abstract area()}

Class: Rectangle {getd(),area()} Class: Triangle {getd(),area()}

abstract class Shape


{
float dim1,dim2;
Shape(float d1,float d2)
{
dim1=d1;
dim2=d2;
}
void disp()
{
System.out.println("Dimension 1 : "+dim1);
System.out.println("Dimension 1 : "+dim2);
}
abstract void area();
}
class Rectangle extends Shape
{
Rectangle(float d1,float d2)
{
super(d1,d2);
}

void area()
{
float a=dim1*dim2;
System.out.println("Area of Rectangle :- "+a);
}
}
class Triangle extends Shape
{
Triangle(float d1,float d2)
{
super(d1,d2);
}
void area()
{
float a=(dim1*dim2)/2;
System.out.println("Area of Triangle :- "+a);
}
}
class ex111
{
public static void main(String args[])
{
System.out.println("\n Name : Roshni \n");
Rectangle r=new Rectangle(10,10);
Triangle t=new Triangle(20,20);
r.area();
t.area();
}
}

You might also like