+ All Categories
Home > Documents > Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science...

Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science...

Date post: 25-Dec-2015
Category:
Upload: lucy-mills
View: 218 times
Download: 2 times
Share this document with a friend
22
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
Transcript
Page 1: 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.

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

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

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

Page 3: 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.
Page 4: 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.
Page 5: 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.

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

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

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

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

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

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

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

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

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

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

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)

Page 11: 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.
Page 12: 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.

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!”); }}

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

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)

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

OutputBox Class

• int literals0, 1, -1, 2, -2, 3, -3, …

• double literals3.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

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

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!”); }}

Page 16: 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.
Page 17: 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.
Page 18: 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.

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)

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

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); }}

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

Concentric Client Program

import CSLib.*;

public class ConcentricClient {

public static void main(String[] args) {

Concentric circles;

circles = new Concentric();

circles.drawThem();

}

}

Page 21: 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.
Page 22: 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.

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


Recommended