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

Class 37

Uploaded by

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

Class 37

Uploaded by

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

Package

========
A package is a collection of classes,interfaces,enums and annotations.

Enum is a special class and annotation is a special interface.

In general , a package is a collection of classes and interfaces.

A package is also known as folder or a directory.

In java packages are divided into two types.

1) Predefined packages
--------------------
Built-In packages are called predefined packages.
ex:
java.lang
java.io
java.util
java.util.stream
java.time
and etc.

2) Userdefined packages
-----------------------
A package which is created by the user based on the application requirement is
called predefined package.

To declare predefined package we need to use package keyword.

It is recommanded to declare a package name in the reverse order of url.

syntax:
-----
package <package_name>;
ex:
package com.google.www;

ex:
----
package com.ihub.www;
import java.util.Calendar;
class Test
{
public static void main(String[] args)
{
Calendar c=Calendar.getInstance();
int h=c.get(Calendar.HOUR_OF_DAY);

if(h<12)
System.out.println("Good Morning");
else if(h<16)
System.out.println("Good Afternoon");
else if(h<20)
System.out.println("Good Evening");
else
System.out.println("Good Night");
}
}

We can compile above program by using below command.


ex:
current directory
|
javac -d . Test.java
|
destination folder

We can run above program by using below command.


ex:

java com.ihub.www.Test
|
package name

Enum
=====
Enum is a group of named constants.

Enum concept introduced in 1.5 version.

Using enum we can create our own datatype called enumerated datatype.

When compare to old language enum, java enum is more powerful.

To declare the enum we will use enum keyword.

syntax:
------
enum type_name
{
value1,value2,.....,valueN
}

Internal implementation of enum


---------------------------------
Every enum internally implements as class concept and it extends with
java.lang.Enum class.

Every enum constant is a reference variable of enum type.

ex:

enum Months final class Months extends java.lang.Enum


{ {
JAN,FEB,MAR ==> public static final Months JAN=new Months();
} public static final Months FEB=new Months();
public static final Months MAR=new Months();
}

Declaration and Usage of enum


--------------------------------
enum Months
{
JAN,FEB,MAR
}
class Test
{
public static void main(String[] args)
{
Months m=Months.FEB;
System.out.println(m);//FEB
}
}

ex:
---
enum Months
{
JAN,FEB,MAR
}
class Test
{
public static void main(String[] args)
{
Months m=Months.FEB;

switch(m)
{
case JAN: System.out.println("January"); break;

case FEB: System.out.println("February"); break;

case MAR: System.out.println("March"); break;


}

}
}

java.lang.Enum class
--------------------
The power to enum will be inherited from java.lang.Enum class.

It contains following two methods.

1) values()
---------
It is used to read set of constants from enum.

2) ordinal()
-----------
It is used to display ordinal number.

ex:
---
enum Week
{
MON,TUE,WED,THU,FRI,SAT,SUN
}
class Test
{
public static void main(String[] args)
{
Week[] w=Week.values();
for(Week w1:w)
{
System.out.println(w1+" ------------- "+w1.ordinal());
}
}
}

When compare to old language enum, java enum is more powerful because in addition
to constants
we can declare variables, methods and constructors.

ex:
----
enum Cloths
{
SILK,COTTON,KHADI;

Cloths()
{
System.out.println("constructor");
}
}
class Test
{
public static void main(String[] args)
{
Cloths c=Cloths.SILK;
}
}

ex:
---
enum Cloths
{
SILK,COTTON,KHADI;

static int i=100;

public static void main(String[] args)


{
System.out.println(i);
}
}

Singleton Class
================
Singleton is one of the design pattern which allows us to create only one object
for a class.

Using a class name if we call any method and that method returns same class object
is called singleton class.

ex:
Calendar c = Calendar.getInstance();
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();

To create a singleton class we required private constructor and static method.


ex:
---
class Singleton
{
static Singleton singleton=null;

private Singleton()
{
}

public static Singleton getInstance()


{
if(singleton==null)
{
singleton=new Singleton();
}

return singleton;
}
}
class Test
{
public static void main(String[] args)
{
Singleton s1=Singleton.getInstance();
System.out.println(s1.hashCode());

Singleton s2=Singleton.getInstance();
System.out.println(s2.hashCode());

Singleton s3=Singleton.getInstance();
System.out.println(s3.hashCode());
}
}

Wrapper classes
===============
The main objective of wrapper classes are.

1) To wrap primitive type to wrapper object and vice versa.

2) To define several utility method.

ex:
Primitive type wrapper class
----------------- -------------
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

constructor
------------
There are two ways to create object for wrapper classes. One will take
corresponding primitive as an argument and another will take corresponding String
as an argument.

ex:
wrapper class constructor
----------------- -------------
Byte byte or String
Short short or String
Integer int or String
Long long or String
Float float or String
Double double or String
Boolean boolean or String
Character char

ex:
---

class Test
{
public static void main(String[] args)
{
Integer i1=new Integer(10);
System.out.println(i1); //10

Integer i2=new Integer("20");


System.out.println(i2); //20
}
}

ex:
---

class Test
{
public static void main(String[] args)
{
Boolean b1=new Boolean(true);
System.out.println(b1);

Boolean b2=new Boolean("false");


System.out.println(b2);
}
}

ex:
---

class Test
{
public static void main(String[] args)
{
Character c=new Character('a');
System.out.println(c);//a
}
}

Utility methods
===============

1) parseXxx()
--------------
It is used to convert string to primitive type.

ex:

class Test
{
public static void main(String[] args)
{
String str="10";

int i= Integer.parseInt(str);
System.out.println(i); // 10

long l= Long.parseLong(str);
System.out.println(l); // 10

float f= Float.parseFloat(str);
System.out.println(f); // 10.0

double d= Double.parseDouble(str);
System.out.println(d); // 10.0
}
}

2) toString()
----------------
It is used to convert wrapper object to string.

ex:
--

class Test
{
public static void main(String[] args)
{
Integer i1=new Integer(10);

String str =i1.toString();

System.out.println(str); // 10
}
}

3) xxxValue()
---------------
It is used to convert wrapper object to primitive type.

ex:

class Test
{
public static void main(String[] args)
{
Integer i1=new Integer(10);

byte b = i1.byteValue();
System.out.println(b);

short s = i1.shortValue();
System.out.println(s);
}
}

Q) Write a java program to perform sum of two binary numbers?

input:
1010
0101
output:
1111

ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the first binary number :");


String binary1=sc.next();

System.out.println("Enter the second binary number :");


String binary2=sc.next();

//convert binary to decimal


int a = Integer.parseInt(binary1,2);
int b = Integer.parseInt(binary2,2);

int c = a + b;

//convert decimal to binary


String result=Integer.toBinaryString(c);
System.out.println(result);
}
}

Assignment
==========
Q) Write a java program to display second highest element from given array?

input:
6 9 2 4 1 2 7

output:
7

You might also like