Java 2 - 2024

Week 1

  • UML Class Diagrams
  • Classes and Objects
  • Collections (List and ArrayList)
  • GitHub Setup

Inheritance

  • Inheritance is a mechanism to prevent writing repeated code.
  • Inheritance models “is a” relationships, enabling you to reuse existing attributes and methods easily.
  • When A inherits from B, we say A is the subclass of B, and B is the superclass of A.
  • The notation for inheritance is a line with an open arrowhead pointing from the subclass to the superclass.
Professor
- salary : double
+ getSalary(): double
+ setSalary(double): void
+ toString(): String
Student
- gradeLevel : int
- gpa : double
+ getGradeLevel(): int
+ setGradeLevel(int): void
+ getGpa(): int
+ setGpa(int): void
+ toString(): String
User
- userId : int
- firstName : String
- lastName : String
+ getUserId : int
+ setUserId : void
+ getFirstName : String
+ setFirstName : void
+ getLastName : String
+ setLastName : void
+ toString : String

is a

is a

Associations

  • Objects are often associated with other objects.

  • For example:

    • A professor instructs a course

    • A student enrolls in a course

    • A course has enrollments

  • Associations are modeled as lines connecting the two classes.

  • Associations can be clarified by depicting labels and directionality.

Enrollment
- student : Student
- course : Course
- finalGrade : char
+ getStudent(): Student
+ setStudent(Student): void
+ getSection(): Section
+ setSection(Section): void
+ getFinalGrade(): char
+ setFinalGrade(char): void
+ toString()
Course
- courseId : int
- title: String
- featured: boolean
+ getCourseId(): int
+ setCourseId(int): void
+ getTitle(): String
+ setTitle(String): void
+ isFeatured(): boolean
+ setFeatured(boolean): void
+ toString(): String

instructs

has

has

Professor
Student
User

Composition Associations

  • If it makes sense to say something is part of (or consists of) something else, then you should use composition (aka aggregation).
  • Composite associations are shown with a diamond.
  • For example, it makes sense to say that:
    • Enrollment is on a grade report -or- a grade report is composed of enrollments.
    • A room is part of a building -or- a building is composed of rooms.
Enrollment
Student
GradeReport
- student : Student
- enrollments : List<Enrollment>
- dateCreated : LocalDate
// methods omitted

receives

composed of

has

Association Multiplicity

  • The multiplicity of the association is labeled on either end of the line, one multiplicity indicator for each direction

  • Each student has 0 or more enrollments.

    • Each enrollment has 1 student.

  • Each professor instructs 0 to 5 courses.

    • Each course has 1 to 3 professors.

Indicator Meaning
0..1 Zero or one
0..* Zero or more
0..n (such as 0..10) Zero to n (such as zero to ten)
n (such as 1) Exactly n (Only one)
1..* One or more
1..n (such as 1..10) One to n (such as one to ten)
Enrollment
Course
Professor
Student
User

1

0..*

0..6

1..3

1

0..*

  • Each course has 0 or more enrollments.

  • Each enrollment has 1 course.

UML Diagramming Tools

Week 1 Lab

Your Task:

Week 1 Assignment

  • An administrator updates the product catalog. Each product has a product id, vendor id, name, price, and description.
  • Each product may be purchased from one or more vendors. Each vendor has an id, name, and address (city, state, zip, country).
  • A customer can place an order by submitting a form. The order form collects the customer's name, address  (city, state, zip, country), phone number, and email address.
  • Each order has an id, date, and customer id. An order consists of one or more order items.
  • Each order item is associated with an order and a product. It also contains the quantity sold and price.

Your Task:

  • Identify six nouns from the problem domain above that can be used as classes.

  • Use a diagramming tool to create a class diagram with access modifiers, data types, parameters, return types, inheritance, associations, composition, multiplicity, and annotations.

Git Setup

  • Open the "java2-demos" folder in VS Code. Make sure the "src", folder is visible.
  • Click the "Terminal" menu and select "New Terminal". Click the arrow next to the "+" button. Choose "Select Default Profile". Select Git Bash. Click the trash can icon to close the terminal.
  • Click the "Source Control" button on the left taskbar.
  • Click the blue "Initialize Repository" button.
    • Alternatively, open a new Terminal and enter this command:
      git init -b main
    • If the task bar at the bottom-left displays "master" instead of "main", run this command:
      git checkout -b main

Git Setup

  • The panel will indicate that there are several uncommitted changes. Hold your mouse over the word "Changes" to see 3 new buttons "View Changes", "Discard All Changes", and "Stage All Changes". Click "Stage All Changes".

Git Setup

  • Open .gitignore and add this line.
    .env
  • Create a .env file in the src/main/resources folder. Add this line to the file:
    # Key=Value
  • Click the Git menu and select Commit. 
  • Select the files that you want to commit, including Unversioned Files. Note that the .env file should not be listed.