0% found this document useful (0 votes)
8 views33 pages

openSAP Java1 Week 2 Transcripts

In Week 2 of the openSAP course on Object-Oriented Programming in Java, the focus is on deepening the understanding of classes, objects, variables, and attributes, particularly through the use of constructors and initialization. The lessons also cover method definitions, return types, and the introduction of arrays and loops, using the metaphor of a detective organizing case files to illustrate these concepts. Additionally, the narrative follows a detective named Duke as he uncovers clues related to a mystery involving a stolen orchid seed and a super villain's plans.

Uploaded by

Regantaz855
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views33 pages

openSAP Java1 Week 2 Transcripts

In Week 2 of the openSAP course on Object-Oriented Programming in Java, the focus is on deepening the understanding of classes, objects, variables, and attributes, particularly through the use of constructors and initialization. The lessons also cover method definitions, return types, and the introduction of arrays and loops, using the metaphor of a detective organizing case files to illustrate these concepts. Additionally, the narrative follows a detective named Duke as he uncovers clues related to a mystery involving a stolen orchid seed and a super villain's plans.

Uploaded by

Regantaz855
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

openSAP

Object-Oriented Programming in Java


Week 2 Unit 0

00:00:00 Hi. I'm Christiane. And I'm Tom.


00:00:02 And we will be hosting you throughout most of the videos this week. Last week, Ann and
Ralf explained
00:00:09 a couple of concepts and techniques that we use in Java and in object-oriented
programming.
00:00:14 And before we present further concepts, we will shortly reiterate over these concepts that
you learned last week,
00:00:20 and we'll mention where the new material of this week will fit in. So, one of the most
important concepts
00:00:28 in object-oriented programming is classes and objects. And you already learned about the
different representations of a class,
00:00:35 like a model, or as program code. And you also learned that at some point,
00:00:40 we need to create objects from the class in order to bring them to life.
00:00:46 While the class is the blueprint of how and what to build, the object is what exactly has been
built.
00:00:54 And we used, or you used, the new operator to create new objects last week.
00:01:01 And in this week, we will dig deeper into this object generation process, and we'll show you

00:01:09 how exactly does this work, and we will talk about constructors, mainly, to do this.
00:01:15 Next to class and objects, you also learned about variables and attributes.
00:01:20 So basically, variables are a container that can hold a certain value of a certain type.
00:01:26 While in most cases, most programming languages, the value can change over time,
00:01:32 in Java, the type of a variable cannot change. It's just impossible.
00:01:37 Once it's declared as a string, it's a string for the rest of the program.
00:01:43 And this is why we call Java a strictly typed language. So, you've also learned how to
declare a variable.
00:01:51 This is letting the program know that you will need a container of a certain type
00:01:56 in the near future, and lets the program allocate some space for it.
00:02:00 And you also learned how to assign a value to a variable, and how to initialize a variable.
00:02:06 And initializing a variable basically is assigning a value to the variable for the first time.
00:02:12 And you also learned how to define a variable. And defining a variable means declaring
00:02:18 and initializing the variable in one step. The term variable actually refers to two different
things here.
00:02:27 First of all, we use it to describe the general concept of a typed container
00:02:32 that can store values, which might change over time. And it is also used to describe
00:02:38 a particular manifestation of that concept. Local variables.
00:02:43 Local variables are only valid within a method, and completely unknown outside of this
method,
00:02:50 and we will talk about this in much detail in one of the upcoming videos.
00:02:56 Attributes, on the other hand, are another manifestation of that concept,
00:03:02 the concept of a typed container. And basically, attributes are variables
00:03:07 that belong to an object, and that are valid within, or throughout this object.
00:03:16 So, an attribute belongs to an object, and this means that, well,
00:03:24 the color of the left Detective is yellowish. The color of the blue Detective, the...
00:03:30 ...the right Detective is blue. And this furthermore means that
00:03:34 each attribute is available, valid, and accessible everywhere,
00:03:39 so in all methods within the object. The local variables, in contrast,
00:03:45 are only available within the methods in which they are declared.
00:03:48 Yellow in yellow, and the yellow variable in the yellow method,
00:03:54 and the red variable in the red method. So, we said that the attribute
00:04:02 belongs to an object, and it is defined, however, in a class.
00:04:07 In this week, we will learn how to initialize an attribute with a different value
00:04:12 while we are instantiating it. Okay. We will also learn about default values
00:04:18 of attributes and variables, and how to distinguish a local variable from an attribute,
00:04:25 in case both have the same identifier, for example. And finally, we will dig deeper
00:04:29 into the scope of variables and attributes. And the term "scope" refers to the...
00:04:34 a variable or attribute can be accessed, can be written or read, and,
00:04:41 within the class or object. So furthermore, you've also learned
00:04:46 how to define and call methods. While defining a method provides us
00:04:52 with the tool for a certain purpose, calling the method puts the tool in action.
00:04:58 And in the example that we see here, we define the method "dig".
00:05:06 And the method dig, kind of, should help us to dig a hole at some point.
00:05:12 Defining this method is kind of comparable to taking or to buying a shovel and to putting that
shovel into the garage,
00:05:21 where it waits for its destination or for its usage. And calling the method actually is
00:05:29 taking out that shovel from the garage and digging a hole.
00:05:34 So, we've also learned that methods can return a value of a certain type.
00:05:40 It is important that the value that is returned has to be of the type that is defined in the
method header.
00:05:46 And each method has a return type. If the method does return a value,
00:05:52 the value can be stored by the caller in a variable for further use.
00:05:57 And theoretically, it is possible to ignore the return value, but if you do that, there should be
a really good reason to do so.
00:06:04 So in most cases, this means that you probably created a method that is not defined well
and has side effects.
00:06:14 However, there are a few cases where it makes sense. So while every method has a return
type,
00:06:20 not every method also has a return value. In this case, the return type is defined as void,
00:06:28 and void just means it doesn't return a value. In this week, we will add further details
00:06:34 on method signatures, on parameters and arguments, and we will introduce a special
method,
00:06:41 the constructor, which we already mentioned will help us to create objects out of a class.
00:06:47 And then, we will also be defining, or we'll show you how to define multiple methods
00:06:54 with the same identifier within a class, and why we might want to do so, actually.
00:07:00 So, other topics of Week 1 were Boolean logic and conditions, and conditions are the first
control structure that we've learned about.
00:07:11 So, in Boolean logic, we're dealing basically with two states.,

2
00:07:15 is something true, or is something false. And conditions are used,
00:07:22 or use these states to decide which path the program should take.
00:07:27 So, based on the truthiness or falsiness of the Boolean expression,
00:07:32 either the statement in one path of the branch, or the other path of the branch will be
executed.
00:07:43 So at some point, these Boolean expressions, they can get quite complex.
00:07:48 And at a certain point, if it gets too complex,
00:07:54 it might make sense to encapsulate these, these complex Boolean expressions
00:08:02 into a method, which provides us with a descriptive identifier
00:08:10 for this complex Boolean expression. And we have an example for that
00:08:16 which we would like to show you now. So, we, see here, the class Detective,
00:08:24 and it has a couple of attributes, Boolean attributes.
00:08:30 It wears a deerstalker, it has a magnifying glass, it plays violin, it smokes a pipe.
00:08:34 And then we have some method, and in this method, if this Detective wears a deerstalker,
00:08:41 and has magnifying glass, and plays violin, and smokes a pipe,
00:08:46 then it should consult Watson, because we assume if it wears a deerstalker,
00:08:50 and has a magnifying glass, and, and, and... then it is probably Sherlock Holmes, and not
Duke.
00:08:57 So... As you see, this is quite complex,
00:09:05 or it's not so complex, and it can even get more complex,
00:09:08 but it's complex enough to say, we don't always want to think about who it is,
00:09:14 if he wears a deerstalker, and has magnifying glass, and so on, so on, so on...
00:09:18 So we define a method that says this is Holmes, so if it does all that stuff, then it probably is
Holmes,
00:09:27 and if it is Holmes, then he should consult Watson.
00:09:31 And calling such methods "is[Hmmhmmhmm]" Ah, yeah. That is a kind of a convention that
we
00:09:40 So this "isHolmes", "isWhatever". "isDuke", "isTree"... whatever.
00:09:49 And in this week, we'll also introduce another control structure. So we'll also learn about
loops this week.
00:09:57 And finally, you learned right at the beginning how to print text on the command line
00:10:03 using System.out.println. So by now, you're pretty well equipped
00:10:07 to tackle basic object-oriented programming tasks. Yeah. We've already mentioned that
00:10:11 we will add further details to many of the topics that we talked about last week
00:10:17 or that Ann and Ralf talked about last week. And we will also introduce
00:10:22 a couple of completely new topics in this week, and these will be arrays and loops.
00:10:28 Yeah, so stay with us... [both] ...on the Java Road.

3
Week 2 Story

00:00:00 Clues, Clues, Clues, After returning from the museum,


