Stage 11 Array Practice With. Zip Code Encoding

Size: px
Start display at page:

Download "Stage 11 Array Practice With. Zip Code Encoding"

Transcription

1 A Review of Strings You should now be proficient at using strings, but, as usual, there are a few more details you should know. First, remember these facts about strings: Array Practice With Strings are not primitives; String is a reference type. In other words, every string is really an instance of the Java s String class. This is because the compiler has to create the memory space for a string without knowing how long the string will be. A String object essentially holds an array of characters (the primitive type char) that, like all arrays, indexes the positions starting at zero. As you have seen, the String class gives us a number of methods to access the individual characters it contains, and, many applications use those capabilities to deconstruct one large string into its constituent parts. For example, if we had a String containing the date in MM/DD/YYYY format, we might want to isolate the substrings for the month, day, and year and treat them separately. Zip Code Encodings Zip Code Encoding The postal service speeds the sorting of mail by printing the zip codes as bar codes that are easily readable by various sorting machines. In this lab, we'll experiment with encoding and decoding zip codes. First, we need to understand how 5-digit zip codes are translated into bar codes. As an example, here is the encoding for the zip code "95014" using the simpler Postnet bar code that is being replaced by the more complex Intelligent Mail code: The encoding is made up of a set of bars that can be full or half height. Since each bar can only be one of two sizes, they are assigned bit values of 0 and 1. The code always begins and ends with a full height bar to help the scanning equipment line up the image. These bits are called frame bits. 241

2 Each digit is encoded using five bits. The first four bits of the encoding are based on positional math, like decimal math, except that the positions are assigned an unique set of multipliers. When using decimal math, a number represents a total value by multiplying each position by a power of 10, starting with a multiplier of 1 in the far right position. Each position to the left has a value of 10 times the value of the position to its right, so 1062 would be the sum of 1* * *10 + 2*1. For our bar codes, each position is a bit (0 or 1) and multiplied by 1, 2, 4, and 7 starting from the right. For example, when the first four bits are 1010, the number encoded is 1*7 + 0*4 + 1*2 + 0*1 giving us a total value of 9. This positional encoding is used for every number except zero. Instead of setting the first four digits of zero to 0000, the encoding is This translates to 1*7 + 1*4 + 0*2 + 0*1 = 11! We'll come back to that to explain why in a minute. The fifth bit in the encoding of a digit is called a parity bit and is used for error checking. The value of that bit is selected so that the encoding of every digit has exactly two full-height bars and three half-height bars. This allows the scanner to detect a variety of scanning errors. For example, it will be able to detect the error of reading a full height bar as a half height bar. If it made that mistake, there would only be one full-height bar and that breaks the rules of our encodings, so we know there is an error. This detection is valid regardless of whether the misread bar is one of the first four bits or the parity bit. Now you should be able to explain why the encoding of 0 is unique. Could we have met the parity rule with the normal 0000 encoding? Can we meet it with the 1100 encoding? How do we know that 1100 isn t used anywhere else? The following table shows the encodings of every digit between 0 and 9 (where 1 is full height and 0 is half height): Digit Multiplier Parity The encoding of the full zip code has one more form of error checking: a check digit. The check digit is chosen so that adding up all of the digits including the check digit results in a sum that is a multiple of 10. For example, the digits in add up to = 22, so the check digit would be 8 and the total sum would be 30 which is divisible by

3 Tricks with the Mod Operator As we ve seen before, the modulus operator (usually shortened to mod and expressed as a % in Java) performs integer division, but, instead of returning the quotient, it returns the remainder. For example, 7 % 3 returns 1 (the remainder left after dividing 7 by 3). Often, the mod operator is used when we cycle through a series of numbers. For example, look at the following code: int count = 0; for (int i = 0; i < 10; i++) System.out.print(count + " "); count = (count + 1) % 4; Run that code with a pencil, what is the output? The interesting statement is the one that is incrementing count. When count is 0, count + 1 is 1 and the remainder when you divide by 4 is still 1, so count appears to increment. However, when count reaches 3, count + 1 is 4 and the remainder when you divide 4 by 4 is 0, so count becomes zero. In other words, count will take on this sequence of values: 0, 1, 2, 3, 0, 1, 2, 3, 0, 1 (and then thefor loop ends). Initializing Arrays at Declaration When we worked on Decks and Cards, we studied array initializers. We re going to use them again, so let s do a quick review. If we know the initial values to be stored in an array, we can put those values in at the declaration of the array. For example, if we wanted an array that contained the first five prime numbers, we could declare and fill the array like this: int[] firstprimes = 1, 2, 3, 5, 7; That would create an array with this memory diagram. We can use this technique for initializing any array regardless of what type of values it holds. For example, boolean[] available = false, true, true, false ; creates this array: 243

4 Initializing arrays at declaration isn t limited to primitive types. For example, if we have a Student class whose constructor takes anint representing the student s id number, this code: Student[] mystudents = new Student(32), new Student(46), new Student(2)); creates this array: Throwing Exceptions In Java, an exception is a condition that is unexpected and requires special handling. You have already seen two exceptions: ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException. Those exceptions occurred when your code tried to use an index that was too large for the structure it was indexing. Since the code didn t know what to do, it caused an exception. In the next lab, we ll study ways to handle exceptions; for now, we will learn how to detect and throw exceptions. When the code detects that an exception has occurred, it throws the exception. This means that the current method stops running (essentially returns without a return value) and the exception is thrown to the code that called the method. For example, if we are developed a method that uses a parameter that it expects to be positive, but passed a negative value into that parameter, that would be an exception. We would want to throw an exception. Our code would look like this: public void sillymethod(int myint) throws Exception if (myint <= 0) throw new Exception("myInt cannot be negative"); // the rest of our method starting here won't // execute if the exception is thrown Since throwing the exception causes the method to return, we can be sure that myint is positive when the rest of the method starts executing. Look carefully at the throw statement; it is usually combined with the new statement. It is creating an instance of type Exception and that is what is being thrown. The parameter to the constructor of Exception is a String that is a message explaining what caused the exception. 244

