[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Back to Project Discussion. On to Building Graphical Programs.
Held Monday, September 20, 1999
Overview
Today, we will consider Java's built-in array type, which
lets you build simple indexed collections of objects. We will compare
arrays to Scheme's lists and vectors and then use arrays to build a
better version of the MailMessage class.
Notes
Contents
Handouts
Summary
MailMessage class.
/**
* Complex numbers. Created as an interface because there are
* different ways to implement them.
*
* @author Samuel A. Rebelsky
* @version 1.0 of September 1999
*/
public interface Complex {
/** Set the value to a + bi. */
public void setValue(double a, double b);
/** Get the real part. */
public double getReal();
/** Get the imaginary part. */
public double getImaginary();
} // interface Complex
import Complex;
import Drawable;
import java.awt.Graphics;
/**
* An implementation of complex numbers. See the corresponding
* interface for more details.
*
* Each complex number is represented with its real part and its
* imaginary part.
*
* @author Samuel A. Rebelsky
* @version 1.0 of September 1999
*/
public class StandardComplex
implements Complex, Drawable
{
// +--------+--------------------------------------------------
// | Fields |
// +--------+
/** The real part of the number. */
public double realPart;
/** The imaginary part of the nubmer. */
public double imaginaryPart;
// +--------------+--------------------------------------------
// | Constructors |
// +--------------+
/**
* Build the complex number 0.0 + 0.0i.
*/
public StandardComplex() {
this.realPart = 0.0;
this.imaginaryPart = 0.0;
} // StandardComplex()
// +---------+-------------------------------------------------
// | Methods |
// +---------+
/** Draw the complex number as a vector. */
public void draw(Graphics pen) {
// Not yet implemented.
} // draw(Graphics)
/** Set the value to a + bi. */
public void setValue(double a, double b) {
this.realPart = a;
this.imaginaryPart = b;
} // setValue(double,double)
/** Get the real part. */
public double getReal() {
return this.realPart;
} // getReal()
/** Get the imaginary part. */
public double getImaginary() {
return this.imaginaryPart;
} // getImaginary()
} // class StandardComplex
StandardComplex class also implements
Drawable
import java.awt.Graphics;
/**
* Things that can draw themselves on the screen.
*
* @author Samuel A. Rebelsky
* @version 1.0 of September 1999
*/
public interface Drawable {
/** Draw the object using a graphics pen. */
public void draw(Graphics pen);
} // interface Drawable
abstract.)
type[] name = new type[size];
stuff,
you would write
double[] stuff = new double(5);
type[] name = { val1, val2, ..., valn };
int[] stuff = {3, 2, 6, 5};
name[index]
stuff with
stuff[3]
i held the value 3, then we could get
element 3 of the stuff with
stuff[i]
name.length
public double sum(double[] stuff) {
double total = 0; // The total of the numbers in stuff
int i; // A counter variable for stepping through
// the array
// Step through the array, adding each subsequent value
for(i = 0; i < stuff.length; i = i + 1) {
total = total + stuff[i];
}
// Return the total value
return total;
} // sum
String[] args
that I always write is an array of strings''.
main as an
array of strings.
import SimpleOutput;
/**
* Print out the parameters. Used as an example of arrays, loops,
* and paramter lists in Java.
*/
public class PrintParams {
public static void main(String[] args) {
SimpleOutput out = new SimpleOutput();
// Print out the number of parameters.
if (args.length == 0)
out.println("There are no parameters.");
else if (args.length == 1)
out.println("There is one parameter.");
else
out.println("There are " + args.length + " parameters.");
for (int i = 0; i < args.length; ++i) {
out.println(i + ": " + args[i]);
}
} // main(String[])
} // class PrintParams
% /home/rebelsky/bin/ji PrintParams % /home/rebelsky/bin/ji PrintParams Hello % /home/rebelsky/bin/ji PrintParams Hello World % /home/rebelsky/bin/ji PrintParams "Hello World" % /home/rebelsky/bin/ji PrintParams 'Hello World' % /home/rebelsky/bin/ji PrintParams Hello-World % set foo = salad % /home/rebelsky/bin/ji PrintParams $foo bar % /home/rebelsky/bin/ji PrintParams "$foo bar" % /home/rebelsky/bin/ji PrintParams '$foo bar' % /home/rebelsky/bin/ji PrintParams ls % /home/rebelsky/bin/ji PrintParams `ls`
MailMessage Class, RevisitedMailMessage class.
MailMessage
to provide.
setXXX and getXXX where XXX
includes: Date, Subject, Sender, and ReplyTo.
setField(String fieldName, String fieldVal)
method.
addRecipient(String name, String email)
addCarbonCopy(String name, String email)
addBlindCarbonCopy(String name, String email)
removeRecipient(String name)
Tuesday, 10 August 1999
Thursday, 16 September 1999
Friday, 17 September 1999
Sunday, 19 September 1999
MailMessage class.
Monday, 20 September 1999
Back to Project Discussion. On to Building Graphical Programs.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
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.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99F/Outlines/outline.15.html
Source text last modified Mon Sep 20 09:52:10 1999.
This page generated on Wed Sep 22 11:16:35 1999 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu