Week 2: Building Multi-Class Applications: Choosing Classes
Week 2: Building Multi-Class Applications: Choosing Classes
Choosing Classes
- objects correspond to nouns in the description of your application
- their behaviour corresponds to public methods contained in their corresponding classes
- choosing classes is not a trivial task
- create classes which represent a single concept (see Ex.2.1)
Class Purse shown below there are 2 concepts: a purse that holds coins and the value of each
coin.
class Purse {
public Purse();
public void addNickels(int count);
public void addDimes(int count);
public void addQuarters(int count);
public double getTotal();
}
It is therefore better to rewrite the class as:
class Coin{
public Coin(double value, String aName);
public double getValue();
}
class Purse {
public Purse();
public void add(Coin aCoin);
public double getTotal();
}
In this case class Purse depends on class Coin as it is using objects of Coin type. Of course, class
Coin does not depend on class Purse.
If class Coin changes that will imply that class Purse has to be rewritten. In general you want to
minimize the number of class dependencies (low coupling).
UML Class connectors:
- arrow for Inheritance
- arrow with a romb instead of > for Aggregation (as in the example above)
- dashed arrow for Dependency
theProduct = aProduct;
quantity = aQuantity;
}
public double getTotalPrice() {
return theProduct.getPrice() * quantity;
}
public String format() {
return String.format("%-30s%8.2f%5d%8.2f",
theProduct.getDescription(), theProduct.getPrice(), quantity, getTotalPrice());
}
private int quantity;
private Product theProduct;
}
public class Product {
public Product(String aDescription, double aPrice) {
description = aDescription;
price = aPrice;
}
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
private String description;
private double price;
}
StudentApplication
- univ1, univ2, univ3: String
- acc1, acc2, acc3: boolean
- name : String
+ setAcceptance(): void
+ toString(): String
Note: A UML object diagram contains a snapshot of the objects at a certain point in the
execution of the program. The notation will include the names of the objects and the values of
the attributes.
We shall note use UML object diagrams in this course.
Java Implementation of the Applications Centre
The Applications class presented below is the driver which allows for
the association of ApplicationCentre and StudentApplication classes. For input/output we shall
use JOptionPane, a class part of the Swing package which produces simple pop-up dialog boxes.
(in most of this course we shall avoid the line input/output used in ITEC1620)
Ex.2.2
// A class which handles applications for university admission
import javax.swing.JOptionPane;
public class Applications {
public static void main( String args[] ) {
ApplicationCentre appCentre= new ApplicationCentre ("Guelph");
String stopper="quit";
int nrStud=0;
StudentApplication s;
// Input data
JOptionPane.showMessageDialog(null,"Enter student names, each followed by 3 choices of universities. To
stop enter for name the word quit","Input", JOptionPane.PLAIN_MESSAGE);
String n = JOptionPane.showInputDialog("Student's name ?");
boolean flag=true;
while (!n.equals(stopper) && flag){
String u1= JOptionPane.showInputDialog("1st university ?");
class ApplicationCentre {
private String name;
private StudentApplication [] st;
private int studentCount;
private int size;
public ApplicationCentre(String s){
name=s;
size=100;
st = new StudentApplication[size];
studentCount=0;
}
public String getName() {
return name;
}
public boolean addStudent(StudentApplication s){
if (studentCount==size) return false;
st[studentCount]=s;
studentCount ++;
return true;
}
The most important concept in this program is the creation and use of the array
of StudentApplication objects st[].
The lines
size=100;
st = new StudentApplication[size];
The lines
StudentApplication s=new StudentApplication (n, u1, u2, u3);
flag=appCentre.addStudent(s);
in the main() of class Applications create the object s of StudentApplication type and then
through
the method addStudent() of class ApplicationCentre the address of s is passed to an element of
the st[ ] array.
This operation is repeated in the while loop until either the array is full or the user
enters the word quit as the name of the student.