1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture An introduction to computer science using java (2nd Edition): Chapter 2 - S.N. Kamin, D. Mickunas, E. Reingold

22 90 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 22
Dung lượng 440,97 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Chapter 2 - Classes and methods I. 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.

Trang 1

   

Chapter 2

Classes and Methods I

Lecture Slides to Accompany

An Introduction to Computer Science

Using Java (2nd Edition)

byS.N Kamin, D Mickunas, E Reingold

Trang 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

Trang 3

   

Trang 4

   

Trang 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

Trang 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 program’s structure and behavior

Trang 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

Trang 8

– 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

Trang 9

– 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

Trang 10

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

Trang 11

   

Trang 13

• void return type

– Means it does not return a value to the caller (client)

Trang 14

– Display text representation of argument and

advance output cursor to the next line

Trang 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(“That’s cold, folks!”);

}

}

Trang 16

   

Trang 17

   

Trang 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

Trang 19

   

Using DrawingBox

import CSLib.*;

public class Concentric {

// Draw concentric circles.

Trang 20

   

Concentric Client Program

import CSLib.*;

public class ConcentricClient {

public static void main(String[] args) {

Trang 21

   

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

Ngày đăng: 11/01/2020, 19:06

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN