ISF DP Computer Science

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 #

suitnumbersymbolprintCard()
spades2null2โ™ 
heartsnull‘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 #

ModelYearMileage
Sedan202210000
SUV20208500

๐Ÿ’ป 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.