5 Exception is the most generic type of exception you can throw. Java also comes with a number of other exception types. If you look in the Java API for the class Exception it will list a number of subclasses which are more specific types of exceptions you can throw. For example, oursillymethod() might have been more specific if it had thrown IllegalArgumentException (which is a more specific version of RunTimeException) in place of the genericexception. When you throw an exception, there is one more thing you have to do. The compiler needs to know that there is a possibility that an exception will be thrown. It will use this information to make sure that the code that called your method will either handle the exception or pass it on to the code that called it. Therefore, if the code in your method causes an exception and doesn t handle it, you must add athrows clause to your method declaration as shown in the previous example. Testing For Exceptions It is just as important that we test that exceptions are thrown correctly as it is that we test the normal functionality of our code. JUnit gives us a simple way to denote that a test should expect an exception to be thrown: after annotation, we put (expected = <expectedexception>.class). With this parameter on the annotation, the test will only pass if it sees the specified exception. For example, suppose the sillymethod() in the previous section was part of a SillyClass. If we modified it to throw an IllegalArgumentException, this test would verify that passing a zero as the parameter caused an (expected = IllegalArgumentException.class) public void testexception() SillyClass s = new SillyClass(); s.sillymethod(0); The only way for that test to pass is if something in it throws anillegalargumentexception. 245

6 Content Questions 1. Explain why 0110 is the encoding for There were two ways that we could have encoded 7: 1000 or Which did we pick and why? 3. Draw the memory diagram for the following code: double[] x = new double[5]; x[3] = 3.14; 4. Draw the memory diagram of the array built by the following code: String[] x = new String[6]; for (int i = 0; i < x.length; i++) x[i] = "String" + i; 5. Write a method that takes a string in the format mm/dd/yyyy and returns the day as an integer. 6. If x contains the String NXzW^&@3f what would each of the following evaluate to? a. x.charat(2); b. x.substring(3,5); c. x.length(); d. x.contains("w&"); e. x.contains("&@3"); f. x.substring(3); g. x =="NXzW^&@3f" 7. What is the output from the following code? String x = "NXzW^&@3f"; int count = 0; for (int i = 0; i < x.length(); i++) if ((x.charat(i) >= 'M') && (x.charat(i) <= 'T')) count++; System.out.println(count); 246

7 8. How do we mark a test that is testing to be sure a particular exception gets thrown? 9. What is the syntax to throw an exception? 10. How do you have to change the method declaration if you throw an exception? 11. In lab 8, when we built the SentenceCounter, we made it return -1 when something strange happened. Go back and look at that and describe a better solution. 12. What is the difference between the keywordsthrow andthrows? 247

8 Set Things Up Lab In this lab, we are going to build a class, ZipCode, that builds the bar code for a given zip code. As a plan for where we are going, here is a class diagram of the class we are working on building: There is one instance variable, zipcode, that will hold the zip code we are working on and these are the methods we are going to create are: a constructor that takes an integer parameter and stores it in the instance variable, tostring() that builds a string containing the zip code, getcheckdigit() that computes the value of the check digit for this zip code, and getbarcode() that builds a String containing the bar code for this zip code. As always, we ll build this a little at a time. Create a project in Eclipse and a JUnit test class called ZipCodeTest. Start With the Constructor You're going to create a ZipCode class that is given an integer representing a five digit zip code at construction. For now, we just want to check to make sure that it can identify itself correctly in its tostring() method. First, we write the test. In the test class, create a test method called testinitializationsimple(). This test should do the following: 1. Create an instance of ZipCode for zip code Check that the tostring() of that zip code returns the string "17257"; Build the test, write enough code to make it compile, and watch it fail. (RED) The simplest way to make this test pass is to make tostring() return the String "17257". Do that to make your test pass. (GREEN) Now, we have some duplication (the is in our test and the tostring()of our ZipCode class). We are going to need to REFACTOR to get rid of it. To do that, follow these instructions: make the constructor store the zip code in its argument into the instance variable in the class diagram and return that variable (converted to a String by concatenating it to the empty String) in the tostring() method. Make sure your tests still pass. (GREEN) Zip Code Encoding 248

