CS计算机代考程序代写 Java SWEN20003 Object Oriented Software Development

SWEN20003 Object Oriented Software Development

Assess Yourself

Sample worded exam questions:

1 Describe how sequential programming differs from asynchronous
programming.

2 Describe the event-driven programming paradigm.

3 How does the observer pattern demonstrate event-driven
programming?

4 Explain (with examples) some of the downsides of using
object-oriented programming for game development.

5 Describe the Entity-Component approach to game development.

Advanced Java and OOP SWEN20003 1 / 46

SWEN20003
Object Oriented Software Development

Advanced Java and OOP

Advanced Java and OOP SWEN20003 2 / 46

The Road So Far

Java Foundations

Classes and Objects

Abstraction

Advanced Java
I Exception Handling
I Generic Classes
I Generic Programming
I Design Patterns
I Software Testing and Design
I Games and Events

Software Development Tools

Advanced Java and OOP SWEN20003 3 / 46

Lecture Objectives

After this lecture you will be able to:

Describe and use enumerated types

Make use of functional interfaces and lambda expressions

Do cool stuff in Java

Advanced Java and OOP SWEN20003 4 / 46

Enumerated Types

Advanced Java and OOP SWEN20003 5 / 46

Assess Yourself

You’ve been hired by gambling company Soulless to design and implement
their newest card game called Horses.

Soulless have asked you to build a preliminary design before telling you the
rules of Horses.

How would you design this game, knowing only that you are implementing
a card game.

Advanced Java and OOP SWEN20003 6 / 46

Assess Yourself

Problem: How do you represent a Card class?

A Card consists of a Suit, Rank, and Colour.

Okay… How do we represent those?

Advanced Java and OOP SWEN20003 7 / 46

Enumerated Types

Keyword

enum: A class that consists of a finite list of constants.

Used any time we need to represent a fixed set of values

Must list all values

Otherwise, just like any other class; they can have methods and
attributes!

Let’s define the Card class and the Rank enum.

Advanced Java and OOP SWEN20003 8 / 46

Defining a Card

public class Card {

private Rank rank;

private Suit suit;

private Colour colour;

public Card(Rank rank, Suit suit, Colour colour) {

this.rank = rank;

this.suit = suit;

this.colour = colour;

}

}

Advanced Java and OOP SWEN20003 9 / 46

Defining a Card

public enum Rank {

ACE,

TWO,

THREE,

FOUR,

FIVE,

SIX,

SEVEN,

EIGHT,

NINE,

TEN,

JACK,

QUEEN,

KING

}

What does it do? How would you expect to use it?

Advanced Java and OOP SWEN20003 10 / 46

Enum Variables

Rank rank = Rank.ACE;

Card card = new Card(Rank.FOUR, …, …);

The values of an enum are accessed statically, because they are constants.

Enum objects are treated just like any other object.

Let’s make the other components…

Advanced Java and OOP SWEN20003 11 / 46

Defining a Card

public enum Colour {

RED, BLACK

}

public enum Suit {

SPADES, CLUBS, DIAMONDS, HEARTS

}

Can anyone see a flaw in our Card design? Any assumptions we’ve
made/not made?

Shouldn’t the Colour and Suit be related in some way?

Advanced Java and OOP SWEN20003 12 / 46

Defining a Card

public enum Suit {

SPADES(Colour.BLACK),

CLUBS(Colour.BLACK),

DIAMONDS(Colour.RED),

HEARTS(Colour.RED);

private Colour colour;

private Suit(Colour colour) {

this.colour = colour;

}

}

Now, every Suit is automatically tied to the appropriate Colour; this may
or may not be useful behaviour.

Advanced Java and OOP SWEN20003 13 / 46

Enum Variables

public static void main(String args[]) {

ArrayList ranks = new ArrayList<>();

ranks.add(Rank.TEN);

ranks.add(Rank.FOUR);

ranks.add(Rank.EIGHT);

ranks.add(Rank.THREE);

ranks.add(Rank.ACE);

System.out.println(ranks);

Collections.sort(ranks);

System.out.println(ranks);

}

[TEN, FOUR, EIGHT, THREE, ACE]
[ACE, THREE, FOUR, EIGHT, TEN]

Advanced Java and OOP SWEN20003 14 / 46

Enum Variables

Enums come pre-built with…

Default constructor

toString()

compareTo()

ordinal()

Enums are also classes, so we can add (or override) any method or
attribute we like.

public boolean isFaceCard() {

return this.ordinal() > Rank.TEN.ordinal();

}

Advanced Java and OOP SWEN20003 15 / 46

Assess Yourself

What is an enum?

What other applications can you think of for them?

Advanced Java and OOP SWEN20003 16 / 46

