java foundations: unit 1faculty.washington.edu/jstraub/javafoundations/unit1.pdfa java program that...
TRANSCRIPT
Java Foundations:Unit 1
The Java Programming Environment
Work Flow
Java Source File
(.java)
Java Compiler (javac)
Java Class File(.class)
Java Class File(.class)
Java Class File(.class)
Java Runtime (java)
Java Runtime (java)
Other class files, jar files
Setting Up Your Java Environment (1)• Download the latest JDK (Java SE) from the java home
page.
Setting Up Your Java Environment (2)• Download and unzip the files from
http://faculty.washington.edu/jstraub/JavaFoundations/ClassDownloads/classes.zip. Make a note of the installation directory.
Setting Up Your Java Environment (3)• Create a directory for storing your work.
Setting Up Your Java Environment (4)• Open a command line, and switch to your work directory.
Microsoft Windows
Unix
Setting Up Your Java Environment (5)• Find the bin directory in your JDK installation.
Setting Up Your Java Environment (6)• Set your path so that it includes the java bin directory.
Microsoft Windows
Unix
Setting Up Your Java Environment (7)• Set your classpath so that it includes the JavaFoundations
class directory.Microsoft Windows
Unix
Setting Up Your Java Environment (8)• Your first Java program; enter it into a source file named
HelloWorld.java exactly as it appears below:
• Compile and execute your program:
public class HelloWorld{
public static void main( String[] args ){
System.out.println( "Hello World!" );}
}
C:\JavaTest>javac HelloWorld.java
C:\JavaTest>java HelloWorldHello World!
Setting Up Your Java Environment (9)• Your second Java program; enter it into a source file
named ToothAche.java; if the compiler can’t find Turtle, it’s because your classpath is wrong.
public class ToothAche{
public static void main( String[] args ){
Turtle spot = new Turtle();spot.paint( 45, 128 );
}}
C:\JavaTest>javac ToothAche.javaToothAche.java:5: error: cannot find symbol
Turtle spot = new Turtle();^
ExerciseEach of the following files contains part of a java program. Download each file from http://faculty.washington.edu/jstraub/JavaFoundations/ClassDownloads/classes.zip and complete the program, then compile and run it. Note that you will have to change the file name before you can compile it. And remember: capitalization counts.
• project1.javaA java program that prints Good morning Dave.
• project2.txtA java program that prints Goodbye cruel world.
• project3.txtA java program that prints Good night Gracie.
• project4.txtA java program that prints Go dog go.
• project5.txtA java program that prints Red dog blue dog.
Variables
Primitives: Integer TypesType Min Value Max Valuelong ‐2e63 2e63 – 1int About ‐2.1 billion About 2.1 billion
short ‐32,768 32,767
char 0 65,535
byte ‐128 127
boolean true or false
Primitives: Integer ConstantsConstant Type42L long42 int
‘a’ char
boolean true or false
Primitives: Integer LiteralsConstant Type42L long42 int
‘a’ char
true or false boolean
0123 int (octal)
0x12F int (hexadecimal)
0b1101 int (binary)
‘u1234’ char (hex/unicode)
Primitives: Escape CharacterConstant Character\b backspace\t tab
\n newline
\f form feed
\r return
\” Double quote
\’ Single quote
\\ backslash
Primitives: Floating Point TypesType Precision, Digits Exponentdouble 15.95 ‐1022 to 1023float 7.22 ‐126 to 127
Primitives: Floating Point LiteralsConstant Type42.0 double42.0f float
4.555e2 double
5.444e2f float
Primitives: Examplepublic class Prims{
public static void main( String[] args ){
int inx = 97;double unit = .63;double result = inx * unit;System.out.println( result );
}}
Objects: Definition• An object stores data and executes actions.• An object has a name and a type.
What a Turtle knows:• What color am I?• What is my position?• What direction am I facing?
What a Turtle can do:• Turn and paint a line.• Turn and move.• Paint a circle.• Fill a box.• Fill a circle.• Etc.; see Turtle and Vic Class Documentation on the class website.
Objects: Declaration and Initialization• An object must be declared:
Turtle pokey;
• An object must be created:pokey = new Turtle();
• An object can be declared and created at the same time:Turtle pokey = new Turtle();
Objects: Methods• A method is a function that is used to interact with an
object.• A method is invoked using the executor (a period).• A method has 0 or more arguments that control its
execution.Turtle pokey = new Turtle();pokey.fillBox( 64, 128 );
executor methodarguments
Objects: Examplepublic class Test{public static void main(String[] args){Turtle todd = new Turtle();todd.move( 180, 128 ); // Move to lefttodd.move( 180, 0 ); // Restore orient.todd.paint( 90, 64 ); // Right sidetodd.paint( 90, 64 ); // Top sidetodd.paint( 90, 64 ); // Left sidetodd.paint( 90, 64 ); // Bottom side
}}
Exercises1. Write a program that uses one Turtle to draw an octagon 64
pixels on a side (note: there are 60 degrees in the external angle of an octagon.
2. Write a program that creates two Turtles; use one to draw a red circle on the left of the screen (use swingAround), use the other to fill a blue box (use switchTo to change colors).
3. Write a program that uses a Turtle to draw a house 200 pixels wide and 150 pixels tall, with one door and two windows.