Week 1

  • Course introduction
  • Version control with Git (we are going to do all this together first)
    • We will store our code in our GitLab instance using your ECS username and password
    • Generating SSH key: Window->Preferences->General->Network Connections->SSH2->Key Management->Generate RSA key
      • Don't forget to save it! The default name and location are fine
      • Copy it into Gitlab: Top-right menu->Settings->SSH Keys (sidebar) - copy and paste the entire text box contents from Eclipse (it will scroll off further than you can see, make sure you get all of it!) and click "Add key"
      • You will only need to do this part once.
    • Everything else to do with Git will be accessed through the Team menu of the right-click menu of your project and its files.
    • We will create a project together after everyone has their keys sorted out.
  • Creating your project
  • Bug World, Bug World part 2, if you have spare time: Bug World part 3
  • Using Git
  • Inheritance and polymorphism
  • 10am Wednesday on Zoom: meeting here
  • The rest of the week's content is below, and there's no hurry. Please watch the presentation video by Thursday morning, when we'll meet to discuss the content again. Carry on with BugWorld until you reach smellFood, or until it's time to watch that video, and then move on to generics.
  • Generic parameters
    • Slides, GitLab project for Box, presentation video
    • Exercise: implementing and using a generic interface
      • The Comparator<T> interface is built in to Java, and is used for defining how two objects of a type compare: which is bigger/smaller/closer/greener/more important/...
        • The compare method returns -1/0/1 according to whether the first argument is less than/equal to/greater than the second
        • The sort() method on Lists and ArrayLists is given a Comparator to decide which values come before or after each other: l.sort(new SomeSuitableComparator());
      • Extend your BugWorld with:
        • A Comparator<Plant> (i.e. a new class that implements this interface: class PlantSizeComparator implements Comparator<Plant> {...}), which can be used to find the biggest Plant with the sort() method from above.
        • A Comparator<Bug>, which compares two Bugs' energy fields
        • Another Comparator<Bug>, which compares the bugs by species and then by name. Hint: Strings have a compareTo(String other) method you can use.
      • Use these comparators to output the bugs in different orders
      • Make your bugs implement Comparable<Bug> with a sensible semantics
      • If you got to that point, update smellFood to use Comparators and/or Comparable to make decisions about the best direction to move. Account for carnivores, herbivores, and omnivores. You may need to create additional interfaces or superclasses. If you didn't, just carry on building BugWorld.
    • Later exercise: implement a class that holds a pair of values of different types (the Left value and the Right value). Make sure it's possible to create a pair with any combination of types. Then:
      • Make sure that your Pair implements toString in a useful way.
      • Implement code in your BugWorld that produces a list of pairs, where each pair contains the name and energy values of one of the bugs in your world.
      • Add more code that converts that list into a Pair of lists, one list of names and one of energies. (If you have more of Bug World part 2 to build, this is a good point to shift back to that)
      • Create an array of every possible pair of bugs in your world (if you have ten bugs, this will have 90 elements). Create a comparator, and use the Arrays.sort method to order the array by the distance between the bugs in the pair (for this, use the number of moves it would take to get from one to the other: the difference in the x coordinates plus the difference in the y coordinates). Print out the nearest and most distant pair of bugs, and the median pair that's right in the middle of the range.
Topic attachments
I Attachment Action Size Date Who Comment
GenericExample.zipzip GenericExample.zip manage 7 K 19 Aug 2021 - 11:04 Main.ahmed Generics, Comparable, and Comparator: Example