00:00:05 Duke sat at his desk again. This time, he was thumbing through his old case files. While the
sword was apparently worth a wagonload of money,
00:00:13 something about the stolen seed made him ponder. A seed? Maybe he could find a hint
which wicked plan Eric was pursuing this time?
00:00:24 Lost in his thoughts, he grabbed the next case file from his shelf. So, a seed. An orchid
seed.
00:00:31 It was rare, there was no question about that, but what did Eric need an orchid seed for?
After skipping over most of this files notes,
00:00:40 one tiny marginalia caught his attention: Experimented with batteries. Yes, that's right!
00:00:47 Eric had a lab in his headquarters, where he experimented with super efficient rechargeable
batteries.
00:00:53 He needed those for his super villain phone and his waterplane. The last time Eric failed
badly, the battery exploded.
00:01:00 Could this be something Eric was up to again? The more he thought about it, the more of a
picture shaped in his mind.
00:01:07 Didn't he just read an article in a magazine last week about a rich landowner who owned
80% of a rare mineral deposit?
00:01:15 How was it called, Accunium? Latest research had shown, that this super rare mineral can
be used for storing more energy
00:01:24 than Eric's waterplane would need in a whole week. Just imagine how many weeks a phone
is running with this.
00:01:29 And well, how come exactly this magazine was stolen from a newsstand recently? It must
have been Eric. Not finding any further clues here,
00:01:39 Duke started thinking about the orchid seed again. What kind of orchid was this?
Bauhinia...something.
00:01:46 In this moment, Duke's phone started ringing. He picked it up. "Hello?" "Hey Duke, it's
Muson. I have found something else.
00:01:55 We cleaned up the museum right after closing time yesterday. Today, I found an unusual
receipt in the trash.
00:02:02 Maybe, the thief threw it away carelessly. I will send it to you via e-mail." "Great, thank
you!", Duke replied.
00:02:10 Finally, his missing last clue might be arriving! Now let's wait for this e-mail and see.
00:02:16 Oh, and ask his robot Robin to find a copy of this magazine ...

4
Week 2 Unit 1

00:00:01 Hi Tom. Hi Christiane.


00:00:03 So Duke is a quite successful Detective and he's solved quite a lot of mysteries already.
00:00:08 And to keep track of all hints he ever found, he stores his old cases in folders on a shelf.
00:00:15 And for easily accessing old cases, whenever he needs to search for additional clues,
00:00:22 he labeled all his folders with a chronological index number and the name of the case.
00:00:29 And, of course, each folder also contains lots of clues. When he solves a new mystery,
00:00:35 he always adds a new folder to his shelf. In the beginning, when he bought his shelf,
00:00:39 it was still quite empty - just one or two cases inside. But at some point, well, his shelf is full,

00:00:46 so he will need to buy a new one. So in an array, we can store items of the same data type,

00:00:55 as Duke does in his shelf with the folders, or with the cases.
00:01:01 We create an array by first defining the data type, and then adding brackets behind the data
type,
00:01:08 and then giving our array an identifier. Arrays can be of primitive data type,
00:01:18 like an array of ints, or of a complex data type, like an array of cases, or an array of folders,

00:01:28 or an array of strings, or whatever. In our example, we have this array of folders,
00:01:37 kind of the shelf, and we do not want to create our own folder objects now
00:01:44 because that would kind of explode the slides and we don't have the space for that.
00:01:48 So we use just strings, to show, to demonstrate.
00:01:54 So the names of the folders, the names of the cases, the mysteries. And we create an
array, and we call this "cases".
00:02:04 And "cases" is an array of strings. That's what you see on the slides here.
00:02:09 Remember that last week, we were not able to store an elephant in our Detective variable.
00:02:17 So today, we are also not able to store folders in our array of strings, and of course,
00:02:25 we also can't store elephants in there. So let's have a closer look at what is happening
00:02:32 when we create our array cases. In line one, we declare our array - this means we will need

00:02:39 an array of strings, and give it an identifier so that we can access it later.
00:02:45 And our array does not have a size yet, so to define the size, we need to initialize our array.

00:02:53 This is what we do in line two. So we initialize our array with the size of 5,
00:02:59 so it can store five strings. Okay. When we initialize our array,
00:03:06 we do this by using the new operator. You also learned how to use the new operator
00:03:12 to create objects in Week 1. And we will cover default values of data types
00:03:20 in much detail in a later video in this week. So for now, you just need to know that some
variables
00:03:31 can have a default data type. Yeah, because when we use this new operator,
00:03:35 we create the array with the default values. Exactly.
00:03:41 It's also possible to define, that means to declare and initialize our array in one step,
00:03:47 as we did with the other variables already. So in order to initialize our array,
00:03:53 we need to add some more syntax. So you need to know some more syntax
00:03:57 for our array declaration. And we add the new operator here

5
00:04:05 to define the same array data type as in our declaration. Then the brackets will follow, and
then within these brackets,
00:04:15 we define the size of the array. That means how many elements this array can take.
00:04:23 So, now how can we fill our array with five strings? Yeah, so this is easy.
00:04:30 We simply add a value at each index of the array. So, did you notice we started here
counting with index 0?
00:04:38 So this is what we always have to do in Java. In Java, we always must start with 0
00:04:44 and end with an index that equals the size of the array minus 1, so here, 4.
00:04:51 And again, we can declare and initialize our array with other values than the default value at
the same time.
00:05:00 To do this, we use the curly braces, and just add our values, separated by a comma.
00:05:06 Then Java knows that this is a value of size 5. An array with the size 5 element,
00:05:13 because we give the five elements already in there. So, let's have a look at another
example here.
00:05:19 And this is about how to access the elements within the array. In line one,
00:05:26 actually, in lines one to three in the example, we declared and initialized our array of strings

00:05:31 with the identifier cases, in just one statement. So we defined that and already have all
these values.
00:05:37 And then, in line four, we kind of call the System.out.println method to print the element
00:05:50 that is in the array cases at index 0. It will print out "Meeting Eike",
00:06:00 because that's the first element in this array. And in line five?
00:06:04 Well, in line five, we print out the element that is at index four of the array cases,
00:06:14 and that would be "The Museum Mystery". It's the last element, the element has five ...
00:06:19 The last element in the array, the array has five elements, and the last one is at index four,
as we start to count at zero.
00:06:26 Yeah, and also, keep in mind that Duke's shelf will be full at some point,
00:06:33 so the same applies for arrays in Java. When we defined our array, we defined the size
00:06:40 and this defines how many items it can store. And this size cannot change dynamically.
00:06:46 So what happens when we try to add an item that exceeds the size of our array?
00:06:52 Tom will show us on the next slide. Whenever we try to add or access an item at a position

00:06:59 that exceeds the array size, the maximum possible size of this array, Java will tell us,
00:07:06 and actually not in a nice way. So when we access cases with index 5, as in line six,
00:07:14 Java will throw an exception. And we will cover exceptions in detail
00:07:21 in one of the later videos this week, so stay with Don't be afraid of exceptions, they don't
hurt.
00:07:32 They actually can be helpful to solve problems, and you will learn about that.
00:07:38 For now, just remember, when you see this kind of error message,
00:07:41 you tried to access or add an item to our array, at a position that does not actually exist.
00:07:50 So stuffing in an additional folder in this shelf just might not work, so the shelf will break,
basically.

6
Week 2 Unit 2

00:00:02 Hello, Christiane. Hello, Tom. Hello, Tom. Hello, Tom. Hello, Tom. Hello, Tom. Hello, Tom.
Hello, Tom.
00:00:08 Hello, learners. So let's consider for whatever reason,
00:00:14 we want to print numbers from one to 100. And in addition, we also want to print
00:00:19 whether this number is odd or even. While the effort for manually writing that down
00:00:25 for like, five numbers is still okay-ish, I definitely would not want to do this for 100 numbers.

00:00:31 And then maybe I accidentally defined one as even, two as odd, three as even, and so on,
00:00:36 and I didn't realize that while writing it down at first. So I added 100 lines of code,
00:00:42 with 100 System.out.println statements that were 100% wrong.
00:00:48 And I have to correct all of these. What a shame.
00:00:51 So this doesn't really sound like much fun, does So it would be really cool
00:00:56 if I just had to write it down once and have this statement executed multiple times.
00:01:01 And this is what loops are for. Okay, this code here produces
00:01:07 exactly the same result as the code on the last slide, but it's way more elegant.
00:01:13 And it's way more maintainable, and it's way more readable.
00:01:17 And it is so by using a loop. Loops are used whenever we need to execute
00:01:26 the same block of statements multiple times. A block of statements, that can be a couple
statements,
00:01:33 it can be one or more. When we use a loop, we have to write down the statements
00:01:38 only once, which saves us a lot of time by writing them down, and it also helps us to recover
from mistakes,
00:01:46 such as the one that Christiane described on the previous slide, way more easily.
00:01:52 Every loop has a loop condition which specifies the iteration.
00:01:59 And it has a loop body that contains the statements to be executed. Generally, we have
three types of loops in Java.
00:02:08 We have count-controlled loops, such as the "for" loop, so from x to y, do something.
00:02:16 Or we have condition-controlled loops, the "while" loop.
00:02:21 As long as x is true, do y. And then we check the condition before the execution of the
statement.
00:02:29 There while Loop checks the condition before the execution of the statement. Then we also
have the "do while" loop,
00:02:36 which is also a condition-controlled loop. And that says do y as long as x is true, basically.

