1 / 20

Introduction to Programming

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 .

arin
Download Presentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  9. Class Methods • Some classes have also define methods. • java.Math • Class that defines math functions to use • Would not want multiple math objects that each implement a 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);

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

  11. Working With TurtlesExample of using classes Parameter that specifies the world to put the turtle in. • 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());

  12. 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);

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

  14. 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);

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

  16. The drawSquare Method } Parentheses required 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 • Open the Turtle.java file found in C:\intro-prog-java\bookClasses • Put the method definition at the bottom of the class where the documentation directs. • Create a World, a Turtle, and draw a square. Opening and Closing Brace Required

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

  18. 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);

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

  20. How do you …? • Save the program? • Put it in a CSTurtleTest class • Hide the turtle? • Draw the squares at an angle to the window?

More Related