Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming Writing Java Beginning Java Programs.

Similar presentations


Presentation on theme: "Introduction to Programming Writing Java Beginning Java Programs."— Presentation transcript:

1 Introduction to Programming Writing Java Beginning Java Programs

2 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

3 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!

4 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

5 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;

6 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.

7 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.

8 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 Turtle myTurtle = new Turtle(myWorld); Turtle myTurtle = new Turtle(myWorld); Class name defines the type Makes a new object Constructor: Method that initializes a new obj.

9 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

10 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);

11 A Basic Java Program // if you have to add import statements put //them here public class CS1Test { public static void main(String [] args) { public static void main(String [] args) { // put you Java program statements here // put you Java program statements here }} ► You will see what this is for in a minute.

12 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  Turtle ourTurtle = new Turtle(csExWorld); ► Check the state of our turtle  System.out.println(ourTurtle);  System.out.println(ourTurtle.toString()); Paramete r that specifies the world to put the turtle in.

13 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);

14 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-prog- java\bookClasses\doc\index.html.

15 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);

16 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.

17 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);} } Body of the method Parentheses required Opening and Closing Brace Required 1.Open the Turtle.java file found in C:\intro-prog- java\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.

18 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

19 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);

20 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.

21 How do you …? ► Save the program?  Put it in a CSTurtleTest class ► Hide the turtle? ► Draw the squares at an angle to the window?


Download ppt "Introduction to Programming Writing Java Beginning Java Programs."

Similar presentations


Ads by Google