Class As User Defined Type - Theory
Class As User Defined Type - Theory
CLASS AS USER
DEFINED TYPE
ROOPESH SAIGAONKAR
1
DATATYPES
ROOPESH SAIGAONKAR
2 2
ROOPESH SAIGAONKAR
CATEGORIES OF
DATATYPES
Primitive
Built-in datatypes
3 3
ROOPESH SAIGAONKAR
4 4
ROOPESH SAIGAONKAR
DEFINING A CLASS
• Syntax:
access_specifier keyword class classname
{
datatype variables; // instance variables
void method()
{
datatype variables; // local variables
statement 1;
statement 2;
.
.
.
statement n;
}
} 6 6
ROOPESH SAIGAONKAR
EXAMPLE
public static class program1
{
int a; // instance variables
public static void main()
{
int b=10; // local variables
a=5;
int c = a+b;
System.out.println(“sum =“+c);
}
}
7 7
ROOPESH SAIGAONKAR
OBJECTS
8 8
ROOPESH SAIGAONKAR
INSTANTIATION/OBJECT
CREATION
SECTION A QUESTION
10 10
ROOPESH SAIGAONKAR
.
• dot ' ' operator is used to access the member of class. Member of the class are variables
(instance variables) and Methods/Functions
WRAPPER CLASS
13 13
ROOPESH SAIGAONKAR
WRAPPER CLASS
14 14
ROOPESH SAIGAONKAR
WRAPPER CLASS
Datatype Wrapper class
char Character
int Integer
byte Byte
short Short
long Long
float Float
double Double
boolean Boolean
15 15
ROOPESH SAIGAONKAR
16 16
ROOPESH SAIGAONKAR
17 17
ROOPESH SAIGAONKAR
CHARACTER WRAPPER
CLASS
Syntax:
Character.function_name(char argument);
18 18
ROOPESH SAIGAONKAR
CHARACTER WRAPPER
CLASS
Function Usage Description
name char ch1 = 'M'; char ch2='9';
isLetter() boolean ans = Checks whether the given
Character.isLetter(ch1); argument is a
alphabet(letter) or not. It
will return answer either
true or false. In the
example ans=true
isDigit() boolean a = Character.isDigit(ch1); Checks whether the given
boolean b = Character.isDigit(ch2); argument is a Digit or not.
It will return answer either
true or false. In the
example
a=false
b=true
19 19
ROOPESH SAIGAONKAR
CHARACTER WRAPPER
CLASS
Function Usage Description
name char ch1 = 'M'; char ch2='9';
isLetterOrDigit boolean a = Checks whether the
() Character.isLetterOrDigit(ch1); given argument is a
alphabet/Digit or
not. It will return
answer either true or
false. In the example
a=true
isWhitespace() boolean a = Character.isWhitespace(ch1); Checks whether the
given argument is a
blank space or not. It
will return answer
either true or false.
In the example
a=false
20 20
ROOPESH SAIGAONKAR
CHARACTER WRAPPER
CLASS
Function Usage Description
name char ch1 = 'M'; char ch2='9';
isLowerCase() boolean a = Character.isLowerCase(ch1); Checks whether the
given argument is in
Small letter/ Lower
case or not. It will
return answer either
true or false. In the
example a=false
isUpperCase() boolean a = Character.isUpperCase(ch1); Checks whether the
given argument is in
Capital letter/ Upper
case or not. It will
return answer either
true or false. In the
example a=true
21 21
ROOPESH SAIGAONKAR
CHARACTER WRAPPER
CLASS
Function Usage Description
name char ch1 = 'M'; char ch2='d';
toLowerCase() char a = Character.toLowerCase(ch1); Converts the given
argument to Small
letter/ Lower case. It
will return answer in
char datatype. In the
example
a='m'
toUpperCase() char a = Character.toUpperCase(ch1); Converts the given
char b = Character.toUpperCase(ch2); argument to Capital
letter/ Upper case. It
will return answer in
char datatype. In the
example
a=‘M‘
b=‘D' 22 22
ROOPESH SAIGAONKAR
WAP TO INPUT A CHARACTER. CHECK AND PRINT WHETHER
THE CHARACTER VALUE IS A LETTER, A DIGIT OR SPECIAL
CHARACTER. ALSO CHECK WHETHER IT IS IN LOWER CASE
OR UPPER CASE
import java.util.*;
class Program111
{
public static void main()
{ else if(Character.isDigit(ch)==true)
Scanner sc=new Scanner(System.in); {
System.out.println("Enter Any value"); System.out.println("It is Number");
char ch=sc.next().charAt(0); }
if(Character.isLetter(ch)==true) else
{ {
if(Character.isUpperCase(ch)==true) System.out.println("It is Special
{ character");
System.out.println("entered value is }
Capital Letter"); } // end of main method
else } // end of class
{
System.out.println("entered value is
Small Letter");
} 23 23
}
ROOPESH SAIGAONKAR
WAP TO INPUT A LETTER. PRINT THE LETTER IN TOGGLE
CASE I.E. IF THE CHARACTER IS IN UPPER CASE THEN PRINT
IN LOWER CASE AND VICE A VERSA
import java.util.*;
class Program112
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Any value"); else
char ch=sc.next().charAt(0); {
if(Character.isLetter(ch)==true) System.out.println(“Not an
{
if(Character.isUpperCase(ch)==true)
alphabet");
{ }
} // end of main method
System.out.println(Character.toLowerCase(ch)); } // end of class
}
else
{
System.out.println(Character.toUpperCase(ch));
} 24 24
}
ROOPESH SAIGAONKAR
INTEGER WRAPPER
CLASS
25 25
ROOPESH SAIGAONKAR
ROOPESH SAIGAONKAR
INTEGER WRAPPER
CLASS
26 26
ROOPESH SAIGAONKAR
INTEGER WRAPPER
CLASS
27 27
ROOPESH SAIGAONKAR
28 28
ROOPESH SAIGAONKAR
29 29
LONG WRAPPER CLASS
30 30
ROOPESH SAIGAONKAR
DOUBLE WRAPPER
CLASS
31 31
ROOPESH SAIGAONKAR
DOUBLE WRAPPER
CLASS
32 32
ROOPESH SAIGAONKAR
DOUBLE WRAPPER
CLASS
33 33
ROOPESH SAIGAONKAR
34 34
FLOAT WRAPPER CLASS
35 35
ROOPESH SAIGAONKAR
36 36