Presentation is loading. Please wait.

Presentation is loading. Please wait.

H OMEWORK 2 Q UIZ E XPLANATIONS, N OTES A BOUT L INKED L ISTS I NSIDE O BJECTS, AND A B IT ABOUT S TREAMS.

Similar presentations


Presentation on theme: "H OMEWORK 2 Q UIZ E XPLANATIONS, N OTES A BOUT L INKED L ISTS I NSIDE O BJECTS, AND A B IT ABOUT S TREAMS."— Presentation transcript:

1 H OMEWORK 2 Q UIZ E XPLANATIONS, N OTES A BOUT L INKED L ISTS I NSIDE O BJECTS, AND A B IT ABOUT S TREAMS

2 W HEN YOU CHECKOUT AT A STORE, YOU LINE UP AND WAIT YOUR TURN. W HICH OF THE DATA STRUCTURES FROM THIS ASSIGNMENT BEST CAPTURES THE ORDER IN WHICH PEOPLE ENTER AND EXIT THE CHECKOUT LINE ? Stack Queue Priority Queue

3 W HEN YOU CHECKOUT AT A STORE, YOU LINE UP AND WAIT YOUR TURN. W HICH OF THE DATA STRUCTURES FROM THIS ASSIGNMENT BEST CAPTURES THE ORDER IN WHICH PEOPLE ENTER AND EXIT THE CHECKOUT LINE ? Stack Stacks are last in, first out, meaning the last one added to the stack is the first one that is then removed. In this example, that would mean whoever showed up to the check out line would immediately go to the front of the line, not the back, and would be helped next. Queue Priority Queue

4 W HEN YOU CHECKOUT AT A STORE, YOU LINE UP AND WAIT YOUR TURN. W HICH OF THE DATA STRUCTURES FROM THIS ASSIGNMENT BEST CAPTURES THE ORDER IN WHICH PEOPLE ENTER AND EXIT THE CHECKOUT LINE ? Stack Queue Priority Queue Priority queue means that if someone showed up (who was maybe considered “more important” than the other people in the line), they would go to the front, and everyone else would queue up in line the normal way. That’s not what was described above, so this is incorrect.

5 W HEN YOU CHECKOUT AT A STORE, YOU LINE UP AND WAIT YOUR TURN. W HICH OF THE DATA STRUCTURES FROM THIS ASSIGNMENT BEST CAPTURES THE ORDER IN WHICH PEOPLE ENTER AND EXIT THE CHECKOUT LINE ? Stack Queue is correct. Queues are “FIFO” (first in, first out), meaning that whoever shows up first is the first one to then be removed. Priority Queue

6 E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (7). INSERT (4). FIRST () I F THIS SEQUENCE RETURNED 4, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them Must be a stack Must be a queue Must be a priority queue Either a stack or a priority queue Either a queue or a priority queue Either a stack or a queue

7 E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (7). INSERT (4). FIRST () I F THIS SEQUENCE RETURNED 4, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them No, because this wouldn’t be a queue. Queues are first in, first out, and since 4 was added last, there’s no way for it to be the first. Must be a stack Must be a queue Again, can’t be a queue. Must be a priority queue Either a stack or a priority queue Either a queue or a priority queue Again, can’t be a queue. Either a stack or a queue Again, can’t be a queue.

8 E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (7). INSERT (4). FIRST () I F THIS SEQUENCE RETURNED 4, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them Must be a stack Must be a queue Must be a priority queue Either a stack or a priority queue This is the correct answer. It could be a priority queue since 4 is the smallest number in the set (4 < 5 and 4 < 7), and it could be a stack since 4 was added last. Either a queue or a priority queue Either a stack or a queue

9 (S AME SETUP AS PREVIOUS QUESTION, BUT DIFFERENT SEQUENCE OF OPERATIONS ) E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (2). REM F IRST (). INSERT (8). FIRST () I F THIS SEQUENCE RETURNED 8, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them Must be a stack Must be a queue Must be a priority queue Either a stack or a priority queue Either a queue or a priority queue Either a stack or a queue

10 (S AME SETUP AS PREVIOUS QUESTION, BUT DIFFERENT SEQUENCE OF OPERATIONS ) E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (2). REM F IRST (). INSERT (8). FIRST () I F THIS SEQUENCE RETURNED 8, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them Cannot be a queue. See below. Must be a stack. Must be a queue This is incorrect because if it were a queue, 2 would be returned, not 8. We added 5, then 2, removed first (5, if this were a queue), then added 8. If it were a queue, 2 is next in line because it was added before 8. Must be a priority queue Either a stack or a priority queue Already explained above – can’t be a priority queue. Either a queue or a priority queue Already explained above – can’t be a queue. Either a stack or a queue – Already explained above – can’t be a queue.

11 (S AME SETUP AS PREVIOUS QUESTION, BUT DIFFERENT SEQUENCE OF OPERATIONS ) E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (2). REM F IRST (). INSERT (8). FIRST () I F THIS SEQUENCE RETURNED 8, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them Cannot be a priority queue or a queue. See below. Must be a stack. This is the correct answer. If you add 5, then add 2, remove first, and add 8, the only way to get 8 back again is if it’s a stack (because stacks are last-in, first out) Must be a queue Must be a priority queue This is incorrect because if it were a priority queue, 2 would be returned, not 8. We added 5, then 2, removed first (5, if this were a queue), then added 8. If it were a priority queue, 2 is next in line because 2 < 8. Either a stack or a priority queue Already explained above – can’t be a priority queue. Either a queue or a priority queue Already explained above – can’t be a queue or a priority queue. Either a stack or a queue

