Introduction
Java is an object oriented (OO) programming language. The key idea behind object oriented programming is to identify objects that need to be represented in your program and then group all of the data associated with the object in a blob of bytes. In addition, we can define behavior that will operate on the the blob of data. This allows us to simplify our programs because all of the code associated with defining and managing the objects can be placed in a separate source file. Only the implementer(s) of the class need to worry about the details of how the objects are stored (what attributes are required) and manipulated (the implementation of methods defining desired behavior).
Object Free Messiness
Suppose we want a program that asks the user to enter two complex numbers and displays the result of multiplying the two complex numbers together. It might look something like this:
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // Get first number
- System.out.println("Enter a complex number in the form: 3.0 + 4.3i");
- String line = in.nextLine();
- Scanner parser = new Scanner(line.substring(0, line.length()-1));
- double realOne = parser.nextDouble();
- parser.next();
- double imagOne = parser.nextDouble();
- // Get second number
- System.out.println("Enter a complex number in the form: 3.0 + 4.3i");
- line = in.nextLine();
- parser = new Scanner(line.substring(0, line.length()-1));
- double realTwo = parser.nextDouble();
- parser.next();
- double imagTwo = parser.nextDouble();
- // Calculate result of multiplying two numbers
- double realAnswer = realOne * realTwo - imagOne * imagTwo;
- double imagAnswer = realOne * imagTwo + imagOne * realTwo;
- System.out.println("(" + realOne + " + " + imagOne + "i) * ("
- + realTwo + " + " + imagTwo + "i) = ("
- + realAnswer + " + " + imagAnswer + "i)");
- }
This produces a result that looks like this:
Enter a complex number in the form: 3.0 + 4.3i 1.0 + 3.0i Enter a complex number in the form: 3.0 + 4.3i 2.0 + 2.0i (1.0 + 3.0i) * (2.0 + 2.0i) = (-4.0 + 8.0i)
Object Oriented Clarity
This code above looks pretty complicated because our main method is
required to manage all of the intricate details of complex numbers.
The rest of this page will focus on developing a Complex
class that will define all of the code specific to managing complex
numbers. When completed, it will allow us to simplify our program so
that it looks like:
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // Get first number
- System.out.println("Enter a complex number in the form: 3.0 + 4.3i");
- Complex c1 = new Complex(in.nextLine());
- // Get second number
- System.out.println("Enter a complex number in the form: 3.0 + 4.3i");
- Complex c2 = new Complex(in.nextLine());
- // Calculate result of multiplying two numbers
- Complex answer = c1.times(c2);
- System.out.println(c1 + " * " + c2 + " = " + answer);
- }
In fact, the code is so much cleaner that we don't really need to include the comments. The intent of the code is so much clearer:
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.println("Enter a complex number in the form: 3.0 + 4.3i");
- Complex c1 = new Complex(in.nextLine());
- System.out.println("Enter a complex number in the form: 3.0 + 4.3i");
- Complex c2 = new Complex(in.nextLine());
- System.out.println(c1 + " * " + c2 + " = " + c1.times(c2));
- }
Defining Custom Classes
A user defined class should be in a separate file that has the
same name as the name of the class (the following would be stored
in a file called Complex.java
). A class is defined
as follows:
public class Complex { // Details of the class implementation go here }
Object Attributes
We can declare attributes for each object from the class as follows:
public class Complex { private double real; private double imag; // ... }
Whenever we create an object from the Complex
class,
the object will have two object variables: real
and
imag
(both double
s) that can be
used to store the value of a complex number. If we were to
create a Complex
object like this:
public static void main(String[] args) { Complex c1 = new Complex(); }
here's what it would look like in memory:
Last modified: Monday, 29-Jul-2024 06:55:07 EDT