Chapter 2 Classes and Methods I - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 2 Classes and Methods I

Description:

An Introduction to Computer Science Using Java (2nd Edition) by ... Exterminate a rodent. // Author: C. Mickunas 11/21/00. public void shout() { TrickMouse alert; ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 23
Provided by: brucem151
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2 Classes and Methods I


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
(No Transcript)
4
(No Transcript)
5
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

6
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
    programs structure and behavior

7
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

8
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

9
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

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

11
(No Transcript)
12
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!)

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

14
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

15
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(Thats cold, folks!)

16
(No Transcript)
17
(No Transcript)
18
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)

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

20
Concentric Client Program
  • import CSLib.
  • public class ConcentricClient
  • public static void main(String args)
  • Concentric circles
  • circles new Concentric()
  • circles.drawThem()

21
(No Transcript)
22
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)
Write a Comment
User Comments (0)
About PowerShow.com