1 / 22

Chapter 2 Classes and Methods I

Chapter 2 Classes and Methods I. Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold. Chapter Preview. In this chapter we will: describe structure of Java programs

holt
Télécharger la présentation

Chapter 2 Classes and Methods I

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. Chapter 2Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold

  2. Chapter Preview In this chapter we will: • describe structure of Java programs • present techniques for creating and using class instances • introduce two classes for doing output: • OutputBox for text output • DrawingBox for graphical output

  3. Running Java Programs • Enter the program source code in a data file called Hitwall.java using an editor • Compile the source program by typing javac Hitwall.java • Execute the compiled program by typing java Hitwall

  4. Program Elements – Part 1 • white space • blank lines and spaces includes in program source listings to make things more readable • comments • lines beginning with two slashes // • single or multiple lines enclosed by /* */ • that allow the programmer to insert notes to help other people understand the program • documentation • program comments and data files describing a program’s structure and behavior

  5. Program Elements – Part 2 • import directive • tells the Java compiler which packages the program makes use of • packages • predefined collections of programs providing services to many programmers (e.g. CSLib.* package used throughout the text) • class heading • needs to be included at the beginning of every program • class name must match root of file name • each class must be stored in a file of its own

  6. Program Elements – Part 3 • main method • the chief computational unit of a Java application • executed first when application is run • functions • also known as methods • define operations that may be applied to a Java data object (class instance) • body • Java statements that contain the implementations of classes and their methods

  7. Program Elements – Part 4 • variable declaration • statement giving the name and data type of a data location used by the program • executable statement • statement which manipulates variables or determines program control during execution • atomic statements • single Java expressions terminated by a ; • variable assignment statement • executable statement which copies a particular value to a data location

  8. Identifiers • Java uses identifiers to name • variables • methods • classes • packages • Syntax rules • Must begin with a letter (upper- or lower-case) • May be followed by any number (including 0) of letters and digits • The characters $ and _ are considered letters • Java identifier names are case sensitive • May not duplicate a Java keyword (e.g. class or main)

  9. Building a Simple Class import CSLib.*; public class WarningMouse { // Exterminate a rodent. // Author: C. Mickunas 11/21/00 public void shout() { TrickMouse alert; alert = new TrickMouse(); alert.setTitle(“WARNING”); alert.speak(“Look out!”); } }

  10. Method shout( ) • public method • Means that methods in other classes (or clients) may use it • void return type • Means it does not return a value to the caller (client)

  11. OutputBox Class • int literals 0, 1, -1, 2, -2, 3, -3, … • double literals 3.45, -48.2, 33.0, … • print • Display text representation of argument and leave output cursor on the same line • println • Display text representation of argument and advance output cursor to the next line

  12. Using OutputBox import CSLib.*; public class Forecast { // Give the weather forecast. // Author: E. Reingold 11/12/00 public void predict() { OutputBox out; out = new OutputBox(); out.print(“The temperature will be ”); out.print(-10); out.println(“ degrees.”); out.println(“That’s cold, folks!”); } }

  13. DrawingBox Window • A DrawingBox window is divided into a rectangular grid of picture elements (pixels) • The size of a pixel depends on the resolution (the number of pixels in the grid) of your workstation monitor • A typical screen resolution might be 1028 by 768 pixels. • Pixels may have two colors (black or white) or many colors (depending on the resolution of the monitor)

  14. Using DrawingBox import CSLib.*; public class Concentric { // Draw concentric circles. // Author: A. Baranowicz 12/2/00 public void drawThem() { DrawingBox g; g = new DrawingBox(); g.setDrawableSize(300, 300); g.drawOval(110, 110, 80, 80); g.drawOval(95, 95, 110, 110); g.drawOval(80, 80, 140, 140); } }

  15. Concentric Client Program import CSLib.*; public class ConcentricClient { public static void main(String[] args) { Concentric circles; circles = new Concentric(); circles.drawThem(); } }

  16. Color • DrawingBox defines several symbolic constants to simplify selecting drawing colors Color.white, Color.black, Color.red, Color.blue, etc. • You may select the color to draw with by calling setColor before drawing g.SetColor(Color.blue); g.fill Rectangle(0, 0, 50, 25);

More Related