Ch.10UsingLibrayClasses-1
Ch.10UsingLibrayClasses-1
10.1 Packages : A package is a named collection of logically related classes. There are two
types of packages in Java.
1. Built-in package
2. User defined package
Common built-in packages in Java:
Package Description
java.lang (Default package) Provides classes for primitive data types, String and Math
functions.
java.io Provides classes for input and output of data
10.1.1 Importing Packages : The general syntax to import a built-in package in java is
import java.package.[subpackage].class; [] optional
10.2.3 Input using the Scanner Class : To input a value using the Scanner class, we will
follow given steps:
Step 1: Scanner class is defined in java.util package hence import package java.util as:
import java.util.Scanner;
or
import java.util.*;
Step 2: Create an object for the Scanner class to access its functions as: To accept input
from keyboard
Scanner sc = new Scanner (System.in);
e.g. : Write a program that accept the temperature in Fahrenheit and converts and display
its equivalent temperature in Celsius.
import java.util.*; //Step 1
public class temperature
{
public static void main(String args[])
{
double f,c;
Scanner sc = new Scanner(System.in); //Step 2
System.out.println("Enter the temperature in Fahrenheit");
f=sc.nextDouble(); //Step 3
c=5*(f-32)/9;
System.out.println(f + " degree Fahrenheit = "+c+" degree Celsius");
}
}
Output
o Change the value in Method: Java supports only call by value. So, if we pass a primitive
value, it will not change the original value. But, if we convert the primitive value in an
object, it will change the original value.
o java.util package: The java.util package provides the utility classes to deal with objects.
The eight classes of the java.lang package are known as wrapper classes in Java. The list of
eight wrapper classes are given below:
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Autoboxing
The automatic conversion of primitive data type into its corresponding wrapper class is known
as autoboxing, for example, byte to Byte, char to Character, int to Integer etc. we do not need
to use the valueOf() method of wrapper classes to convert the primitive into objects.
int a=20;
Integer i=Integer.valueOf(a); //converting int into Integer explicitly
Integer j=a; //autoboxing
System.out.println(a+" "+i+" "+j);
}
}
Output:
20 20 20
Unboxing
The automatic conversion of wrapper type into its corresponding primitive type is known as
unboxing. It is the reverse process of autoboxing.
Output: 333
10.3 Working with Strings: A String can be defined as a group of characters.
To deal with single character data, we have the Character class, which is a wrapper class.
Character class: This class offers many methods to manipulate or inspect single-character
data.
e.g.
Character.isUpperCase(char ch); // returns true if the character ‘ch’ is in upper case.
1. String class: The Java String class provides a lot of methods to perform operations on
strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(),
substring() etc.
Implicit Instantiation
String <object>=”value”;
e.g. String str=”Java Programming”;
Explicit Instantiation
String <object> = new String (“Value”);
e.g. String str=new String(“Java Programming”);
(ii) StringBuffer class
StringBuffer <object> = new StringBuffer(“value”);
e.g. StringBuffer str= new StringBuffer(“Java Programming”);
E.G
import java.util.*;
class SortWords
{
public static void main()
{
Scanner sc = new Scanner(System.in);
String str,str1="",w;
int l,i;
System.out.println("Enter the Sentence");
str=sc.nextLine();
int len=str.length();
char lch=str.charAt(len-1); // To store last character
if(lch=='.' || lch=='?' || lch=='!' )
{
str=str.toUpperCase(); //To convert string in UpperCase
StringTokenizer st=new StringTokenizer(str," ,?!.");
l=st.countTokens(); //To count no. of words
System.out.println("No. of words in Sentence = "+l);
for(i=0;i<l;i++)
{
w=st.nextToken(); //To get word from the sentence
String ws="";
// To arrange characters of word in alphabetical order
for(char ch='A';ch<='Z';ch++)
{
for(int j=0;j<w.length();j++)
{
if(w.charAt(j)==ch)
ws=ws+w.charAt(j);
}
}
str1= str1 +ws+" ";
}
System.out.println("Original Sentence :"+str);
System.out.println("Resultant Sentence :"+str1);
} // end of if
else // if sentence is not terminated with ‘.’ , ’?’ or ‘!’
System.out.println("INVALID INPUT");
}
}