Introduction to Java

Download Report

Transcript Introduction to Java

Introduction to
Programming
Writing Java Beginning Java
Programs
Review of Lab 1
► Built-in
Types
 Integer types
►No
decimal point
►integer math-op integer yields an integer
►byte, short, int, long
 Real types
►Have
decimal point
►real math-op real yields a real
►float, double
Review of lab 1 Continued
► Java
Variables
 Attributes
► Name
► Type
► Value
 Specifies where data is stored in memory
 Java requires that a variable be declared before it can
be used.
 Declaration Form:
► Type
variable-name;
► Can only declare once!
Review of Lab 1 Continued
► Variable
Naming Conventions
 Start with letter or _ follow with any number of
letters, digits, or _
 Names are case sensitive
 Convention – variable names start with lower
case letter.
 Names should be meaningful.
►sales
►noItems
►qtyOnHand
Camel notation
Review of Lab 1
► What
is wrong?
int firstNum = 0;
int firstNum = firstNum + 1;
► What
does the = operator in Java mean?
► What is the type of the result of
double total, price;
int amt;
total = price * amt;
► What
is wrong with
amt = total/price;
The Semicolon Rule
► Always
end a Java statement with a
semicolon!
 Be careful! The interaction pane does not
require a semicolon after each statement.
 In a program typed into the definition pane,
Java does.
 In the interaction pane, typing a semicolon after
a statement does not show the value of the
result. Leaving the semicolon off does.
Object Variables
► Java is an Object-oriented programming language.
► We work mostly with objects.
► However, built-in numeric types are not objects.
ie. int, float, double, …
► Strings are objects.
► String myName;
 myName is a reference to a string object
 Objects must be created before they are assigned.
 myName has the initial value of null (it does not
reference a string object)
 Much more on this later.
Classes and Objects
► Classes
are templates that define what objects
look like and how they behave.
► An object is an instantiations of a class
► Class names in Java should start with a capital
letter.
► Turtle is a class
 It defines object methods that let you use a Turtle
object.
 //create a turtle object from the Turtle class
Constructor:
Turtle myTurtle = new Turtle(myWorld);
Method that
Class name defines
the type
Makes a
new object
initializes a
new obj.
Object (Instance) Methods
► The
behavior of an object is defined by the
methods that belong to the method.
► Each instance of an object has it own copy
of all the methods defined in the object’s
class that are not Class Methods.
► Turtle turtle1 = new Turtle(myWorld);
Turtle turtle2 = new Turtle(myWorld);
turtle1.turnRight(); turtle2.turnRight();
Note: both turtles have a turnRight method
Class Methods
► Some
classes have define methods.
► java.Math
 Class that defines math functions to use
 Would not want multiple math objects that each
implement an abs method.
 Class methods are methods that belong to only
the class and hence there is only one copy of
the method for all objects of the class.
 int myVal = Math.abs(-12);
A Basic Java Program
// if you have to add import statements put
//them here
public class CS1Test {
public static void main(String [] args) {
// put you Java program statements here
}
}
► You will see what this is for in a minute.
Working With Turtles
Example of using classes
► Get
a world for our turtle to live in
 World csExWorld = new World();
► Put
a turtle in our world
Paramete
r that
specifies
the world
to put the
turtle in.
 Turtle ourTurtle = new Turtle(csExWorld);
► Check
the state of our turtle
 System.out.println(ourTurtle);
 System.out.println(ourTurtle.toString());
Talking to a Turtle
► When
we want an object to do something
we send it a message.
► Make the turtle wander around





ourTurtle.forward(20);
ourTurtle.turnLeft();
ourTurtle.forward(40);
ourTurtle.turn(45);
ourTurtle.forward(65);
How Do I Know What Turtles Can
Do?
► The
set of methods that define what a class
can do is called an API (Application Program
Interface).
► Java defines a special format called JavaDoc
for presenting APIs.
► You can find a link to the doc on the
desktop of the lab machines and at
C:\JavaJars\intro-progjava\bookClasses\doc\index.html.
Problem: Draw a square
►
►
Describe how the turtle will travel in a square.
Program Code:
//draw side one
ourTurtle.forward(100);
// turn 90 deg counterclockwise
ourTurtle.turnLeft();
// draw side two
ourTurtle.forward(100);
// turn 90 deg counterclockwise
ourTurtle.turnLeft();
// draw side three
ourTurtle.forward(100);
//turn 90 degrees counterclockwise
ourTurtle.turnLeft();
// draw side four
ourTurtle.forward(100);
Reusing Code
► What
if we wanted to draw a second
square?
 We would have to retype the code a second
time.
 Not good! Is not there a better way?
► Define
a method that draws a square.
► We can invoke it every time we need to
draw a square.
The drawSquare Method
public void drawSquare() {
}
//draw side one
this.forward(100);
// turn 90 deg counterclockwise
this.turnLeft();
// draw side two
this.forward(100);
// turn 90 deg counterclockwise
this.turnLeft();
// draw side three
this.forward(100);
//turn 90 degrees counterclockwise
this.turnLeft();
// draw side four
this.forward(100);
Opening and Closing Brace
Required
Parentheses
required
Body of the method
1. Open the Turtle.java file
found in C:\intro-progjava\bookClasses
2. Put the method definition at
the bottom of the class
where the documentation
directs.
3. Create a World, a Turtle,
and draw a square.
How do we draw two squares?
► Call
drawSquare() twice.
► What happened?
► Cannot call drawSquare again it will draw over top
the square we have drawn.
► Need to move the Turtle before we draw again.
► How? Look at documentation
 Pen up
 Move to new position
 Pen down
Parameters
► How
do we draw squares of different sizes?
► Observation: Each time we draw a square we
need to change the 100 to the size we want.
► This could generate a lot of almost identical
methods.
► Use a parameter to specify the width.
 public void drawSquare(int width) { … }
 Change 100 to width
► Call
as:
 ourTurtle.drawSquare(50);
Draw the squares in Different Colors
► Back




to the documentation
public void setColor(java.awt.Color color)
Need a Color object found in java.awt package
Must import this package to use it
Find information in Java 1.5 API Documentation
► Invoke
 ourTurtle.setColor(Color.BLUE);
 Draw a blue square.
How do you …?
► Save
the program?
 Put it in a CSTurtleTest class
► Hide
the turtle?
► Draw the squares at an angle to the
window?