Recitation R13 - Program Correctness and the Fraction Class Spring 2016

CS160: Foundations in Programming

Preview

Part I:
• Ungraded programming quiz for attendence
• Introduction to assert statements
• Practice with deriving pre and post-conditions
• Practice with identifying invariants in a loop
Part II:
• Work more with Objects by completing the Fraction class

Announcements

You will have until Sunday midnight to finish and turn in the recitations.
Don't forget to click on the link for the final-practice Hackerrank problems. I reserve the right to add problems until Sunday midnight. This will be ungraded but will serve as a portion of the study guide for your final.

Quiz Four

• Download this text file to your Q4 Project (this is one level above the source folder).
Please exit out of all tabs that do not pertain to the quiz.
You will have 30 minutes to complete the quiz.

Set Up

In eclipse create an R13 project and download the following three files:

Part I.I: Enabling Assertions

There are several ways of debugging your code. One way is through visual inspection. Another way is by putting print statements in your code so that you can see what the values of your variables are changing to throughout the program.
In this lab we will offer an additional resource called assert statements.
• Open ProgramCorrectness.java
• Check out the method funcOne. The precondition for this method states that the range of x should be between -10 and 7.
• In your main method, make a call to funcOne that is out of this range.
• Notice that funcOne is a static method.
This means that we can call it in our main method without instantiating an object of type ProgramCorrectness.
In fact, since there aren't any instance variables in this class, all the methods should be static.
• To make a call that's out of range, you need to provide an argument that is less than -10 or greater than 7.
• Nothing happened!! Let's enable the assertions.
• Once you have enabled the assertions. Change the assertion so that it prints something useful. To do this change assert (x >= -10 && x <= 7); to
assert (x >= -10 && x <= 7) : "x needs to be between -10 and 7, it is: " + x;

Part I.II: Program Correctness

Answer the questions in the associated text document. You can use the class to test ranges. It would be a good idea to loop through the specified ranges and make sure you are getting all the right numbers for the pre and post conditions.
• Questions 1 and 2 give you preconditions and ask you find the postcondition.
• You should add code to main to test the functions for the range specified in the precondition.
• Question 3 gives you a postcondition and asks you to find a precondition.
• Step One: Add code to main method to try a large range of parameter values.
• Step Two: Use a conditional statement that will help you narrow down the code.
• Question 4 asks you to verify a postcondition. Your reasoning should show a firm understanding of what the method does and why or why not the post condition is valid.
• Figure out what the code is doing, then use logical reasoning to verify.
• Question 5 asks you to find the loop invariants.
• You can get the information by adding print statements to the function code or by using the debugger.
• Question 6 asks you to verify that a swap occurs.
• Figure out what the code is doing, then use logical reasoning to verify.

The Fraction Class

In this section you'll complete the Fraction class! We will talk about a few topics here that will really help jump start you into the next semester. Let's get ready to do some coding!

You will need to submit the following three files:
• ProgramCorrectness.java
• ProgramCorrectness.txt
• Fraction.java
To do this:
• Right click your R13 project and select Export
• Expand Java and and select Jar File
• Make sure that ProgramCorrectness.txt is selected and that Java Source File and Resources is selected
• In the Export Destination type in R13/R13.jar

Hackerrank

Here it is! It will be changing a lot over the next few days.
Submit your R13.jar file (with the text file) to Checkin to receive credit.