Bài giảng An Introduction to Computer Science Using Java - Chapter 2 Classes and Methods I

Tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 2 Classes and Methods I: Chapter 2 Classes and Methods ILecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:describe structure of Java programs present techniques for creating and using class instancesintroduce two classes for doing output: OutputBox for text outputDrawingBox for graphical outputRunning Java ProgramsEnter the program source code in a data file called Hitwall.java using an editorCompile the source program by typingjavac Hitwall.java Execute the compiled program by typingjava HitwallProgram Elements – Part 1white spaceblank lines and spaces includes in program source listings to make things more readablecommentslines beginning with two slashes // single or multiple lines enclosed by /* */that allow the programmer to insert notes to help other people understand the programdocumentationprogram comments and data files describing a program’s structure and behaviorProgram Elements – Part 2i...

ppt22 trang | Chia sẻ: honghanh66 | Lượt xem: 782 | Lượt tải: 0download
Bạn đang xem trước 20 trang mẫu tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 2 Classes and Methods I, để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Chapter 2 Classes and Methods ILecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:describe structure of Java programs present techniques for creating and using class instancesintroduce two classes for doing output: OutputBox for text outputDrawingBox for graphical outputRunning Java ProgramsEnter the program source code in a data file called Hitwall.java using an editorCompile the source program by typingjavac Hitwall.java Execute the compiled program by typingjava HitwallProgram Elements – Part 1white spaceblank lines and spaces includes in program source listings to make things more readablecommentslines beginning with two slashes // single or multiple lines enclosed by /* */that allow the programmer to insert notes to help other people understand the programdocumentationprogram comments and data files describing a program’s structure and behaviorProgram Elements – Part 2import directivetells the Java compiler which packages the program makes use of packagespredefined collections of programs providing services to many programmers (e.g. CSLib.* package used throughout the text) class headingneeds to be included at the beginning of every programclass name must match root of file nameeach class must be stored in a file of its ownProgram Elements – Part 3main methodthe chief computational unit of a Java applicationexecuted first when application is runfunctionsalso known as methodsdefine operations that may be applied to a Java data object (class instance) bodyJava statements that contain the implementations of classes and their methodsProgram Elements – Part 4variable declarationstatement giving the name and data type of a data location used by the programexecutable statementstatement which manipulates variables or determines program control during executionatomic statementssingle Java expressions terminated by a ;variable assignment statementexecutable statement which copies a particular value to a data locationIdentifiersJava uses identifiers to namevariablesmethodsclassespackagesSyntax rulesMust begin with a letter (upper- or lower-case)May be followed by any number (including 0) of letters and digitsThe characters $ and _ are considered lettersJava identifier names are case sensitiveMay not duplicate a Java keyword (e.g. class or main)Building a Simple Classimport 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 methodMeans that methods in other classes (or clients) may use itvoid return typeMeans it does not return a value to the caller (client)OutputBox Classint literals0, 1, -1, 2, -2, 3, -3, double literals3.45, -48.2, 33.0, printDisplay text representation of argument and leave output cursor on the same lineprintlnDisplay text representation of argument and advance output cursor to the next lineUsing OutputBoximport 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 WindowA 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 monitorA 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 DrawingBoximport 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 Programimport CSLib.*;public class ConcentricClient { public static void main(String[] args) { Concentric circles; circles = new Concentric(); circles.drawThem(); }}ColorDrawingBox defines several symbolic constants to simplify selecting drawing colorsColor.white, Color.black, Color.red, Color.blue, etc.You may select the color to draw with by calling setColor before drawingg.SetColor(Color.blue);g.fill Rectangle(0, 0, 50, 25);

Các file đính kèm theo tài liệu này:

  • pptchapter2_4731.ppt