9 Constructor Border Cases Part 1 In our constructor, we are passing the zip code as an integer even though zip codes always have the same number of digits. In this case, the border cases involve the zip code beginning or ending with zeroes because these are the cases in which we are most likely to lose information, so we d like to write tests for each of those possibilities. First, let s address the issue of trailing zeros. Build a new test that does the following: 1. Create an instance of ZipCode passing to the constructor. 2. Check that the tostring() of that zip code returns the string "12340"; This test is making sure that the trailing zero in the zip code is handled properly. Luckily, it will go GREEN without any work. That doesn t mean the test isn t useful, it just means it didn t require any special code. If you want to be very thorough, make your test verify zip codes 12300, 12000, and to make sure that all trailing zeros work. Now, let s deal with leading zeros. Consider the zip code If we think of that as an integer, we wouldn t normally write the leading zero. In Java, if we use as an int, the leading zero tells the compiler to treat the number as base 8 instead of base 10. There s a long explanation for this, but, for this exercise, it s enough to know that we shouldn t put leading zeroes on ints. To test for zip codes with leading zeros, build a test that does the following: 1. Create an instance of ZipCode passing 1234 to the constructor. 2. Check that the tostring() of that zip code returns the string "01234"; Build the test and watch it fail. (RED) Clearly, leading zeroes are going to require some special handling. tostring() Phase 2 Making the leading zeros test pass is going to take a little more work, but you've done this before. Remember the issue we had when we output seconds in the duration of a song. All you have to do is tag on the leading zero to the output in the appropriate case. Don't go on until the test passes. (GREEN) More tostring() Testing Zip Codes can be any value between 1 and 99999, so they may have more than one leading zero. I doubt we've handled all of those cases, but let's improve our tests to be sure. For each of the following values, create a test, watch to see it fail (RED), then write the code to make it pass again. (GREEN) 123, 12, and 1. At this point, there s a good chance your tostring() method is getting messy. Take a minute to REFACTOR and make sure your tests still run. Right now, we are working with five-digit zip codes. However, sometimes our customers change their minds about details like that. It d be nice if we coded this in a way that would work for nine-digit zipcodes as well. Define a constant for the number of digits in a zip code and make your tostring() use it to decide when to stop adding leading zeros. Hint: you are going to need a loop. 249

10 Tests for Computing Check Digits The first step in encoding a zip code is to compute the check digit. As always, write the test first. Create a new test named testcheckdigit() that creates a number of different instances of zip code and checks that the getcheckdigit() method returns the appropriate integer. Watch the test fail. (RED) Computing the Check Digit Computing the check digit isn t hard, but it takes a sequence of calculations. First, we need to compute the sum of the digits in the zip code. We ve built loops that compute running sums before, so the question is, How do we tear the digits apart? To figure that out, think about what happens if evaluate x%10 for any integer x. If it helps, try it with a few different values of x. Once you see the magic that gives you, think about what happens to x if you execute: x = x/10; Together, modulus by ten and division by ten can make a beautiful sentinel-controlled loop that sums the digits of the zip code. Once you have the sum, you need to pick a number that, when added to that sum will give an overall total that is divisible by 10. That phrase, divisible by 10, should be a clue that, once again, the mod operator can help you. It might be valuable to think about what the check digits are when the sum of the digits is 2, 12, and 22. Write the code to get to GREEN and REFACTOR if necessary. Border Cases for Check Digits? As always, we need to think about the border cases. Where does the behavior of this computation change? It changes at the point where the sum of the digits is a multiple of ten. When the sum is a multiple of ten, the check digit will be zero, but when the sum is one more than a multiple of ten, the check digit will be nine. For example, the zip code needs a check digit of 0, but the zip code needs a check digit of 9. A change of one in the sum that causes a change of nine in the check digit that s certainly a cause for concern. Write a test that contains the border cases of check digits. If it passes right away, great, now we re sure it works. If not, fix the code and make sure that all of the tests run. Get to GREEN and don t forget to REFACTOR. Bar Codes - the Test Now we're really ready to generate the bar codes (or at least it feels that way!). Let's start by writing a simple test: 1. Create an instance of the zip code Check to make sure that getbarcode() returns the correct bar code Wait a minute! How should we display the bar code? It should be a string, but we need to represent full height and half height bars. Let's use these characters: and ` (which looks upside down, but it's sufficient for now). Our bar code should look like this: ``` ``` `` ` ` ` ` ``` `` ` (You should be able to generate that from the zip code!) So, the assertion in the test will look like this: assertequals(" ``` ``` `` ` ` ` ` ``` `` ` ", zip.getbarcode()); Make the test compile. Run it and watch it fail. (RED) 250

11 Refactoring How We Store the Zip Code (background) Encoding our zip code is going to require us to look at its digits individually. We already did that once to generate the check digit. We could copy that code and modify it in getbarcode(), but it's never good to duplicate code (that duplicates bugs, too). It's time to think about how we're storing the zip code. This is an excellent example of the need to refactor. We often start with one solution and then realize that there is a better way to build the system. In these cases, we refactor the code in small steps to incrementally change from the current design to the improved design. Since we have test cases, we can be sure that we don't break anything along the way. The easiest initial strategy was to store the zip code as an int and that has carried us this far. However, since we often want to look at the individual digits, maybe it'd be better to store the digits in an array of ints. Here is a class diagram of where we are going: We re making a pretty fundamental change to our class and it s a change that will affect much of our code. The best strategy is to make this change in a series of small steps, which are detailed in the following sections of this lab. The basic idea is to create the new way we will store the data (the integer array) and then, one method at a time, convert the code to using the new array. At each step, we ll run the tests to be sure we haven t broken anything, and, if the tests are broken, the portion of the code that we have modified since the last time the tests pass will be relatively small so it shouldn t be difficult to locate the problem. We ll delete the original integer instance variable only after we have converted all of the code to use the array. While we do this, the one test of getbarcode() will be RED, but we should keep the rest of the tests GREEN. When we are refactoring, it s most comfortable if the tests are green at all times. For now, let s mark the getbarcode test (next to You ll have to import org.junit.ignore to get it to compile. Then, when you run the tests, JUnit will show GREEN, and the one test that isn t complete will be shown as ignored. That way, we won t forget to come back to it after the refactoring. Refactoring How We Store the Zip Code (phase 1) For the start of this refactoring, declare the instance variable named zipdigits as an array of integers and fill it with the digits of the zip code in the constructor. (Note - we are not yet removing the integer zipcode in which you originally stored the zip code). You ll want to think about which order you want to store the digits in the array. You have two choices. For example, here are two ways we could store the zip code 17257: The first of those diagrams may seem most logical to you because you see the zip code in the order you expect. However, it s often easier to store it in the order of the second diagram because the digits that are multiplied by higher powers of ten are in the higher numbered position of the array. This means that we can calculate the value by multiplying the value in each position by ten to the power of the position minus one. In this lab, you can pick which order you prefer. And you can always change it by refactoring later! To test that you stored the digits into the array correctly, change tostring() to build the string from the array of integers. Don't go on until JUnit is GREEN. 251

