[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]
Held: Wednesday, January 21, 1998
alternate program
used in the back two rows.
let.
block command in scheme.
if
statement. In most imperative languages, if
selects between two alternative sequences of actions (or decides
whether or
not to execute a single alternative) based on the value of
a boolean expression.
do loop of scheme.
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
On to Describing Classes
Back to Introduction to Java
Outlines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Current position in syllabus
[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [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/98S/home/rebelsky/public_html/Courses/CS152/98S/Outlines/outline.03.html
Source text last modified Tue Jan 12 11:52:16 1999.
This page generated on Mon Jan 25 09:48:42 1999 by SiteWeaver. Validate this page.
Contact our webmaster at rebelsky@math.grin.edu