Building a Simple Java Class
Goals
This laboratory exercise provides practice designing and implementing a
simple Java class for quadratic functions of the form f(x) =
ax2 + bx + c.
A Quadratic Function Class
A simple Quadratic function class might have the following
elements:
-
fields for values a, b, and c,
-
a constructor with no parameters (setting a, b, and
c to 1),
-
a constructor with 3 parameters, to specify a, b, and
c,
-
three extractors that return the values of a, b, and
c, respectively,
-
a modifier that requires three parameters, one each to give new values to
a, b, and c,
-
a compute method that takes a parameter x and that returns the
value f(x) for the given values of a, b, and
c, and
a main method that exercises each of the methods.
Using program ~walker/java/examples/course/Course.java
as a model, write a class Quadratic.
Notes:
-
Class Quadratic should be placed in a new package
functionRoots, based in a directory by that name within your
java subdirectory.
-
Class Quadratic should be defined in file Quadratic.java,
as the class name and file name must agree.
-
Quadratic will have three data fields: a, b,
and c -- each of which should be of type double.
-
Quadratic will have two constructors: one with no parameters, and
one with three parameters. Thus, there will be two methods named
Quadratic, although these two methods will have different numbers
of parameters. We say that the constructor method name Quadratic
is overloaded as one method name is used in two contexts.
In Scheme, you experienced this overloading many times: for example,
+ was used to add integers, rational numbers, real numbers, and
complex numbers in any combination. In that case, + represented
different operations (based on the type of data encountered), but the same
symbol + was used in each case.
In Java, you have already seen that + can be used to represent the
concatenation of strings. In addition, + is the symbol used to
add numbers. Thus, "3" + "5" yields "35", while
3 + 5 gives 8.
-
Quadratic will have three extractor methods: geta,
getb, and getc. None of these methods should require any
values be supplied as parameters.
-
Quadratic will have a modifier method setCoefficients
that will require three double parameters and that should change
an object's data fields appropriately.
-
Quadratic will have a compute method that has one
parameter x and that computes the value of f(x) for that
x and for the values of a, b, and c
stored in that object.
As in other languages, Java provides arithmetic operations +,
-, *, and / for addition, subtraction,
multiplication, and division, respectively. Note, however, that when
/ is applied to two integers, the result is an integer and any
remainder is discarded. Thus, 3.0 / 5.0 (for real numbers) gives
0.6, while 3 / 5 (for integers) yields 0
-
Quadratic will have a main method that declares a
PrintWriter variable and at least three Quadratic
objects. Then main should do sufficient work with the
Quadratic objects to use both constructors; retrieve and print
values of a, b, and c; change these values;
retrieve and print those values again; and perform some computations of
f(x).
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/153.sp05/labs/lab-building-classes.shtml