12 Refactoring How We Store the Zip Code (phase 2) Our code now has duplication because we have the zip code stored in two places: the integer and the integer array. We need to get rid of our original integer instance variable. The only other place where we use it is in getcheckdigit(). Modify that method to use the new array (which should make it simpler). Don't go on until JUnit shows GREEN. Refactoring How We Store the Zip Code (phase 3) At this point, the original integer instance variable is not being used anywhere but the constructor. Remove it from your code and re-run your tests to be sure that everything is OK. It's important that we pay attention to what we just did. We've made a significant change to our system - its storage mechanisms are completely different. However, we made the change in stages (leaving the old storage around and replacing its use one method at a time). At every stage, we made sure that our tests pass. This way, if we broke anything, we only have to look at a small amount of modified code to find our mistakes. Back to bar codes Now that our zip code is stored in an integer array, generating the bar code will be much easier. Remove tag and our test will be RED. We are going to build the string in pieces, so the test won t go GREEN for a couple of steps. However, at each step we ll be able to look at the error message from JUnit to verify that we are getting closer. To start, we will need some way of generating the substrings for each digit. A simple way to do that is to store the mapping between digit and encoded string. We can define an array of strings that hold the encodings of each digit. For example, the string in position zero of this array will be " ```" since that is the encoding of 0. Similarly, to our declaration of the suits of the Cards, we can declare our array of strings for each encoded digit like this: private static final String[] CODE = " ```", "``` ", "`` ` ", "`` `", "` `` ", "` ` `", "` ``", " ``` ", " `` `", " ` ``"; You should be able to verify each string in this array. 252

13 Bar code generation (phase 1) Now, we're finally ready to start generating the bar code. In getbarcode(), you're going to build a string holding the bar code, but it has several pieces that we'll put together one step at a time. Remember, the bar code contains: 1. a framing full-height bit 2. encodings of each of the digits in the zip code 3. encoding of the check digit 4. a framing full-height bit Start by building a string that has the initial framing bar and make the method return that. (I know it's hard to match up these encoded strings.) Make sure the test fails with an appropriate error message. (RED) Bar code generation (phase 2) Now we need to walk through the array holding the digits of the zip code and concatenate each digit's encoding onto the string we're building. Here's a clue: since the array holding the digits is named zipdigits, the encoding of the first digit will be CODE[zipDigits[0]] Play with that code until you understand how it works. Here s how to analyze it: zipdigits[0] evaluates to an integer that is the first digit of the zip code. For example, if our zip code is and we stored the digits in the order we see them, zipdigits[0] will evaluate to 1. Therefore, CODE[zipDigits[0]] will be CODE[1]. That will be the second string in our CODE array which is "``` ". That is the string that will accurately represent the first number of our zip code. Once you see how that code works, combining it with our usual walking-through-the-array loops should be straightforward. You should be able to add the strings for the digits in the zip code to your initial framing bar. Just be careful about the direction you want to walk through the array. When you run your test, it still isn't going to pass, but you should be able to verify that the error message shows your progress. If that's too complex (because the strings are repetitive), put a breakpoint on the return statement and verify the contents of the string in the debugger. Bar code generation (completion) We're in the home stretch! You just need to concatenate the encoding of the check digit (you can call getcheckdigit() to get its value) and tack on the last framing bit. Watch all of your tests pass! (GREEN) You ve written quite a bit of code to get this test to pass. Go back and clean things up. Remember to check comments, variable names, and indentation. (REFACTOR) Let's be Thorough Just to be thorough, beef up your tests for bar code generation to include zip codes with leading and trailing zeroes and to use every digit from 0 to 9 in at least one zip code. If you code the tests correctly (and don't have any typos in the codes array declaration), they should still pass when you're finished. What Could Possibly Go Wrong? Our constructor takes an integer parameter and interprets it as a zip code, but there are integers that aren t valid zip codes. For example, negative numbers are not valid zip codes. If someone calls our constructor passing in a negative number as the zip code, that is clearly an exception (remember one definition of exception is: a situation that does not normally occur and requires special handling). Our code should detect that this has occurred and throw an appropriate exception (this time, the definition of exception is: An instance of the class Exception). 253

