packages packages
play

Packages Packages 1. Every Java object resides in a package . 2. An - PowerPoint PPT Presentation

Packages Packages 1. Every Java object resides in a package . 2. An objects 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(


  1. Packages

  2. 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!" ); } }

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

  4. Packages 3. Packages allow different programming organizations to choose the same class name without fear of confusion: java.awt.List java.util.List

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

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

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

  8. Objects

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

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

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

  12. Turtle Objects

  13. 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.*;

  14. A Turtle’s Habitat

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

  16. Painting (1)

  17. Painting (2)

  18. Painting (3)

  19. Moving

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

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

  22. Swinging Around

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

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

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

  26. Parts of a Java Program

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

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

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

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

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

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

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

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

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

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

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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend