CHAPTER12
CHAPTER12
Library Classes
Section 3: Assignment Questions
7. Describe wrapper class methods available in Java to parse string values to their numeric
equivalents.
Ans.
Wrapper
SNo Method Name Description
Class
i. parseInt(string) Integer Parses the string argument as a signed integer. The characters
in the string must be digits or digits separated with a decimal.
The first character may be a minus sign (-) to indicate a negative
value or a plus sign (+) to indicate a positive value. Syntax: int
parseInt(String s)
ii. parseLong(string) Long Parses the string argument as a signed long. The characters in
the string must be digits or digits separated with a decimal. The
first character may be a minus sign (-) to indicate a negative
value or a plus sign (+) to indicate a positive value. Syntax: long
parseLong(String s)
iii. parseFloat(string) Float Returns a float value represented by the specified string.
Syntax: float parseFloat(String s)
iv. parseDouble(string) Double Returns a double value represented by the specified string.
Syntax: double parseDouble(String s)
8. How can you check if a given character is a digit, a letter or a space? www.bhuvantechs.com
Ans. we can check it by the following methods:
1. isDigit(char) Returns true if the specified character is a digit; returns false otherwise.
Syntax: boolean isDigit(char ch)
2. isLetter(char) Returns true if the specified character is a letter; returns false otherwise.
Syntax: boolean isLetter(char ch)
3. isWhitespace(char) Returns true if the specified character is whitespace; returns false otherwise.
Syntax: boolean isWhitespace(char ch)
10. Define a class (using the Scanner class) to generate a pattern of a word in the form of a triangle or
in the form of an inverted triangle, depending upon user's choice.
Sample Input: Enter a word: CLASS
Enter your choice: 1 Enter your choice: 2
Sample Output: Sample Output:
C CLASS
CL CLAS
CLA CLA
CLAS CL
CLASS C
Ans. import java.util.Scanner;
public class triangle {
public static void main(String args[]) { Scanner sc = new Scanner(System.in);
System.out.println("**************MENU*************");
System.out.println("Type 1 for a triangle and ");
System.out.println("Type 2 for an inverted triangle");
System.out.print("Enter the number: "); int n = sc.nextInt();
String s = "CLASS"; int i, j, terms=s.length();
www.bhuvantechs.com
switch(n) { case 1:
for(i=1; i<=terms; i++) { for(j=0; j<i; j++) System.out.print(s.charAt(j)+" ");
System.out.println(""); } break;
case 2:
for(i=terms-1; i>=0; i--) { for(j=0; j<=i; j++) System.out.print(s.charAt(j)+" ");
System.out.println(""); } break;
default: System.out.println("Wrong choice"); } }
}