14 Testing for Negative Zip Codes Before we can throw exceptions, we first have to create our own exception class. The Java API includes some exceptions, but it d be more specific if we define our own. Create a new class called ZipCodeException and make it look like this: /** * Exceptions caused by invalid zip codes Merlin * */ public class ZipCodeException extends Exception private String message; public ZipCodeException(String string) this.message = string; public String tostring() return message; In Lab 13, we ll learn what that extends keyword really means, but for now it s enough to know that it means that objects of type ZipCodeException can do everything that objects of type Exception can do. Essentially, we ve made a new type that matches Exception except that it has a different name. This way, our tests will be able to verify that the correct exception is being thrown by our code. In addition, we ve allowed that class to store a message describing what occurred just like the other exceptions that come with Java. The first exception we re going to work on is when the zip code we are given is negative. The border case for this would be when the parameter has a value of -1. Here is the JUnit = ZipCodeException.class) public void testnegative() ZipCode z = new ZipCode(-1); Run the test and it should be RED. Depending on how you coded the constructor, this might cause some other exception to be thrown (like ArrayIndexOutOfBoundsException) or, in the worst case, it could cause a loop to run forever (in this case, you won t see red the green bar in JUnit will never get to the end and the fans on your computer will probably turn on). The only way for this test to pass is for the constructor to throw the ZipCodeException. You ll need to add code at the beginning of the new constructor that checks the value of the parameter. If it is negative, throw an instance of ZipCodeException and put a reasonable description of the problem in the parameter to the new statement. When you add the throw statement, the compiler will complain because it isn t expecting that constructor to throw any exceptions. In order to fix that, you ll have to add a throws clause to the constructor definition like this: public ZipCode(int zipcode) throws ZipCodeException That will make your ZipCode class compile but it will make a number of your old tests not compile. Now that the compiler knows that the constructor might throw an exception, every method that calls that constructor must either handle the exception (which we don t yet know how to do) or just throw it on. In other words, since we aren t going to handle the exception in the tests, they will automatically throw it onto the code that called the test. Therefore we have to add a throws clause to the signature of each of those test methods. That should make everything compile and let you get back to RED. Get to GREEN and REFACTOR to clean things up. 254

15 Other Exceptions There are other ranges of values of the parameter to the constructor which would also represent invalid zip codes. For each, write an appropriate test. Get to RED. Then make the constructor detect the situation and throw an appropriate exception to get to GREEN. 255

16 Lab Questions 1. What is the bar code for the zip code 01437? 2. Explain your implementation ofgetcheckdigit(). 3. What does this code evaluate to? 4. Define these terms: CODE[getCheckDigit()] a. frame bit b. parity bit c. check digit 5. Explain the way we refactored the code to change how we stored the zip code. 6. Why did we create our own exception class? 7. Why did we have to change the definition of every test method when we made the constructor throw an exception? That seems like a terrible burden if the method throwing the exception is used in a lot of places. Why would the designers of Java forced it to work that way? 256

17 JUnit annotation marking a test method so that it will not be run. Check digit a digit added to data with a particular characteristic that makes it unique so that errors made reading the data can be detected. Exception (1) a situation that does not normally occur and requires special handling. (2) A class provided by Java that holds information description an exception that has occurred. (3) An instance of the class Exception. Frame bit A bit at the beginning or ending of a bar code whose purpose is to allow the machine reading to bar code to orient the bar code for reading. Parity bit A bit that is added to a value to provide the ability to detect errors in reading the value. throw A statement that is used when an exception condition has been detected and handling of that exception is being passed to the calling method. throws A clause that is added to a method declaration to denote the fact that it might throw exceptions of the specified types. 257

Table of Laplace Transforms

Table of Laplace Transforms Table of Laplace Transforms 1 1 2 3 4, p > -1 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Heaviside Function 27 28. Dirac Delta Function 29 30. 31 32. 1 33 34. 35 36. 37 Laplace Transforms

More information

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change Chapter01.fm Page 1 Monday, August 23, 2004 1:52 PM Part I The Mechanics of Change The Mechanics of Change Chapter01.fm Page 2 Monday, August 23, 2004 1:52 PM Chapter01.fm Page 3 Monday, August 23, 2004

More information

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch Purpose: We will take a look at programming this week using a language called Scratch. Scratch is a programming language that was developed

More information

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

More information

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials Fundamentals We build up instructions from three types of materials Constants Expressions Fundamentals Constants are just that, they are values that don t change as our macros are executing Fundamentals

More information

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time) CSE 11 Winter 2017 Program Assignment #2 (100 points) START EARLY! Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time) PROGRAM #2: DoubleArray11 READ THE ENTIRE ASSIGNMENT BEFORE STARTING In lecture,

More information

Basic Keywords Practice Session

Basic Keywords Practice Session Basic Keywords Practice Session Introduction In this article from my free Java 8 course, we will apply what we learned in my Java 8 Course Introduction to our first real Java program. If you haven t yet,

More information

The compiler is spewing error messages.

The compiler is spewing error messages. Appendix B Debugging There are a few different kinds of errors that can occur in a program, and it is useful to distinguish between them in order to track them down more quickly. Compile-time errors are

More information

6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

More information

Repetition Through Recursion

Repetition Through Recursion Fundamentals of Computer Science I (CS151.02 2007S) Repetition Through Recursion Summary: In many algorithms, you want to do things again and again and again. For example, you might want to do something

More information

Divisibility Rules and Their Explanations

Divisibility Rules and Their Explanations Divisibility Rules and Their Explanations Increase Your Number Sense These divisibility rules apply to determining the divisibility of a positive integer (1, 2, 3, ) by another positive integer or 0 (although

More information

CS201 - Assignment 3, Part 1 Due: Friday February 28, at the beginning of class

CS201 - Assignment 3, Part 1 Due: Friday February 28, at the beginning of class CS201 - Assignment 3, Part 1 Due: Friday February 28, at the beginning of class One of the keys to writing good code is testing your code. This assignment is going to introduce you and get you setup to

More information

Post Experiment Interview Questions

Post Experiment Interview Questions Post Experiment Interview Questions Questions about the Maximum Problem 1. What is this problem statement asking? 2. What is meant by positive integers? 3. What does it mean by the user entering valid

More information

6.001 Notes: Section 17.5

6.001 Notes: Section 17.5 6.001 Notes: Section 17.5 Slide 17.5.1 Now, let's look at one example in which changing the evaluation model allows us to explore a very different kind of computational problem. Our goal is to show how

More information

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm) CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, 2017 11:30pm) This assignment focuses on recursive backtracking. Turn in the following files using the link on the course

