Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error messages.
class Vehicle { private Engine e; Vehicle (Engine e) { this.e = e; } }.
Last month, I presented you with a single exercise and two questions. Here are those questions and my answers in red:
realPart + imaginaryPart * i, where i represents the square root of -1. The program should include the following:Complex class that represents the real and imaginary parts in private floating-point variables
public get methods that return the real and imaginary parts
public add(Complex c) method that separately adds the real parts of the current and c argument Complex objects together, and the imaginary parts of both objects together
public subtract(Complex c) method that separately subtracts the real part of the c-referenced Complex object from the current Complex object, and the imaginary part of the c-referenced Complex object from the current Complex object
public print() method that prints the contents of the current Complex object using the format (a, b) -- where a represents the real part and b represents the imaginary part
The following listing provides source code to a UseComplex application, with a Complex class and a UseComplex class that creates and manipulates Complex objects.
UseComplex.java
// UseComplex.java
class UseComplex
{
public static void main (String [] args)
{
Complex c1 = new Complex (2.0, 5.0); // 2.0 + 5.0i
Complex c2 = new Complex (-3.1, -6.3); // -3.1 - 6.3i
c1.add (c2); // c1 is now -1.1 - 1.3i
c1.print ();
}
}
class Complex
{
private double re, im;
Complex (double real, double imag)
{
re = real;
im = imag;
}
public double getRe ()
{
return re;
}
public double getIm ()
{
return im;
}
public void add (Complex c)
{
re += c.getRe ();
im += c.getIm ();
}
public void subtract (Complex c)
{
re -= c.getRe ();
im -= c.getIm ();
}
public void print ()
{
System.out.println ("(" + re + "," + im + ")");
}
}
An enumerated type is important because its restricted set of values prevents irrational code that can lead to bugs. Such irrational code arises from the use of integers (instead of enumerated types) to distinguish among objects of the same type.
Read "When Is a Singleton Not a Singleton?" by Joshua Fox (JavaWorld, January 2001).
Answer the following question and work on the following exercise:
CarDemo application already use composition?
CarDemo to use composition.