Objects in Java. Object-Based Programming Outline. Introduction Implementing a Complex class Class scope Controlling Access to Members Initializing Class Objects: Constructors Using set & get Methods. Object-Based Programming Outline . Software Reusability final Instance Variables
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Objects in Java
Peter Cappello
into objects.
// Complex - implements a complex number
public class Complex
{
private double real, // the real part
imag; // the imaginary
public Complex(double r, double i)
{
real = r;
imag = i;
}
public Complex(Complex c)
{
real = c.getReal();
imag = c.getImag();
}
public double getReal() { return real; }
public double getImag() { return imag; }
public void add(Complex c)
{
real += c.getReal();
imag += c.getImag();
}
public void multiply(Complex c)
{
double temp = real * c.getReal() - imag * c.getImag();
imag = real * c.getImag() + imag * c.getReal();
real = temp;
}
public double sizeSquared() { return real*real + imag*imag; }
} // end Complex class
import java.awt.*;
import java.applet.*;
public class test1 extends Applet
{
public void paint(Graphics g)
{
Complex c = new Complex(1,0), d = new Complex(c), e = c;
System.out.println("e.real = " + e.getReal() + ", d.real = " + d.getReal());
c.add(c);
System.out.println("e.real = " + e.getReal() + ", d.real = " + d.getReal());
}
}
Output:
e.real = 1.0, d.real = 1.0
e.real = 2.0, d.real = 1.0
// compute the count associated with a complex point
final private int getCount(Complex c, int lim)
{
Complex z = new Complex(c);
int count = 1;
for (; z.sizeSquared() < 4.0 && count < lim; count++)
{
z.multiply(z);
z.add(c);
}
return count;
}
Complex c = new Complex(0,0);
. . .
c = new Complex(1,1);
public double getReal()
{ return real; }
z.multiply(z);
z.add(c);
import java.applet.*;
public class test1 extends Applet
{
public void paint(Graphics g)
{
Complex c = new Complex(0,0);
c.real = 1; // compile error - real is private
}
}
“Variable real in Complex not accessible from Complex”
public class Complex
{
private double real, // the real part of the complex number
imag; // the imaginary part of the number
public Complex(double r, double i)
{
real = r;
imag = i;
}
public Complex(Complex c)
{
real = c.getReal();
imag = c.getImag();
}
. . .
import java.applet.*;
import java.awt.*;
public class checkers extends Applet {
public void paint( Graphics g ) {
final int SIZE = 8, EDGE = 40, X = 10, Y = 10;
g.fillRect(X, Y, SIZE*EDGE, SIZE*EDGE); // paint black square
// paint red squares
g.setColor(Color.red);
for (int row = 0; row < SIZE; row++)
for (int col = 0; col < SIZE; col += 2)
// paint a row of red squares
g.fillRect(X + (row % 2 + col)*EDGE, Y + row*EDGE, EDGE, EDGE);
}
}
return this; // return myself!