Download Guidelines for Programming Style

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Name:___________________________________________Project: _______________
Guidelines for Program Projects
_______ Design (65%)
 _______ Meets the design criteria as specified in the instructions.
 _______ No syntax errors.
 _______ No logic errors.
_______Program Style (20-25%)
________ Layout
 _____Use either the courier or new-courier font. Any font that does not have a uniform
width for all characters will receive no credit for programming style.
 _____ All matching brace pairs must be aligned in the same vertical column.
 _____Nested braces must be indented at least 3 spaces. A single tab is acceptable if it is
at least 3 spaces.
 _____Only one statement per line.
 _____ Declare a list of variables in columns rather than horizontally across the page.
 _____ All instance fields must be declared as private.
 _____Put spaces between operands and operators of expressions.
 _____Put at least one space after a comma. Be consistent with the number of spaces after
a comma throughout your program.
 _____"Paragraph" your code by grouping statements that perform a well defined task
together, include at least one blank line before and then after the group of well defined
statements.
 _____ Do not allow wrap-around of long lines for program statements, headerlines, etc.
Limit the line length to no more than 100 characters.
________Naming
 _____All variable-names, method-names, and class names should be descriptive.
 _____Variable-names and method-names begin with a lowercase letter and have the first
letter of each successive word in the name capitalized, as in payRate, or
computeInterestRate.
 _____Class names should follow a similar convention as variables names, but start with
an uppercase character, as in Manager, or BankAccount.
 _____ Named constants using the final keyword in the declaration should be all
uppercase letters using underscores between words, as in: UPPER_BOUND or
TX_CHARACTER.
 _____Replace literal constants with named constants. The named constants should be
declared and grouped together at the start of a method or class.
 _____Loop counter variables are the only variables that may be named with a single
character provided the loop is only a few lines in length (say 5 lines). All other variable
names must be at least 2 characters long and have a descriptive name.
 _____Two character variable names should begin with a letter followed by a digit. Two
character variable names should be restricted to a block of code or a paragraph of code.
_______Documentation (10-15%):
 _____A program file must contain a comment header at the beginning of the file. Use the
examples attached to this document as a guide for your comment header. Include a
description of program inputs and outputs.
 _____All classes and all methods other than the main static-method, must have a
comment header before the header line. Use the attached examples as a guide for your
comment headers. Begin all class and method headers with /** and conclude the
comment header with */
Method headers should include a description of each argument in the formal parameter
list. Each argument should have a brief description in the header and begin with @param
(see examples below).
Methods that return a result should have a description of the return value. The description
of the return value should begin with @return (see examples below).
 _____Single line comments should appear throughout your program. Single line
comments should use the //. Single line comments can be on the same line as a statement
or before the statement(s) being commented.
 Do not allow wrap-around for comments. Keep line lengths to no more than 100
