Library Classes Programs
- Categories Java, Wrapper Classes
1 . Write a program to assign character constants ‘A’, ‘d’, ‘5’ to the appropriate character variables and using character functions check the status of the letters and print the output in true or false.
public class problem1
{
public void check()
{
char ch = 'D', ch1 = '8', ch2 = 'q';
System.out.println( ch +" is a capital letter, the result is = "+Character. isUpperCase ( ch ));
System.out.println( ch1 +" is a digit, the result is = "+ Character. isDigit ( ch1 ));
System.out.println( ch2 +" is a small letter, the result is ="+ Character. isLowerCase ( ch2 ));
System.out.println( ch +" is a small letter, the result is = "+Character. isLowerCase ( ch ));
System.out.println( ch1 +" is an alphabet, the result is = "+Character. isLetter ( ch1 ));
}
}
2 . Write a program to define a function void Show( char ch ) to print whether the character argument ch’ is an uppercase letter or a small letter or a letter/digit or space character.
public class problem2
{
public void Show( char ch )
{
System.out.println( ch +" is a capital letter? -> the result is = "+ Character. isUpperCase( ch ));
System.out.println( ch +" is a small letter? -> the result is = "+Character. isLowerCase( ch ));
System.out.println( ch +" is a digit or letter? -> the result is = " + Character. isLetterOrDigit( ch ));
System.out.println( ch +" is a space character? -> the result is =" + Character. isLowerCase(ch ));
}
}
3 . Design a class printMessage with a function void message( ) with two print statements of your choice. Create another function void Result() to create an object of class and call the method to print messages.
public class printMessage
{
public void message( )
{
System.out.println("Computer Application ");
System.out.println("Programming is Fun ");
}
public static void Result()
{
printMessage obj = new printMessage( );
obj. message( );
}
}
4 . Write a program to initialize an uppercase character of your choice. Convert the character in lowercase form using character function. Print the original and new character with suitable headings.
public class problem4
{
public void change_Character()
{
char ch = 'Q', newchar;
newchar = Character. toLowerCase( ch );
System.out.println("Original character = "+ ch );
System.out.println("New character = " + newchar );
}
}
5 . Write a program to initialize a lowercase character of your choice. Convert the character in uppercase form using character function. Print the original and new character with suitable headings.
public class problem5
{
public void Change_Character()
{
char st = 'h', ds;
ds = Character.toUpperCase( st );
System.out.println("Original character = "+ st );
System.out.println("New character = "+ ds );
}
}
6 . A Class SalaryCalculation has been defined to calculate the total salary of an employee on the fixed salary and allowances.
The details of the class are as follows:
Class name : SalaryCalculation
Data members/ Instance variables: codeNo (long integer type data)
basicPay, specialAlw, coveyanceAlw; gross, pf,
netSalary, AnnualSal (all double type data)
Methods / Members functions of the class :
(i) void giveValues( ) : to assign data members codeNo (code
number),basicPay (basic salary)with some
values and Rs. 1000.00 as conveyanceAlw
(conveyance allowance).
(ii) void SalaryCal() :to calculate other allowances and
salaries as given: specialAlw = 25% of
basic salary (basicPay).
gross = basicPay + specialAlw +conveyanceAlw.
pf = 8.33% of basicPay.
netSalary = gross – pf.
AnnualSal = 12 month netSalary.
(iii) void display() :to print codeNo along with other
data members with headings.
Define a main() function to create the object of the class and invoke the methods to calculate allowances, salary and print all the data of an employee.
public class SalaryCalculation
{
long codeNo;
double basicPay, specialAlw, conveyanceAlw, gross, pf, netSalary,AnnualSal;
void giveValues()
{
codeNo = 32145;
basicPay = 25000.0;
conveyanceAlw = 1000.0;
}
void SalaryCal()
{
specialAlw = 0.25 * basicPay;
gross = basicPay + specialAlw + conveyanceAlw;
pf = (8.33/100.0) * basicPay;
netSalary = gross -pf;
AnnualSal = netSalary * 12;
}
void display()
{
System.out.println("Code number of the employee3 " + codeNo);
System.out.println("Basic salary= " + basicPay );
System.out.println("Special allowance= " + specialAlw );
System.out.println("Conveyance allowance= " + conveyanceAlw );
System.out.println("Gross salary= " + gross);
System.out.println("Provident fund= "+pf );
System.out.println("Net salary= "+ netSalary );
System.out.println("Annual salary= " + AnnualSal );
}
public static void main()
{
SalaryCalculation obj = new SalaryCalculation();
obj. giveValues();
obj. SalaryCal();
obj. display();
}
}
7 . Example of AutoboxingUnboxing
public class AutoboxingUnboxing
{
void AutoBoxing_UnBoxing()
{
char ch = 'F';
int num = 70;
double V = 848.20;
Integer obj = new Integer( num );
Double val = new Double( V);
Character st = ch;
System.out.println("Values after Autobexing :");
System.out.println("Value of Wrapper class Integer object obj= " +obj);
System.out.println("Value of Wrapper class Double object val= "+val);
System.out.println("Value of Wrapper class Character object st= "+ st );
ch = st;
num = obj;
V= val;
System.out.println("Values after Unboxing below :");
System.out.println("Value of primitive data type int num= "+num );
System.out.println("Value of primitive data type double V=" + V );
System.out.println("Value of primitive data type char ch=" + ch );
}
}