Question 9
Write a program using a method Palin( ), to check
whether a string is a Palindrome or not. A
Palindrome is a string that reads the same from the
left to right and vice versa.
Sample Input: MADAM, ARORA, ABBA, etc.
import java.util.Scanner; String s = “madam”
public class palindrome s.length= 5
{
void palin(String s) 01234
{ s = “madam”
String s1= “ " ; // to store reverse the string extra right to left
char ch; madam
int len=s.length(); // store length of the string
for(int i=len-1;i>=0;i--) madam
{ s.charAt (i) );
ch=s.charAt(i); //extracting the character from right to left
s1=s1+ch; // getting reverse string
}
if(s.equals(s1)) //checking both string are equal i=len-1 means 5 – 1=4
System.out.println("palindrome string"); ( i>=0) repeat the process last
else index to first index
System.out.println(" not palindrome string"); i- - //one by one dec the index
}
public static void main(String args[])
{
palindrome ob=new palindrome();
Scanner sc = new Scanner(System.in);
System.out.println("enter string");
String str=sc.next();
ob.palin(str);
}
}
QUESTIONS 10
Write a program in Java to accept a String from the user.
Pass the String to a function Display(String str) which
displays the consonants present in the String.
Sample Input: computer
Sample Output:
c
m
p
t
r
import java.util.Scanner;
public class Cons
{ 01234567
public void display(String str) { string str= “computer”
String rs=" ";
for( int i=0;i<str.length();i++) i = 0-7;
{
if(str.charAt(i)=='a'||str.charAt(i)=='A'||
str.charAt(i)=='e'||str.charAt(i)=='E'|| checking the vowels
str.charAt(i)=='i'||str.charAt(i)=='I'||
str.charAt(i)=='o'||str.charAt(i)=='O'||
str.charAt(i)=='u'||str.charAt(i)=='U' )
import java.util. Scanner;
public class Cons
01234567
{ str= “computer”
public void display(String str) { for loop i = 0 - 7 ;
i=0 --.> 0 location of word
String rs=""; i< str.length- 7
for( int i=0;i<str.length();i++) // 1 2 3 4 5 i++ - increase to 1 2 3 4
{
if(str.charAt(i)=='a'||str.charAt(i)=='A'|| str.charAt(i) == a
str.charAt(i)=='e'||str.charAt(i)=='E'|| (c) ==a // matching char
str.charAt(i)=='i'||str.charAt(i)=='I'||
str.charAt(i)=='o'||str.charAt(i)=='O'||
str.charAt(i)=='u'||str.charAt(i)=='U' ) //matching the character
continue; //
else
rs=rs+str.charAt(i); // c m p t
}
System.out.println(rs);
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter string: ");
String s = in.nextLine();
Cons obj = new Cons();
obj.display(s);
}
}
. Question 13
Write a program in Java to accept a String from
the user. Pass the String to a function
First(String str) which displays the first
character of each word.
Sample Input : Understanding Computer
Applications
Sample Output:
U
C
A
import java.util.Scanner;
public class Character { 01 2 345 6 789
public void first(String str) { string str = “Un Com App”
int i, l; char ch;
str=str.trim(); trim () removes whitespace from both end of a string
str=" "+str; str = “ ” space
l=str.length(); l = 10;
for(i=0;i<l;i++) { i = 0 – 9;
ch=str.charAt(i); charAt- extract a character ch =U
if(ch==' ') // checking the space of the character
System.out.println(str.charAt(i + 1));
}
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = in.nextLine();
Character obj = new Character();
obj.first(s);
}
}
Question 10
Write a program in Java to accept a String from the
user. Pass the String to a function Display(String
str) which displays the consonants present in the
String.
Sample Input: computer
Sample Output:
c
m
p
t
r
import java.util.Scanner;
public class Cons
{ 01234567
public void display(String str) { string str= “computer”
String rs=" ";
for( int i=0;i<str.length();i++) i = 0-7;
{
if(str.charAt(i)=='a'||str.charAt(i)=='A'||
str.charAt(i)=='e'||str.charAt(i)=='E'|| checking the vowels
str.charAt(i)=='i'||str.charAt(i)=='I'||
str.charAt(i)=='o'||str.charAt(i)=='O'||
str.charAt(i)=='u'||str.charAt(i)=='U' )
continue;
else
rs=rs+str.charAt(i);
}
System.out.print("resulting is ="+rs);
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter string: ");
String s = in.nextLine();
Cons obj = new Cons();
obj.display(s);
}
}
Question 15
Write a class with the name Area using function overloading that
computes the area of a parallelogram, a rhombus and a trapezium.
Formula:
Area of a parallelogram (pg) = base * ht
Area of a rhombus (rh) = (1/2) * d1 * d2
(where, d1 and d2 are the diagonals)
Area of a trapezium (tr) = (1/2) * ( a + b) * h
(where a and b are the parallel sides, h is the perpendicular distance
between the parallel sides)
import java.util.Scanner;
public class Area
{
public double area1(double base, double height) {
return base * height;
}
public double area2(double c, double d1, double d2) {
return c * d1 * d2;
}
public double area3(double c, double a, double b, double
h) {
return c * (a + b) * h;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
Area obj = new Area();
double base,ht,d1,d2;
double a,b,h;
System.out.print("Enter the base: ");
base = in.nextDouble();
System.out.print("Enter the height : ");
ht = in.nextDouble();
System.out.println("Area of parallelogram = " +
obj.area1(base, ht));
System.out.print("Enter first diagonal: ");
d1 = in.nextDouble();
System.out.print("Enter second diagonal: ");
d2 = in.nextDouble();
System.out.println("Area of rhombus = " +
obj.area2(0.5, d1, d2));
System.out.print("Enter first parallel: ");
a = in.nextDouble();
System.out.print("Enter second parallel : ");
b = in.nextDouble();
System.out.print("Enter height of trapezium: ");
h = in.nextDouble();
System.out.println("Area of trapezium = " +
obj.area3(0.5, a, b, h));
}
}