Chapter-2 Library Classes
Answer the following questions
Solutions to Unsolved Java Programs
Question 1
Write a program in Java to input a character. Find and display the next 10th character in the ASCII
table.
import [Link];
public class TenthChar
public static void main(String [] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter a character: ");
char ch = [Link]().charAt(0);
char nextCh = (char)(ch + 10);
[Link]("Tenth character from "
+ ch + " is " + nextCh);
Question 2
Write a program in Java to input a character. Display next 5 characters.
import [Link];
public class FiveChars
public static void main(String [] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter a character: ");
char ch = [Link]().charAt(0);
[Link]("Next 5 characters from "
+ ch + " are:");
for (int i = 1; i <= 5; i++) {
[Link](++ch);
Question 3
Write a program in Java to generate all the alternate letters in the range of letters from A to Z.
public class AlternateLetters
public static void main(String [] args) {
for (char ch = 'A'; ch <= 'Z'; ch = (char)(ch + 2)) {
[Link](ch);
Question 4
Write a program to input a set of 20 letters. Convert each letter into upper case. Find and display the
number of vowels and number of consonants present in the set of given letters.
import [Link];
public class LetterSet
public static void main(String [] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter any 20 letters");
int vc = 0, cc = 0;
for (int i = 0; i < 20; i++) {
char ch = [Link]().charAt(0);
ch = [Link](ch);
if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U')
vc++;
else if (ch >= 'A' && ch <= 'Z')
cc++;
[Link]("Number of Vowels = " + vc);
[Link]("Number of Consonants = " + cc);
Question 5
Write a program in Java to accept an integer number N such that 0<N<27. Display the corresponding
letter of the alphabet (i.e. the letter at position N).
[Hint: If N =1 then display A]
import [Link];
public class tInteger2Letter
public static void main(String [] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter integer: ");
int n = [Link]();
if (n > 0 && n < 27) {
char ch = (char)(n + 64);
[Link]("Corresponding letter = " + ch);
else {
[Link]("Please enter a number in 1 to 26 range");
Question 6
Write a program to input two characters from the keyboard. Find the difference (d) between their
ASCII codes. Display the following messages:
If d=0 : both the characters are same.
If d<0 : first character is smaller.
If d>0 : second character is smaller.
Sample Input :
Sample Output :
d= (68-80) = -12
First character is smaller
import [Link];
public class ASCIIDiff
public static void main(String [] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter first character: ");
char ch1 = [Link]().charAt(0);
[Link]("Enter second character: ");
char ch2 = [Link]().charAt(0);
int d = (int)ch1 - (int)ch2;
if (d > 0)
[Link]("Second character is smaller");
else if (d < 0)
[Link]("First character is smaller");
else
[Link]("Both the characters are same");
Question 7
Write a program to input a set of any 10 integer numbers. Find the sum and product of the numbers.
Join the sum and product to form a single number. Display the concatenated number.
[Hint: let sum=245 and product = 1346 then the number after joining sum and product will be
2451346]
import [Link];
public class ProdConcat
public static void main(String [] args){
Scanner in = new Scanner([Link]);
[Link]("Enter 10 integers");
long sum = 0, prod = 1;
for (int i = 0; i < 10; i++) {
int n = [Link]();
sum += n;
prod *= n;
String s = [Link](sum) + [Link](prod);
long r = [Link](s);
[Link]("Concatenated Number = " + r);
Question 8
Write a menu driven program to generate the upper case letters from Z to A and lower case letters
from 'a' to 'z' as per the user's choice.
Enter '1' to display upper case letters from Z to A and Enter '2' to display lower case letters from a to
z.
import [Link];
public class Letters
public static void main(String [] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter '1' to display upper case letters from Z to A");
[Link]("Enter '2' to display lower case letters from a to z");
[Link]("Enter your choice: ");
int ch = [Link]();
int count = 0;
switch (ch) {
case 1:
for (int i = 90; i > 64; i--) {
char c = (char)i;
[Link](c);
[Link](" ");
count++;
//Print 10 characters per line
if (count == 10) {
[Link]();
count = 0;
break;
case 2:
for (int i = 97; i < 123; i++) {
char c = (char)i;
[Link](c);
[Link](" ");
count++;
//Print 10 characters per line
if (count == 10) {
[Link]();
count = 0;
break;
default:
[Link]("Incorrect Choice");
}
Question 9
Write a program to input a letter. Find its ASCII code. Reverse the ASCII code and display the
equivalent character.
Sample Input: Y
Sample Output: ASCII Code = 89
Reverse the code = 98
Equivalent character: b
import [Link];
public class ASCIIReverse
public static void main() {
Scanner in = new Scanner([Link]);
[Link]("Enter a letter: ");
char l = [Link]().charAt(0);
int a = (int)l;
[Link]("ASCII Code = " + a);
int r = 0;
while (a > 0) {
int digit = a % 10;
r = r * 10 + digit;
a /= 10;
[Link]("Reversed Code = " + r);
[Link]("Equivalent character = " + (char)r);
}
Question 10
Write a menu driven program to display
(i) first five upper case letters
(ii) last five lower case letters as per the user's choice.
Enter '1' to display upper case letters and enter '2' to display lower case letters.
import [Link];
public class MenuUpLowCase
public static void main(String [] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter '1' to display upper case letters");
[Link]("Enter '2' to display lower case letters");
[Link]("Enter your choice: ");
int ch = [Link]();
switch (ch) {
case 1:
for (int i = 65; i <= 69; i++)
[Link]((char)i);
break;
case 2:
for (int i = 118; i <= 122; i++)
[Link]((char)i);
break;
default:
break;
Question 11
Write a program in Java to display the following patterns:
(i)
ab
ABC
abcd
ABCDE
public class Pattern
public void displayPattern() {
for (int i = 65; i < 70; i++) {
for (int j = 65; j <= i; j++) {
if (i % 2 == 0)
[Link]((char)(j+32));
else
[Link]((char)j);
[Link]();
}
(ii)
ZYXWU
ZYXW
ZYX
ZY
public class Pattern
public void displayPattern() {
for (int i = 86; i <= 90; i++) {
for (int j = 90; j >= i; j--) {
[Link]((char)j);
[Link]();
(iii)
ABCDE
ABC
public class KboatPattern
public void displayPattern() {
for (int i = 69; i >= 65; i = i - 2) {
for (int j = 65; j <= i; j++) {
[Link]((char)j);
[Link]();
Output
(iv)
PRTV
PRT
PR
public class KboatPattern
public void displayPattern() {
for (int i = 86; i >= 80; i = i - 2) {
for (int j = 80; j <= i; j = j + 2) {
[Link]((char)j);
[Link]();
(v)
A*B*C*D*E*
A*B*C*D*
A*B*C*
A*B*
A*
public class Pattern
public void displayPattern() {
for(int i = 69; i >= 65; i--) {
for (int j = 65; j <= i; j++) {
[Link]((char)j + "*");
[Link]();
(vi)
aaaaa
bbbbb
AAAAA
BBBBB
public class Pattern
public void displayPattern() {
int a = 97;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 5; j++) {
[Link]((char)a + " ");
a++;
if (i == 2)
a = 65;
[Link]();
Question 1
What is a package?
In Java, a package is used to group related classes. Packages are of 2 types:
1. Built-In packages — These are provided by Java API
2. User-Defined packages — These are created by the programmers to efficiently structure
their code.
[Link], [Link] are a couple of examples of built-in packages.
Question 2
What is the significance of '*' while importing a package?
The asterisk(*) sign indicates that all the classes in the imported package can be used in the
program.
Question 3
Define a wrapper class.
Wrapper classes wrap the value of a primitive type in an object. Wrapper classes are present in
[Link] package. The different wrapper classes provided by Java are Boolean, Byte, Integer, Float,
Character, Short, Long and Double.
Question 4
Differentiate between:
(a) isUpperCase() and toUpperCase()
isUpperCase()
It is used to check if the character given as its argument is in upper case or not.
Its return type is boolean
toUpperCase()
It is used to convert the character given as its argument to upper case
Its return type is char
(b) parseInt() and toString() functions
parseInt()
It converts a string to an integer
Its return type is int
toString()
It converts an integer to a string
Its return type is String
(c) primitive type and composite type data
Primitive Data Types
Primitive Data Types are Java's fundamental data types
Primitive Data Types are built-in data types defined by Java language specification
Examples of Primitive Data Types are byte, short, int, long, float, double, char, boolean
Composite Data Types
Composite Data Types are created by using Primitive Data Types
Composite Data Types are defined by the programmer
Examples of Composite Data Types are Class and Array
Question 5(i)
int res = 'A';
What is the value of res?
Value of res is 65.
Question 5(ii)
Name the package that contains wrapper classes.
[Link]
Question 5(iii)
Write the prototype of a function check which takes an integer as an argument and returns a
character.
char check(int n)
Write the purpose of the following functions
Question 1
[Link]()
It is used to convert string data into float data.
Question 2
[Link]()
It is used to convert double data to a string.
Question 3
[Link]()
It is used to convert string data into the Integer wrapper object.
Question 4
[Link]()
It is used to check if the character given as its argument is a digit.
Question 5
[Link]()
It is used to check if the character given as its argument is a whitespace.
Find the output of the following program snippets
Question 1
char ch = '*';
boolean b = [Link](ch);
[Link](b);
Output
false
Question 2
char c = 'A';
int n = (int) c + 32;
[Link]((char)n);
Output
Question 3
String s= "7";
int t =[Link](s);
t=t+1000;
[Link](t);
Output
1007
Question 4
char c = 'B';
int i = 4;
[Link](c+i);
[Link]((int)c+i);
Output
70
70
Question 5
char ch = 'y';
char chr = [Link](ch);
int p = (int) chr;
[Link](chr + "\t" + p);
Output
Y 89
Question 6
int n = 97;
char ch = [Link]((char)n);
[Link](ch + " Great Victory");
Output
A Great Victory
Question 7
char ch = 'x'; int n = 5;
n = n + (int)ch;
char c = (char)n;
[Link]((char)((int)c-26));
Output
Question 8
char ch = 'A';
char chr = [Link](ch);
int n = (int)chr-32;
[Link]((char)n + "\t" + chr);
Output
Aa