关键词 > SOFT2201/COMP9201

SOFT2201/COMP9201 Week 3 Tutorial

发布时间:2023-01-20

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

SOFT2201/COMP9201

Week 3 Tutorial

UML and Implementation

UML

Unified Modelling Language is a visual language that allows software designers to visualise data and functionality of their system and how they interact with each other. There are several different UML diagram types including class diagram, interaction diagram and use case diagram.

Question 1: Heirarchy and Properties

You have been provided an example UML class model, this is a simple class hierarchy, outlining shared properties and functionality between the base class and sub-types.

Figure 1: UML Class diagram

Discuss and answer the followings:

• What kind of class is Canine? What keyword should we use if we want to represent this kind of class in UML?

What is the parent class of Wolf?

What properties are shared between DomesticDog and Wolf?

What methods are shared between DomesticDog and Wolf?

• Outline the access level associated with each field

Does either sub-type of Canine override one of its methods?

Question 2: Associations

Analyse the UML diagram and identify and answer the following:

• Outline all the associations within the UML Diagram

How many Aeroplanes can a Hangar contain? Annotate the multiplicity of the association

How many Tickets can a Customer hold? Annotate the multiplicity of the association

• Identify any association relationships where an object cannot exist without at least one other type (Composition)

Question 3: Critiquing a model and Refactoring the design

You will be asked through the semester to identify software design issues and refactor them. Not only will this improve your ability to debug problems but will help understand and identify issues within a code base.

Common issues that arise from improper modelling:

Weak base classes

Improper parent or interface type associated

Repetitive logic and data

• Class dependent on other classes

• Classes performing more functionality than it should be

Misassigned responsibility to an object

Given the following class diagram, discuss with your class and identify issues with the design.

Question 4: Extract Pokemon

You have been given a le that stores pokemon data called pokedex . json. Use the simple- json library as a way to read the pokedex.json file and output the contents.

You can add the dependency to your project by adding the following to the dependecies section in your gradle.build le.

implementation com .googlecode . json-simple:json-simple:1 . 1 . 1’ The element within pokedex.json file is in the following layout:

{

"kanto_no" : ,

"type_1" : first_type ,

"type_2" : second_type ,

"pokemon" : name

}

Import the following types into your project and attempt to parse the pokedex . json  file .

import org . json .simple .JSONArray;

import org . json .simple .JSONObject;

import org . json .simple .parser .JSONParser ;

JSON-Simple provides some examples in its wiki, you can access this here.

Read the contents of the le into a String object and utilise the .parse method to interpret the data into a JSONArray. You may need to apply a cast on the return result.

Command-query Separation

Command-query separation principle within imperative and object-oriented programming asks the programmer to design their methods to only perform a command or a query.  A command mutates and operates on the data provided or the instance itself. A query will not mutate data but provide an answer when invoked.

Question 5: Refactor the code

Identify methods which violate command-query separation and refactor the code, this may require breaking the code up into different functions.

public class Product {

private String  name ;

private int qty;

private int max;

private double price ;

/ **

*  Constructs  the  product,  providing  name  and  quantity

*  @param  name

*  @param  qty */

public Product (String  name , int qty , int max , double price)  { this .name  =  name ;

this .qty  =  qty;

this .max  =  max;

this .price  =  price ;

}

public Object  process (String  request , double value)  {

if (request .equals ( "getqty" )  {

return new Integer (qty);

} else if (request .equals ( "updateqty" ))  {

qty  +=   (int)  value ;

return null;

} else if (request .equals ( "getprice" ))  {

return new Double (price);

} else if (request .equals ( "updateprice" ))  {

price  +=  price ;

return null;

}

return null;

}

/ **

*  Returns  the  name  of  the  product

*  @return  name

*/

public String  getName ()  {

return name ;

}

/ **

*  Passes  the  order

*  @param  o

*/

public void order (Order  o , int qty)  {

o .addProduct (this ,  qty);

}

}

• What kind of issues does the method process create?

Is it the responsibility of the Product class to perform this kind of logic?

Refactor the code and separate any logic that does not belong in the Product class