00:02:44 The difference is that it executes the statement and then checks the condition.
00:02:49 That means the statements in the body are always executed at least once,
00:02:56 while in the "while" loop, they might never be executed. Actually, we will not cover the do
while loop
00:03:03 in this course, as it's rather rarely used and actually not so important.
00:03:09 But if you want to learn more about it or find out more details about it,
00:03:15 we will provide you with some literature resources where you can read about that.
00:03:23 Finally, we also have the collection-controlled loops, and we will cover collections
00:03:29 and these collection-controlled loops in Week 5. So let's start with the count-controlled
loops.
00:03:36 First, we need a loop count, "i", which is initialized at the start to 0.
00:03:43 And then we need a termination expression, what is the target size for a loop count.

7
00:03:49 And then we need to specify how to increment the loop count. And in this case, we choose
"++", which means
00:03:57 the same as plus one, which you might remember already. So sometimes we also might call
this the step size of the iteration.
00:04:07 So seen more abstractly, the loop condition contains four elements - the keyword, here
"for",
00:04:16 the initialization expression, the termination expression, and the increment expression.
00:04:22 So let's have a closer look at the loop condition. We use the keyword "for", as Christiane
already said,
00:04:29 to indicate that it's a for loop. So we already know that.
00:04:33 And then the loop count "i" is initialized with the value "0", because we want to start
00:04:43 at position zero, for example. And before each iteration of the body,
00:04:50 it is checked against the termination expression, in this case, i is less than three,
00:04:58 and then the statements are executed, and then the loop count is incremented
00:05:05 to the next position. So same example, but now actually executing a statement
00:05:12 in the loop body. So we have i.
00:05:15 i is 0 at first. And 0 is smaller than 3.
00:05:20 So we print out "Number 0". At the end of the loop body,
00:05:27 i is increased to 1. And with this, we go to the next iteration of our for loop.
00:05:34 So we check i against 3, i is 1, so it's still smaller than 3,
00:05:40 so we print out "Number 1". Then increase i to 2, again, check this against
00:05:47 still smaller, so we print out "Number 2". After this execution, i gets incremented to 3.
00:05:55 3 is not smaller than 3, it is 3, so it does not execute the statement
00:06:01 in the loop body anymore. Now let's have a look at the condition-controlled loop, or the
while loop.
00:06:09 As in the for loop, the condition will be checked each time
00:06:12 before the statements in the loop body are executed. In contrast to the for loop, however,
00:06:18 there is no explicit loop count. Instead, we check before every iteration
00:06:24 if a certain conditional expression still holds true. If yes, the statements in the loop body will
be executed.
00:06:32 Anything that evaluates to either true or false can serve as this conditional expression.
00:06:42 And this could be, as we show on the slides here, "i" is less than "j", or "a" does not equal
"b",
00:06:50 or it can be just "a", if "a" is a Boolean expression, or Boolean variable.
00:06:56 Can be not "a". But it also can be the return value of a method that is called,
00:07:03 and that method might be, has the user canceled a certain operation?
00:07:11 So did he press a button or did she press a button? Or it can also be the interaction
00:07:16 between certain objects within the program. So as long as Duke has not caught the bad
guy,
00:07:24 do something. Search for him.
00:07:27 The problem with while loops is that we can easily create infinite loops
00:07:32 and actually, in, well we were discussing about that... if it's 100% of the cases where you
don't want to have infinite loops
00:07:41 and actually, you don't want to have an infinite because even if you want to have an infinite
loop,
00:07:46 it should be terminatable by user interaction. Yeah, and not by...
00:07:53 ...killing the program. So it would also be possible to misuse a while

8
00:08:02 as a count-controlled loop. And we consider this bad practice
00:08:05 due to a couple of reasons, but it's possible. So here, we initialize our loop count
00:08:14 outside of the loop. So our statement that we have in the for loop,
00:08:22 we have it here outside of the loop. And we then increment...
00:08:30 the loop count inside of our while loop. So the problem with that is that as we initialize,
00:08:40 or as we declare and define the loop count outside of the loop, it is also not encapsulated in
the loop
00:08:47 as it would be in the for loop, which is a bad thing, and we will cover that later on also
00:08:53 in the scope video, in the video about scopes. So finally, we have an example
00:09:03 where we use a count-controlled loop to iterate over an array. The important thing to notice
here
00:09:10 is that we use the length of the array, the attribute length of the array
00:09:19 that returns to us the amount of elements that are available or possible within this array.
00:09:28 And we can go through this loop without knowing exactly how many elements
00:09:35 are contained in this array. But just calling or using this length attribute of the array.
00:09:46 But make sure to understand that this doesn't return the amount of values that are stored in
the array,
00:09:54 but the amount that could be stored. So even if the shelf is not full,
00:09:58 it would still tell us, well, there are still one, two, three places available for our folders,
00:10:03 but there are no folders inside. So let's go through this example.
00:10:08 So in line one, we define the array, so we have a string array again,
00:10:13 and it's a couple of cases. And line one till line two actually,
00:10:18 and then in lines four to six, we loop through this array.
00:10:24 We start at the first element of the array, that's why we start with the index 0,
00:10:30 or with the loop count 0 as well. And then that, yeah, prints us out
00:10:38 the value of this index of the array, which in this case is "A Strange Phone Call",
00:10:45 one of the first or the early cases of Duke. And the next iteration,
00:10:53 and then after printing that out, the loop count is incremented,
00:10:59 and then the next element is printed out again, and so on and so on, until we have reached
00:11:07 the end of the array and magically it works.

9
Week 2 Unit 3

00:00:00 Have you ever wondered what we put in the parenthesis of System.out.println?
00:00:05 Well, some methods expect additional information to create a result, and for this we need
parameters and arguments.
00:00:14 So when a method is defined, as we do here in line four, we have to define the parameters
that this method expects.
00:00:25 And, within the parenthesis, we define how many parameters the method expects,
00:00:32 and of which type these parameters have to be. When the method is then called in line one,

00:00:39 we pass actual values to these parameters. And these values are called arguments.
00:00:46 A parameter is a particular type of a local variable, and it is only valid within the method,
00:00:53 but it also provides the possibility to pass a value from outside of the method,
00:00:58 to the inside of the method. And basically,
00:01:02 as we see in line seven, we can use then these parameters
00:01:06 within the method as a placeholder, and this placeholder will then be replaced
00:01:12 with the argument that is passed to the parameter. As we've already seen on the previous
slide,
00:01:20 maybe you've noticed or not, not only literals can be passed as arguments.
00:01:25 And literals, that's a special term for real values that we want to introduce here.
00:01:31 For example, the strings that are passed here on line three, Hello, Duke, or the colons.
00:01:39 It could also be an int, like 5, 7, or 0, or a Boolean true or false.
00:01:47 In line six, we are also calling a method and we are also passing an argument,
00:01:53 but in this case actually it's not a literal. In this case it's a variable,
00:01:59 and it's the variable that we have previously defined in line three,
00:02:03 so it's the result of the concat method, and we pass that as an argument to the print line
method.
00:02:12 And basically instead of defining this variable and passing that as an argument,
00:02:21 we could also omit that step and directly put the return value of the concat method
00:02:29 into the print line method as an argument.
00:02:34 Actually as you can imagine, you can go on and on and on, and you can also
00:02:40 add return values of a method where the arguments to the concat method are passed
00:02:47 and you can create pretty unreadable code with that, so, you should be knowing what you're
doing
00:02:56 at that point, and don't overdo it. So just to sum it up,
00:03:00 the arguments that are passed can be literals like "Hello", "5", or "true".
00:03:07 It can be variables and attributes, and it can also be return values of methods.
00:03:13 And basically everything that resolves the correct type of the expected parameters
00:03:19 can be passed as an argument. The arguments that are passed to a method
00:03:27 have to match the expected parameters exactly. So, if three parameters are expected,
00:03:34 we have to pass three arguments. If arguments of the types
00:03:41 String, String, and int are expected, we have to arguments of the types String, String, and
int.
00:03:46 And we have to do that exactly in this order. And in some of the upcoming videos,
00:03:53 we will learn how to add standard values for some parameters if they often tend to be the
same.
00:03:59 So that's what we call method overloading, and as you said, we will learn about that later.

