Está en la página 1de 11

A

Term Paper Report


On
Working with class programming in Java
Bachelor of Computer Science
Session-(2015-2018)

SHRI RAMSWAROOP MEMORIAL GROUP OF

PROFESSIONAL COLLEGE

LUCKNOW

Affiliated to

LUCKNOW UNIVERSITY

Submitted To: Submitted By:

Mr.C.K Pandey Akash Jaiswal

ROLLNO: 16065106008

ACKNOWLEDGEMENT

I would like to express my special thanks of gratitude to my teacher


Mr.C.K. Pandey sir as well as our HOD Dr.Vinay Kumar Mishra sir who
gave me the golden opportunity to do this wonderful project on the topic
Working with class programming in Java, which also helped me in doing a
lot of Research and i came to know about so many new things I am really
thankful to them.
Secondly i would also like to thank my parents and friends who helped me a lot
in finalizing this project within the limited time frame.

INTRODUCTION
Java is a true OO language and therefore the underlying structure of all
Java programs is classes.
Anything we wish to represent in Java must be encapsulated in a class that
defines the state and behaviour of the basic program components
known as objects.
Classes create objects and objects use methods to communicate between
them. They provide a convenient method for packaging a group of
logically related data items and functions that work on them.
A class essentially serves as a template for an object and behaves like a
basic data type int. It is therefore important to understand how the fields
and methods are defined in a class and how they are used to build a Java
program that incorporates the basic OO concepts such as encapsulation,
inheritance, and polymorphism.

Classes and Objects

A class is the template for an object and a way to encapsulate both data
(called fields in Java) and the functions (called methods) that operate on
that data. The Inheritance, enables a class, called the subclass, inheriting
the capabilities of a base class, called a superclass in Java. The
polymorphism enables you to create virtual methods that can be
implemented differently in derived classes. In this article, you'll apply
what you know about object-oriented programming towards creating Java
classes.

Defining a Simple Class

As I said, a class is sort of a template for an object. In this way, a class is


equivalent to a data type such as int. The main difference is that Java
already knows what an integer is. However, when you create a class, you
must tell Java about the class's characteristics. You define a class by using
the class keyword along with the class name, like this:

class MyClass

Believe it or not, the preceding lines are a complete Java class. If you save
the lines in a file called MyClass.java, you could even compile the class
into a .CLASS file, although the file won't actually do anything if you tried
to run it. As you can see, the class definition begins with the keyword class
followed by the name of the class. The body of the class is marked off by
curly braces just like any other program block. In this case, the class's
body is empty. Because its body is empty, this example class doesn't do
anything. You can, however, compile the class and even create an object
from it. To create an object from a class, you type the class's name
followed by the name of the object. For example, the line below creates an
object from the MyClass class:

MyClass myObject = new MyClass();

Objects and Classes


Programmers implement classes
Classes are templates or blueprints for Objects
Data and methods are defined within Classes
Classes must provide an implementation such that objects created
from those classes behave as those defined in the Object model.
An Object is the manifestation of a class
An object is an Instance of a class
The process of creating an object is called instantiation
The attributes of an object are called instance variables
The methods of an object are called instance methods
In Java, Objects are created using the new keyword:
Employee anEmployee = new Employee();

Defining Classes

A class definition must have the following:


The keyword "class" followed by the name of the class
The class body
Before the keyword "class" is the optional modifier "public"
If a class is public, it must be defined within a file which is the same
name as the class with a ".java" extension.
i.e. Classname.java
eg. HelloWorld.java, Account.java, Ledger.java, Transaction.java
most classes are declared public
The class body contains:
Zero or more instance variables
Zero or more methods

Example Class Definition


public class Employee
{
String name;
int salary;
Date startingDate;
[... more variable definitions ...]

public int getSalary()


{
return salary;
}
public int computeHourlyRate()
{
// calculate hourly rate from salary
}
[... more method definitions ...]
}

Classes as types

When a class is defined, the compiler regards the class as a new type.
When a variable is declared, its type can be a primitive type or "Class"
type.
Any variable whose type is a class is an object reference.
The variable is a reference to an instance of the specified class.
The variables holds the address (in memory) of the object.

Declaring Fields for a Class

As I said, the MyClass example class doesn't do much yet. In order to be


useful, it needs both data fields and methods. You declare fields for your
class in much the same way you declare any variable in a program, by
typing the data type of the field followed by the name of the field, like
this:

int myField;

The above line declares a data field of type integer. However, looking at
the above line doesn't tell you much about how data fields are used with
classes. In fact, you can't tell from the above line whether myField is
actually part of an object or just a normal variable. To clear up this
ambiguity, you can plug the above line into the MyClass class definition,
as shown in Listing1.

Listing 1: Adding a Data Field to a Class.

class MyClass
{
int myField

Now you can see that myField is a data field of the MyClass class.
Moreover, this data field is by default accessible only by methods in the
same package. (For now, you can think of a package as a file.) You can
change the rules of this access by using the public, protected, and private
keywords. A public data field can be accessed by any part of a program,
inside or outside of the class in which it's defined. A protected data field
can only be accessed from within the class or from within a derived class
(a subclass). A private data field cannot even be accessed by a derived
class.

Defining a Constructor

You have now added a data field to MyClass. However, the class has no
methods and so can do nothing with its data field. The next step in
defining the class, then, is to create methods. One special type of method,
called a constructor, enables an object to initialize itself when it's created.
A constructor is a public method (a method that can be accessed anywhere
in a program) with the same name as the class. Listing2 shows the
MyClass class with its constructor in place.

Listing 2: Adding a Constructor to a Class.

class MyClass

{
int myField;

public MyClass(int value)

{
myField = value;
}
}

As you can see, the class's constructor starts with the public keyword. This
is important because you want to be able to create an object from the class
anywhere in your program, and when you create an object, you're actually
calling its constructor. After the public keyword comes the name of the
constructor followed by the constructor's arguments in parentheses. When
you create an object of the class, you must also provide the required
arguments.

Example: Creating an Object by Calling a Constructor

If you want to create an object from MyClass, you must supply an integer
value that the class uses to initialize the myField data field. This integer is
the MyClass constructor's single argument. You'd create an object of the
class like this:

MyClass myObject = new MyClass(1);

This line not only creates an object of the MyClass class, but also
initializes the myField data field to 1. The first word in the line tells Java
that myObject is going to be an object of the MyClass class. The next
word is the object's name. After the equals sign comes the keyword new
and the call to the class's constructor.

Creating Classes

Objects in the object model are formalized


Objects are abstracted into classes
Only attributes and methods relevant to our domain are classified.
Attributes are formalized into instance variables
Behaviour is formalized into methods
Classes are represented on a class diagram
Object interaction is also abstracted
Associations are identified
Added to class diagram

Classes and the class diagram represent the static structure of the
system
How the system behaves is not represented by this model

Example Object Model


Debit

Credit

Debit

Debit

Debit

null References
null means refers to no object"
Object references can be compared to null to see if an object is
present or not.
null is the default value of an object reference before it is initialized
e.g :-
Employee anEmployee;
[...]
if (anEmployee == null)
{
}

REFRENCE

1. https://www.slideshare.net/BhagawatAdhikari1/root-finding-

method?qid=a3e3c177-d5ec-48f9-a883-e59c08a49980&v=&b=&from_search=2
2. http://math.tutorvista.com/calculus/bisection-method.html

3. https://en.wikiversity.org/wiki/The_bisection_method

4. https://ece.uwaterloo.ca/~dwharder/class/programming/10RootFinding/bisection

También podría gustarte