More information

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)... Remembering numbers (and other stuff)... Let s talk about one of the most important things in any programming language. It s called a variable. Don t let the name scare you. What it does is really simple.

More information

Chapter 5 Errors. Bjarne Stroustrup

Chapter 5 Errors. Bjarne Stroustrup Chapter 5 Errors Bjarne Stroustrup www.stroustrup.com/programming Abstract When we program, we have to deal with errors. Our most basic aim is correctness, but we must deal with incomplete problem specifications,

More information

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMSC 201 Fall 2016 Lab 09 Advanced Debugging CMSC 201 Fall 2016 Lab 09 Advanced Debugging Assignment: Lab 09 Advanced Debugging Due Date: During discussion Value: 10 points Part 1: Introduction to Errors Throughout this semester, we have been working

More information

Reliable programming

Reliable programming Reliable programming How to write programs that work Think about reliability during design and implementation Test systematically When things break, fix them correctly Make sure everything stays fixed

More information

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

CSE 142/143 Unofficial Commenting Guide Eric Arendt, Alyssa Harding, Melissa Winstanley

CSE 142/143 Unofficial Commenting Guide Eric Arendt, Alyssa Harding, Melissa Winstanley CSE 142/143 Unofficial Commenting Guide Eric Arendt, Alyssa Harding, Melissa Winstanley In Brief: What You Need to Know to Comment Methods in CSE 143 Audience o A random person you don t know who wants

More information

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit. Com S 227 Fall 2017 Miniassignment 1 50 points Due Date: Monday, October 16, 11:59 pm (midnight) Late deadline (25% penalty): Tuesday, October 17, 11:59 pm General information This assignment is to be

More information

Introduction to Programming

Introduction to Programming CHAPTER 1 Introduction to Programming Begin at the beginning, and go on till you come to the end: then stop. This method of telling a story is as good today as it was when the King of Hearts prescribed

More information

Week 3 Classes and Objects

Week 3 Classes and Objects Week 3 Classes and Objects written by Alexandros Evangelidis, adapted from J. Gardiner et al. 13 October 2015 1 Last Week Last week, we looked at some of the different types available in Java, and the

More information

If Statements, For Loops, Functions

If Statements, For Loops, Functions Fundamentals of Programming If Statements, For Loops, Functions Table of Contents Hello World Types of Variables Integers and Floats String Boolean Relational Operators Lists Conditionals If and Else Statements

More information

Slide 1 CS 170 Java Programming 1 Testing Karel

Slide 1 CS 170 Java Programming 1 Testing Karel CS 170 Java Programming 1 Testing Karel Introducing Unit Tests to Karel's World Slide 1 CS 170 Java Programming 1 Testing Karel Hi Everybody. This is the CS 170, Java Programming 1 lecture, Testing Karel.

More information

CSE 331 Final Exam 3/16/15 Sample Solution

CSE 331 Final Exam 3/16/15 Sample Solution Question 1. (12 points, 3 each) A short design exercise. Suppose Java did not include a Set class in the standard library and we need to store a set of Strings for an application. We know that the maximum

More information

4. Java Project Design, Input Methods

4. Java Project Design, Input Methods 4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java

More information

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist In Handout 28, the Guide to Inductive Proofs, we outlined a number of specifc issues and concepts to be mindful about when

More information

Excel Basics: Working with Spreadsheets

Excel Basics: Working with Spreadsheets Excel Basics: Working with Spreadsheets E 890 / 1 Unravel the Mysteries of Cells, Rows, Ranges, Formulas and More Spreadsheets are all about numbers: they help us keep track of figures and make calculations.

More information

COMP 202 Java in one week

COMP 202 Java in one week COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

More information

Problem Solving for Intro to Computer Science

Problem Solving for Intro to Computer Science Problem Solving for Intro to Computer Science The purpose of this document is to review some principles for problem solving that are relevant to Intro to Computer Science course. Introduction: A Sample

More information

(Python) Chapter 3: Repetition

(Python) Chapter 3: Repetition (Python) Chapter 3: Repetition 3.1 while loop Motivation Using our current set of tools, repeating a simple statement many times is tedious. The only item we can currently repeat easily is printing the

More information

Skill 1: Multiplying Polynomials

Skill 1: Multiplying Polynomials CS103 Spring 2018 Mathematical Prerequisites Although CS103 is primarily a math class, this course does not require any higher math as a prerequisite. The most advanced level of mathematics you'll need

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA 1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018 CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 14: Exceptions 10:00 AM, Feb 26, 2018 Contents 1 Exceptions and How They Work 1 1.1 Update to the Banking Example.............................

More information

Initial Coding Guidelines

Initial Coding Guidelines Initial Coding Guidelines ITK 168 (Lim) This handout specifies coding guidelines for programs in ITK 168. You are expected to follow these guidelines precisely for all lecture programs, and for lab programs.

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

CSE 143: Computer Programming II Winter 2019 HW6: AnagramSolver (due Thursday, Feb 28, :30pm)

CSE 143: Computer Programming II Winter 2019 HW6: AnagramSolver (due Thursday, Feb 28, :30pm) CSE 143: Computer Programming II Winter 2019 HW6: AnagramSolver (due Thursday, Feb 28, 2019 11:30pm) This assignment focuses on recursive backtracking. Turn in the following files using the link on the