10
00:04:05 So, now to the method signature. The signature of a method, or the method header,
00:04:11 defines the public interface of a method. And it gives us the return type,
00:04:17 and the amount and type of parameters that are expected by the method.
00:04:22 This is basically all the information we need to properly call the method.
00:04:28 So, let's look at our example again. We have a method, concat,
00:04:34 which concatenates two strings with a connector string. And like all methods, it consists of
two parts,
00:04:42 the method header and the method body. The method header provides the method
signature,
00:04:49 and this signature actually provides us with all the information that we need to correctly
00:04:57 call this method. So, first of all, of course, we need to know the identifier.
00:05:03 So, how do we know how to call the method, or by which name we're going to call the
method?
00:05:10 Then we need to know the return type, so that we know to what type of variable
00:05:15 we can assign the return value of the method, and then we need to know the amount and
type of parameters
00:05:24 that we pass into that method, so that we can provide the correct arguments that we...
00:05:31 throw into that method at that point. And actually, for all that, or to do all that,
00:05:38 we do not have to know at all what is written in the method body,
00:05:43 and what the method is actually doing. We can call a method with the correct arguments,
00:05:53 the correct return value, and take it to the correct variable,
00:06:01 without knowing what the method is doing. That is basically that.
00:06:05 Then the method signature provides all necessary information on
00:06:08 how to call a method without producing an error. And it tells us the correct identifier,
00:06:14 the correct return type, and the number and the type of arguments
00:06:18 that we have to pass. If we use descriptive identifiers,
00:06:23 to some extent it also gives us information on what the method will do with the passed
arguments,
00:06:29 and it helps us to decide whether this is the correct method or not.
00:06:35 So, to make this maybe more clear, at least we hope so,
00:06:40 let's have a look at another example. So here we have a method "url",
00:06:46 and this method url is defined to expect three strings, the scheme, the host, and the path of
the url,
00:06:54 basically from the signature, it is exactly the same as the concat method
00:06:58 that we used in the previous example. And the body of the method then builds a very
simplified,
00:07:10 basic url, and returns this as a string again. In the current example, by the way,
00:07:20 it builds the string that leads to openHPI, our course listing, for example -
00:07:26 that might be very interesting for you as well to have a look there.
00:07:31 So, if I'm familiar with URLs, I know what kind of arguments the method expects from me,
00:07:40 and to some extent, it informs me what it will do with the parameters,
00:07:46 and I will get a correct URL, but I have to know what a scheme is,
00:07:50 I have to know what a host is, and I have to know what a path of a URL is.
00:07:55 So in case I'm not familiar with these, and I don't know what a URL is,
00:07:59 I might as well pass in arguments that will not lead to correct URLs.
00:08:03 So, if I think that a scheme is the Detective, and a host, well Duke, he's a person, he might
host you,

11
00:08:10 and a path, well you can go through Berlin - you can also pass that into your "url" method,
00:08:16 but it will not produce a correct URL, if you try to open that URL.
00:08:25 But at this point, it will not produce an error because you passed in the correct arguments.
00:08:31 Exactly, So, you pass in the strings and you get out a string. But it's definitely not a valid
URL.
00:08:39 And it will kind of, yeah, hit you when you don't expect it.

12
Week 2 Unit 4

00:00:01 When we create a variable in a loop, we are not able to access this variable from outside of
the loop.
00:00:07 This actually is due to the scope. In Java, every set of curly braces,
00:00:15 and we also call this sometimes a code block, defines a scope.
00:00:20 And the scope defines the zone of validity for a variable. Every variable is valid.
00:00:28 That means it can be accessed, or it can be read or written within the scope in which it is
declared.
00:00:36 The scope of an attribute is defined by the curly braces that the limit declares,
00:00:43 that kind of surrounds the attribute. The scope of a local variable is defined
00:00:49 by the curly braces that delimit the method. And the scope of a loop variable is defined
00:00:57 by the curly braces that delimit the loop. In theory, actually, we could just place a set of
00:01:06 curly braces anywhere within a method, surrounding a variable, and then it would be only
00:01:14 valid, readable, accessible within that set of curly braces, if we ever wanted it to do that.
00:01:19 Yeah, why should we do it? But we could.
00:01:23 So, in this example, the attribute age is defined within the scope of the class Detective.
00:01:30 And it is valid throughout the class, including all of its subscopes, so methods,
00:01:35 whatever may come within that class, and we could say that the curly braces
00:01:41 are semi-permeable. And, they let in variables that we have declared outside,
00:01:48 but they do not let out variables that have been declared inside.
00:01:55 The local variable, minAge, here is defined only within the scope
00:02:01 of the method allowedToDrive, and it just does not exist in the class
00:02:06 or in the class's other methods. And the same applies here for the parameter
00:02:13 minAge in this example. This also only is available within the scope
00:02:19 of the method allowedToDrive. But, in contrast to a simple local variable,
00:02:24 it provides a medium to transport data from the outside to the inside of a method.
00:02:30 And similar to the way the return statement provides a method with the option to transport
00:02:37 from the inside to the outside of the method. Loop variables that are defined within the
scope
00:02:45 of the loop, as in this example, are also only valid within the scope of the loop.
00:02:53 This comes in quite handy when we have a couple of loops following each other within one
method, for example,
00:03:02 then we can also always reuse the same loop variable and do not have to, like, iterate the
complete alphabet to do so.
00:03:12 But, we have to be careful when we are talking about nested loops. Nested loops is a topic
that we will be dealing with in the...
00:03:22 ...in the deep dive of this week. So, these are loops that are kind of nested within each
other.
00:03:29 And, in this case, as Christiane already said, these curly braces are sort of semi-permeable.

00:03:38 We cannot use the same loop variable in the inner loop as we use in the outer loop
00:03:44 because then the, kind of, the loop variable, the scope of the outer loop,
00:03:52 the loop variable in the outer loop would also trickle down to the inner loop and then that
would be, kind of,
00:03:58 not working any more. So, yeah.
00:04:05 Also, if we are working with conditions, we have to be careful.

13
00:04:10 So we should not declare a variable within one branch of the condition if we also want to
use it
00:04:16 within the other branch of the condition. Because, in this case, as we did here,
00:04:21 that would result in an error because in the second branch of the condition, as in line five,
00:04:28 this variable just would not exist. So, it's only available within the first branch
00:04:33 in line three. The solution to solve this problem would be
00:04:41 declaring the variable outside of both branches, outside of the condition,
00:04:47 and then just assigning the values within the branches of the condition. So, theoretically,
you can declare
00:04:55 a scope wherever you want within a method of a class by just adding a set of curly braces.
00:05:02 The variable declared inside of the braces is only valid within that scope.

14
Week 2 Unit 5

00:00:00 Hi, Tom. Hi, Christiane.


00:00:02 So, in this video we will focus on what exactly is happening
00:00:07 when we create an object using the "new" operator. And whenever we use the new
operator,
00:00:12 we actually call the class's constructor. And constructors are a very important
00:00:18 concept of object-oriented programming, as they help us to instantiate objects from a class.

00:00:24 And the constructor, or better the statements that are executed within the constructor,
00:00:29 then you find the initial state of the object. And using the new operator, and the constructor
with that,
00:00:37 we can create any amount of objects from a class. So that's what we actually mean
00:00:43 when we always say that a class is a blueprint, while the objects are the blueprint turned
into "reality".
00:00:52 So far, we have worked with several classes, and actually a couple of objects,
00:00:56 but we have never defined a constructor in any of these classes. How is that so?
00:01:01 So, in Java, this is possible as Java provides each class with an implicit default constructor,

00:01:10 yeah, which is called a default constructor. And this default constructor initializes
00:01:14 all attributes with the default values. So the default value that is defined
00:01:20 for the type of this attribute. For example, if you're dealing with an int, that would be 0,
00:01:28 or if you're dealing with a boolean, that would be false. And we'll cover default values for
attributes
00:01:34 in more detail in one of the upcoming videos, so don't worry about that too much.
00:01:41 And as all of these attributes are initialized with the default values,
00:01:46 the default constructor does not need to take, and does not take, any parameters.
00:01:52 So it's parameter-free, so to speak. And what we want to emphasize,
00:01:59 and say again, is that the default constructor is not explicitly
00:02:06 manifested in our code, so we don't write it.
00:02:09 It's just there, it's provided by Java and you don't see it.
00:02:12 You just can assume it is there. The only thing is,
00:02:20 in the moment when you write a custom constructor, it's unfortunately not there anymore.
00:02:27 So if you have a custom constructor and still need the default constructor, then actually you
will have to write it in your
00:02:35 and you would do that as we've shown that in the example. So, why should we actually
define our own constructors,
00:02:44 if we override the default constructor then? Well, the problem is that with the default
constructor
00:02:50 we can only instantiate the objects that we create from the class with the default values,
00:02:57 with the values for all these attributes that are defined as a default.
00:03:02 And the idea of custom constructors is that we want to create any sort of object from this
class.
00:03:12 So, in the case of our Robot, maybe we want to have not only
00:03:16 a green Robot or a blue Robot maybe we also want to have a green Robot and a blue
Robot
00:03:21 and a red Robot and a violet Robot and a yellow Robot and so on, and so on.

