16
© 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class. Check the schedule Identifier Rules and Conventions Constants Static Methods JavaDoc Requirements

© 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

Embed Size (px)

Citation preview

Page 1: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley

Tuesday 9-2-14Tuesday 9-2-14

After answering the questions after logging in, open BlueJ and the Formula class. Check the schedule

Identifier Rules and ConventionsConstants

Static Methods

JavaDoc Requirements

Page 2: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley2

Java Program StructureJava Program Structure

A Java application always contains a method called main

Our Point and Formula classes do not contain a main method. They are classes but they are not applications.

Our aMain class is an application, it has a main method.

We could put a main method in the Point and Formula Class.

Page 3: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley3

Three Types of CommentsThree Types of Comments

// this comment runs to the end of the line

/* this comment runs to the terminating symbol, even across line breaks */

/** * this is a javadoc comment * */

Page 4: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley4

What is an identifier?What is an identifier?

Identifiers are variables, constants, and methods the programmer creates and uses in a program.

Rules for creating identifiers must be followed or the code will NOT compile.

Conventions are adopted programming practices. The code will still compile if the convention is not followed.

Page 5: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley5

2 Rules2 Rules for Creating Identifiers for Creating Identifiers

Rule 1 : An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar sign

Rule 2 : Identifiers cannot begin with a digit

Java is case sensitive - Total, total, and TOTAL are different identifiers

Identifiers should be meaningful, not too short or long.

Page 6: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley6

ConventionsConventions – Class Names – Class Names

The First letter in each word is capitalized.

• Point

• Formula

• Math

• HelloWorld

• ArrayList

Page 7: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley7

ConventionsConventions – Variable and Method Names – Variable and Method Names

The first word is all lowercase. The first letter of following words are capitalized.

• name

• average

• lastName

• xCoord

• getX

public double CylinderVolumewithRadiusandHeight (double R, double H) {return Math.PI*(R*R)*H;}public double CubeVolumewithSide (double X) {return X*X*X;}

Page 8: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley8

ConventionsConventions – Constants – Constants

Constants are special identifiers that can NOT be changed.

Constants are all UPPERCASE.

• MAX

• PI

Multiple word constants are separated by an underscore _

• TAX_RATE

• FT_MILE

Page 9: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley

JavaDoc RequirementsJavaDoc Requirements

Class JavaDoc• Description of the class• @author followed by your name without parentheses

@author Larry Kedigh //correct @author (Larry Kedigh) //incorrect

• @version followed by the date the class was created.

Method JavaDoc• Description of the method• @param <variable> description of the variable• @return description of what is returned.

9

Page 10: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley

public double areaHexagon1 (double x) { return 2.598076211*Math.pow(x,2) ; }

Example 1 – ConstantsExample 1 – Constants

10

Page 11: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley

Example 1Example 1

11

public double areaHexagon1 (double x) { return 2.598076211*Math.pow(x,2) ; }

Magic Number

Page 12: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley

public double areaHexagon2 (double x){ final double CON_FACTOR = 3*Math.pow(3,.5)/2; //hexagon formula return CON_FACTOR*Math.pow(x,2) ; }//end aHex

Example 1 – ConstantsExample 1 – Constants

12

public double areaHexagon3 (double x) { // conversion factor in area of hexagon formula // (3 * sqrt(3))/2 return 2.598076211*Math.pow(x,2) ; } //end aHex

Page 13: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley

/** * Calculates the area of a Hexagon * * @param x Length of a side * @return The area in square units*/ public double areaHexagon2 (double x) { final double CON_FACTOR = 3*Math.pow(3,.5)/2;//hexagon formula return CON_FACTOR*Math.pow(x,2) ; }

Example 1 – JavaDocExample 1 – JavaDoc

13

Page 14: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley

Formula Class AssignmentFormula Class Assignment

Edit your Formula class so that • The identifier naming convention is followed• JavaDoc for each method• Magic numbers removed

Link to submission form – will be on the site after class

14

Grading - 80 pts possibleThe identifier naming convention is followed in all methods(10 pts) JavaDoc for 10 of the 20 method (3 pts/method, 30 total)Magic number / Constants are appropriate in all methods(10 pts)Style and consistency (10 pts) All methods are static (10 pts)Class compiles and works appropriately (10 pts) This due midnight tonight (Tuesday 9-2) One day late -15% or 12 pts. More than one day late - 50% or 40 pts

Page 15: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley

EXTRASEXTRAS

15

Page 16: © 2011 Pearson Education, publishing as Addison-Wesley Tuesday 9-2-14 After answering the questions after logging in, open BlueJ and the Formula class

© 2011 Pearson Education, publishing as Addison-Wesley

/** * Calculates the area of a Hexagon * @param x Length of a side * @return The area in square units * <br> * <img src="http://www.wikihow.com/images/f/f5/Calculate-the-Area-of-a-Hexagon-Step-1-Version-3.jpg">

* @see <a href="http://www.drking.org.uk/hexagons/misc/area.html">info</a> */ public double areaHexagon2 (double x) { final double CON_FACTOR = 3*Math.pow(3,.5)/2;//hexagon formula return CON_FACTOR*Math.pow(x,2) ; }

Insert Images and References in JavaDocInsert Images and References in JavaDoc

16