characters.
/**
Author:
Date:
Language Version:
Course:
Section:
File Name:
Classes:
JChildress
10-05-06
Java2 1.5.0_06
CS1043
1 and 2
ObjArray.java
ObjArray
Description:
This program demonstrates how to use the ArrayList class and
demonstrates how to parse a string containing different data
types. The ArrayList accumulates objects of type Employee.
Inputs:
The user is prompted for input strings containing a name and
a wage.
Outputs:
The highest wage is displayed, and all the input names and
wages are displayed.
*/
import java.util.Scanner;
import java.util.ArrayList;
public class ObjArray
{
public static void main( String[] args )
{
Scanner record = new Scanner( System.in );
// Create an ArrayList to hold Employee objects.
ArrayList<Employee> employeeList = new ArrayList<Employee>();
// continued on the next page…
boolean go = true;
do
{
System.out.print(
"Enter name and wage (press return to stop): " );
String inputLine = record.nextLine();
if ( inputLine.length() <= 0 ) // test for an empty string
{
go = false; // stop loop
}
else // parse the data in the inputLine.
{
Scanner fields = new Scanner( inputLine );
String name = fields.next();
double wage = fields.nextDouble();
Employee worker = new Employee( name, wage );
}
// Insert the new worker in the ArrayList "employeeList"
employeeList.add( worker );
// end if statement
} while ( go );
// end loop.
Employee bigShot = employeeList.get( 0 );
for ( int i=1; i<employeeList.size(); i++ )
{
Employee worker = employeeList.get(i);
if ( bigShot.getWage() < worker.getWage() )
{
bigShot = worker;
}
}
if ( bigShot != null )
{
System.out.printf( bigShot.getName() +
" has the highest wage: $" + bigShot.getWage() + "\n" );
}
System.out.printf( "The employee and wage list: \n" );
for( int i=0; i<employeeList.size(); ++i )
{
Employee wrk = employeeList.get(i);
System.out.printf( wrk.getName() + " : $"
+ wrk.getWage() + "\n" );
}
}
}
// end main
// end class ObjArray
/**
Author:
Date:
Language Revision:
Course:
Section:
File Name:
Classes:
J. Childress
10-05-06
Java2 1.5
CS1043
1 and 2
Employee.java
Employee
Description:
This file contains the Employee class. The Employee class
is used to declare objects of type Employee.
Private Instance Variables (Fields)
name - The employee name.
wage - The employee wage.
Methods:
getName - return the "name" instance field.
getWage - return the "wage" instance field.
Constructors:
This class has one constructor. The constructor is used to
initialize the instance fields with user supplied values from
the formal parameter list.
*/
public class Employee
{
private String name_;
private double wage_;
// instance variable
// instance variable
/**
Constructor Name: Employee
Description:
This constructor is used to set all the instance fields with user
supplied input values.
Inputs:
@param name = used to set the name_ instance field.
@param wage = used to set the wage_ instance field.
*/
public Employee ( String name, double wage ) // constructor
{
name_ = name;
wage_ = wage;
}
/**
method Name: getName
Description:
The getName() accessor method returns the name_ instance field.
Inputs:
None
Outputs:
@return the name_ instance field.
*/
public String getName( ) // accessor method
{
return( name_ );
}
/**
method Name: getWage
Description:
The getWage() accessor method returns the wage_ instance field.
Inputs:
None
Outputs:
@return the wage_ instance field.
*/
public double getWage( ) // accessor method
{
return( wage_ );
}
}
// end class Employee
/**
Author: Roger Wainwright 8-29-00 Java2
Revised: James Childress 9-25-06 Java2 1.5
Course: CS1043
Section: 1 and 2
File Name: MethodExample.java
Class Names: MethodExample
Project: 0
Program Description:
This is a static method example. The main program will prompt
for three inputs, then compute and display the future value
of an investment based on the inputs.
Inputs:
1. Present value amount in dollars and cents. (Type double)
2. The annual percentage interest rate (APR). (Type double)
Input as a percentage %.
3. Number of years for the investment to grow. (Type int)
Outputs:
The future value is computed and displayed using the formula:
FV = PV*(1+APR/12)^(12*years)
*/
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
public class MethodExample
{
// ------------------------------------------------------------------public static void main( String[] args )
{
Scanner console = new Scanner( System.in );
System.out.print("\nPlease enter the present value amount: ");
double presentV = console.nextDouble();
System.out.print("\nPlease enter the rate of investment in percent: ");
double rate = console.nextDouble();
System.out.print("\nPlease enter the number of years of the " +
"investment (as an integer): ");
int years = console.nextInt();
double futureV = futureValue( presentV, rate, years );
NumberFormat formatter
= NumberFormat.getCurrencyInstance( Locale.US );
System.out.printf("\nFinal Investment value compounded monthly = "
+ formatter.format( futureV ) + "\n\n" );
}// end main
//-------------------------------------------------------------------------
/**
Method Name: futureValue
Description:
This function will calculate and return the future value of
investing an inital amount in dollars for y-years at the rate
of r-APR. The interest is compounded monthly.
Inputs:
@param pVal is the the present value (aka the initial balance).
@param rate is the APR, annual percentage rate of interest.
@param years is the number of years for the investement to grow.
Outputs:
@return fVal is the future value amount returned by the method.
*/
public static double futureValue( double pVal, double rate,
{
final int M_YEAR = 12;
int years )
double fVal = pVal * Math.pow( 1 + rate/(M_YEAR * 100), M_YEAR * years );
return fVal ;
}// end futureValue
//------------------------------------------------------------------------}// end MethodExample
/**
Note: the futureValue header line can be placed on one line because it will
“fit” on one line and be readable. Long formal parameter lists can be
broken up into one line per argument.
public static double futureValue( double pVal,
double rate,
int years )
*/