CreatingClasses-Mod13-part4 - Coweb

Download Report

Transcript CreatingClasses-Mod13-part4 - Coweb

Creating Classes
part 4
Barb Ericson
Georgia Institute of Technology
Oct 2005
Georgia Institute of Technology
Learning Goals
• Computing concepts
– Comments
•
•
•
•
•
Why add comments?
Types of comments
How to add Javadoc comments
How to preview HTML from Javadoc comments
How to create HTML for all classes in a directory
– Creating another class
• Using UML to show classes and the relationships
between them
Georgia Institute of Technology
Comments
• You should add comments to your code
– To make it easier to read and change
• Comments are ignored by the complier
– Not added to the byte codes
• Java has 3 kinds of comments
– // comment ends at the end of this line
– /* comment ends with next */
– /** Javadoc comment that ends with */
• can be used by the javadoc utility to create HTML
documentation
Georgia Institute of Technology
Javadoc Comments
• Add a comment before the class definition
– That explains the purpose of this class
– And says who wrote it
• @author Barb Ericson
/**
* Class that describes a student. A student has a
* name and an array of grades. You can get
* information about a student such as her/his
* name and grade average.
*
* @author Barb Ericson
*/
public class Student
Georgia Institute of Technology
Method Comments
• Add a comment before each method
• What the parameters are
– @param name info
• What is returned
– @return info
/**
* Method to set the name for this student
* @param theName the new name to use
* @return true if success else false
*/
public boolean setName(String theName)
Georgia Institute of Technology
Previewing Javadoc HTML
• Click on Tools
• Click on Preview Javadoc for Current
Document
– This will generate the HTML from the javadoc
comments and display it
• The HTML document will display
Georgia Institute of Technology
Generating all HTML for Directory
• In DrJava click on the Javadoc button
– to create the HTML documentation
– based on the Javadoc comments
• This will generate HTML for all files in the
same directory as all open files
• Generates an index.html as a starting
point
Georgia Institute of Technology
Javadoc Exercise
• Add a class javadoc comment and method
javadoc comments to the Student class
• Execute Javadoc and check out the
created documentation
Georgia Institute of Technology
Create a ClassPeriod
• A teacher has students in each class
period
– Each period can have different students
• For a class period we might want to know
– The teacher’s name
– The period number
– The students in that period
Georgia Institute of Technology
UML Class Diagram
• There is a standard way to diagram objectoriented classes
– It is a UML class diagram
• It shows the classes as boxes and the
relationships between them
Shows inheritance
Has a or association
Georgia Institute of Technology
Creating a ClassPeriod class
• We want fields for the
– Teacher’s name
– Period number
– Students in the period
• What type should each of these be?
– A name can be a string
– A period number can be an integer
– The students in the period can be an array of
Student objects
• With a max of 35 students in a class period
Georgia Institute of Technology
Create a Class Exercise
• Declare a new class ClassPeriod in the file
ClassPeriod.java
• Add the fields:
– private String teacherName;
– private int periodNumber;
– private Student[] studentArray = new
Student[35];
Georgia Institute of Technology
Create a Class Exercise - Continued
• Add constructors to ClassPeriod
– Add the no argument constructor
– Add a constructor that takes the teacher’s
name and the period number
• Remember that constructors are declared
as
public ClassName(paramList)
{
}
Georgia Institute of Technology
Add Accessors and Modifiers
• Add methods to get and set the fields in
the class period
public String getTeacherName()
{
return this.teacherName;
}
public void setTeacherName(String theName)
{
this.teacherName = theName;
}
Georgia Institute of Technology
Override toString
• Add the following method to ClassPeriod
public String toString()
{
}
• Add code to the method to return a String with
the teacher’s name, the period number, and the
number of students in the class period
– The length of the array isn’t a count of the actual
students
– Find out how many in the array are not null
Georgia Institute of Technology
Summary
• Comments are added to make a program
– Easier to read and understand
– Comments are ignored by the compiler
• There are three types of comments in Java
– // end of line
– /* multi line */
– /** java doc */
• Javadoc is a utility that comes with the jdk
– Produces HTML documentation from Javadoc comments
• UML is a standard way to diagram object-oriented
designs
– A class diagram shows classes and the relationships between
them
Georgia Institute of Technology