Exercise: Classes, objects, equals() un hashCode()

Last modified by superadmin on 2018-01-12 20:27

Exercise: Classes, objects, equals() un hashCode()

Goal: Create classes accordingly to the UML diagram created in UMLClassDiagram

Mapping UML attributes happens as follows: "+" sign before an attribute name means public, "#" means protected and "-" means private. In our diagram all attributes are private.

  1. Create a new project - right-click "Application1" in JDeveloper and pick "New Project"; in the "New" gallery pick General -> Projects -> Empty Project and leave the default name for the project, namely "Project3" (see picture)

Unknown macro: picture.
Figure: Creating a new project for "Application1"

  1. Right-click the "Project3", and pick in the "New" gallery General -> Java Class. Write name "User" as class name and leave the default package - "project3".
Unknown macro: picture.
  1. Coding User class is straightforward - it does not have any outgoing relationships: 
package project3;

public class User {
  private String firstName;
  private String lastName;
  private String loginId;
  private String password;
  private String email;
  // also add all getXxx(), setXxx() methods
}

 To add get/set methods, right-click somewhere in the code editor and pick from the menu Source -> Generate Accessors...

  1. Similarly create Assignment class. The outgoing navigable relationship to multiple Questions means that there should be a collection of Question objects. Since they are ordered within a module, we pick List<Question> rather than Set<Question>
package project3;

import java.util.List;

public class Assignment {
  private User user;
  private List<Question> questions;
  private String name;
  private String title;
  // also add all getXxx(), setXxx() methods
}
  1. Create "Question"; it has multiple choices; again assume that they are ordered: 
package project3;
import java.util.List;
public class Item {
  protected String content;
  protected List<Alternative> alternatives;
  // also add all getXxx(), setXxx() methods
}
  1. Create "Choice"; it has no outgoing navigable relationships, so it should have just one attribute: 
package lv.ante.xwiki.testdigester.domain;
public class Choice {
  protected String content;
  // also add all getXxx(), setXxx() methods
}
  1. Notice that the "composite relationship" is not different from any other relationship from the Java point of view. 

Note: This exercise only covers the issues of using generics classes and collections (rather than developing our own). See Sun's generics tutorial for more difficult examples.

Tags:
Created by Kalvis Apsītis on 2008-02-28 13:44
    
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 6.4 - Documentation