More information

T H E I N T E R A C T I V E S H E L L

T H E I N T E R A C T I V E S H E L L 3 T H E I N T E R A C T I V E S H E L L The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform. Ada Lovelace, October 1842 Before

More information

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Hello, in this lecture we will learn about some fundamentals concepts of java.

More information

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017 Due: Wednesday, October 18, 11:59 pm Collaboration Policy: Level 1 Group Policy: Pair-Optional Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017 In this week s lab, you will write a program that can solve

More information

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Excerpt from Art of Problem Solving Volume 1: the Basics 2014 AoPS Inc. Chapter 5 Using the Integers In spite of their being a rather restricted class of numbers, the integers have a lot of interesting properties and uses. Math which involves the properties of integers is

More information

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Errors. Lecture 6. Hartmut Kaiser  hkaiser/fall_2011/csc1254.html Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2011/csc1254.html 2 Abstract When we program, we have to deal with errors. Our most basic aim is correctness, but we must deal with

More information

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

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

More information

Chapter 5 Errors. Hyunyoung Lee. Based on slides by Bjarne Stroustrup.

Chapter 5 Errors. Hyunyoung Lee. Based on slides by Bjarne Stroustrup. Chapter 5 Errors Hyunyoung Lee Based on slides by Bjarne Stroustrup www.stroustrup.com/programming 1 Abstract When we program, we have to deal with errors. Our most basic aim is correctness, but we must

More information

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

More information

SCRATCH MODULE 3: NUMBER CONVERSIONS

SCRATCH MODULE 3: NUMBER CONVERSIONS SCRATCH MODULE 3: NUMBER CONVERSIONS INTRODUCTION The purpose of this module is to experiment with user interactions, error checking input, and number conversion algorithms in Scratch. We will be exploring

More information

Test-Driven Development (TDD)

Test-Driven Development (TDD) Test-Driven Development (TDD) CS 4501 / 6501 Software Testing [Lasse Koskela, Test Driven, Chapters 2-3] 1 Agile Airplane Testing Test harness: Appearance matches Color coding in place Fly 6ft (or 2m)

More information

Assignment 4: Dodo gets smarter

Assignment 4: Dodo gets smarter Assignment 4: Dodo gets smarter Algorithmic Thinking and Structured Programming (in Greenfoot) 2017 Renske Smetsers-Weeda & Sjaak Smetsers 1 Contents Introduction 1 Learning objectives 1 Instructions 1

More information

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE Program 10: 40 points: Due Tuesday, May 12, 2015 : 11:59 p.m. ************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE *************

More information

Variables and Data Representation

Variables and Data Representation You will recall that a computer program is a set of instructions that tell a computer how to transform a given set of input into a specific output. Any program, procedural, event driven or object oriented

More information

CSE143X: Computer Programming I & II Programming Assignment #9 due: Monday, 11/27/17, 11:00 pm

CSE143X: Computer Programming I & II Programming Assignment #9 due: Monday, 11/27/17, 11:00 pm CSE143X: Computer Programming I & II Programming Assignment #9 due: Monday, 11/27/17, 11:00 pm This assignment will give you practice with recursive backtracking. You are to create a class called AnagramSolver

More information

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CSE 142 Su 04 Computer Programming 1 - Java. Objects Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request

More information

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 Announcements Midterm Exams on 4 th of June (12:35 14:35) Room allocation will be announced soon

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

Math Modeling in Java: An S-I Compartment Model

Math Modeling in Java: An S-I Compartment Model 1 Math Modeling in Java: An S-I Compartment Model Basic Concepts What is a compartment model? A compartment model is one in which a population is modeled by treating its members as if they are separated

More information

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM Objectives Defining a wellformed method to check class invariants Using assert statements to check preconditions,

More information

How to approach a computational problem

How to approach a computational problem How to approach a computational problem A lot of people find computer programming difficult, especially when they first get started with it. Sometimes the problems are problems specifically related to

More information

Supplemental Handout: Exceptions CS 1070, Spring 2012 Thursday, 23 Feb 2012

Supplemental Handout: Exceptions CS 1070, Spring 2012 Thursday, 23 Feb 2012 Supplemental Handout: Exceptions CS 1070, Spring 2012 Thursday, 23 Feb 2012 1 Objective To understand why exceptions are useful and why Visual Basic has them To gain experience with exceptions and exception

More information

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units. This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units. Introduction Overview Advancements in technology are

More information

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Math 25 and Maple 3 + 4;

Math 25 and Maple 3 + 4; Math 25 and Maple This is a brief document describing how Maple can help you avoid some of the more tedious tasks involved in your Math 25 homework. It is by no means a comprehensive introduction to using

More information

Types, Expressions, and States

Types, Expressions, and States 8/27: solved Types, Expressions, and States CS 536: Science of Programming, Fall 2018 A. Why? Expressions represent values in programming languages, relative to a state. Types describe common properties

More information

CS 115 Lecture 4. More Python; testing software. Neil Moore

CS 115 Lecture 4. More Python; testing software. Neil Moore CS 115 Lecture 4 More Python; testing software Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 8 September 2015 Syntax: Statements A statement

More information

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

Tips from the experts: How to waste a lot of time on this assignment Com S 227 Spring 2018 Assignment 1 100 points Due Date: Friday, September 14, 11:59 pm (midnight) Late deadline (25% penalty): Monday, September 17, 11:59 pm General information This assignment is to be

More information

Java/RealJ Troubleshooting Guide

