Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
There is also a special version of the multi-line comment, /***/, called the documentation comment.
The javadoc tool will pull out all of these
comments to make documentation of a class as a web page.
It’s a good idea to use the documentation comment in front of classes, methods, and instance
variables in case you want ot use this tool.
/*** MyClass.java* @author My Name* @since Date* This class keeps track of the max score.*/publicclassMyClass(){privateintmax=10;// this keeps track of the max score/* The print() method prints out the max */publicprint(){System.out.println(max);}
If you recall from Unit 5, is a good idea to keep in mind the preconditions and the postconditions for
each method and write them in the comments.
A precondition is a condition that must be true for your method code to work,
for example the assumption that the parameters have values and are not null.
The methods could check for these preconditions, but they do not have to.
The precondition is what the method expects in order to do its job properly.
A postcondition is a condition that is true after running the method.
It is what the method promises to do. Postconditions describe the outcome of running the method,
for example what is being returned or the changes to the instance variables.
These assumptions are very useful to other programmers who want to use your class and get the correct results.
Here is an example of preconditions, postconditions, and @param in the Turtle code that we have used in the past for our drawing turtles.
/** * Constructor that takes the x and y position for the * turtle * Preconditions: parameters x and y are coordinates from 0 to * the width and height of the world. * Postconditions: the turtle is placed in (x,y) coordinates * @param x the x position to place the turtle * @param y the y position to place the turtle */publicTurtle(intx,inty){xPos=x;yPos=y;}
Coding Exercise
Try to break the preconditions of the Turtle constructor below. Does the Turtle constructor behave properly if you break the preconditions that x and y are between 0 and 300. Try giving the Turtle constructor x and y values out of these ranges. What happens? Does the method give good results? Does it give any warnings? What about the t.forward() method? Does it have any preconditions that you can break?
(If the code below does not work for you, you can copy the code into this repl.it link (refresh page after forking and if it gets stuck) or download the files here to use in your own IDE.)
The Turtle constructor’s precondition is that x and y should be between 0 and the width and height of the world. If it receives values out of this range, it sets x and y to the closest legal values that it can so that the turtle appears just at the edge of the world. Similarly, the forward() method will not allow the turtle to leave the world.
Check your understanding
6-3-2: Consider the following class definition.
publicclassTestScore{privateStringstudentName;privatedoublescore;privatedoubleextraCredit;publicTestScore(Stringn,doubles,doubleec){studentName=n;score=s;extraCredit=ec;}/* Other methods not shown */}WhichofthefollowingpreconditionsarereasonablefortheTestScoreconstructor?
/* Precondition: s <= 0 */
It is not reasonable the s which sets the score should be negative.
/* Precondition: score >= 0 */
The precondition should be about the parameters of the constructor. score is not the parameter variable.
/* Precondition: s and ec >= 0 */
Correct. It is reasonable that the score and extraCredit should be set to positive values using the parameters s and ec.
/* Precondition: n is not the empty String */
Correct. It is reasonable that the parameter n which sets the name should be not empty.
/* Precondition: studentName is not the empty String */
The precondition should be about the parameters of the constructor. score is not the parameter variable.
Determining the preconditions and postconditions help us to test our code and determine the validity of our software. Software validity tests whether the software does what it is supposed to do before it is released. This is sometimes very important. For example, if the code is part of a satellite going to outerspace or is going to be used in an emergency condition, we want to test it thoroughly and make sure it works and is valid before it is put into use.
Good software testers actually try to break the code! They try all kinds of input to see what the software will do because you never know what users will try or what conditions there will be. So, always think what the preconditions of your code are and see what happens when you break them, and then see if you can protect or warn against that.
Preconditions and postconditions can also help us to design better software systems. Software designers often first draw a high-level Use-Case Diagram of a system that shows the different ways that a user might interact with a system before they build it. Here is a simple Use-Case Diagram of a restaurant system. It shows 2 actors in the system: the customer and the staff at the restaurant, and 3 use-cases in circles. A Use-case is a particular user interaction or situation in the system or software, and they often become methods in the program.
After drawing a Use-Case Diagram, designers write down the preconditions and the postconditions for each Use-Case. Often the successful post-condition for one use-case becomes the preconditions for the next use-case. For example, for the “Order Food” and “Eat Food” Use Cases:
Preconditions for “Order Food”: Customer enters restaurant. Staff is ready to take the order.
Postconditions for “Order Food”: Customer orders the food. Staff takes the order.
Preconditions for “Eat Food”: Customer has already ordered food. Staff has delivered food.
Postcondition for “Eat Food”: Customer eats the food.
Check your understanding
6-3-3: What are the preconditions and postconditions of the use-case “Pay for food”? Remember that these are often related to the other use-case conditions “order food” and “eat food”.
There are many different models for software development. The waterfall model, developed in the 1970s, is a step by step model where each phase is finished before the next phase begins. This model has recently been criticized because it is not very adaptable. The more recent Agile development model involves iterative, incremental development where teams works in short 2-3 week sprints to completely develop, test, and release a component of the project to the customer for feedback. It is very adaptable as project requirements change because of early testing, immediate customer feedback and collaboration.
One very popular type of agile development is called Scrum. The following short video describes software development with Scrum.
Group Exercise
6.3.5. Programming Challenge : Comments and Conditions¶
Come up with 4 steps that a user must do to purchase a product, for example a book on Java, in an online store, and list the preconditions and postconditions for each step. You could pretend to buy something online to come up with the steps. (You could use an online drawing tool like Creately.com (choose Use-Case Diagrams) to draw a Use-Case diagram for the Online Store System, but it is not required). Don’t forget to list the preconditions and postconditions for each step. You can type in your answer below.
6-3-5: Write down 4 steps that a user must do to purchase a product, for example a book on Java, in an online store, and list the preconditions and postconditions for each step.
Here is a simple class called User that could be used in an online store. Add good commenting to this code before the class, the instance variables, and the methods.
Comments are ignored by the compiler and are not executed when the program is run.
Three types of comments in Java include /**/, which generates a block of comments, //, which generates a comment on one line, and /***/, which are Javadoc comments and are used to create API documentation.
A precondition is a condition that must be true just prior to the execution of a section of program code in order for the method to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.
A postcondition is a condition that must always be true after the execution of a section of program code. Postconditions describe the outcome of the execution in terms of what is being returned or the state of an object.
Programmers write method code to satisfy the postconditions when preconditions are met.
6.3. Comments and Conditions¶
6.3.1. Comments¶
Recall there are 3 types of comments in Java:
//
Single line comment/*
Multiline comment*/
/**
Documentation comment*/
There is also a special version of the multi-line comment,
/**
*/
, called the documentation comment. The javadoc tool will pull out all of these comments to make documentation of a class as a web page. It’s a good idea to use the documentation comment in front of classes, methods, and instance variables in case you want ot use this tool.6.3.2. Preconditions and Postconditions¶
If you recall from Unit 5, is a good idea to keep in mind the preconditions and the postconditions for each method and write them in the comments. A precondition is a condition that must be true for your method code to work, for example the assumption that the parameters have values and are not null. The methods could check for these preconditions, but they do not have to. The precondition is what the method expects in order to do its job properly.
A postcondition is a condition that is true after running the method. It is what the method promises to do. Postconditions describe the outcome of running the method, for example what is being returned or the changes to the instance variables. These assumptions are very useful to other programmers who want to use your class and get the correct results.
Here is an example of preconditions, postconditions, and @param in the Turtle code that we have used in the past for our drawing turtles.
Coding Exercise
Try to break the preconditions of the Turtle constructor below. Does the Turtle constructor behave properly if you break the preconditions that x and y are between 0 and 300. Try giving the Turtle constructor x and y values out of these ranges. What happens? Does the method give good results? Does it give any warnings? What about the t.forward() method? Does it have any preconditions that you can break?
(If the code below does not work for you, you can copy the code into this repl.it link (refresh page after forking and if it gets stuck) or download the files here to use in your own IDE.)
The Turtle constructor’s precondition is that x and y should be between 0 and the width and height of the world. If it receives values out of this range, it sets x and y to the closest legal values that it can so that the turtle appears just at the edge of the world. Similarly, the forward() method will not allow the turtle to leave the world.
Check your understanding
6-3-2: Consider the following class definition.
6.3.3. Software Validity and Use-Case Diagrams¶
Determining the preconditions and postconditions help us to test our code and determine the validity of our software. Software validity tests whether the software does what it is supposed to do before it is released. This is sometimes very important. For example, if the code is part of a satellite going to outerspace or is going to be used in an emergency condition, we want to test it thoroughly and make sure it works and is valid before it is put into use.
Good software testers actually try to break the code! They try all kinds of input to see what the software will do because you never know what users will try or what conditions there will be. So, always think what the preconditions of your code are and see what happens when you break them, and then see if you can protect or warn against that.
Preconditions and postconditions can also help us to design better software systems. Software designers often first draw a high-level Use-Case Diagram of a system that shows the different ways that a user might interact with a system before they build it. Here is a simple Use-Case Diagram of a restaurant system. It shows 2 actors in the system: the customer and the staff at the restaurant, and 3 use-cases in circles. A Use-case is a particular user interaction or situation in the system or software, and they often become methods in the program.
After drawing a Use-Case Diagram, designers write down the preconditions and the postconditions for each Use-Case. Often the successful post-condition for one use-case becomes the preconditions for the next use-case. For example, for the “Order Food” and “Eat Food” Use Cases:
Preconditions for “Order Food”: Customer enters restaurant. Staff is ready to take the order.
Postconditions for “Order Food”: Customer orders the food. Staff takes the order.
Preconditions for “Eat Food”: Customer has already ordered food. Staff has delivered food.
Postcondition for “Eat Food”: Customer eats the food.
Check your understanding
6-3-3: What are the preconditions and postconditions of the use-case “Pay for food”? Remember that these are often related to the other use-case conditions “order food” and “eat food”.
6.3.4. Agile Software Development¶
There are many different models for software development. The waterfall model, developed in the 1970s, is a step by step model where each phase is finished before the next phase begins. This model has recently been criticized because it is not very adaptable. The more recent Agile development model involves iterative, incremental development where teams works in short 2-3 week sprints to completely develop, test, and release a component of the project to the customer for feedback. It is very adaptable as project requirements change because of early testing, immediate customer feedback and collaboration.
One very popular type of agile development is called Scrum. The following short video describes software development with Scrum.
Group Exercise
6.3.5. Programming Challenge : Comments and Conditions¶
Come up with 4 steps that a user must do to purchase a product, for example a book on Java, in an online store, and list the preconditions and postconditions for each step. You could pretend to buy something online to come up with the steps. (You could use an online drawing tool like Creately.com (choose Use-Case Diagrams) to draw a Use-Case diagram for the Online Store System, but it is not required). Don’t forget to list the preconditions and postconditions for each step. You can type in your answer below.
6-3-5: Write down 4 steps that a user must do to purchase a product, for example a book on Java, in an online store, and list the preconditions and postconditions for each step.
Here is a simple class called User that could be used in an online store. Add good commenting to this code before the class, the instance variables, and the methods.
6.3.6. Summary¶
Comments are ignored by the compiler and are not executed when the program is run.
Three types of comments in Java include
/* */
, which generates a block of comments,//
, which generates a comment on one line, and/** */
, which are Javadoc comments and are used to create API documentation.A precondition is a condition that must be true just prior to the execution of a section of program code in order for the method to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.
A postcondition is a condition that must always be true after the execution of a section of program code. Postconditions describe the outcome of the execution in terms of what is being returned or the state of an object.
Programmers write method code to satisfy the postconditions when preconditions are met.