Variadic Parameters

Advanced Java and OOP SWEN20003 17 / 46

What?

List list = Arrays.asList(12, 5);

List list = Arrays.asList(12, 5, 45, 18);

List list = Arrays.asList(12, 5, 45, 18, 33);

How does this method work? Is it overloaded for any number of
arguments…?

There is a better way!.

Advanced Java and OOP SWEN20003 18 / 46

Variadic Parameters

Keyword

Variadic Method: A method that takes an unknown number of arguments.

public String concatenate(String… strings) {

String string = “”;

for (String s : strings) {

string += s;

}

return string;

}

Variadic methods implicitly convert the input arguments into an array. Be
careful!

Advanced Java and OOP SWEN20003 19 / 46

Assess Yourself
Write a variadic method that computes the average of an unknown
number of integers.

public double average(int… nums) {

int total = 0;

for (int i : nums) {

total += i;

}

return 1.0 * total / nums.length;

}

System.out.println(average(1));

System.out.println(average(1, 2, 3));

System.out.println(average(10, 20, 30, 40, 50));

1.0

2.0

30.0

Advanced Java and OOP SWEN20003 20 / 46

Functional Interfaces

Advanced Java and OOP SWEN20003 21 / 46

Functional Interfaces

Keyword

Functional Interface: An interface that contains only a single abstract
method; also called a Single Abstract Method interface.

@FunctionalInterface

public interface Attackable {

public void attack();

}

Functional interfaces can contain only one “new” non-static method;
adding more will raise an error.

Advanced Java and OOP SWEN20003 22 / 46

Functional Interfaces

Cool story… But… Why?

Functional interfaces are a tool that we can use with other techniques…

But let’s look at a few functional interfaces first.

Advanced Java and OOP SWEN20003 23 / 46

Functional Interfaces

public interface Predicate

The Predicate functional interface…

Represents a predicate, a function that accepts one argument, and
returns true or false

Executes the boolean test(T t) method on a single object

Can be combined with other predicates using the and, or, and
negate methods

Advanced Java and OOP SWEN20003 24 / 46

Functional Interfaces

public interface UnaryOperator

The UnaryOperator functional interface…

Represents a unary (single argument) function that accepts one
argument, and returns an object of the same type

Executes the T apply(T t) method on a single object

Advanced Java and OOP SWEN20003 25 / 46

Lambda Expressions

Advanced Java and OOP SWEN20003 26 / 46

Lambda Expressions

Keyword

Lambda Expression: A technique that treats code as data that can be
used as an “object”; for example, allows us to instantiate an interface
without implementing it.

public interface Predicate

Predicate p = i -> i > 0;

The Predicate functional interface is now an object that implements the
function to test if integers are greater than zero.

Advanced Java and OOP SWEN20003 27 / 46

Lambda Expressions

(sourceVariable1, sourceVariable2, …)

->

A lambda expression takes zero or more arguments (source variables) and
applies an operation to them

Operations could be:

Doubling an integer

Comparing two objects

Performing a boolean test on an object

Copying an object

Advanced Java and OOP SWEN20003 28 / 46

Assess Yourself

What does this code do?

Predicate p1 = i -> i > 0;

Predicate p2 = i -> i%2 == 0;

Predicate p3 = p1.and(p2);

List nums = Arrays.asList(1, 2, 5, 6, 7, 4, 5);

for (Integer i : nums) {

if (p3.test(i)) {

System.out.println(i);

}

}

2

6

4

Advanced Java and OOP SWEN20003 29 / 46

Assess Yourself

public abstract class List {

public void replaceAll(UnaryOperator operator);

}

List names = Arrays.asList(“Tony”, “Thor”, “Thanos”);

names.replaceAll(name -> name.toUpperCase());

System.out.println(names);

[“TONY”, “THOR”, “THANOS”]

Advanced Java and OOP SWEN20003 30 / 46

Anonymous Classes vs. Lambdas

Lambda expressions can often be used in place of anonymous classes, but
are not the same thing.

Anonymous Class

starWarsMovies.sort(new Comparator {

public int compare(Movie m1, Movie m2) {

return m1.rating – m2.rating;

}

});

Lambda Expression

starWarsMovies.sort((m1, m2) -> m1.rating – m2.rating);

Advanced Java and OOP SWEN20003 31 / 46

Lambda Expressions

Lambda expressions are instances of functional interfaces, that allow us to
treat the functionality of the interface as an object.

This makes our code much neater, and easier to read.

What next?

Advanced Java and OOP SWEN20003 32 / 46

Method References

Advanced Java and OOP SWEN20003 33 / 46

Rewind a Bit

List names = Arrays.asList(“Tony”, “Thor”, “Thanos”);

