Lab02: Creating Classes and Methods

Objectives

Setup

First, create a lab02 directory:

$ cd cs190m
$ mkdir lab02
$ cd lab02

Then, download Complex.java and Lab02.java into the folder you just created.

Complex Numbers

Complex or imaginary numbers are a mathematical concept composed of a real part and an imaginary part. The real part of a number is simply any real number, while the imaginary part is any real number multiplied by the square root of -1 (in standard algebraic terms, you can't define the square root of -1, so instead we call it i). A complex number is written in the form "a+bi", where a is the real part and b is the coefficient for the imaginary part. Complex numbers, like real numbers, can be added, subtracted, multiplied, and divided. The rules for doing so are explained here.

There are five distinct parts to the Complex class that we'll be creating. You'll notice that the file you downloaded is mostly empty, except for two lines. Your job will be to implement all 5 parts of the class.

Part 1: Instance variables

As described above, there are two parts to a complex number. Translating this to code, this means we have to declare two instance variables of type double. Recall that complex numbers take the form "a+bi"; the instance variables will represent a and b.

Part 2: Constructor

Constructors are used to initialize new objects, as you learned in class. Your task here is to make a constructor that takes two parameters that represent the a and b of the new complex number and assign it to the instance variables you declared.

Part 3: Accessors

Since code outside of the Complex class currently cannot access the instance variables you have (if they can, something is wrong), you should create one accessor method for each variable.

Part 4: Arithmetic operations

The article linked above describes the formulas for the four arithmetic operations over complex numbers. You should implement the four operations as four methods: add, subtract, multiply, and divide. All methods should take one parameter, which should be another Complex object and return a new Complex object that represents the result of the operation. It should not modify either object.

Note: We return a new object on arithmetic operations to closely emulate arithmetic with standard ("real") numbers. That is, when we take x + y, we don't expect x or y to change. Rather, a new value is generated and can be stored in a new variable (e.g., z = x + y). Likewise, we express complex addition as Complex z = x.add(y).

Part 5: String representation

You should create a method called toString that returns a String. It should return a string that represents the complex number in the form "a+bi". So if a is 3 and b is 4, the string returned should be "3+4i".

Testing Your Program

The Lab02.java file has some basic tests that performs some simple operatins and prints the result to screen. It's up to you to verify that it's correct; also, all cases may not be covered so you may wish to try other ones.

Turning In Your Lab

To turn in your lab:

$ cd ~/cs190m/lab02
$ turnin -v -c cs190m -p lab02 *.java

Getting Help

Many of you will probably run across problems while programming at some point during a lab. If that's the case, here are the resources you should use, in order:

  1. Your textbook
  2. Lecture notes
  3. Java API documentation
  4. Your lab TA

Lab created by: Daniel Tang