15
00:03:27 And this is why we need these custom constructors, and I hope you can show us how to do
that, Christiane.
00:03:34 Yeah, I'd love too. So let's have a look at the example.
00:03:38 Constructors are basically methods, with some special features.
00:03:41 So they initialize the attributes of an object. And first in Java they have to use
00:03:47 the same identifier as its class. And they have to be written in the same way,
00:03:52 so if our class Robot starts with a capital R, our constructor identifiers also
00:03:58 called Robot with a capital R. This always has to be the same.
00:04:03 So, second... you never define an explicit return value for your constructor,
00:04:10 as you can see there the Robot doesn't have any type in front of it.
00:04:14 So, it's not only the return value, but also the return type.
00:04:18 So, the constructor itself always returns a newly created object, and...
00:04:26 of the class type, so this is always returned there by default.
00:04:31 And so you also don't have to define a return statement for that constructor
00:04:35 because it will do that anyway. That's right.
00:04:39 And constructors as methods can take any number of arguments,
00:04:44 which could be zero or any other number. But you always need to add the parenthesis at
the end of the constructor.
00:04:54 So, in the code example that we have here, you see a simple constructor which initializes
00:05:00 our Robot with the color of blue. And to instantiate...
00:05:12 an object of this class and for this example, you would do so as you can see in the last line
of the slide,
00:05:19 and you would say that Robot robby... that's kind of the variable where we store that in,
00:05:27 a reference, we will come to that. And then we assign this newly created Robot
00:05:35 to this variable. And...
00:05:39 the original question was, what are we doing if we want to have a green and a red and a
blue Robot?
00:05:44 Because in our example they would always be blue. Yeah that's right.
00:05:48 We still need to do something. So, to achieve this we need to define
00:05:53 a constructor which takes a parameter. So we see that in this example.
00:05:57 We have the Robot which takes the parameter, "color". And if the parameter has actually
00:06:05 the same identifier as the attribute, we have to add this dot,
00:06:10 and of the attribute to clearly distinguish which of the ones is the class's attribute.
00:06:18 So, Tom, let's have a look at our Robots. Yeah, well, we've brought you something.
00:06:24 We have these two Robots here. And this color of this Robot is gray
00:06:30 while this color of this Robot is well, reddish, kind of.
00:06:37 So this dot always refers to the object in which we currently are moving, so to speak.
00:06:48 And to call a constructor with parameters, we just need to call the constructor with the
argument,
00:06:56 as shown in the example at the bottom. So, as with all other methods,
00:07:03 constructors can take any amount of parameters. We already said that.
00:07:07 And we have provided you here with another example, and in this example, we have added
a second parameter,
00:07:14 and this is called numberOfEyes, it's of type int.
00:07:16 And if we call this constructor, we now have to pass both arguments,
00:07:21 the color and the number of eyes, and you see that in the last line of the slide.

16
00:07:26 So we have to pass into this constructor the two arguments, "blue" and...
00:07:34 "two eyes" in this case, yeah.

17
Week 2 Unit 6

00:00:00 Welcome back to our constructors, and this time we will show you how to write classes
00:00:05 with multiple constructors, and this technique is actually called constructor overloading.
00:00:11 Okay, so let's have a look at an example. Most of our Robots are blue and have two eyes.
00:00:18 Some of our Robots are green or red. And very few of our Robots have more than two eyes,
maybe.
00:00:24 So wouldn't it be great if we could define default, initial values for our standard objects?
00:00:30 The blue ones with the two eyes. And if we could use a parameter-free constructor
00:00:36 for these standard objects, the blue ones with the two eyes. And if we only had to specify
initial values,
00:00:43 and pass them as arguments for those objects that deviate from the standard, so the red
ones,
00:00:50 or the green ones, and those that have more than two eyes. So that would save us a lot of
code, I guess.
00:00:55 Yeah, it sounds like a cool idea. So let's have a look at this example,
00:00:59 where we have multiple constructors in one class. So we're overloading our constructors.
00:01:05 This code example spans over two slides and what we're actually doing here,
00:01:10 this technique we can call it convenience constructors because we actually reduce the
amount of code that we have to write.
00:01:18 It makes it more convenient for us to write code later on. To write classes for...
00:01:25 create objects for blue-eyed Robots with two eyes, and red Robots with one eye, or
whatever.
00:01:34 So the first constructor in the example takes two parameters, which can be defined
00:01:40 with a custom argument that we pass into this constructor when the object is created.
00:01:45 So one of the parameters that we have is the color and the other one is the number of eyes.

00:01:51 The second constructor actually takes no arguments and initializes our Robot
00:01:58 with some standard values - the color blue, and the number of eyes, two.
00:02:05 And here we have another constructor, and this only takes the number of eyes
00:02:09 as a parameter, and initializes all Robots that are created this way,
00:02:14 or instantiated this way with the color of green, and then we can specify how many eyes this
Robot should have.
00:02:21 And finally, our fourth constructor takes a custom color and assigns a standard amount of
two eyes.
00:02:28 So overloading constructor is only possible if a couple of requirements are fulfilled.
00:02:37 And these are actually more or less two requirements. And this is that either the amount of
parameters
00:02:46 that are passed into this constructor, that are defined for this constructor, differs
00:02:52 or that the type of these parameters differs. Or both.
00:03:00 And if we have a look at the example, on line one, we can see that we have
00:03:05 a constructor that takes two parameters, the String "color" and the int "numberOfEyes".
00:03:12 In the next line, we have a constructor that takes zero parameters, so the amount of
parameters differs.
00:03:22 Requirements fulfilled. In line five, we have a constructor that takes
00:03:26 one parameter, and this is of type int, so number differs with the two previous ones,
00:03:35 so requirements are still fulfilled. And in line seven, now we have another parameter
00:03:42 that also takes one parameter. So the first requirement is not fulfilled,

18
00:03:47 so it has the same amount of parameters as the constructor in line five,
00:03:51 but as the type of the parameter differs, it's a String in this case, and not an int, as in line
five,
00:03:58 the requirements are still fulfilled. However, we cannot add the constructor in line nine.
00:04:04 We there have one parameter and we also have the same type, the String. And although
the identifier of the String
00:04:13 is called differently than the one in line seven, so name here instead of color,
00:04:17 our requirement is not fulfilled, so we cannot do this.

19
Week 2 Unit 7

00:00:00 Okay, now let's have a closer look at "this". There are two ways, actually, to employ "this".

00:00:08 That's "this dot" and "this parenthesis". And we already have encountered "this."
00:00:16 in various places in previous videos, and now we will explain that in more detail
00:00:22 and we also will have a look at "this()". Yes. So, "this." allows us to
00:00:28 access the attribute, and sometimes the method, of the current object.
00:00:33 So, in line five, when there is a naming collision between the identifier of the local variable
00:00:41 and the identifier of an attribute, we must use "this." to clearly distinguish
00:00:46 which one of those two is the attribute of the object. And in Java, using the same identifier
for an attribute
00:00:56 and a local parameter is a quite common pattern, so you have this collision actually quite
often.
00:01:02 And in line eight, there is no collision, and you might still want to use this there for better
readability,
00:01:15 but it's not necessary. "This parenthesis",
00:01:22 which is what we look at now, on the other hand, allows us to call the
00:01:28 overloaded constructors of our class, so if we have more than one constructor in the class,
00:01:34 at least one of them will be overloaded. And to have a look at that now, let's revisit
00:01:42 the example that we had in a previous video. Till line eight...
00:01:51 Yeah, nothing has changed in our code. And we still have two attributes
00:01:56 and a constructor that initializes these attributes with the two passed arguments.
00:02:01 However, in line 11, instead of directly assigning now the attributes,
00:02:08 we call "this()", and by this we call the constructor
00:02:15 that was defined in line five, and pass the arguments.
00:02:20 And the same applies here in line 16, and in line So and even if this doesn't look like much
of an improvement,
00:02:29 this technique actually substantially improves the maintainability of our code.
00:02:34 So maybe you might remember in the last video, there were some inconsistencies
00:02:41 with the usage of "this.", we used it with some attributes at some positions,
00:02:49 and we didn't use it at other positions. The code was completely correct,
00:02:53 so there wouldn't be a mistake or an error happening, but it wasn't nice, it wasn't good.
00:02:59 So, using this technique actually not only reduces the amount of lines of code that we write
here,
00:03:08 but also it improves the readability and it improves the, kind of, the style of our code.
00:03:15 And what we actually would encourage you to is to compare the example of the previous
videos,
00:03:21 so just download the slides with this example to get a better feeling for what we actually did
here,
00:03:27 and so you can kind of play around with that and use it. Okay, so to sum up,
00:03:33 let's compare "this." and "this()" again. So, "this." followed by the identifier of the attribute
00:03:42 allows us to access the current object's attributes and methods. And "this." is required when
there is a naming conflict
00:03:51 between a parameter or a local variable and an attribute. However, it is implicitly set when
there is no collision,
00:03:59 but even in this case, where it is not required, you might still want to use it for better
readability.

