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 89 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

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.

Chapter 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 • present techniques for creating and using class instances • introduce two classes for doing output: – OutputBox for text output – DrawingBox for graphical output             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     Program Elements – Part • 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   Program Elements – Part • 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   Program Elements – Part • 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     Program Elements – Part • 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     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)         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!”); } }     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)     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     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!”); } }             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)     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); } }     Concentric Client Program import CSLib.*; public class ConcentricClient { public static void main(String[] args) { Concentric circles; circles = new Concentric(); circles.drawThem(); } }         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);     ... it • void return type – Means it does not return a value to the caller (client)     OutputBox Class • int literals 0, 1, -1 , 2, -2 , 3, -3 , … • double literals 3.45, -4 8 .2, 33.0, … • print – Display...             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... 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

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