0% found this document useful (0 votes)
5 views

CHAPTER12

The document discusses library classes in Java, which are pre-written classes that provide methods for input/output operations and string handling. It defines primitive, composite, and user-defined data types, as well as wrapper classes and their methods for converting between primitive types and objects. Additionally, it includes examples of using the Scanner class and creating custom classes for specific tasks.

Uploaded by

Borra ram kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CHAPTER12

The document discusses library classes in Java, which are pre-written classes that provide methods for input/output operations and string handling. It defines primitive, composite, and user-defined data types, as well as wrapper classes and their methods for converting between primitive types and objects. Additionally, it includes examples of using the Scanner class and creating custom classes for specific tasks.

Uploaded by

Borra ram kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CHAPTER 12

Library Classes
Section 3: Assignment Questions

1. What are the library classes in Java? What is their use?


Ans. Library classes are the pre-written classes which are a part of the Java system. For example, the
String and the Scanner class. The Java environment has a huge library of built-in classes that contain pre-
defined methods to support the input/output operations and String handling operations. They also
provide various methods for the development of network and graphical user interface.

2. Define the following terms:

i. Primitive data type


Ans. Primitive data types are pre-defined by the language and form the basic building blocks of
representing data. A primitive data type stores a single value of a specific declared type at a time. For
example, a variable defined as int can store an integer at a time.

ii. Composite data type


Ans. A composite data type is a data type which can be constructed in a program using the
programming language's primitive data types. In other words, it is a collection of primitive data types.
For example, the String data type you use in Java is actually a class which is a collection of char data
types.

iii. User-defined data type


Ans. The data type defined by the user to perform some specific task is known as a user-defined data
type. The classes created by the user are user-defined data types.

3. Why is a class called a composite data type? Explain.


Ans. In a class, we can assemble items of different data types to create a composite data type. The class
can also be considered as a new data type created by the user, that has its own functionality. The classes
allow these user-defined types to be used in programs.

4. What is a wrapper class? Name three wrapper classes in Java.


Ans. A wrapper class allows you to convert a primitive data type into an object type. Each of Java's eight
primitive data types has a wrapper class dedicated to it. These are known as wrapper classes because
they wrap the primitive data type into an object of that class. Thus, there is an Integer class that wraps
an int variable, a Double class that wraps a double variable, and a Boolean class that wraps Boolean
data.
5. Explain the terms, Autoboxing and Auto-unboxing in Java.
Ans. Autoboxing is the automatic conversion of the primitive types into their corresponding object
wrapper classes. For example, automatic conversion from an int to an Integer and from a double to a
Double.
Auto-unboxing is the reverse process of Autoboxing. That means it is the automatic conversion of a
wrapper class object into its corresponding primitive type. For example, automatic conversion from an
Integer to an int and from a Long to a long.

6. How do you convert a numeric string into a double value?


Ans. Numeric string can be converted into a double value with the help of parseDouble(string) method.
public class Parsing {
public static void main(String args[]) {
String strInt = "-1234";
double numdouble = Double.parseDouble(strInt);
System.out.println("Double value is: " + numdouble); }
}

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)

Library Classes ~2~


9. Distinguish between the following methods:

i. isLowerCase() and toLowerCase()


Ans. isLowerCase() - Returns true if the specified character is a lowercase character; returns false
otherwise.
toLowerCase() - Converts the character argument into lowercase.

ii. isUpperCase() and toUpperCase()


Ans. isUpperCase() - Returns true if the specified character is an uppercase character; returns false
otherwise.
toUpperCase() - Converts the character argument into uppercase.

iii. isDigit() and isLetter()


Ans. isDigit() - Returns true if the specified character is a digit; returns false otherwise.
isLetter() - Returns true if the specified character is a letter; returns false otherwise.

iv. parseFloat() and parseDouble()


Ans. parseFloat() – Returns a float value represented by the specified string.
parseDouble() - Returns a double value represented by the specified string.

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"); } }
}

Library Classes ~3~


11. Define a class called mobike with the following description:
Instance variables/Data members:
int bno – to store the bike’s number
int phno – to store the phone number of the customer
String name – to store the name of the customer
int days – to store the number of days the bike is taken on rent
int charge – to calculate and store the rental charge
Member Methods:
void input( ) – to input and store the details of the customer
void computer( ) – to compute the rental charge
The rent for a mobike is charged on the following basis:
First five days Rs 500 per day;
Next five days Rs 400 per day;
Rest of the days Rs 200 per day.
void display ( ) – to display the details in the following format:
Bike No. Phone No. No. of days Charge
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Ans. import java.util.Scanner;
public class mobike
{
int bno, phno, days, charge ;
public void input() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Bike Number: ");bno = keyboard.nextInt();
System.out.print("Enter Phone Number: ");phno = keyboard.nextInt();
System.out.print("Enter Number of days: ");days = keyboard.nextInt();
keyboard.close(); }
public void calculate() {
if (days>10)
charge=5*500+5*400+(days-10)*200;
else if (days>5 && days<=10)
charge=5*500+(days-5)*400;
else
charge=days*500; }
public void display() {
System.out.println("Bike Number: " + bno);
System.out.println("Phone Number : " + phno);
System.out.println("Number of days: " + days);
System.out.println("Bill Amount : Rs." + charge); }
www.bhuvantechs.com

public static void main(String args[]) {


mobike objbike = new mobike();
objbike.input();
objbike.calculate();
objbike.display(); }
}

Library Classes ~4~

You might also like