Varun Kaushik, BCA 2nd Year

17
pg. 1 INFORMATION TECHNOLOGY PROJECT REPORT JAVA PROGRAMMING TOPIC Wrapper class and Nesting of Method SUBMITTED BY Varun Kaushik BCA II nd Dezyne E’cole College www.dezyneecole.com

Transcript of Varun Kaushik, BCA 2nd Year

Page 1: Varun Kaushik, BCA 2nd Year

pg. 1

INFORMATION TECHNOLOGY

PROJECT REPORT

JAVA PROGRAMMING

TOPIC

Wrapper class and

Nesting of Method

SUBMITTED BY

Varun Kaushik

BCA IInd

Dezyne E’cole College

www.dezyneecole.com

Page 2: Varun Kaushik, BCA 2nd Year

pg. 2

Project Report

On Java Programming

At

Dezyne E’cole College

Ajmer

Submitted to

Dezyne E’cole College

Towards the

Partial fulfillment on

Bachelor computer application

By

Varun Kaushik

Dezyne E’cole College

106/10, Civil lines, Ajmer

Tel:-0145-2624679

www.dezyneecole.com

2016-2017

Page 3: Varun Kaushik, BCA 2nd Year

pg. 3

ACKNOWLEDGMENT

I Varun Kaushik, student of Dezyne E’cole College, an

Extremely Grateful to each and every individual who

has contributed in successful completion of my project.

I Express my Gratitude towards Dezyne E’cole College

for their. Guidance and Constant super vision as well as

for providing the necessary information and support

regarding the completion of project.

Page 4: Varun Kaushik, BCA 2nd Year

pg. 4

SYNOPSIS

This project is a minar project made, based on the

theoretical concepts of Java this project has made our

basic concepts on Java storng.

Page 5: Varun Kaushik, BCA 2nd Year

pg. 5

Wrapper Classes:

As pointed out earlier, vectors cannot handle primitive data types like int, float,

char, and double. Primitive data type may be converted into object types by

using the wrapper classes contained in the java.lang Package. Following table

shows the simple data types and their corresponding wrapper class types.

Wrapper Classes For Converting Types

Simple Type Wrapper Class

Boolean Boolean

Char Character

Double Double

Float Float

Int Integer

Long Long

The wrapper classes have a number of unique methods for handling primitive

data type and objects. They are listed in the following tables.

Converting Primitive Numbers to Object Number Using Constructor

Method

Constructor Calling Conversion Action

Integer IntVal=new Integer(i); Primitive integer to Integer Object

Float FloatVal=new Float(f); Primitive float to Float Object

Double DoubleVal=new Double(d); Primitive double to Double Object

Long LongVal=new Long(l); Primitive long to Long Object

Converting Object Numbers to Primitive Number Using typeValue()

Method

Method Calling Conversion Action

int i=IntVal.intValue(); Object to Primitive integer

float f=FloatVal.floatValue(); Object to Primitive float

long l=LongVal.longValue(); Object to Primitive long

Page 6: Varun Kaushik, BCA 2nd Year

pg. 6

double d=DoubleVal.doubleValue(); Object to Primitive double

Converting Numbers to String Using to String() Method

Method Calling Conversion Action

str = Integer.toString(i); Primitive integer to string

str = Float.toFloat(f); Primitive float to string

str = Double.toDouble(d); Primitive double to string

str = Long.toLong(l); Primitive long to string

Converting String Objects to Numbers Objects Using the Static Method

valueOf()

Method Calling Conversion Action

DoubleVal = Double.valueOf(str); Converts string to Double object

FloatVal = Float.valueOf(str); Converts string to Float object

IntVal = Integer.valueOf(str); Converts string to Integer object

LongVal = Long.valueOf(str); Converts string to Long object

Converting Numeric String to Primitive Numbers Using Parsing Methods

Method Calling Conversion Action

int i = Integer.parseInt(str); Converts string to primitive integer

float f = Float.parseFloat(str); Converts string to primitive float

long l = Long.parseLong(str); Converts string to primitive long

double d = Double.parseDouble(str); Converts string to primitive double

Page 7: Varun Kaushik, BCA 2nd Year

pg. 7

1. Converting Primitive Numbers to Object Number

Output

Page 8: Varun Kaushik, BCA 2nd Year

pg. 8

2. Converting Object Numbers to Primitive Numbers

Output:-

Page 9: Varun Kaushik, BCA 2nd Year

pg. 9

3. Converting Numbers to String

Output:-

Page 10: Varun Kaushik, BCA 2nd Year

pg. 10

4. Converting string object to numeric object :-

Output :-

Page 11: Varun Kaushik, BCA 2nd Year

pg. 11

5. Converting numeric string to primitive number :-

Output :-

Page 12: Varun Kaushik, BCA 2nd Year

pg. 12

AutoBoxing and unboxing:-

The autoboxing and unboxing feature, introduced in j2se 5.0,

facilitates the process of handling primitive data types in collections.

We can use this feature to convert primitive data types to wrapper

class types automatically. The compiler generates a code implicity

To convert type to the corresponding wrapper alss type and vice

versa. For example , consider the following statements:

Double d_object =98.42;

double d_primitive=d_object;

Using the autoboxing and unboxing feature, we can rewrite the above

code as:

Double d_object =98.42;

double d_primitive=d_object;

How , the java compiler provides restriction to perform the following

conversions:

1. Convert from null type to any primitive type.

2. Convert to the null type other than the identify conversion.

3. Convert from any class type c to any array type if c is not object.

Page 13: Varun Kaushik, BCA 2nd Year

pg. 13

6. Vector without using autoboxing and unboxing:-

Output :-

Page 14: Varun Kaushik, BCA 2nd Year

pg. 14

7. Vector with using autoboxing and unboxing:-

Output:-

Page 15: Varun Kaushik, BCA 2nd Year

pg. 15

Nesting Of Methods :-

We discussed earlier that a method of a class can be called only by

an object of that class (or class itself, in the case of static method

)using the dot operator. However, there is an exception to this. A

method can be called by using only its name by another method of the

same class. This is known as nesting of methods.

Program illustrates the nesting of methods inside a class. The class

Nesting defines one constructor and two methods, namely largest()

and display(). The method display() calls the method largest() to

determine the largest of the two numbers and then displays the result.

1.Nesting of Methods:-

Output:-

2.Another program of nesting of methods:-

Page 16: Varun Kaushik, BCA 2nd Year

pg. 16

Output:-

Amethod can call any number of methods. It is also possible for a

called methods to call anther method. That is , method1 may call

b ,which in trun may call method3.

Page 17: Varun Kaushik, BCA 2nd Year

pg. 17

Thank you