25T2 Week 3
Friday 1pm-4pm (F13A)
Start 1:05pm
By: Sam Zheng (z5418112)
Original Slides by: Alvin Cherk
At the design time, responsibilities are clearly assigned to different software elements, clearly documented and enforced during the development and using unit testing and/or language support.
Every software element should define a specification (or a contract) that govern its transaction with the rest of the software components.
A contract should address the following 3 conditions:
public class Bird {
private int height;
public void fly(int height) {
if (height <= 5) {
throw new IllegalArgumentException("Height must be greater than 5 meters to fly.");
}
this.height = height;
}
}
public class Penguin extends Bird {
@Override
public void fly(int height) {
if (height != 0) {
throw new IllegalArgumentException("Penguins cannot fly! Height must be 0 meters.");
}
// does nothing
}
}
from 0 <= theta <= 90 to 0 <= theta <= 180 is weakening
[0, 90] => [0, 180]
Why?
Liskov Substitution Principle (more next week). I should be able to use the subclass's implementation in place of my super class.
UML
Composition is a specialized form of aggregation. In composition, if the parent object is destroyed, then the child objects also cease to exist. Composition is actually a strong type of aggregation and is sometimes referred to as a "death" relationship.
On the other hand, aggregation still involves one object being a part of another, but if the parent object is destroyed, the child object can still exist.
Examples:
- private attribute
+ static method
# protected attribute
- FINAL_VARIABLE
+ public method
Create an OO domain model for a system with the following requirements.
A Car has one or more engines and a producer. The producer is a manufacturing company who has a brand name. Engines are produced by a manufacturer and have a speed. There are only two types of engines within UNSW's cars:
Cars are able to drive to a particular location x, y.
Since UNSW is a world-leader in technology innovation, they want you to be able to model the behaviour of Time Travelling for any vehicle, and to model a time travelling car. A vehicle that travels in time stays in the same location but travels to a LocalDateTime.
Create an OO domain model for a system with the following requirements.
A Car has one or more engines and a producer. The producer is a manufacturing company who has a brand name. Engines are produced by a manufacturer and have a speed. There are only two types of engines within UNSW's cars:
Cars are able to drive to a particular location x, y.
Since UNSW is a world-leader in technology innovation, they want you to be able to model the behaviour of Time Travelling for any vehicle, and to model a time travelling car. A vehicle that travels in time stays in the same location but travels to a LocalDateTime.
Text
- time LocalDateTime
A generally good diagram maker software that is free for UNSW students:
https://cgi.cse.unsw.edu.au/~cs2511/25T2/setup/lucidchart-setup
Wonderous.java
package wondrous;
import java.util.ArrayList;
import java.util.List;
public class Wondrous {
private final int MY_MAGIC_NUMBER = 42;
public List<Integer> wondrous(int start) {
int current = start;
List<Integer> sequence = new ArrayList<Integer>();
while (current != 1) {
sequence.add(current);
if (current % 2 == 0) {
current /= 2;
} else {
current = (current * 3) + 1;
}
}
return sequence;
}
}The Wondrous Sequence is generated by the simple rule:
Make sure your VSCode is open in the correct folder, otherwise, these buttons wont appear
Make sure your VSCode is open in the correct folder, otherwise, these buttons won't appear
package wondrous;
import java.util.ArrayList;
import java.util.List;
// If the current term is even, the next term is half the current term.
// If the current term is odd, the next term is three times the current term, plus 1.
public class Wondrous {
public List<Integer> wondrous(int start) {
int current = start;
List<Integer> sequence = new ArrayList<Integer>();
while (true) {
sequence.add(current);
if (current == 1) {
break;
} else if (current % 2 == 0) {
// Even
current /= 2;
} else {
// odd
current = (current * 3) + 1;
}
}
return sequence;
}
}Lets write some more tests
package wondrous.test;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import wondrous.Wondrous;
public class WondrousTest {
@Test
public void testBasic() {
Wondrous w = new Wondrous();
List<Integer> expected = new ArrayList<Integer>(Arrays.asList(3, 10, 5, 16, 8, 4, 2, 1));
assertEquals(expected, w.wondrous(3));
}
@Test
public void testOne() {
Wondrous w = new Wondrous();
List<Integer> expected = new ArrayList<Integer>(Arrays.asList(1));
assertEquals(expected, w.wondrous(1));
}
@Test
public void testInvalid() {
Wondrous w = new Wondrous();
assertThrows(IllegalArgumentException.class, () -> {
w.wondrous(0);
});
}
}What are they?
Checked vs Unchecked
How can we use it in Wonderous to make the code better?
Checked or Unchecked?
How can we use it in Wonderous to make the code better?
package wondrous;
import java.util.ArrayList;
import java.util.List;
// If the current term is even, the next term is half the current term.
// If the current term is odd, the next term is three times the current term, plus 1.
public class Wondrous {
public List<Integer> wondrous(int start) {
int current = start;
List<Integer> sequence = new ArrayList<Integer>();
if (start < 1) {
throw new IllegalArgumentException("wondrous start must be >= 1");
}
while (true) {
sequence.add(current);
if (current == 1) {
break;
} else if (current % 2 == 0) {
// Even
current /= 2;
} else {
// odd
current = (current * 3) + 1;
}
}
return sequence;
}
}