Learn to code in Java and improve your programming and problem-solving skills. You will learn to design algorithms as well as develop and debug programs. Using custom open-source classes, you will write programs that access and transform images, websites, and other types of data. At the end of the course you will build a program that determines the popularity of different baby names in the US over time by analyzing comma separated value (CSV) files.
After completing this course you will be able to:
1. Edit, compile, and run a Java program;
2. Use conditionals and loops in a Java program;
3. Use Java API documentation in writing programs.
4. Debug a Java program using the scientific method;
5. Write a Java method to solve a specific problem;
6. Develop a set of test cases as part of developing a program;
7. Create a class with multiple methods that work together to solve a problem; and
8. Use divide-and-conquer design techniques for a program that uses multiple methods.

DN

You need a little more experience in coding that just the first course, or try not to leave everything by the end of the week(as I did) to find what you need for the assignments. Great course !

JS

May 19, 2019

Filled StarFilled StarFilled StarFilled StarFilled Star

Really practical course content with great tutorials. The programming assignments are fun and challenging and deal with real world data and problems which makes the course all the more useful!

レッスンから

Strings in Java

This module begins with a short presentation from Raluca Gordân, an assistant professor in Duke University’s Center for Genomic and Computational Biology, about an important problem genomics scientists encounter regularly: how to identify genes in a strand of DNA. To tackle this problem, you will need to understand strings: series of characters such as letters, digits, punctuation, etc. After learning about Java methods that work with strings, you will be able to find genes within a DNA string as well as tackle other string related problems, such as finding all of the links in a web page. By the end of this module, you will be able to: (1) Use important methods for the Java String class; (2) Use conditionals, for loops, and while loops appropriately in a Java program; (3) Find patterns in the data represented by strings to help develop the algorithm for your program; (4) Understand the importance of designing programs that keep different data processing steps separate; (5) Use the StorageResource iterable for this course to store some data for further processing; and (6) Rely on Java documentation to better understand how to use different Java packages and classes.

講師

Owen Astrachan

Professor of the Practice

Robert Duvall

Lecturer

Andrew D. Hilton

Associate Professor of the Practice

Susan H. Rodger

Professor of the Practice

字幕

Okay, now you have an algorithm which works with any of the three stopCodons. However, let's explore how you can make this code work if we decided to have find stopCodon return at -1 when there's no valid stopCodon, rather than the length of the string. Note that this is a perfectly valid design choice and mirrors what the .indexOf method does. But we need to learn a new concept to make it work with our code. You'd want to change line six of the gene finding algorithm to reflect this choice. You cannot just take the minimum since -1 is smaller than any valid index. So what you want to do is pick the smallest number that's not -1. Let's look at a few examples. Here, taaIndex is -1, and tgaIndex is 3, and tagIndex is 6. What index should you pick for the stopCodon? Well, we want 3 because it's the smallest number which is not -1. Notice, we can't just pick the minimum one because that would give us -1 which is not a valid value here. What if we had 5, -1, and 8? We'd want 5. LIkewise, with 10, 4, and -1 we'd want 4. And with -1, -1 and 11 we'd want 11. Let's think about how we can express this decision process algorithmically. In the first example we compared these two numbers first. Even if you think you're comparing all three at once, you're really comparing two at a time very quickly and perhaps without thinking about what you're doing. Of these two values we prefer 3 since the alternative is -1. And then we compare 3 against 6. We choose 3 since it's smaller than 6. In the second example we compare 5 to -1. Then we choose 5 and compare this against 8. In this choice you'd pick the smaller, which is 5. In the third example you'd compare 10 against 4. You'd choose 4 because it's smaller. And then you'd compare 4 against -1 and choose 4. In the final example, you're comparing -1 against -1. It doesn't really matter which you pick, they're the same. You then compare -1 against 11 and you choose 11. Note, you should think about what you would do if all three values were negative. What would that mean? It would mean that no valid stopCodon exists. We'll have to use that in our code. Now how do we think through how we made these choices? We're only going to look at the logic involved in making the choice between one pair. That same logic will work with the second choice. In the first example we picked tgaIndex because taaIndex was -1. In the second example we picked taaIndex because tgaIndex was -1. In the third example where neither is -1 we picked tgaIndex because it's smaller than taaIndex. And in the final example it didn't matter which we picked, since both were -1. Let's write down what our logic is for making that selection. If taaIndex is -1, we'd want to pick tgaIndex. But that's not all there is to it. We'd also choose tgaIndex if both tgaIndex is different than -1 and tgaIndex is less than taaIndex. Notice how we've expressed this logic with or and and. Logical connectives which let us make complex conditionals out of simple ones. Now let's put that logic into the algorithm we're working on. We'll use that logic to pick between taaIndex, and tgaIndex. And we'll store the best choice there is in a variable called minIndex. Then we'll choose the same logic to choose between minIndex and tagIndex. If minIndex is -1, there were no valid stopCodons so we'll give back the empty string. Otherwise, we've found a gene. Now let's see how we express these ORs and ANDs in Java. You can express AND with two ampersands. This is a Shift+7 on most U.S. keyboards. We can see in this example if (x < y && y < z). You can express OR with two vertical bars, also called pipes. On most U.S. keyboards this symbol is Shift+backslash, and we can see an example here. if (a > b || c < d). In the particular case the algorithm we're working on, you could express the step as shown here. Notice how the OR corresponds to the two vertical bars and the AND corresponds to the two ampersands. AND and OR have special rules called short circuit evaluation. Which basically means that if Java can figure out the result of an entire expression involving AND and OR by evaluating only the first operand, then Java will skip evaluating the second operand. Let's look at an example. Suppose x is 8 and y is 1. x Is less than y is not true because 8 is not less than 1. So x < y evaluates to false. There's no reason to evaluate y < z, because whether it's true or false the whole AND expression will be false. Because false AND anything is false. For OR, consider this example and suppose a is 3, and b is 1. Here, a is greater than b is true, because 3 is greater than 1. So the whole OR expression will be true, regardless of whether c is less than d or not. So there's no point in evaluating the expression c < d because true OR anything is true. Why is short circuit evaluation important? If you skip evaluating y < z in this example, it doesn't really matter so much. But it's much more useful when the second operand could crash your program if it's evaluated. For example, here we're checking if x is less than the length of a string AND that the character at the xth index of the string is the letter a. If the first condition is false then x is not a valid index, it's beyond the end of the string. So trying to get the x character with the .charAt method would crash the program with a StringIndexOutOfBoundsException. But fortunately this line of code is safe because of short circuit evaluation. The .charAt method will never be called when x is greater than or equal to the length of the string. Relying on short circuit evaluation is a great example of defensive programming, and one of the tools in your Java programming toolkit. Have fun!