Example Program. public class ComputeArea {

Similar documents
Lecture 8 " INPUT " Instructor: Craig Duckett

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Programming Exercise 7: Static Methods

Loops. CSE 114, Computer Science 1 Stony Brook University

STUDENT LESSON A7 Simple I/O

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

St. Edmund Preparatory High School Brooklyn, NY

Text User Interfaces. Keyboard IO plus

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Controls Structure for Repetition

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Oct Decision Structures cont d

Tips from the experts: How to waste a lot of time on this assignment

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Chapter 4: Conditionals and Recursion

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Fundamentals of Programming Data Types & Methods

4. Java Project Design, Input Methods

Write for your audience

Tips from the experts: How to waste a lot of time on this assignment

COMP 202 Java in one week

Introduction to Software Development (ISD) David Weston and Igor Razgon

Full file at

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

COMP 202 Java in one week

Chapter 3. Selections

Chapter Two Bonus Lesson: JavaDoc

Coding Standards for Java

Object Oriented Programming. Java-Lecture 1

Chapter 5 Lab Methods

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Midterm Exam 2 CS 455, Spring 2013

Static Methods. Why use methods?

COMP 110 Project 1 Programming Project Warm-Up Exercise

F I N A L E X A M I N A T I O N

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

Tips from the experts: How to waste a lot of time on this assignment

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Midterms Save the Dates!

Java Coding 3. Over & over again!

Chapter 9 Lab Text Processing and Wrapper Classes

CSE 1223: Exam II Autumn 2016

Building Java Programs

Write a program which converts all lowercase letter in a sentence to uppercase.

Midterm Examination (MTA)

Lab1 Solution. Lab2 Solution. MathTrick.java. CoinFlip.java

CS 177 Recitation. Week 1 Intro to Java

Exceptions Handeling

Computer Science 210 Data Structures Siena College Fall Topic Notes: Methods

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Final Exam Practice. Partial credit will be awarded.

STUDENT LESSON A12 Iterations

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Advanced Computer Programming

EXCEPTIONS. Fundamentals of Computer Science I

Chapter 4: Control Structures I

Lecture Set 2: Starting Java

Repetition, Looping. While Loop

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Lecture Set 2: Starting Java

Chapter 11 Introduction to Programming in C

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

Building Java Programs

Chapter 7 User-Defined Methods. Chapter Objectives

A+ Computer Science -

CSE 114 Computer Science I

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Programming with Java

Advanced Java Concept Unit 1. Mostly Review

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CSCI 2101 Java Style Guide

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Data dependent execution order data dependent control flow

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Lab5. Wooseok Kim

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

An exception is simply an error. Instead of saying an error occurred, we say that an.

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Building Java Programs

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

Chapter 15. Exception Handling. Chapter Goals. Error Handling. Error Handling. Throwing Exceptions. Throwing Exceptions

Midterm Exam 2 CS 455, Fall 2012

COMP-202A, Fall 2007, All Sections Assignment 1

CS 455 Midterm Exam 1 Fall 2013 [Bono] Wednesday, Oct. 2, 2013

Control Structures: if and while A C S L E C T U R E 4

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Transcription:

COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for a reason because human beings need to know what is in the code that the computer is executing. High-level computer programming languages (like Java) were developed specifically to make it easier for human beings to understand the code they write. However, even high-level programming languages can often be cryptic to people trying to understand the code. Often a programming language is not expressive enough to be able to really get across to a human being what the code is supposed to be doing which is an important thing when a human being is trying to debug or modify a piece of code (especially when that code was written by another person). Because of this, every computer language possesses the ability to embed natural language descriptions called comments into the code. These comments allow programmers to make notes to themselves and to others about what the code they are writing is supposed to do. In this class it is important that your code have good comments and good structure. The Eclipse IDE helps you with some of the structure (whitespace, bracket placement, etc.), and good choices for variable names and method names goes a long way to having good structure. But what makes for good comments? This document describes the commenting conventions for this class (CSE 214). Your labs will be graded with the following conventions in mind. Be aware that if you deviate from these conventions you may be assessed a penalty on your final lab score. It is also important to note that if we have problems understanding your code and you leave no comments to help us figure out what your code is doing, your final grade on your lab may be impacted good comments provide a way for us to figure out where you might have gone wrong with your code (and assess partial credit accordingly).

Example Program The following program, computearea.java, shows an example of the expected commenting conventions used for this class. These conventions make use of the Java DocBook commenting format, which you can feel free to read more about if you are interested. However, we will not be using all of the features of the DocBook commenting format in this class. / computearea.java A simple program that computes the area of rectangles Used to demonstrate the proper use of comments for CSE 214 @author Jeremy Morris @version 20110328 / import java.util.; public class ComputeArea { / prompt Asks the user to provide an integer value. If the user does not provide an integer value, responds with an error and asks the user again until a valid integer is provided. @param in - Scanner object that input is taken from @param message - Message to display to the user to prompt for input @return - An integer value provided from the Scanner object / private static int prompt(scanner in, String message) { int response=0; // Loop until a valid response is obtained boolean validresponse = false; while (!validresponse) { try { // If the user provides an integer at the prompt, the loop ends System.out.print(message); response = in.nextint(); validresponse=true; catch(inputmismatchexception e) { // If the user provides anything except an integer, catch the // InputMismatchException and display an error message System.out.println("ERROR! Only integer values are allowed!"); System.out.println("Please try again."); // Since the "nextint()" above had an exception, the bad data // is still in the Scanner. Use a "nextline()" to clear it out // so the user can provide a valid response. if (in.hasnext()) in.nextline(); return response;

/ computearea Computes the area of a rectangle @param length - length of the rectangle @param width - width of the rectangle @return - area of the rectangle / private static int computearea(int length, int width) { return lengthwidth; / main Provides the main input loop, taking information about rectangle length and width from the user. Program ends when the user gives a rectangle with at least one side with a length of 0. @param args - command line arguments (ignored in this program) / public static void main(string[] args) { Scanner keyboard = new Scanner(System.in); // Since the loop depends on two values that we haven't read yet, // use a boolean variable to control the loop and use those two values // to set this variable boolean endprogram = false; while (!endprogram) { // First, prompt the user for length and width System.out.println("Enter a zero for either value to quit"); int len = prompt(keyboard, "Enter the length of the rectangle: "); int width = prompt(keyboard, "Enter the width of the rectangle: "); // Check to see if we should stop the program if (len!= 0 && width!= 0) { // If both length and width are not zero, compute the area int area = computearea(len,width); System.out.println("The area is: "+area); System.out.println(); else { // If either of length or width is zero, the program ends endprogram=true; System.out.println("Goodbye"); Let s examine each of these pieces in turn:

Program Header / computearea.java A simple program that computes the area of rectangles Used to demonstrate the proper use of comments for CSE 214 @author Jeremy Morris @version 20110328 / The program header comment should be at the very top every piece of Java code you write. It needs to contain the name of your program and a short description of what the program does in your own words. This should be a short summary of the lab assignment, not just a cut-and-pasting of the lab requirements into the program and it should be no more than 5-6 sentences even for a complex lab. Following the short description are two special tags. The @author tag indicates the name of the person who wrote the code this should be your name and it needs to be in the Program Header for every piece of Java code you write. The second tag is the @version tag normally this tag would contain a version number for your code. For the purposes of this class the version number should be a date in YYYYMMDD format (so the above date indicates that this code was written on March 28, 2011). This date can either be the date you started to code the lab, the date you finished coding the lab, or the due date for the lab. But both your name and the date need to be in the program header of your code. Main Method Header / main Provides the main input loop, taking information about rectangle length and width from the user. Program ends when the user gives a rectangle with at least one side with a length of 0. @param args - command line arguments (ignored in this program) / The main method header sits above your main method. The first line should contain the name of the method (main), followed by a short description of what the method is supposed to do. Again this is a short description no more than a few sentences. The main method will include a single tag the @param tag. This tag indicates what parameters the method takes. For a main method, the args parameter is always taken and indicates arguments passed to the code on the command line. For labs that do not use command line arguments, this parameter is ignored and that should be noted in your comment (as in the example above).

Other Method Headers / prompt Asks the user to provide an integer value. If the user does not provide an integer value, responds with an error and asks the user again until a valid integer is provided. @param in - Scanner object that input is taken from @param message - Message to display to the user to prompt for input @return - An integer value provided from the Scanner object / Other method headers follow the same format as the main method the first line is the name of the method, followed by a short description of what the method is supposed to do. Unlike the main method, other methods may contain multiple @param tags. There should be one @param tag for each parameter passed into your method, and following the name of the parameter you should provide a short description of what the parameter is and how the method is supposed to use it. Any method that is a function (rather than a procedure) will return a value and this value should be noted with the @return tag. Again, provide a short description of what is returned by the method to the calling program. Embedded Comments // If the user provides anything except an integer, catch the // InputMismatchException and display an error message System.out.println("ERROR! Only integer values are allowed!"); System.out.println("Please try again."); // Since the "nextint()" above had an exception, the bad data // is still in the Scanner. Use a "nextline()" to clear it out // so the user can provide a valid response. if (in.hasnext()) in.nextline(); Embedded comments are comments made in the code to make it clear to people looking at the code what particular lines (or groups of lines) are supposed to do. Note that these should be fairly short comments if you re writing more than a few sentences to explain what you re doing you might be making things too complicated, either in your code or in your explanation. Following these commenting conventions will make your code more readable and more understandable. In the short term this helps us grade your labs appropriately good comments are often the only way we can tell what you were thinking when you wrote a piece of code and be able to assess partial credit appropriately. But a good commenting style is probably even more important outside of the classroom often good comments make the difference between code that is easy to maintain in a production environment and code that is a nightmare to work with. Good commenting styles are something that good programmers come to appreciate.