CSO Gaddis Java Chapter06 6ge
CSO Gaddis Java Chapter06 6ge
R6
A First
Look at
Classes
keyboard Scanner
variable object
rand Random
variable object
outputFile PrintWriter
variable object
Rectangle
length
width
setLength()
setWidth()
getLength()
getWidth()
getArea()
box.setLength(10.0);
The box A Rectangle object
variable holds
the address of length: 10.0
address
the width: 0.0
Rectangle
object.
Method parameters
are shown inside the Rectangle
parentheses using the
same notation as
variables. - width : double
class header
ClassName
{
Fields Fields
Methods Methods
}
public Rectangle()
{
length = 1.0;
width = 1.0;
}
©2016 Pearson Education, Ltd. 6-47
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.
• For instance:
The first call would use the no-arg constructor and box1 would
have a length of 1.0 and width of 1.0.
The second call would use the original constructor and box2
would have a length of 5.0 and a width of 10.0.
+BankAccount()