Page 23: (new InputStreamReader(System.
in));
Q. Enter a string and print number of vowels and System.out.println("Enter the String:");
consonants present in it using a constructor. String str=br.readLine();
import java.io.*; int len=str.length();
import java.util.*; if(len<=100) {
public class TheString { TheString obj=new TheString();
String str; obj.countFreq();
int len,wordcount,cons; obj.display(); }
int v=0; else
TheString() { System.out.println("Enter more than 100
str="";len=0; wordcount=0; cons=0; } characters"); } }
public void countFreq() { OUTPUT:
StringTokenizer s=new StringTokenizer(str); Enter the String:
while(s.hasMoreTokens()) { ARIHANT KUMAR
String p=s.nextToken(); ARIHANT KUMAR 2 7
wordcount++;
for(int i=0;i<p.length();i++) {
char ch=p.charAt(i);
if((ch=='A') || (ch=='E') || (ch=='I') || (ch=='O') || (ch=='U'))
v++; } }
cons=(str.length()-((wordcount-1)+v));}
public void display() {
System.out.println(str+"words"+wordcount+"consonant"+cons); }
public static void main(String args[])throws IOException{
BufferedReader br=new BufferedReader
PG-25
OUTPUT:
Generate Fibonacci series using constructor.
Enter the number of terms
import java.util.*;
5
public class FiboString {
ab
static Scanner sc=new Scanner(System.in);
ba
static String x,y,z;
baba
static int n;
babababa
FiboString() {
babababababababa
x="a";y="b";z="ba"; }
babababababababababababababababa
public void accept() {
System.out.println("Enter the number of terms");
n=sc.nextInt(); }
public void generate(int n) {
if(n>0) {
z=y+x;
System.out.println(z);
y=z;
x=y;
generate(n-1); } }
public static void main(String args[]) {
FiboString obj=new FiboString();
obj.accept();
System.out.println(x+" "+y);
obj.generate(n); } }
PG-27 obj.exfirstlast();
WAP to accept a sentence and interchange the first obj.display(); }}
alphabet with the last alphabet using constructor. OUTPUT:
import java.util.*; Enter a sentence
public class Exchange { ram is a good boy
static Scanner sc=new Scanner(System.in); mar si a doog yod
String sent, rev;
int size;
Exchange() {
sent="";rev="";size=0; }
public void readsentence() {
System.out.println("Enter a sentence");
sent=sc.next(); }
public void exfirstlast() {
StringTokenizer str=new StringTokenizer(sent);
while(str.hasMoreTokens()) {
String p=str.nextToken();
char first=p.charAt(0);
char last=p.charAt(str.length()-1);
rev=(rev+" "+first.substring(1,p.length()-1)+last); } }
public void display() {
System.out.println("Original"+sent+"reverse"+rev); }
public static void main(String args[]) {
Exchange obj=new Exchange();
obj.readsentence();
PG-29 if(d==1)
WAP to enter elements in the array count++;
and check for odious number. x=a+" "+x;
import java.util.*; n=n/2; }
public class Odious { System.out.println("Binary Number= "+x+" of "+num[i]);
static Scanner sc=new Scanner(System.in); System.out.println("Number of 1's= "+count); } }
int n; public static void main(String args[]) {
int num[]=new int[150]; Odious obj=new Odious();
Odious() { obj.inputArray();
for(int i=0;i<150;i++) { obj.printOdious(); } }
num[i]=0; } } OUTPUT :
Odious(int nx) { Enter the limit of the array
n=nx; } 5
public void inputArray() { Enter Elements of the Array
System.out.println("Enter the limit of the array"); 1 2 3 4 5
n=sc.nextInt(); Binary number= 0001 of 1
System.out.println("Enter Elements of the Array"); Number of 1's= 1
for(int i=0;i<n;i++) { Binary number= 0010 of 2
num[i]=sc.nextInt(); } } Number of 1's= 1
public void printOdious() { Binary number= 0011 of 3
int d,count=0,a=0; Number of 1's= 2
String x=""; Binary number= 0100 of 4
for(int i=0;i<n;i++) { Number of 1's= 1
while(num[i]>0) { Binary number= 0101 of 5
d=n%2;
PG-31 obj.FillArray();
WAP to find prime factors of numbers. obj.show_PrimeFacts(); } }
import java.util.*; OUTPUT:
public class Prime_Factors { Enter the limit of the array
static Scanner sc=new Scanner(System.in); 3
long arr[]=new long[10]; Enter elements in the array
int s; 24 6 11
Prime_Factors(int lim) { Prime Factors of 24 are: 2 2 2 3
s=lim; } Prime Factors of 6 are: 2 3
public void FillArray() { Prime Factors of 11 are: 11
System.out.println("Enter the limit of the array");
s=sc.nextInt();
System.out.println("Enter elements in the array");
for(int i=0;i<s;i++) {
arr[i]=sc.nextLong(); } }
public void show_PrimeFacts() {
for(int i=0;i<s;i++) {
for(int j=2;j<arr[i];j++) {
while(arr[i]%j==0) {
System.out.println("Prime Factors of "+arr[i]+" are: "+j+" ");
arr[i]=arr[i]/j; } }
if(arr[i]>2)
System.out.println(arr[i]); } }
public static void main(String args[]) {
Prime_Factors obj=new Prime_Factors()
PG-33 System.out.println("Sum of right diagonal= "+rd);
WAP to input a array and print the sum of left System.out.println("Sum of boundary elements= "+bd); }
diagonal, right diagonal and boundary elements public void show() {
import java.util.*; for(int i=0;i<m;i++) {
public class DDArray { for(int j=0;j<n;j++) {
static Scanner sc=new Scanner(System.in); if((i==0) || (j==0) || (i==(n-1)) || (j==(n-1)))
int mat[][]=new int[50][50]; System.out.print();
int m,n,rd=0,ld=0,bd=0; else
DDArray(int nr,int nc) { System.out.println(); } } }
m=nr;n=nc; } public static void main(String args[]) {
public void readMatrix(); { System.out.println("Enter number of rows and columns");
System.out.println("Enter matrix elements"); nr=sc.nextInt();
for(int i=0;i<m;i++) nc=sc.nextInt();
for(int j=0;j<n;j++) DDArray obj=new DDArray();
mat[i][j]=sc.nextInt(); } obj.readMatrix();
public void sums() { obj.sums();
for(int i=0;i<m;i++) { obj.display(); } }
for(int j=0;j<n;j++) { OUTPUT:
if(i==j) Enter number of rows and columns
rd=rd+mat[i][j]; 44
if((i+j)==(n-1)) Enter matrix elements
ld=ld+mat[i][j]; 3456173448625913
if((i==0) || (j==0) || (i==(n-1)) || (j==(n-1))) Sum of left diagonal= 19
bd=bd+mat[i][j]; } } Sum of right diagonal= 22
System.out.println("Sum of left diagonal= "+ld); Sum of boundary elements= 47