parent
1cda7cceb1
commit
5a9c9516fd
@ -1,3 +0,0 @@
|
|||||||
|
|
||||||
Sprint planning:
|
|
||||||
Determine tasks to be completed this week, with quick difficulty estimates. Tasks should be relatively small, specific, completable pieces of work. Plan how it's going to get done. Use the kanban in Gitea to populate it. Spring ends with next Monday's class meeting.
|
|
After Width: | Height: | Size: 18 KiB |
Binary file not shown.
@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
public class CoinInventory {
|
||||||
|
private int[] coinCounts; //cp (pennies), sp (dimes), ep (half-dollars), gp (dollar bills), pp (five dollar bills)
|
||||||
|
private int weight; //The weight is the sum of all the coins.
|
||||||
|
|
||||||
|
public CoinInventory() {
|
||||||
|
coinCounts = new int[5];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCoins(int[] a) {
|
||||||
|
coinCounts = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getCoins() {
|
||||||
|
return coinCounts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWeight(int w) {
|
||||||
|
weight = w;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWeight() {
|
||||||
|
return weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Binary file not shown.
@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
public class Item {
|
||||||
|
private String description;
|
||||||
|
private int weight;
|
||||||
|
|
||||||
|
public Item(String description, int weight) {
|
||||||
|
this.description = description;
|
||||||
|
this.weight = weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWeight() {
|
||||||
|
return weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
@ -0,0 +1,77 @@
|
|||||||
|
//The design among these 3 classes needs improvement, and the code has a couple of bugs, too.
|
||||||
|
//Improve the design and fix the bugs.
|
||||||
|
//The public interface of Player should remain the same as it is now.
|
||||||
|
|
||||||
|
//Hints:
|
||||||
|
//Avoid duplicate data
|
||||||
|
//Methods and instance data should have high cohesion
|
||||||
|
//Coupling should be loose between classes
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class Player {
|
||||||
|
private String name;
|
||||||
|
private CoinInventory coinInventory;
|
||||||
|
private ArrayList<Item> otherInventory;
|
||||||
|
private int encumbrance;
|
||||||
|
|
||||||
|
public Player(String name) {
|
||||||
|
this.name = name;
|
||||||
|
coinInventory = new CoinInventory();
|
||||||
|
otherInventory = new ArrayList<Item>();
|
||||||
|
encumbrance = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItem(Item item) {
|
||||||
|
otherInventory.add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void collectMoney(int cp, int sp, int ep, int gp, int pp) {
|
||||||
|
int[] moneys = coinInventory.getCoins();
|
||||||
|
moneys[0] += cp;
|
||||||
|
moneys[1] += sp;
|
||||||
|
moneys[2] += ep;
|
||||||
|
moneys[3] += gp;
|
||||||
|
moneys[4] += pp;
|
||||||
|
coinInventory.setCoins(moneys);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getRunningSpeed() {
|
||||||
|
double speed = 5 * (1 - getEncumbrance() / 1000.0); //Speed 5 if unencumbered; speed 0 if 1000 units carried or more.
|
||||||
|
if (speed < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMoneyValue() {
|
||||||
|
int[] moneys = coinInventory.getCoins();
|
||||||
|
return .01 * moneys[0] + .1 * moneys[1] + .5 * moneys[2] + 1 * moneys[3] + 5 * moneys[4];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getEncumbrance() {
|
||||||
|
int[] moneys = coinInventory.getCoins();
|
||||||
|
return moneys[0] + moneys[1] + moneys[2] + moneys[3] + moneys[4] + encumbrance;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Test method.
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Player p = new Player("Whistlestick");
|
||||||
|
Item sword = new Item("Magic sword +2", 20);
|
||||||
|
Item shield = new Item("Normal shield", 40);
|
||||||
|
p.addItem(sword);
|
||||||
|
p.addItem(shield);
|
||||||
|
if (p.getEncumbrance() != 60) throw new RuntimeException("getEncumbrance() should be 60, not " + p.getEncumbrance());
|
||||||
|
p.collectMoney(10,2,0,40,2);
|
||||||
|
if (p.getEncumbrance() != 60+54) throw new RuntimeException("getEncumbrance() should be 114, not " + p.getEncumbrance());
|
||||||
|
if (p.getMoneyValue() != 50.3) throw new RuntimeException("getMoneyValue() should be 50.3, not " + p.getMoneyValue());
|
||||||
|
p.collectMoney(0,0,0,6,0);
|
||||||
|
if (p.getEncumbrance() != 120) throw new RuntimeException("getEncumbrance() should be 120, not " + p.getEncumbrance());
|
||||||
|
if (p.getRunningSpeed() != 4.4) throw new RuntimeException("getRunningSpeed() should be 4.4, not " + p.getRunningSpeed());
|
||||||
|
System.out.println("All tests passed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
@ECHO OFF
|
||||||
|
C:
|
||||||
|
CD "\stuff\cs495\notes\Day 38 (class and function design)\Improve the design"
|
||||||
|
"C:\Program Files\Microsoft\jdk-11.0.12.7-hotspot\bin\java.exe" -cp .;"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.base.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.controls.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.fxml.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.graphics.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.media.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.swing.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.web.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx-swt.jar" --module-path "C:\Program Files\Java\javafx-sdk-17.0.0.1\lib" --add-modules=javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web Player
|
||||||
|
PAUSE
|
Binary file not shown.
@ -0,0 +1,18 @@
|
|||||||
|
import java.util.*;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class CS495 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
ArrayList<Student> students = new ArrayList<Student>();
|
||||||
|
Scanner in = new Scanner(new File("superheroes.txt"));
|
||||||
|
while(in.hasNextLine()) {
|
||||||
|
String s = in.nextLine();
|
||||||
|
students.add(new Student(s));
|
||||||
|
}
|
||||||
|
in.close();
|
||||||
|
|
||||||
|
for(Student s : students) {
|
||||||
|
System.out.println(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
public class Student {
|
||||||
|
public static int nextStudentID = 1;
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Student(String name) {
|
||||||
|
id = nextStudentID;
|
||||||
|
nextStudentID++;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "Student ID: " +id+ ", name: "+name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
Clark Kent
|
||||||
|
Bruce Wayne
|
||||||
|
Hal Jordan
|
||||||
|
Oliver Queen
|
||||||
|
Steve Rogers
|
||||||
|
Barry Allen
|
||||||
|
Barbara Gordon
|
||||||
|
Tony Stark
|
||||||
|
Peter Parker
|
||||||
|
Billy Batson
|
||||||
|
Bruce Banner
|
||||||
|
Diana Prince
|
Binary file not shown.
@ -0,0 +1,25 @@
|
|||||||
|
// Two problems with information hiding in this example program.
|
||||||
|
|
||||||
|
public class CS495 {
|
||||||
|
public static int nextStudentID = 1;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Student[] students = new Student[12];
|
||||||
|
students[0] = new Student("Clark Kent");
|
||||||
|
students[1] = new Student("Bruce Wayne");
|
||||||
|
students[2] = new Student("Hal Jordan");
|
||||||
|
students[3] = new Student("Oliver Queen");
|
||||||
|
students[4] = new Student("Steve Rogers");
|
||||||
|
students[5] = new Student("Barry Allen");
|
||||||
|
students[6] = new Student("Barbara Gordon");
|
||||||
|
students[7] = new Student("Tony Stark");
|
||||||
|
students[8] = new Student("Peter Parker");
|
||||||
|
students[9] = new Student("Billy Batson");
|
||||||
|
students[10] = new Student("Bruce Banner");
|
||||||
|
students[11] = new Student("Diana Prince");
|
||||||
|
|
||||||
|
for(Student s : students) {
|
||||||
|
System.out.println(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
public class Student {
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Student(String name) {
|
||||||
|
id = CS495.nextStudentID;
|
||||||
|
CS495.nextStudentID++;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "Student ID: " +id+ ", name: "+name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
Most common reasons for project failure: Poor requirements/planning/management
|
||||||
|
|
||||||
|
Most common *technical* reason for project failure: unmanaged complexity
|
||||||
|
|
||||||
|
Abstraction: from the outside, we view an object or function as a coherent idea on a high level of detail
|
||||||
|
|
||||||
|
Information hiding / encapsulation: from the outside, internal implementation details are not viewable
|
||||||
|
|
||||||
|
Loose coupling:
|
||||||
|
* Size (prefer fewer methods and parameters)
|
||||||
|
* Visibility (e.g. avoid sneaking info between objects through a global variable)
|
||||||
|
* Flexibility (objects and functions can be rearanged with relative ease)
|
||||||
|
Example: Passing primitives is looser coupling than passing complex objects
|
||||||
|
Avoid: some object/function requires knowledge of *internal* workings of another object/function.
|
||||||
|
|
||||||
|
High cohesion: Methods and data in a class are highly related and belong together
|
||||||
|
|
||||||
|
All of this (abstraction, information hiding, loose coupling, high cohesion) mainly comes down to two motivations:
|
||||||
|
Limit the amount of complexity we deal with at a time
|
||||||
|
Limit the effects when a change is made to the code
|
||||||
|
This reduces the cognitive load required to understand any given part of the system at a time.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[The CouplingVsCohesion.svg image is public domain, obtained from https://upload.wikimedia.org/wikipedia/commons/0/09/CouplingVsCohesion.svg]
|
Loading…
Reference in new issue