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

Java C2. Programming Basics docx

61 393 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

1 Chapter 2. Programming Basics ITSS Java Programming CAO Tuan-Dung, HUT 2 Comments  Explanation to improve readability of program  // comments one line  int nquest; // number of questions.  /* */ comments multiple lines  javadoc comments  Comments that in form of /** …*/ are used by the javadoc program to produce HTML documentation for the program  Example: /** This is an example of special java doc comments used for \n generating an html documentation. It uses tags like: @author Florence Balagtas @version 1.2 */ 3 Javadoc  Tool that pulls out the class and the method etc., defined in the source file, and creates the reference manual of the program  Main tag of ‘javadoc’  @see Reference destination name : Make the reference link of other classes and related packages from the class of the object .  @exception Explanation of exception class name: Describe the explanation of the exception that the method of object has possibility to throw.  @param Explanation of argument name  @return The explanation of the return value of the method of the object is described. 4 Hand on Lab: Javadoc  Use your text editor or Eclipse to create a java program named: Circle.java  The content of this file will be shown in next slides  javadoc –private Circle.java  javadoc -author -version Circle.java 5 Circle.java import java.awt.*; import java.applet.*; /** * A simple circle class. * * @author Samuel A. Rebelsky * @version 1.1 of February 1999 */ public class Circle { /** The current color of the circle. */ protected Color color; /** The current diameter of the circle. */ protected int diameter; /** * Create a new circle with specified diameter and color. */ public Circle(int diameter, Color color) { this.diameter = diameter; this.setColor(color); } // Circle(int, Color) /** * Create a new circle with specified diameter and default color. */ public Circle(int diameter) { this.diameter = diameter; this.setColor(Color.blue); // Blue is as good a default as any. } // Circle(int) 6 Circle.java /** * Draw this circle centered at a particular position in a * graphics thingy. To make it look more interesting, I outline * the circle in some color. */ public void draw(int x, int y, Graphics g) { g.setColor(this.color); g.fillOval( x-this.diameter/2, // Left margin y-this.diameter/2, // Top margin this.diameter, // Width this.diameter); // Height } // draw(int,int,Graphics) /** * Get the diameter of this circle. */ public int getSize() { return this.diameter; } // getSize() /** * Set the color of this circle. */ public void setColor(Color newColor) { this.color = newColor; } // setColor(Color) /** * Set the diameter of this circle. */ public void setSize(int newDiameter) { this.diameter = newDiameter; } // setSize(int) } // class Circle 7 Result 8 Identifier  Tokens that represent names of variables, methods, classes  Java identifiers are case-sensitive.  This means that the identifier Hello is not the same as hello.  Identifiers must begin with either a letter, an underscore “_”, or a dollar sign “$”. Letters may be lower or upper case. Subsequent characters may use numbers 0 to 9. 9 Identifier  Nolimit of length  Identifiers cannot use Java keywords like class, public, void, etc. 10 Coding guidelines – Naming Rule  For names of classes, capitalize the first letter of the class name. For example,  ThisIsAnExampleOfClassName, Circle, Account,  For names of methods and variables, the first letter of the word should start with a small letter. For example,  thisIsAnExampleOfMethodName, setColor, isFull,  The basic data type constant is all capital letters and the object constant is all small letters,  MAX_LENGTH, TAX_VALUE, Color.red, System.out [...].. .Java keywords  Keywords are predefined identifiers reserved by Java for a specific purpose You cannot use keywords as names for your variables, classes, methods etc 11 Java keywords  The Java reserved words: abstract assert boolean break byte case catch char class const continue default do double... Reference type   int num = 10; // primitive type String name = "Hello"; // reference type 21 Java Literal   Literals are tokens that do not change - they are constant The different types of literals in Java are:      Integer Literals Floating-Point Literals Boolean Literals Character Literals String Literals 22 Java Literal  Integer     Decimal: 100 or 100L Octal: 013 or 013L Hexadecimal: 0x52... Character Strings   A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string literal."  Every character string is an object in Java, defined by the String class Every string literal represents a String object  Character strings can be connected by using + operator  Examples String message = “Hello” + “ World.”; 17 Variable Initialization... value in the declaration int sum = 0; int base = 32, max = 149;  When a variable is referenced in a program, its current value is used 18 Basic data type and reference type  Two types of variables in Java:    Primitive Variables Reference Variables Primitive Variables   Variables with primitive data types such as int or long Stores data in the actual memory location of where the variable is 150... package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while 12 Primitive Data  There are eight primitive data types in Java  Four of them represent integers:   Two of them represent floating point numbers:   float, double One of them represents characters:   byte, short, int, long char And one of them represents... 100 or 100L Octal: 013 or 013L Hexadecimal: 0x52 or 0xFFL Floating Point (default is double type)   Decimal Expression: 0.23445 or 0.23445F (float type) Exponent (Scientific) Expression: 6.02E13 23 Java Literal  Boolean literals have only two values, true or false  Character literals: ‘a’, ‘\t’, ‘\u3042’  String literal: “Go ahead!”  Null literal (reference type)  Null (there no data should . on Lab: Javadoc  Use your text editor or Eclipse to create a java program named: Circle .java  The content of this file will be shown in next slides  javadoc –private Circle .java  javadoc. 1 Chapter 2. Programming Basics ITSS Java Programming CAO Tuan-Dung, HUT 2 Comments  Explanation to improve readability. slides  javadoc –private Circle .java  javadoc -author -version Circle .java 5 Circle .java import java. awt.*; import java. applet.*; /** * A simple circle class. * * @author Samuel A. Rebelsky

Ngày đăng: 28/06/2014, 03:20

Xem thêm: Java C2. Programming Basics docx

Mục lục

    Hand on Lab: Javadoc

    Coding guidelines – Naming Rule

    Basic data type and reference type

    Relational & Logic Operators

    Logical Operators: &&(logical) and &(boolean logical) AND

    Array of reference type

    Question: What is the result of this code?

    while and do while statement

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

TÀI LIỆU LIÊN QUAN