Hey!guys this is hitesh,and today we'll learn to create user defined static members.
After that we will also see some basic concepts of data conversion using wrapper classes.
How we can
create our own static members?
Create some class and create the static reference of your own
classes then use anywhere.
import java.io.*;
import java.util.*;
public class My
{
public static
Scanner sc=new Scanner(System.in);
public static
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
}
import static java.lang.System.*;
import java.io.*;
public class VoterTest1
{
public static void
main(String args[]) throws IOException
{
out.print("Name : ");
String
name=My.br.readLine();
out.print("Age : ");
int
age=My.sc.nextInt();
if(age>=18)
out.printf("%s you can vote", name);
else
err.printf("%s you cannot vote",name);
}
}-------------------------------------------------------------------------------------------------------------------------------------
Wrapper
Classes
Special classes corresponding to data types which provides
advance functions on the data types and data conversion from string to value
types
Data type Wrapper
Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
Example 1
WAP to
input a number and show that number in decimal, octal, hexa and binary
import
java.util.Scanner;
public
class WrapperTest1
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.print("Enter a number :
");
int num=s.nextInt();
System.out.println("Decimal is :
"+num);
System.out.println("Hexa is :
"+Integer.toHexString(num));
System.out.println("Binar is :
"+Integer.toBinaryString(num));
System.out.println("Octal is :
"+Integer.toOctalString(num));
}
}
Example 2
WAP to
input a character and check it to be alphabet, digit or special character
Use read()
method of System.in to read a
character
int read() throws IOException
public
class WrapperTest2
{
public static void main(String args[])
throws java.io.IOException
{
System.out.print("Enter a
character : ");
char ch=(char)System.in.read();
if(Character.isLetter(ch))
System.out.printf("%c is an
alphabet",ch);
else if(Character.isDigit(ch))
System.out.printf("%c is a
digit", ch);
else
System.out.printf("%c is
special character", ch);
}
}
Data conversion from string to value type
datatype
variable=wrapperclass.parseDatatype(stringdata);
Example
WAP to
input name and age of a person and check it to be valid voter.
import
java.io.*;
public
class Voter
{
public static void main(String args[])
throws IOException
{
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
System.out.print("Name : ");
String name=br.readLine();
System.out.print("Age : ");
int
age=Integer.parseInt(br.readLine());
if(age>=18)
System.out.printf("Dear %s you
can vote",name);
else
System.out.printf("Dear %s you
cannot vote",name);
}
}
THAT's all about wrapper classes and we also learned about static members and to create them.
Thank you
Keep coding :)
Keep learning :)
No comments:
Post a Comment