Chapter 5 Part II
Chapter 5 Part II
}
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”
}
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));
}
}