Msbte JPR All Program Chapter-2
Msbte JPR All Program Chapter-2
class Rectangle
length = a;
width = b;
int RectArea ()
{
return(length * width);
}
}
{
Q.2] Following programe demonstrates the parameterized constructor.
}
Cubel (int l, int b, int h)
length = 1;
breadth = b;
height = h;
{
Cubel cubeObj1, cubeObj2;
cubeObj1 = new Cubel();
cubeObj2 = new Cubel(10, 20, 30);
System.out.println("Volume of Cubel is:" +cubeObjl.getVolume());
System.out.println("Volume of Cube2 is:" +cubeObj2.getVolume());
}
}
Q.3] Write a program to Find Factorial of Given number using command line
arguments
class Factorial
int result = 1;
while(num>0)
num--;
}
Q.4] Write a program to count number of numeric character in a string.
import java.io.*;
class Program2
{
Try
{
BufferedReader din=new BufferedReader (new
InputStreamReader(System.in));
System.out.print(" Enter the String with Numeric Number :- ");
String s-din.readLine();
int counter-0;
int l=s.length();
char ch;
for(int i=0;i<l;i++)
{
ch-s.charAt(i); if(ch>='0'&&ch<='9')
counter++;
}
}
System.out.print(" The number of digits in String is :- "+counter);
}
catch(Exception e)
{
}
}
}
Q.5] Write a program to count number of vowels character in a string.
import java.io.*;
class Program2
{
public static void main(String args[])
{
Try
{
int vcnt=0;
int l=s.length();
char ch;
for(int i=0;i<l;i++)
{
ch=s.charAt(i);
if (ch'a' || ch='A' ||
ch'e' || ch='E' ||
ch'i' || ch=T ||
ch= 'o' || ch='0' ||
ch= 'u' || ch=U')
vcnt++;
{
}
}
System.out.print(" The number of vowels in String is :- "+vcnt);
}
catch(Exception e) {}
}
}
Q.6] Write a program to count number of occurrences of character 'a' in given
string.
import java.io.*;
class Program2
{
public static void main(String args[])
{
String s;
s="Java";
int i,l,f=0,ctr=0;
I=s.length();
for(i=0;i<l;i++)
{
if(s.charAt(i)='a')
{
f-1;
ctr++;
}
}
if(f=1)
}
else
{
System.out.println(" No A Found ");
}
Q.7] Write a program to string in reverse order (Java - Output avaJ).
import java.io.*;
class Program4
String s;
s="COMPUTER";
int i,l;
I=s.length();
COMPUTER");
for(i=1-1;i>=0;i-)
System.out.print(s.charAt(i));
}
Q.8] Write a program to accept first name, middle name and sumame in three
different strings and then concatenate the three strings to make full name.
import java.io.*;
class Programs
String s,s1,s2,s3;
sl=args[0];
s2-args[1];
s3=args[2]:
s-s1.concat(s2);
s-s.concat(s3);
System.out.println("Surname :-"+s3);
}
}
Q.9] Write a program in Java to read 10 strings from console and then print the
sorted strings on console(Use String Class).
import java.io.*;
import java.lang.*;
public class sort
{
public static void main(String args[])
{
String str[] = new String[10]:
//create array of object of class string
int i,j;
try
{
InputStream Reader in= new InputStreamReader(System.in);
BufferedReader br = new
BufferedReader(in);
System.out.println("Enter the strings to be sort::");
for(i=0;i<10;i++)
str[i] = br.readline();
}
System.out.println("Sorted strings are::."); for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
if(str[j].compareTo(str[i] < (0)
{
String temp = str[i];
Str[i] = str[j];
Str[j] = temp;
}
}
System.out.println(str[i]);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Q.10]Write a program to Java StringBuffer Replace method.
/* Java StringBuffer Replace Example This example shows how contents of java
StringBuffer can be replaced using replace method of Java StringBuffer class. */
*/
sb.replace(0,5,"Hi");
}
Q.11] Write a program to delete remove character or clear using StringBuffer
class.
*/
/*
To clear contents of a StringBuffer use delete(int start, int end) method in the
below given way
*/
/*
StringBuffer deleteCharAt(int index) deletes the character at specified index.
This method throws StringIndexOutOfBound
Exception if index is negative or greater than or equal to the length.
*/
/*
1. StringBuffer StringBuffer() Construct empty StringBuffer with initial capacity
of 16
*/
/*
2. StringBuffer StringBuffer(int length) Constructs empty StringBuffer with
initial capacity of length
*/
/*
3. StringBuffer StringBuffer(String str) constructs StringBuffer with the contents
same as argument String
*/
}
Q.13] Write a program to demonstrate Java StringBuffer length.
/*
int length() method of Java StringBuffer class returns the length of a
StringBuffer object.
*/
}
Q.14] Demonstrate the use of Vector - creation of Vector, insertion of object
into Vector and how to store Vector elements in an enumerated data type.
import java.util.*;
class Languagevector
{
{
public static void main(String args[])
Vector list-new Vector();
int length args.length;
seven
elemem
position
import
import
class V
P
{
for(int i=0;i<length;i++)
{
list.addElement(args[i]);
}
list.insertElementAt("COBOL",2);
int size-list.size();
import java.util.Enumeration;
class Vectordemo
{
public static void main(String args[])
{
Vector v-new Vector();
v.addElement(new Integer(10));
v.addElement(new Integer(30));
v.addElement(new Integer(50));
v.addElement(new Integer(20));
v.addElement(new Integer(40));
v.addElement(new Integer(10));
v.addElement(new Integer(20));
System.out.println(" Initial vector size :- " +v.size());
v.removeElementAt(3);
v.removeElementAt(4);
+v.size());
}
}
Q.16] Write a program to accept as many integers as user wants in a vector.
Delete specific element and display remaining list.
v.add("10");
v.add("20");
v.add("30");
v.add(1,"First INSERTED ELEMENT");
v.add(2,"Fisrt INSERTED ELEMENT");
/* add method DOES NOT overwrites the element previously at the specified
index in the Vector. It shifts the elements to right side and increasing the
Vector size by 1. */
System.out.println("Vector contains...");
(strBinary Number,2);
}
Q.19] Write a program to accept number from user and convert it into binary
by using wrapper class method.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.*;
import java.io.IOException;
public class ConvertIntToBinary }
public static void main(String[] args) throws Exception
{
BufferedReader br-new BufferedReader (new InputStreamReader(System.in));
String str;
int i;
try
str-br.readLine();
i-Integer.parseInt(str);
String strBinary Number =Integer.toBinaryString(i);
System.out.println(i+strBinary Number);
}
catch(IOException ie)
{
}
}
}
Q.20] Write a program to convert Integer To Hexadecimal Number using
Integer Wrapper class.
int i = 32;
String strHexNumber = Integer.toHexString(i);
System.out.println ("Convert decimal number To hexadecimal number
example");
System.out.println("Hexadecimal value of " +i+" is "+ strHexNumber);
}
}
Q.21] Write a program to convert Integer To Java String Object using Integer
Wrapper class.
}
}
Q.22] Write a program to convert Java Int To Integer Object using Integer
Wrapper class.
public class IntToInteger
{
public static void main(String[] args)
{
int i = 10;
/*Use Integer constructor to convert int primitive type to Integer object.*\
Q.23] Write a program to convert Java String To Integer Object using Integer
Wrapper class.
public class StringToInteger
{
public static void main(String[] args)
{
/*We can convert String to Integer using following ways.
1. Construct Integer using constructor.*/
/use byte Value method of Integer class to convert it into byte type,/ byte b =
intObj.byteValue(); System.out.println("Convert Integer (10) to byte: "+b);
double d = intObj.doubleValue();
System.out.println("Convert Integer (10) to double: "+d);
}
Q.25] Write a program to convert Octal Number To Decimal Number using
Integer Wrapper class.
}
Q.26] Write a program to convert String To Java Int using Integer Wrapper
class.
import java.lang.*;
/* use parseInt method of Integer class to convert String into int primitive data
type. This is a static method. Please note that this method can throw a
NumberFormatException if the string is not parsable to int.*/
int i = Integer.parseInt(str);
System.out.println("Conversion of String to Int : "+i);
}
}
Q.27] This program accepts principal, rate of interest and number of years
from user and calculate simple interest.
class wrapperdemo
}
float princpl:
float rate;
int no;
float interest;
wrapperdemo() //constructor to accept data
{
BufferReader in = new BufferReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter principal amount::");
princpl Float.parseFloat(in.readLine());
System.out.println("Enter rate of interest::");
interest = Float.parseFloat(in.readLine());
System.out.println("Enter no of years:.");
no = integer.parseInt(in.readLine());
}
catch(Exception e)
{
}
}
void display_interest()
{
interest = (princpl * rate * no)/100;
String s = Float.toString(interest);
System.out.println("The Simple Interest is::" +s);
}
public static void main(String args[])
{
wrapperdemo a = new wrapperdemo(); //instantiate wrapperdemo class or
create object a.display_interest();
}
}
Q.28] Accept three numbers from user and display greatest number by using
wrapper class methods.
import java.io.*;
class Programl
{
public static void main(String args[])
{
try
{
import java.io.*;
class Program2
{
public static void main(String args[])
{
try
{
BufferedReader din=new BufferedReader (newInputStreamReader(System.in));
System.out.print(" Enter the String with Numeric Number :- ");
String s=din.readLine();
int counter-0;
int l=s.length();
char ch;
for(int i=0;i<l;i++)
{
ch-s.charAt(i);
if(ch>='0'&&ch<='9')
{
}
counter++;
}
System.out.print(" The number of digits in String is :- "+counter);
}
catch(Exception e)
{
}
}
}
Q.30] Accept numbers from user and convert it into binary by using wrapper
class methods.
import java.io.*;
class Program3
{
public static void main(String args[])
{
try
{
int i=Integer.parseInt(din.readLine());
int j=0;
System.out.print(" Its Binary Conversion:-");
while(i>1)
{
j=i%2;
i=i/2;
System.out.print(j);
}
System.out.println(i);
}
catch(Exception e)
{
}
}
}