Packages
Packages 1. Every Java object resides in a package . 2. An object’s package is explicitly declared on the first line of code in a .java file: package edu.pcwe.uw.javaintro.misc; public class HelloWorld { public static void main( String[] args ) { System.out.println( "Hello world!" ); } }
Packages 3. The fully qualified name of a class includes its package name; the fully qualified name of the class in the preceding slide is: edu.pcwe.uw.javaintro.misc.HelloWorld
Packages 3. Packages allow different programming organizations to choose the same class name without fear of confusion: java.awt.List java.util.List
Packages 4. A .java file without a package statement is said to reside in the default package. Note: We will not be using explicit packages for the code we write in this class. However you have to know a little bit about packages in order to use pre ‐ assembled code.
Libraries 1. A library is a collection of packages. 2. A library may reside in a dedicated directory… 3. … but is more commonly stored in a java archive (.jar) file.
Libraries 4. To use an object from a library, you must: a) Import the object from its package into your code: import edu.uweo.javaintro.tools.Turtle; b) Tell java where the library lives that contains the package:
Objects
Definition 1. An object stores information and performs actions. 2. An object has a name and a type. 3. The name of an object must be unique, and allows you to have many objects of the same type. 4. The type of an object determines what it can store and what it can do. A java Integer object stores whole numbers; it can perform basic arithmetic, such as adding and subtracting. A Cartograph object might store the coordinates of cities, and be able to compute the distance between them.
Declaration 1. A declaration determines the name and type of an object: Person cartman; ^ ^ ^ type name | end-declaration 2. Objects need to be created before you can use them: cartman = new Person(); 3. An object can be declared and created in one step: Person cartman = new Person();
Methods 1. A method (a.k.a. function or procedure ) is how you usually interact with an object. 2. A method has a name and a type . 3. A method requires 0 or more arguments enclosed in parentheses. 4. A method is invoked using the executor (a period). Person cartmen = new Person(); cartman.takeAHike( 3, 2 ); ^ ^ ------ executor | ^ | arguments | name
Turtle Objects
Introduction See also: Turtle Documentation on the class web site. 1. To work with the Turtle class you must import it from edu.uweo.javaintro.tools. 2. A Turtle is an object that can draw simple figures. 3. A Turtle starts at the center of a blank screen facing “east.” import edu.uweo.javaintro.tools.Turtle; -or- import edu.uweo.javaintro.tools.*;
A Turtle’s Habitat
The paint Method The paint method draws a line and changes the position of a Turtle. It takes two arguments: angle and distance. paint( angle, distance ) angle: Causes a turtle to turn a number of degrees; positive numbers turn left; negative numbers turn right. distance: Tells a turtle how many pixels to walk; positive numbers walk forward; negative numbers walk backward.
Painting (1)
Painting (2)
Painting (3)
Moving
The move Method The move method changes the position of a Turtle without drawing. It takes two arguments: angle and distance. move( angle, distance ) angle: Causes a turtle to turn a number of degrees; positive numbers turn left; negative numbers turn right. distance: Tells a turtle how many pixels to walk; positive numbers walk forward; negative numbers walk backward.
The swingAround Method The swingAround method tells a turtle to draw a circle; it does not change the position of the turtle. It takes one argument: the radius of the circle. swingAround( radius ) radius: Specifies the radius of the circle.
Swinging Around
Comments (1) • Adding comments to your code can make it more readable; the java compiler ignores comments. • The double slash (//) begins a comment that continues to the end of the line: Turtle sue = new Turtle(); sue.paint( 45, 200 ); // start by drawing a diagonal line sue.move( -135, 300 ); // make sue face due south
Comments (2) • Another kind of comment begins with slash-asterisk (/*) and ends with asterisk-slash (*/): /* This program renders a surrealistic sketch of a monkey eating a banana. Author: Jordan Alispiegal Date: 23-June-2008 */ public class Monkey { . . .
Exercises Textbook, Chapter 1, page 1-5: Use an editor ( notepad++ ) to enter the program TwoSquares from listing 1.2. Use javac to compile the program, and java to run it. Textbook, Chapter 1, page 1-7: Exercises 1.3, 1.5 and 1.7 (note: the house in exercise 1.7 should have a flat roof).
Parts of a Java Program
class + name matching public class HelloWorld braces { /* body of class goes here */ } must live in HelloWorld.java A class creates a new type , something which can contain data and define actions. In java, nothing can happen outside a class. Every class has a name, such as Turtle . class HelloWorld creates a new class named HelloWorld .
public + class public class HelloWorld { /* body of class goes here */ } Declaring a class to be public means that it can be used by anyone. There are other options, such as private, but we won't be talking about those for a while.
Braces public class HelloWorld { public static void main( String[] args ) { System.out.println( "Hello World!" ); } } In java, braces are used to group things together. In the context of a class the left brace ( { ) signals the start of the class, and the right brace ( } ) signals the end of the class. Braces are always used in pairs.
main public class HelloWorld { public static void main ( String[] args ) { System.out.println( "Hello World!" ); } } When you start a java program with the java tool, you must designate the class to start executing. This class must have a block of code (called a method) named main , which must be written exactly as shown: public static void main( String[] args )
void public class HelloWorld { // main does not calculate a value. public static void main( String[] args ) { System.out.println( "Hello World!" ); } } In Java, some methods can be used to calculate values, for example, Math.abs is used to calculate the absolute value of a number: System.out.println( Math.abs( -3 ) ); When a method does not calculate a value, it must be declared void .
public method-name public class HelloWorld { public static void main ( String[] args ) { System.out.println( "Hello World!" ); } } In Java, some methods can be used by anyone, while access to other methods is restricted. A method declared public can be used by anyone. There are other options, such as private which we’ll talk about later.
static method-name public class HelloWorld { public static void main ( String[] args ) { System.out.println( "Hello World!" ); } } In Java, some methods can be used anytime, but others can only be used after you create an object (you can’t tell a Turtle to draw anything until you create one). static methods, on the other hand, can be used whether you have an object or not.
String[] args public class HelloWorld { public static void main( String[] args ) { System.out.println( "Hello World!" ); } } String[] args is used to process command line arguments . Don’t worry about that right now; just remember that your main method must be declared this way.
Spacing public class HelloWorld{public static void main(String[]args){“Hello World”);}} Spaces and line breaks are (usually) optional in Java code. We use them to make our code more readable.
Indentation public class HelloWorld { public static void main( String[] args ) { System.out.println( "Hello World!" ); } } Indentation is optional in Java code, but it makes your programs much easier to read. Put the braces that delimit a block of code on lines by themselves, and line them up with the start of the block. Inside the block indent four spaces.
Exercises (1 of 2) 1. Go into your HelloWorld program and change main to sam . Compile it with the javac command. What happens when you try to run it with the java command? (Before you forget, change sam back to main.) 2. Add the getGreeting method to your HelloWorld program, and change the "HelloWorld" line as follows: public class HelloWorld { public static void main( String[] args ) { System.out.println( getGreeting() ); } public static String getGreeting() { return "Hi there, world"; } } (continued on next slide)
Exercises (2 of 2) (continued) Compile and run the program. Explain: What happened when you substituted getGreeting() for "HelloWorld" ? • What is the purpose of String in the line public static String getGreeting() ? • 3. Download and complete TweedleActivity from the class web site.
Recommend
More recommend