WebReference.com - Part 5 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (6/6)
[previous] |
Beginning Java 2 SDK 1.4 Edition
Summary
In this chapter you have learned all the essentials of defining your own classes. You can now create your own class types to fit the context of the problems you are dealing with. We will build on this in the next chapter to enable you to add more flexibility to the operations on your class objects by showing you how to realize polymorphism.
The important points covered in this chapter are:
- A class definition specifies the variables and methods that are members of the class.
- Each class must be saved in a file with the same name as the class, and with the extension
.java
. - Class variables are declared using the keyword
static
, and one instance of each class variable is shared amongst all objects of a class. - Each object of a class will have its own instance variables--these are variables declared without using the keyword
static
. - Methods that are specified as
static
can be called even if no class objects exist, but astatic
method cannot refer to instance variables. - Methods that are not specified as
static
can access any of the variables in the class directly. - Recursive methods are methods that call themselves.
- Access to members of a class is determined by the access attributes that are specified for each of them. These can be
public
,private
,protected
,package private
, or nothing at all. - Classes can be grouped into a package. If a class in a package is to be accessible from outside the package the class must be declared using the keyword
public
. - To designate that a class is a member of a package you use a
package
statement at the beginning of the file containing the class definition. - To add classes from a package to a file you use an
import
statement immediately following any package statement in the file. - A native method is a method implemented in a language other than Java. Java programs containing native methods cannot be applets and are no longer portable.
- A field does not get ploughed by turning it over in your mind.
Exercises
Define a class for rectangle objects defined by two points, the top-left and bottom-right corners
of the rectangle. Include a constructor to copy a rectangle, a method to return a rectangle object,
that encloses the current object and the rectangle passed as an argument, and a method to display
the defining points of a rectangle. Test the class by creating four rectangles, and combining
these cumulatively, to end up with a rectangle enclosing them all. Output the defining points
of all the rectangles you create.
Define a class, mcmLength
, to represent a length measured in meters, centimeters,
and millimeters, each stored as integers. Include methods to add and subtract objects, to
multiply and divide an object by an integer value, to calculate an area resulting from the
product of two objects, and to compare objects. Include constructors that accept: three
arguments--meters, centimeters, and millimeters; one integer argument in millimeters; one
double
argument in centimeters and no arguments, which creates an object with
the length set to zero. Check the class by creating some objects and testing the class
operations.
Define a class, tkgWeight
, to represent a weight in tons, kilograms, and grams,
and include a similar range of methods and constructors as the previous example. Demonstrate
this class by creating and combining some class objects.
Put both the previous classes in a package called Measures
. Import this package
into a program that will calculate and display the total weight of the following: 200
carpets--size: 4 meters by 2 meters 9 centimeters, that weigh 1.25 kilograms per square
meter; and 60 carpets--size: 3 meters 57 centimeters by 5 meters, that weigh 1.05 kilograms
per square meter.
Define a class for rectangle objects defined by two points, the top-left and bottom-right corners of the rectangle. Include a constructor to copy a rectangle, a method to return a rectangle object, that encloses the current object and the rectangle passed as an argument, and a method to display the defining points of a rectangle. Test the class by creating four rectangles, and combining these cumulatively, to end up with a rectangle enclosing them all. Output the defining points of all the rectangles you create.
Define a class, mcmLength
, to represent a length measured in meters, centimeters,
and millimeters, each stored as integers. Include methods to add and subtract objects, to
multiply and divide an object by an integer value, to calculate an area resulting from the
product of two objects, and to compare objects. Include constructors that accept: three
arguments--meters, centimeters, and millimeters; one integer argument in millimeters; one
double
argument in centimeters and no arguments, which creates an object with
the length set to zero. Check the class by creating some objects and testing the class
operations.
Define a class, tkgWeight
, to represent a weight in tons, kilograms, and grams,
and include a similar range of methods and constructors as the previous example. Demonstrate
this class by creating and combining some class objects.
Put both the previous classes in a package called Measures
. Import this package
into a program that will calculate and display the total weight of the following: 200
carpets--size: 4 meters by 2 meters 9 centimeters, that weigh 1.25 kilograms per square
meter; and 60 carpets--size: 3 meters 57 centimeters by 5 meters, that weigh 1.05 kilograms
per square meter.
[previous] |
Created: August 5, 2002
Revised: August 5, 2002
URL: https://webreference.com/programming/java/beginning/chap5/5/6.html