Java/RealJ Troubleshooting Guide Java/RealJ Troubleshooting Guide by Bob Clark / Sharon Curtis / Simon Jones, September 2000 Some of these tips you will come across during your practical sessions, however we felt it would be helpful to

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

More information

n! = 1 * 2 * 3 * 4 * * (n-1) * n

n! = 1 * 2 * 3 * 4 * * (n-1) * n The Beauty and Joy of Computing 1 Lab Exercise 9: Problem self-similarity and recursion Objectives By completing this lab exercise, you should learn to Recognize simple self-similar problems which are

More information

16 Multiple Inheritance and Extending ADTs

16 Multiple Inheritance and Extending ADTs Object-Oriented Design Lecture 16 CS 3500 Fall 2009 (Pucella) Tuesday, Nov 10, 2009 16 Multiple Inheritance and Extending ADTs We looked last time at inheritance and delegation as two ways to reuse implementation

More information

6.001 Notes: Section 15.1

6.001 Notes: Section 15.1 6.001 Notes: Section 15.1 Slide 15.1.1 Our goal over the next few lectures is to build an interpreter, which in a very basic sense is the ultimate in programming, since doing so will allow us to define

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

Import Statements, Instance Members, and the Default Constructor

Import Statements, Instance Members, and the Default Constructor Import Statements, Instance Members, and the Default Constructor Introduction In this article from my free Java 8 course, I will be discussing import statements, instance members, and the default constructor.

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

CS103 Spring 2018 Mathematical Vocabulary

CS103 Spring 2018 Mathematical Vocabulary CS103 Spring 2018 Mathematical Vocabulary You keep using that word. I do not think it means what you think it means. - Inigo Montoya, from The Princess Bride Consider the humble while loop in most programming

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

Signed umbers. Sign/Magnitude otation

Signed umbers. Sign/Magnitude otation Signed umbers So far we have discussed unsigned number representations. In particular, we have looked at the binary number system and shorthand methods in representing binary codes. With m binary digits,

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

LOOPS. Repetition using the while statement

LOOPS. Repetition using the while statement 1 LOOPS Loops are an extremely useful feature in any programming language. They allow you to direct the computer to execute certain statements more than once. In Python, there are two kinds of loops: while

More information

MITOCW watch?v=0jljzrnhwoi

MITOCW watch?v=0jljzrnhwoi MITOCW watch?v=0jljzrnhwoi The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

More information

Mathematical Data Operators

Mathematical Data Operators Mathematical Data Operators Programming often requires numbers to be manipulated. This usually involves standard mathematical operators: addition, subtraction, multiplication and division. It can also

More information

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to A PROGRAM IS A SEQUENCE of instructions that a computer can execute to perform some task. A simple enough idea, but for the computer to make any use of the instructions, they must be written in a form

More information

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too) CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too) HW6 NOTE: Do not use the STL map or STL pair for HW6. (It s okay to use them for the contest.)

More information

MITOCW watch?v=zm5mw5nkzjg

MITOCW watch?v=zm5mw5nkzjg MITOCW watch?v=zm5mw5nkzjg The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 9 Date:

More information

COMP-202 Unit 4: Programming with Iterations

COMP-202 Unit 4: Programming with Iterations COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

More information

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING CMPE13/L: INTRODUCTION TO PROGRAMMING IN C SPRING 2012 Lab 3 Matrix Math Introduction Reading In this lab you will write a

More information

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors First Program Compilation and Execution Simplifying Fractions Loops If Statements Variables Operations Using Functions Errors C++ programs consist of a series of instructions written in using the C++ syntax

More information

5 R1 The one green in the same place so either of these could be green.

5 R1 The one green in the same place so either of these could be green. Page: 1 of 20 1 R1 Now. Maybe what we should do is write out the cases that work. We wrote out one of them really very clearly here. [R1 takes out some papers.] Right? You did the one here um where you

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 04: Exception Handling MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Creating Classes 2 Introduction Exception Handling Common Exceptions Exceptions with Methods Assertions and

More information

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style CS125 : Introduction to Computer Science Lecture Notes #4 Type Checking, Input/Output, and Programming Style c 2005, 2004, 2002, 2001, 2000 Jason Zych 1 Lecture 4 : Type Checking, Input/Output, and Programming

More information

COP Programming Assignment #7

COP Programming Assignment #7 1 of 5 03/13/07 12:36 COP 3330 - Programming Assignment #7 Due: Mon, Nov 21 (revised) Objective: Upon completion of this program, you should gain experience with operator overloading, as well as further

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 04: Exception Handling MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Introduction Exception Handling Common Exceptions Exceptions with Methods Assertions

More information

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.

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. Introduction to Netbeans This document is a brief introduction to writing and compiling a program using the NetBeans Integrated Development Environment (IDE). An IDE is a program that automates and makes

More information

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts) CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts) Problem 0: Install Eclipse + CDT (or, as an alternative, Netbeans). Follow the instructions on my web site.

More information

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5 C++ Data Types Contents 1 Simple C++ Data Types 2 2 Quick Note About Representations 3 3 Numeric Types 4 3.1 Integers (whole numbers)............................................ 4 3.2 Decimal Numbers.................................................

More information

contain a geometry package, and so on). All Java classes should belong to a package, and you specify that package by typing:

contain a geometry package, and so on). All Java classes should belong to a package, and you specify that package by typing: Introduction to Java Welcome to the second CS15 lab! By now we've gone over objects, modeling, properties, attributes, and how to put all of these things together into Java classes. It's perfectly okay

More information

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information