20
00:04:06 And "this()" on the other hand, calls an overloaded constructor of the same class,
00:04:14 and it only can be called within another constructor from the same class and within this
class,
00:04:20 or within one class. And if you want to call another constructor within your constructor,
00:04:26 "this()", so to call to this other constructor, must be the first statement
00:04:32 in the constructor that you use. After that, you can add other statements, but not before that.

00:04:39 And as we already said, using "this()" to call other constructors of your class
00:04:47 improves the maintainability of your code. So, "this." actually is something that we
00:04:52 definitely need at some points in our code, because we can't do it in another way,
00:04:57 while "this()" is more convenient to use and better maintainable,
00:05:02 so we should use it but it's not necessary. Yeah, there's a workaround for that,
00:05:07 but actually this work around kind of produces not-so-nice code.

21
Week 2 Unit 8

00:00:00 In our previous video, we learned how to overload constructors.


00:00:05 And, in the first video about constructors, we also learned that constructors are basically
methods
00:00:10 with some special features. So is there a possibility to use overloading
00:00:15 on any kind of methods? Oh well, obviously there is
00:00:18 because otherwise we wouldn't do this video. But let's step back and remember the criteria
that have to
00:00:25 be fulfilled if we want to overload constructors. So, to define multiple versions of a
constructor,
00:00:33 the amount or the type of the parameters that we pass, or that we define for this constructor,
has to be different,
00:00:42 or both, as we already said. So, in this example,
00:00:47 we had constructors with two parameters or with one or with different types.
00:00:53 Actually... Yeah, the same applies for any method.
00:00:58 So actually we have overloaded methods, we've used it multiple times already,
00:01:03 when we think about System.out.println. So there we had print line with String,
00:01:10 print line with an int, with a double, or with a boolean.
00:01:14 So we used different types in our System.out.println. And so, overloading means to define
00:01:20 multiple versions of a method with the same identifier, and we can do that with any method,

00:01:26 not only with constructors. As long as we fulfill the requirements,


00:01:31 that we have a different amount or a different type of parameters.
00:01:36 Or both. So...
00:01:42 if the overloaded methods just have a different return type, as in this example,
00:01:48 the requirements are not fulfilled. So just returning another type while keeping
00:01:52 the same amount and type of parameters is not sufficient. So in this case, this doesn't work,

00:02:03 no way - we need to do it differently. Yeah, and also here,


00:02:07 the requirements are not fulfilled when we just different identifiers for the parameters,
00:02:12 like we've already shown for the constructors. And if you want to use overloading for
methods,
00:02:22 for plain methods, you always should think twice before you are overloading a method.
00:02:27 As you can see in this example here, we have a multiply method,
00:02:32 the one that takes two parameters - "int a" and "int b" - and then returns the product of these
parameters.
00:02:40 We have an overloaded method, which is also called multiply,
00:02:44 but only takes one parameter, "int a", and then returns the square of this parameter.
00:02:52 And this actually is a bad example for overloading because the identifier that we used for
overloading this method
00:03:01 isn't descriptive anymore. It kind of suggests different things that it does,
00:03:06 and so in this case it really would be better to just write a different... or to use a different
identifier
00:03:14 for this method and call it square because that's what it does.
00:03:17 Yep. So to sum up, methods with only one class that share the same identifier
00:03:24 but have different parameters, either type, amount, or both,
00:03:27 are called overloaded methods. And every method can be overloaded,

22
00:03:32 and constructors basically are methods, and therefore can also be overloaded.
00:03:37 And actually constructors are a very typical use for method overloading, as in their case you
cannot change the identifier.
00:03:49 On the other hand, when you design a method for a class where you want to use
overloading,
00:03:56 make sure to use it carefully and chose descriptive names,
00:04:01 in case the class... then the method does something else,
00:04:04 even slightly different. And to emphasis that again,
00:04:08 method overloading, or constructive overloading, always happens within one class,
00:04:14 within the same class. So if we talk about overloading of methods or constructors,
00:04:19 this happens within one class. And we will see, later on,
00:04:23 another technique that might be a little bit confusing because it uses a very similar name,
00:04:29 and that is working within different classes. But we'll come to that later on.

23
Week 2 Unit 9

00:00:01 Hi, Ann. Hi, Tom.


00:00:03 Let's revisit the class Detective. We declare an attribute in line two,
00:00:06 which is called name, and we also a method getName that returns this attribute.
00:00:11 Okay, so let's do a short example, and we have a class example,
00:00:15 and in this class example we have a main method, and in the main method we create a new
Detective object,
00:00:23 and then we try to print out the name of this Detective, so what's going to happen now?
00:00:30 I would suggest, take your time, stop the video and think about it.
00:00:36 A couple of seconds, a minute. So, what happens here is that
00:00:43 on the console null is printed out, and that is due to the following.
00:00:48 So, we have in our class Detective an attribute called name,
00:00:52 but it's only declared and not defined, so there is no value associated with it.
00:00:56 And the thing is, in Java, attributes have default values. And the default value for String is
null,
00:01:02 and that is why null is printed out. Exactly.
00:01:07 Let's define this word null. It's a reserved keyword, you can't
00:01:11 just invent something, you have to use that word. It's a placeholder for objects
00:01:17 that actually do not exist yet. You can test whether a value is null
00:01:24 with the equality operator, and you can test whether a value is not null
00:01:29 with the inequality operator. And the thing is, String is an object,
00:01:33 and that is why it has a default value of null. Exactly.
00:01:40 If we try now to... call an operation, or call a method
00:01:45 on such a null object, null string in this case,
00:01:51 what's going to happen, Ann? So the thing is, like we already talked about,
00:01:55 that duke.getName() is null. And if we call a method on a non-existent object,
00:02:00 that obviously won't work, right? And that is exactly what our program does.
00:02:04 It crashes and tells us, "Okay no, that isn't going to happen."
00:02:07 And this is called an exception, which we cover in the next video in detail.
00:02:12 But for now, this is a NullPointerException because we kind of want to do something on null
but it can't because it is null.
00:02:20 So null doesn't have any methods. Null isn't a Detective... or isn't a String in this case.
00:02:25 So, you can't call lowercase on null. You can call it on a String - there is no String,
00:02:32 so that's why it doesn't work. So in the future, we want to try to avoid exceptions
00:02:38 and those not-so-nice error messages because that's not cool for a program,
00:02:43 it just crashes in the middle. Okay, so,
00:02:47 what can we do to avoid these messages? In this case,
00:02:53 first possibility is that your program contains an error. And that's the most...
00:03:00 probable case in our context here, within this course.
00:03:05 So the fix is: "Fix it!" Just, like, find your problem and fix it.
00:03:12 And we will show you how to find a problem in the next video.
00:03:16 The other possibility is... that it is somehow to be expected
00:03:23 that a certain value can be null. That's often the case, for example,
00:03:27 if a variable or an attribute depends on some sort of user input. In this case, check for null.

24
00:03:35 Just like as we said in a previous slide, test if...
00:03:43 a certain variable or a certain value is null, and only if it's not null, then call a method on it.
00:03:50 So, the question is, why is the String null exactly? So, this String is an attribute,
00:03:56 and attributes have default values in Java, and here we have a table that tells those default
values,
00:04:02 and for the primitive data types like int or double, it is some sort of zero?
00:04:09 Some representation of zero. Some representation, exactly,
00:04:11 for the different data types. And for boolean, it's false.
00:04:15 And for all object data types, just a String or all other objects that we create, it's null.
00:04:20 So, if we have some sort of attribute, it likely... it always has a default value
00:04:24 that has this representation of zero. The thing is that is only true for attributes.
00:04:29 So for local variables, it is not true. They do not have default values.
00:04:33 Yeah, so, you will get another error and we will cover that also in the next video.

25
Week 2 Unit 10

00:00:01 Let's revisit exceptions. Cool idea.


