Java Ch 6

Ace your homework & exams now with Quizwiz!

getLength2

: Returns the value of the length field. public double getLength() ...

getWidth2

: Returns the value of the width field. public double getWidth() ...

setLength2

: Sets the value of the length field. public void setLength(double len) ...

setWidth2

: Sets the value of the width field. public void setLength(double w) ...

No-Arg Constructor

A constructor that does not accept arguments is known as a no-arg constructor. The default constructor (provided by Java) is a no-arg constructor. public Rectangle() { length = 1.0; width = 1.0; }

Method Signature

A method signature consists of the method's name and the data types of the method's parameters, in the order that they appear. The return type is not part of the signature.

Shadowing

A parameter variable is, in effect, a local variable. Within a method, variable names must be unique. A method may have a local variable with the same name as an instance field. This is called shadowing. The local variable will hide the value of the instance field. Shadowing is discouraged and local variable names should not be the same as instance field names.

Access Specifiers

An access specifier is a Java keyword that indicates how a field or method can be accessed.

Objects and Classes

An object exists in memory, and performs a specific task. Objects have two general capabilities: Objects can store data. The pieces of data stored in an object are known as fields. Objects can perform operations. The operations that an object can perform are known as methods.

Data Hiding

An object hides its internal, private fields from code that is outside the class that the object is an instance of. Only the class's methods may directly access and make changes to the object's internal data. Code outside the class must use the class's public methods to operate on an object's private fields.

Constructors

Classes can have special methods called constructors. A constructor is a method that is automatically called when an object is created. Constructors are used to perform operations at the time an object is created. Constructors typically initialize instance fields and perform other object initialization tasks.

Packages and import Statements

Classes in the Java API are organized into packages. Explicit and Wildcard import statements Explicit imports name a specific class import java.util.Scanner; Wildcard imports name a package, followed by an * import java.util.*; The java.lang package is automatically made available to any Java class.

Constructors 2

Constructors have the same name as the class. Constructors have no return type (not even void). Constructors may not return any values. Constructors are typically public.

Instance Fields and Methods

Fields and methods that are declared as previously shown are called instance fields and instance methods. Objects created from a class each have their own copy of instance fields. Instance methods are methods that are not declared with a special keyword, static.

Finding the classes

Get written description of the problem domain Identify all nouns, each is a potential class Refine list to include only classes relevant to the problem

Rectangle Class Constructor Overload

If we were to add the no-arg constructor we wrote previously to our Rectangle class in addition to the original constructor we wrote, what would happen when we execute the following calls? Rectangle box1 = new Rectangle(); Rectangle box2 = new Rectangle(5.0, 10.0);

Stale Data

It would be impractical to use an area variable here. Data that requires the calculation of various factors has the potential to become stale. To avoid stale data, it is best to calculate the value of that data within a method rather than store it in a variable.

The String Class Constructor

One of the String class constructors accepts a string literal as an argument. This string literal is used to initialize a String object. String name = "Michael Long";

Converting the UML Diagram to Code

Putting all of this information together, a Java class file can be built easily using the UML diagram. The UML diagram parts match the Java class file structure.

Stale Data Correction

Rather than use an area variable in a Rectangle class: public double getArea() { return length * width; } Now, any change to the length or width variables will not leave the area of the rectangle stale.

Uninitialized Local Reference Variables

Reference variables can be declared without being initialized. Rectangle box; This statement does not create a Rectangle object, so it is an uninitialized local reference variable. A local reference variable must reference an object before it can be used, otherwise a compiler error will occur. box = new Rectangle(7.0, 14.0); box will now reference a Rectangle object of length 7.0 and width 14.0.

The Default Constructor2

The default constructor is a constructor with no parameters, used to initialize an object in a default configuration. The only time that Java provides a default constructor is when you do not write any constructor for a class. A default constructor is not provided by Java if a constructor is already written.

getArea

The getArea method will return the area of the rectangle, which is the result of the object's length multiplied by its width.

getLength

The getLength method will return the value in an object's length field.

getWidth

The getWidth method will return the value in an object's width field.

Mutator Method

The methods that modify the data of fields are called mutators. programmer wishes to be modified by other classes needs a mutator.

Accessor Method

The methods that retrieve the data of fields are called accessors. Each field that the programmer wishes to be viewed by other classes needs an accessor. Each field that the

Binding

The process of matching a method call with the correct method is known as binding. The compiler uses the method signature to determine which version of the overloaded method to bind the call to.

setLength

The setLength method will store a value in an object's length field.

setWidth

The setWidth method will store a value in an object's width field.

Identify the responsibilities

Things a class is responsible for knowing Things a class is responsible for doing Refine list to include only classes relevant to the problem

Overloading Methods and Constructors

Two or more methods in a class may have the same name as long as their parameter lists are different. When this occurs, it is called method overloading. This also applies to constructors. Method overloading is important because sometimes you need several different ways to perform the same operation.

UML Data Type and Parameter Notation

UML diagrams are language independent. UML diagrams use an independent notation to show return types, access modifiers, etc.

Objects and classes 1

When a program is running, it can use the class to create, in memory, as many objects of a specific type as needed. Each object that is created from a class is called an instance of the class.

The Default Constructor

When an object is created, its constructor is always called. When an object is created, its constructor is always called.

private

When the private access specifier is applied to a class member, the member cannot be accessed by code outside the class. The member can be accessed only by methods that are members of the same class.

public

When the public access specifier is applied to a class member, the member can be accessed by code inside the class or outside.

Passing Objects as Arguments

When you pass a object as an argument, the thing that is passed into the parameter variable is the object's memory address. As a result, parameter variable references the object, and the receiving method has access to the object.

Random

for generating random numbers

Scanner

for reading input

PrintWriter

for writing data to files

class

is code that describes a particular type of object. It specifies the data that an object can hold (the object's fields), and the actions that an object can perform (the object's methods).

Rectangle object

length. The length field will hold the rectangle's length. width. The width field will hold the rectangle's width.

Unified Modeling Language (UML)

provides a set of standard diagrams for graphically depicting object-oriented systems.


Related study sets

when to reject or fail to reject null hypothesis

View Set

Chapter 5 Practice Quiz- Part three (Principles of Management)

View Set

Chapter 6 Electricity and Magnetism

View Set

Washington Laws and Rules Pertinent to Insurance

View Set

تاريخ الفصل الرابع 💜

View Set

Chapter 6 Test Questions (PHYS EDUCATION)

View Set