names.replaceAll(name -> name.toUpperCase());

System.out.println(names);

What does this code do?

How would you describe the effect of the lambda expressions?

The lambda expression applies one method to every element of the list.
We can take this a step further…

Advanced Java and OOP SWEN20003 34 / 46

Method References

names.replaceAll(String::toUpperCase);

Keyword

Method Reference: An object that stores a method ; can take the place of
a lambda expression if that lambda expression is only used to call a single
method.

Method references can be stored in the same way a lambda expression can:

UnaryOperator operator = s -> s.toLowerCase();

UnaryOperator operator = String::toLowerCase;

Advanced Java and OOP SWEN20003 35 / 46

Method Reference Examples

Static methods:

Class::staticMethod

Person::printWarning

Instance methods:

Class::instanceMethod || object::instanceMethod

String::startsWith || person::toString

Constructor:

Class::new

String::new

Method arguments are now implied, and given when the method is called.

Advanced Java and OOP SWEN20003 36 / 46

Method Reference Examples

public class Numbers {

public static boolean isOdd(int n) {

return n % 2 != 0;

}

}

public static List findNumbers(

List list, Predicate p) {

List newList = new ArrayList<>();

for(Integer i : list) {

if(p.test(i)) {

newList.add(i);

}

}

return newList;

}

Advanced Java and OOP SWEN20003 37 / 46

Method Reference Examples

List list = Arrays.asList(12, 5, 45, 18, 33, 24, 40);

// Using an anonymous class

findNumbers(list, new Predicate() {

public boolean test(Integer i) {

return Numbers.isOdd(i);

}

});

// Using a lambda expression

findNumbers(list, i -> Numbers.isOdd(i));

// Using a method reference

findNumbers(list, Numbers::isOdd);

Advanced Java and OOP SWEN20003 38 / 46

Streams

Advanced Java and OOP SWEN20003 39 / 46

Assess Yourself

Write a function that accepts a list of String objects, and returns a new
list that contains only the Strings with at least five characters, starting
with “C”. The elements in the new list should all be in upper case.

public List findElements(List strings) {

List newStrings = new ArrayList<>();

for (String s : strings) {

if (s.length() >= 5 && s.startsWith(“C”)) {

newStrings.add(s.toUpperCase());

}

}

}

Advanced Java and OOP SWEN20003 40 / 46

Motivation

Now that we have these fancy new tools, what can we do with them?

What if we wanted to apply multiple functions to the same data?

That’s where streams come in!

Keyword

Stream: A series of elements given in sequence, that are automatically put
through a pipeline of operations.

Advanced Java and OOP SWEN20003 41 / 46

Using Streams

We can think of that example as applying a sequence of operations to our
list:

Iterating through the list…

Selecting elements with length greater than five…

And elements with first character “C”…

Then, converting those elements to upper case…

And adding them to a new list

list = list.stream()

.filter(s -> s.length() > 5)

.filter(s -> s.startsWith(“C”))

.map(String::toUpperCase)

.collect(Collectors.toList());

Advanced Java and OOP SWEN20003 42 / 46

Streams

Streams are a powerful Java technique that allow you to apply sequential
operations to a collection of data. These operations include:

map (convert input to output)

filter (select elements with a condition)

limit (perform a maximum number of iterations)

collect (gather all elements and output in a list, array, String…)

reduce (aggregate a stream into a single value)

Given this…

Advanced Java and OOP SWEN20003 43 / 46

Assess Yourself

Implement a stream pipeline that takes a list of Person objects, and generates a
String consisting of a comma separated list.

The list should contain the names (in upper case) of all the people who are
between the ages of 18 and 40.

List people = Arrays.asList(

new Person(“Peter Parker”, 18),

new Person(“Black Widow”, 34),

new Person(“Thor”, 1500),

new Person(“Nick Fury”, 67),

new Person(“Iron Man”, 49)

);

Advanced Java and OOP SWEN20003 44 / 46

Assess Yourself

Implement a stream pipeline that takes a list of People, and generates a String
consisting of a comma separated list.

The list should contain the names (in upper case) of all the people who are
between the ages of 18 and 40.

String output = people.stream()

.filter(p -> p.getAge() >= 18)

.filter(p -> p.getAge() <= 40) .map(Person::getName) .map(String::toUpperCase) .collect(Collectors.joining(", ")); "PETER PARKER, BLACK WIDOW" Advanced Java and OOP SWEN20003 45 / 46 Metrics You should be able to conceptually describe all of the techniques presented in this lecture. You should be able to read and interpret code using any of the techniques in this lecture. You will not be expected to write code on anything from today. Advanced Java and OOP SWEN20003 46 / 46