CSC 153 Grinnell College Spring, 2005
 
Computer Science Fundamentals
Laboratory Exercise
 

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:

Using program ~walker/java/examples/course/Course.java as a model, write a class Quadratic.

Notes:

  1. Class Quadratic should be placed in a new package functionRoots, based in a directory by that name within your java subdirectory.

  2. Class Quadratic should be defined in file Quadratic.java, as the class name and file name must agree.

  3. Quadratic will have three data fields: a, b, and c -- each of which should be of type double.

  4. 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.

  5. Quadratic will have three extractor methods: geta, getb, and getc. None of these methods should require any values be supplied as parameters.

  6. Quadratic will have a modifier method setCoefficients that will require three double parameters and that should change an object's data fields appropriately.

  7. 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

  8. 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.cs.grinnell.edu/~walker/courses/153.sp05/labs/lab-building-classes.shtml

created April 8, 2001
last revised March 24, 2005
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.