UML #
This lab provides several examples of simple UML diagrams for single classes, and examples of how that translates into Java code.
[0] Setup #
For this lab, we will continue working in the lab_oop
repository
[1] Example UML Diagram + Java Code #
Here is an example of a Java class and its correspoinding UML diagram
public class Student {
// attributes
private String name;
private String[] subjects;
private int idNumber;
//constructor
public Student(String name, String[] subjects, int idNumber) {
this.name = name;
this.subjects = subjects;
this.idNumber = idNumber;
}
//accessors and mutators
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getSubjects() {
return subjects;
}
public void setSubjects(String[] subjects) {
this.subjects = subjects;
}
public int getIdNumber() {
return idNumber;
}
public void setIdNumber(int idNumber) {
this.idNumber = idNumber;
}
}
classDiagram class Student { -String name -String[] subjects -int idNumber +Student(String name, String[] subjects, int idNumber) + getName(): String + setName(name: String): void + getSubjects(): String[] + setSubjects(subjects: String[]): void + getIdNumber(): int + setIdNumber(idNumber: int): void }
[2] Exercises #
Cards 🃞 #
classDiagram class Card { -String suit -int number -String symbol + Card(suit: String, number: int, symbol: String) + getSuit(): String + setSuit(suit: String): void + getNumber(): int + setNumber(number: int): void + getSymbol(): char + setSymbol(symbol: char): void + printCard(): void }
Example card
objects
#
suit | number | symbol | printCard() |
---|---|---|---|
spades | 2 | null | 2♠ |
hearts | null | ‘K’ | K❤️ |
💻 Use the UML to create the class
💻 Add a main
method and thoroughly test your code
Extensions #
💻 In your main
method, write a nested loop that generates all 52 cards in the deck
💻 Add an isHigher
method.
+isHigher(Card otherCard): boolean
It should return true
if this
card is higher, false
if the other card is higher.
Vehicle 🚗 #
classDiagram class Vehicle { - model: String - year: int - mileage: int + Vehicle(plate: String, model: String, year: int) + increaseMileage(milesToAdd: int) : void + getModel(): String + setModel(model: String): void + getYear(): int + setYear(year: int): void + getMileage(): int + setMileage(mileage: int): void }
Example vehicle
objects
#
Model | Year | Mileage |
---|---|---|
Sedan | 2022 | 10000 |
SUV | 2020 | 8500 |
💻 Use the UML to create the class
💻 Add a main
method and thoroughly test your code
Extensions #
💻 Add an showVehicle
method that prints out an ASCII art representation of the vehicle.
💻 Add error handling to your methods so that:
- The user cannot set the year to a year in the future or before cars existed
- The mileage can only go up, not down
[3] Deliverables #
⚡✨Push to Github #
💻 Select Commit from the menu on the left.
Select all your updated files. Be sure to include a descriptive commit message.
💻 Click Commit and Push
💻 Click Push
[4] Extension: GUI #
Follow this tutorial and try to add in a GUI using Java Swing.