12 HW4: L INKED L IST I NSIDE OF A C LASS. H OW TO A DD TO I T AND A CCESS I T Ex: You have a class called “Dillo,” which has: A string for its name An int for its length A LinkedList of Dillos, which are its friends Let’s say you want to go through the list of a dillo’s friends and print to the console the name of each friend, if its length is greater than 5

13 HW4: L INKED L IST I NSIDE OF A C LASS class Dillo { String name; int length; LinkedList friends; public Dillo (String name, int length, LinkedList friends){ this.name = name; this.length = length; this.friends = friends; }

14 A DDING TO A L INKED L IST I NSIDE OF A C LASS Examples { Examples() {} Dillo christinaDillo = new Dillo(“Christina”, 5, new LinkedList ()); // List of friends starts empty (  ) Dillo nicoleDillo = new Dillo(“Nicole”, 5, new LinkedList ()); // Empty list of friends Dillo karaDillo = new Dillo(“Kara”, 6, new LinkedList ()); // Again, empty friend list // Let’s add these two new Dillos to Christina’s list of friends: christinaDillo.friends.add(nicoleDillo); // Format is “name of object”.“name of list”.add(“item to add”) christinaDillo.friends.add(karaDillo); // christinaDillo is the object name. friends is the list name. karaDillo is who we’re adding. }

15 A CCESSING A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } Let’s break down each piece of this method. What is this method doing?

16 public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } “void” means return nothing. We have no return statement and aren’t returning anything. We’re just doing an action (printing to the console). A CCESSING A L INKED L IST I NSIDE OF A C LASS

17 public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } This says, “For each Dillo in the list that is this.friends, refer to that object (that friend) as ‘aDilloFriend.’” Let’s break that down a bit more… A CCESSING A L INKED L IST I NSIDE OF A C LASS

18 public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } This says, “ For each item in the list that is this.friends, refer to that object (that friend) as “aDilloFriend.”

19 A CCESSING A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } This says, “For each Dillo in the list that is this.friends, refer to that object (that friend) as “aDilloFriend.”

20 A CCESSING A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } This says, “For each Dillo in the list that is this.friends, each is a Dillo and refer to that object (that friend) as “aDilloFriend.”

21 A CCESSING A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } Refer to that object (that friend) as “aDilloFriend.” This means that every time you write “aDilloFriend” inside of the for loop, what is inside of that object is the current item in the list that you’re looking at.

22 I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS Examples { Examples() {} Dillo christinaDillo = new Dillo(“Christina”, 5, new LinkedList ()); // List of friends starts empty (  ) Dillo nicoleDillo = new Dillo(“Nicole”, 5, new LinkedList ()); // Empty list of friends Dillo karaDillo = new Dillo(“Kara”, 6, new LinkedList ()); // Again, empty friend list // Let’s add these two new Dillos to Christina’s list of friends: christinaDillo.friends.add(nicoleDillo); christinaDillo.friends.add(karaDillo); // We can call that method we wrote: christinaDillo.printFriendsLongerThanFive(); }

23 I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } If I call this method in this way: christinaDillo.printFriendsLongerThanFive(); then “ this.friends ” will specifically be christinaDillo’s list of friends. Notice how we don’t need to pass in her list of friends because the object already has access to it with the “this” keyword. Passing in the list of friends would be redundant because it already has access to this list.

24 I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } christinaDillo’s list of friends contains: nicoleDillo (“Nicole”, 5, empty list of friends ); karaDillo (“Kara”, 6, empty list of friends );

25 I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } At the line in blue, aDilloFriend would first be populated with nicoleDillo. Remember that nicoleDillo’s length was 5, and since 5 is not greater than 5, her name will not be printed. We don’t go inside of this if statement this time.

26 I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } Now at the line in blue, aDilloFriend would be populated with karaDillo.

27 I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } Remember that karaDillo’s length was 6, and since 6 is greater than 5, her name WILL be printed.

28 S TREAMS WITH S TOPPING C ONDITION List myStreamOfNumbers = new LinkedList<>(); numbers.stream().anyMatch(x -> { test1.add(x); return x == -999 ; }); The text in blue is your stopping condition.

29 I NTEGERS WITH S CANNER If you attempt to get an integer using a Scanner and do it in this form: Scanner keyboard = new Scanner(System.in); int x; keyboard.nextInt(x); It will throw a “PatternSyntaxException” exception. (If you pass a parameter to nextInt, it's interpreted as the radix of the entered integer. See here for more info: http://stackoverflow.com/questions/26750042/scanner-nextintx-throws- patternsyntaxexception) http://stackoverflow.com/questions/26750042/scanner-nextintx-throws- patternsyntaxexception Instead use: x = keyboard.nextInt();


Download ppt "H OMEWORK 2 Q UIZ E XPLANATIONS, N OTES A BOUT L INKED L ISTS I NSIDE O BJECTS, AND A B IT ABOUT S TREAMS."

Similar presentations


Ads by Google