Held: Thursday, March 12, 1998
public)
class and
the name of the class. Custom says that we capitalize the names
of classes.
class
with public.
public we use with the class).
boolean, for true/false values
char, for characters (unlike many other languages,
Java officially supports more than just the basic American
set; it has built-in support for full Unicode)
byte, for very small integers in the range -128 to
127 (integers are numbers without fractional portions)
short, for small integers in the range -32,768 to 32,767.
int, for integers in the approximate range -2 billion to
2 billion
long, for integers in a much bigger range
float, for numbers that may have a fractional
component (e.g., 10.32533).
double, for better accuracy
public class Complex
{
double real_part;
double imaginary_part;
}
Complex has two fields, each of
type double.
/**
* A simple representation of Complex Numbers.
*
* @author Samuel A. Rebelsky
* @version 1.1 of January 1998
*/
public class Complex
{
/**
* The real part of a complex number in standard format.
* The "x" in "x + y*i".
*/
double real_part;
/**
* The imaginary part of a complex number in standard format.
* The "y" in "x + y*i".
*/
double imaginary_part;
} // Complex
class-name variable-name = new class-name()
Complex c1 = new Complex()
return statement).
/**
* A simple representation of Complex Numbers.
*
* @author Samuel A. Rebelsky
* @version 1.1 of January 1998
*/
public class Complex
{
/**
* The real part of a complex number in standard format.
* The "x" in "x + y*i".
*/
double real_part;
/**
* The imaginary part of a complex number in standard format.
* The "y" in "x + y*i".
*/
double imaginary_part;
/**
* Set the value of our imaginary number.
*/
public void setValue(double rpart, double ipart)
{
real_part = rpart;
imaginary_part = ipart;
} // setValue
/**
* Get the real part of this complex number.
*/
public double getReal()
{
return real_part;
} // getReal
/**
* Get the imaginary part of this complex number.
*/
public double getImaginary()
{
return imaginary_part;
} // getImaginary
} // Complex
main method, which is where
the interpreter begins. main has type void
and must be public and static. It takes
one parameter, an array of strings.
main routine for our sample class
/**
* Test various aspects of our complex class.
*/
public static void main(String[] args) {
// Set up an object for printing output
SimpleOutput out = new SimpleOutput();
// Set up a new complex variable
Complex c1 = new Complex();
// Print the initial values of the real number
out.print("Initially, the real part is ");
out.println(c1.getReal());
out.print("Initially, the imaginary part is ");
out.println(c1.getImaginary());
// Set the value to a particular value and see if it's changed
c1.setValue(1.0,3.4);
out.print("Now, the real part is ");
out.println(c1.getReal());
out.print("Now, the imaginary part is ");
out.println(c1.getImaginary());
// Exit gracefully
System.exit(0);
} // main
DrawableObject class
and refine that to particular drawable objects, such as Circle
or Square.
extends keyword.
public class Circle
extends DrawableObject
{
...
} // Circle
DrawableObject obj = new Circle();
public void drawWithStrangeColors(DrawableObject obj)
{
...
} // drawWithStrangeColors(DrawableObject)
...
drawWithStrangeColors(new Circle());
extends clause,
Java assumes that your object extends java.lang.Object.
We'll work on computing assignment 8, which delves further into this simulation.
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Thu May 7 10:53:53 1998.
This page generated on Thu May 7 10:58:50 1998 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu