Return to Video

Friends Done Right - Intro to Java Programming

  • 0:01 - 0:03
    To do this, I'll first add a new variable. I'll call
  • 0:03 - 0:06
    it friends2 for now. Now in the constructor, I need to
  • 0:06 - 0:10
    initialize friends2, and there's that error. I needed to import it.
  • 0:10 - 0:14
    So far so good. Now I'll fix addfriend. I'll add the friend
  • 0:14 - 0:16
    to both of the lists that I'm keeping track of right
  • 0:16 - 0:18
    now. I tried to add the whole friend instead of just the
  • 0:18 - 0:22
    name. Much better. Let's see if the tests are still passing.
  • 0:22 - 0:26
    Person tester. Looks like things are matching, so I'll go to getfriends.
  • 0:26 - 0:29
    Instead of using friends, I want to use friends2, but friends2
  • 0:29 - 0:31
    is an array list, so I need to get a string
  • 0:31 - 0:35
    representation of it. But toString returns brackets around what I
  • 0:35 - 0:39
    want, so I'll need to remove the brackets using substring. I'll
  • 0:39 - 0:42
    put it in a variable, and now I'll return substring,
  • 0:42 - 0:46
    starting with the second character and stopping before the last character.
  • 0:46 - 0:48
    Let's see if the tests still pass. Looks like everything
  • 0:48 - 0:52
    is still matching, except that there's a trailing comma right here.
  • 0:52 - 0:54
    But that's the getfriend method, so I'll come back to
  • 0:54 - 0:58
    that. And now for the especially satisfying one. I'll get to
  • 0:58 - 1:01
    delete all of this code and replace it with one line.
  • 1:01 - 1:04
    I'll get the element of the ArrayList at friendIndex, and now
  • 1:04 - 1:07
    everything is actually matching what we wanted it to. But
  • 1:07 - 1:10
    I need to clean up my extra unused variables. I still
  • 1:10 - 1:14
    have this private String friends, and I'm not using it anywhere,
  • 1:14 - 1:16
    or at least I shouldn't be. So I'll delete it there,
  • 1:17 - 1:20
    and here, and here, and I think that's all of it.
  • 1:20 - 1:23
    But now friends2 has a sort of silly name. So I
  • 1:23 - 1:27
    just want to change friends2 to be friends. So I'll use find
  • 1:27 - 1:31
    replace and replace all of the occurences. Creating an extra variable
  • 1:31 - 1:34
    might have looked like extra work, and if you didn't do
  • 1:34 - 1:36
    all of that, that's fine. I did it to show you
  • 1:36 - 1:40
    how you might approach a much larger refactoring. Sometimes when you're
  • 1:40 - 1:43
    working on a very big refactoring, it's good to make sure
  • 1:43 - 1:46
    that everything still works at each intermediate stage. You'll learn more
  • 1:46 - 1:48
    about that if you take a job as a software engineer.
タイトル:
Friends Done Right - Intro to Java Programming
概説:

more » « less
Video Language:
English
Team:
Udacity
プロジェクト:
cs046: Intro to Programming
Duration:
01:49

English subtitles

改訂 Compare revisions