00:00:04 So in the last video, we talked about null and the NullPointerException.
00:00:08 We modified this example a little, so our method "npe" is called from the main method.
00:00:14 Besides that, it is basically the same, what is happening. We have null, under null we call a
method,
00:00:19 and we get a NullPointerException here. So maybe we want to have a look at
00:00:24 what this exception error message exactly tells Yeah, let's decompose it and go through it,
line by line.
00:00:32 So basically, it tells us what happened, a NullPointerException, and where it happened.
00:00:40 In the class ExceptionExamples, in the method which is located in the
ExceptionExamples.java file on line seven.
00:00:52 And it is also... so that's where the method is defined. And it's also called in the
ExceptionExamples.java file on line three.
00:01:04 So as you'll remember from the last slide, we had the main method, which was
00:01:07 where the call from npe was on line three, and the execution of the line that threw the
exception was on line seven.
00:01:15 So actually, this whole exception error message tells us exactly where it happened, and
what happened,
00:01:20 which is super helpful. Exactly, and that's what we said in the last video,
00:01:25 go there and fix the problem. So what is an exception exactly?
00:01:29 It is short for "exceptional event", so it is basically a thing that shouldn't happen.
00:01:34 So it normally disrupts the flow of the program and just crashes, which is not so cool.
00:01:39 So we want to kind of cover in more detail what exceptions there are and how to recover
from them.
00:01:45 Exactly, so let's have a look at another exception, that's the
ArrayIndexOutOfBoundsException.
00:01:51 Wow, that worked first time in a row. So in this example, we have an array.
00:01:59 It's an array of ints and it has two elements, element at index 0 and the element at index 1.

00:02:07 And we go through it with this loop and assign values to that.
00:02:14 In the next line, in line eight, we try to print the value at the element in the array at index 2.
00:02:26 And actually at index 2, there is nothing because the array only has two elements,
00:02:32 at index 0 and index 1, so that can't work. And we also have this nice exception message
here.
00:02:38 And what is new here, that also the index has told us where it happened.
00:02:41 So we have this java.lang.ArrayIndexOutOfBoundsException, and then we have a 2 behind
that,
00:02:47 and this 2 exactly tells us, okay, it was index 2 where it happened.
00:02:50 So this is really nice to see, okay, where shouldn't we, like where did we try to access and
where shouldn't we?
00:02:57 Exactly, it's just a hint. Go there, look for your error,
00:03:01 or look what's wrong and fix it. Another exception is the so-called type mismatch.
00:03:06 So we have an integer variable here, which is called i, and we want to assign a value.
00:03:12 The thing is, as you already can see due to the quotation marks,
00:03:15 that we are not assigning a number here, but a String, a text.
00:03:19 So that obviously can't work, because Java can't ultimately convert from "5",

26
00:03:24 in quotation marks, a text to a number, to an int. And that is why a type mismatch exception
is thrown.
00:03:31 And it also, of course, tells us the line number, so we just can go there and fix the problem.

00:03:36 So this kind of error or exception also often happens when you,
00:03:42 kind of, return the wrong value in a method and try to assign that to a variable.
00:03:49 This is a very easy, simple example. It can come in a little bit more disguise.
00:03:56 Yeah, so another exception type is with default values. Okay, so that's actually what we
talked about
00:04:03 in the last slide already. So we do not have default values for local variables.
00:04:09 And in this case, we have declared this local variable test should be a String, there's no
value to it.
00:04:17 And then we try to access it or print it, and as there is no value, we will get this
00:04:26 error that says the variable is not initialized. So be careful with default values.
00:04:31 Only attributes have default values, and as we already saw with the NullPointerException,
00:04:36 even that is pretty dangerous because we can a NullPointerException from that.
00:04:40 So always make sure that your variables and attributes are properly defined, declared,
assigned -
00:04:46 whatever is necessary in your context. So there is actually a way to catch exceptions.
00:04:53 This is the so-called try-catch construct. And in a try block, we write a code
00:04:59 that could potentially raise an error, and in the catch block,
00:05:04 there are the steps that we want to take if we have an exception and want to recover from
that.
00:05:08 So a potential use case for that might be that we have an exception
00:05:12 but we do not want the program to crash, so we just kind of print out in a log file what
happened,
00:05:18 where did it happen, so just log whatever went wrong, so that somebody can have a look
later on.
00:05:23 Exactly, and we kind of provide an alternative maybe we still have a problem and something
didn't work,
00:05:31 something couldn't be saved to a file or whatever, but at least the program doesn't crash at
that point.
00:05:37 And that's the idea. The thing with this exception, or with this try- catch construct
00:05:45 is, for the current course, you shouldn't, you needn't use it.
00:05:51 For all of our examples, if there is an exception happening, then you should handle it in
another way,
00:06:02 mostly by fixing the problem, just fixing the problem. But we'll try to give you an example of

00:06:09 when that would be a proper solution. So if you have, for example,
00:06:13 a program that tries to write something to a file. And then the user kind of gets the strange
idea
00:06:21 that he wants to rename the file while it's open. In some operating systems, that doesn't
work so well.
00:06:28 In others, it works without a problem. So you can get this idea,
00:06:34 and then you just kind of rename the file while the file is open, and then you try to save
something
00:06:39 to that file that actually doesn't exist anymore. That would be maybe a good...
00:06:44 a good example for when to use an exception because that is something that the program
can't handle,

27
00:06:50 it's sort of a user input. And you have to somehow deal with that.
00:06:55 Yeah, so we just told you how you can decompose these exception messages,
00:07:01 so that is one way to recover from them, just have a look what was going wrong in your
program.
00:07:06 Just try to fix the null for the NullPointerException, and so on and so forth.
00:07:11 The question is, what are you going to do, like, after the course? During the course, you
can, of course, use the course forum.
00:07:17 So just like, "Okay, I got a NullPointerException, what does this actually mean?"
00:07:21 is a proper question you could ask to the forum. So just ask for help there.
00:07:26 And after the course, if you do not want to kind of use the course forum anymore, maybe,
00:07:30 then there is another great resource. Just Google for that, or just go to Stack Overflow.
00:07:34 That is like a great resource, where loads of developers ask questions, provide answers,
and so on and so forth.
00:07:40 So like, for most common types of exception, like basically every type of exception out there
probably,
00:07:46 there's probably loads of forum posts in Stack Overflow that kind of try to explain what went
wrong.
00:07:52 So that's like a great resource if you want to recover from a problem that you can't handle
yourself.
00:07:56 And the good thing about Stack Overflow is that googling the error message mostly brings
you
00:08:02 directly to the discussion in Stack Overflow. So like, some people even suggested, some
years ago,
00:08:08 that "programming" should be renamed to "stack overflowing" things. That's how popular
Stack Overflow is in the programming community.

28
Week 2 Unit 12

00:00:00 Hey, Tom. Hi, Christiane.


00:00:02 So, in this week's deep dive, we will cover the topics "nested loops" and "two-dimensional
arrays".
00:00:09 Furthermore we will talk about the keywords continue, break, and return.
00:00:15 Let's start with a nested loop and two-dimensional arrays. Okay, so, here we have the first
example.
00:00:22 We see two "for" loops that are nested in each other. And the outer loop starts at line one,
00:00:31 and ends at line seven. And it will run two times,
00:00:35 as we have... as we start counting with 0,
00:00:39 as you can see that in i, 0. And the termination condition, or the terminating condition,
00:00:47 is i has to be less than 2, and so we stop counting when the counter
00:00:54 is no longer smaller than 2. That makes 0 and 1 valid values,
00:01:01 and not 2, because 2 is not smaller than 2 obviously. The inner loop starts at line three
00:01:09 and ends at line five. And in this time, we have
00:01:14 the termination condition, that j, a new variable, a new counting variable that
00:01:18 we introduced for the inner loop, has to be less than 3,
00:01:23 so it counts actually one time more than the outer loop. So to go through that example,
00:01:32 we first start with the outer loop, where i is 0, which is smaller than 2,
00:01:37 so the statement in line two is executed first. "Ping" is printed to the command line.
00:01:42 And now the inner loop is entered, and j also is 0, which is also smaller than 3,
00:01:48 so "Pong!" is printed on the command line and i is increased.
00:01:53 So, now, j is increased, sorry. Exactly.
00:01:57 Now j is 1, and the termination expression is still not fulfilled
00:02:02 so another "Pong!" is printed on the command line, j is increased another time,
00:02:06 j is now 2, still smaller than 3,
00:02:09 so another time "Pong!" is being displayed. Then, after increasing j another time, it is 3,
00:02:16 which is not smaller than 3 anymore, so the termination expression is met
00:02:22 and we jump out of the inner loop. And afterwards we continue with the statement in line six.

00:02:30 So we have line break added to our to our command line there,
00:02:35 and nothing else is executed in this outer loop anymore. So afterwards we increase i to one,

00:02:43 and print out the "Ping" again on our command line. And then again, "Pong! Pong! Pong!"
00:02:50 three times it's being printed to the command line, and the inner loop is finished again,
00:02:56 won't be run anymore, and... another time the line break is added at the end.
00:03:05 Maybe just to add that, we've always used print line so far,
00:03:10 most of the time we've used print line when we've printed something to the command line.
In this case we actually used the print method,
00:03:16 which does not add... the line break, so we basically add the line break manually in this "\n".

00:03:26 This is kind of a... this is the line break.


00:03:31 So...
00:03:34 Okay. A use case for nested loops and also for
00:03:40 two-dimensional arrays, and this is what we come to now, is images. And images basically
are two-dimensional arrays.

29
00:03:49 So it's a grid of rows and columns of pixels. And in our example we decided to use a
grayscale image,
00:03:57 because that makes it a little bit easier. As each pixel in such a grayscale image
00:04:04 contains, or is defined by, a gray value, and gray values basically are nothing other than
ints,
00:04:13 starting with 0 for black, and then going through all this grayscale.
00:04:21 Up to 255, which is, which would be white, then. Yes, and the width and the height of the
image
00:04:30 define the amount of rows and columns in our array. And this is also the size of the image.
00:04:36 So in the last bullet point, we see how we would define such a two-dimensional array in
Java.
00:04:42 So, there we have our int, with now two pairs of brackets.
00:04:48 And our constructor then defines with that height for our array.
00:04:54 So the two dimensions, the two sizes of the array, the width and the height.
00:04:58 Yeah. So...
00:05:02 what we have here actually is a... a very simple and also pretty old image format,
00:05:10 that's called a bitmap. Bitmaps - the good thing about bitmaps is that you can
00:05:19 open them in different representations. On the upper half we see
00:05:24 the text representation of such a bitmap file and on the lower half we see the image or the...

00:05:34 we open the same file in an image editor. So while the text editor will show us
00:05:40 the text that lies behind the image, the image editor basically shows us the image
00:05:48 which represents that text. And to make it a little bit more visible, we cheated a little.
00:05:54 So we blew it up to 3600% because otherwise it would be
00:06:01 just a small one that you see on the left. That would be the original size,
00:06:04 so it's just 10 x 10 pixels. So...
00:06:10 yeah, how would we do that in the... Oh no.
00:06:15 Let me add, or just explain a little bit the text representation of the file.
00:06:22 So, it starts with the line P1. And P1 actually defines the type of this bitmap,
00:06:30 and P1, in this case, just says it consists of black and white pixels. And 0 represents a white
pixel
00:06:38 and 1 represents a black pixel in this case. The next line is just a comment,
00:06:43 which says what is in there, so the comment is certified by this hash (#) in this case.
00:06:52 Then we have the size of the image, which is 10 x 10 pixels, so that's 10 and 10.
00:07:01 And finally we have the actual two-dimensional array. These are these lines of 0s and 1s
00:07:08 that we see in the last couple of lines of this text file. And now Christiane will show us
00:07:14 how we translate that into Java code. Yeah, so on the right-hand side of the slide,
00:07:20 we see the Java code. We skip the P1 and the comment,
00:07:22 we don't need it anymore in Java. But we first define our width and our height,
00:07:29 which are both 10. Then we define the rows of the array,
00:07:36 with one value that is either 0 or 1, depending on whether it's black or white.
00:07:42 And finally we add up all the rows to our two-dimensional array. So a two-dimensional array
basically is an array of arrays.
00:07:54 I think it's kind of the easiest thing, how to explain that or how to visualize that.
00:08:04 So, yeah, we use a nestled loop then to traverse through this two-dimensional array.
00:08:10 And just print out the result. And unfortunately we haven't added line numbers to this slide.

30
00:08:17 So, you have to look for the line within the inner part of the nestled loop, this
System.outprintln, image, and then the bracket...
00:08:30 the opening bracket, y, closing bracket, opening bracket, x. This is how we access the
values within this two-dimensional array.
00:08:40 And actually the y is the... the y value in this case is the row that we access,
00:08:47 so row 0 would be the first row and "row0" is what the variable is called in the code.
00:08:57 And the x would then be specifying the position in the x direction.
00:09:06 So that's pretty easy actually
to visualize. We will have some more information on that in a link.
00:09:15 Yeah. And we also will provide you with
00:09:19 a link to the Wikipedia page of the bitmap images, so how these are defined if you are
interested in
00:09:26 getting some more details on that, and of course we will also provide you with the file.
00:09:31 And we will provide you with an exercise, where you can play around a little bit with this
code,
00:09:36 and maybe try to produce funny images and I guess we'll probably have a thread in the
forum
00:09:44 where we will collect the results... ...your lovely pictures!
00:09:48 Yeah, exactly. So, that's a contest.
00:09:52 Okay, so now another part, the second part of our deep dive.
00:09:58 We will have a look at something which is quite handy when it comes to loops -
00:10:01 to continue, break, and also return. So let's start with a schematic overview.
00:10:08 As always in Java, we start with a class. Everything starts with a class.
00:10:12 And within this class we have a method, and within this method we have a loop.
00:10:17 This loop then has two conditions. We have defined two conditions within this loop.
00:10:24 So it's not the loop condition, to make that clear.
00:10:27 So just two other conditions for our loop. And in the first condition,
00:10:33 if this condition is met, we add a "continue" to our code.
00:10:37 So if this condition is met, then the loop will continue with the next duration,
00:10:44 and it won't get into the next condition and won't execute the statements that
00:10:50 follow below our first condition anymore. And in our second condition,
00:10:55 we added a "break". And if this condition is met,
00:11:00 then we actually quit the loop. The loop won't be executed another time,
00:11:06 depending on what comes next. And...
00:11:11 now we're back in our method. And our method then gets a return statement,
00:11:16 and if this return statement is executed, we quit the method and come back to our class.
00:11:25 Basically we have the same situation here translated into Java code.
00:11:30 As the space on the slides is limited, we kind of omitted the surrounding class
00:11:36 and jumped directly into the method. The method is called "position",
00:11:42 and the task of this method is to find a certain word, which we pass as a parameter
00:11:49 in a certain array of words, Strings, basically,
00:11:56 which we also pass as a parameter. So, we pass an array of Strings
00:12:00 and a String, and if this String is found within this array of Strings, then the method will
return the position
00:12:11 at which this word has been found in this array. Let's go through that line by line.
00:12:21 And in line two, we start the whole thing by defining a local variable,

31
00:12:30 which we call "output". And the output...
00:12:35 we initialize the output to -1, which will be returned when the word has not been found.
00:12:42 So if the word is not contained in this array of words, then it will return -1. That's quite a
common pattern for doing...
00:12:51 ...return statements. Exactly.
00:12:55 Then in line three, we start a loop and we go, with this loop,
00:13:02 we walk through the whole words array that we have passed into this method.
00:13:09 So we start at 0 and end at the last element of the words array.
00:13:14 And then if the word at the current position of the loop, or at the current counter of the loop,

00:13:23 equals "Test", then we say we are not interested in the rest of the code anymore.
00:13:33 We just continue with the next element. So, if we actually would be looking for the word
"Test"
00:13:39 within this array of words, we would be unlucky because then we wouldn't meet this
condition,
00:13:45 and the word "Test" would not be found. In the next condition, we are looking into the...
00:13:59 we try to figure out if the word at the current position of the array equals the word that we
hand in.
00:14:06 ...to the method.
00:14:09 That we have handed in as a parameter to the method. So if these two are equal,
00:14:14 then obviously we have found the word. And then we, print out "Found it!'
00:14:21 and we assign the value of the counter, of the counter variable, to the output
00:14:25 because this is the position at which the word has been found. And then we execute a break
statement,
00:14:33 and break statement, as Christiane already explained on the last slide, says that we leave
the whole loop.
00:14:40 So the next elements of this array won't be tested anymore because we already have found
it, so...
00:14:45 Even if it's in there again. Yeah, so we just want to find...
00:14:49 ...the first position. Yeah.
00:14:53 Okay, so in this case we... kind of just leave the loop.
00:14:59 If we haven't found it, we always print out this "Not found yet". And then if we have found it,
or if we have not found it,
00:15:07 we just return the output, which will either be the position of the word, or -1. Yeah.
00:15:12 And then we have added another statement, another print line system, or print line
statement, at line 16.
00:15:19 Just to make sure, to show you, yeah...
00:15:24 It won't be executed anymore because it is written after the return statement.
00:15:29 And whatever comes after the return statement won't be executed. Exactly, because return
kind of leaves...
00:15:35 not "kind of" - leaves the method. It just leaves the method and then that's it.
00:15:39 So, maybe to sum that up. The continue and break, these are...
00:15:47 tools to manipulate loops, or to, kind of, leave or skip certain lines in a loop,
00:15:56 while return just returns completely out of the method. The method, yes.
00:16:02 No matter if it's called within the loop, or wherever it is called.

32
www.sap.com/contactsap

© 2018 SAP SE or an SAP affiliate company. All rights reserved.


No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.

The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary softwar e components of other software vendors.
National product specifications may vary.

These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated companies shall not be liable
for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are set forth in the express warranty statements
accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release any functionality
mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products, and/or platform directions and functionality are
all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The information in this document is not a commitment, promise, or legal obligation
to deliver any material, code, or functionality. All forward-looking statements are subject to various risks and uncertainties that could cause actual results to differ materially from e xpectations. Readers are
cautioned not to place undue reliance on these forward-looking statements, and they should not be relied upon in making purchasing decisions.

SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other
countries. All other product and service names mentioned are the trademarks of their respective companies. See http://www.sap.com/corporate-en/legal/copyright/index.epx for additional trademark
information and notices.

You might also like