user1354270
user1354270

Reputation: 31

UML diagram to Java code

I'm new to Java and am trying to work through some questions where I have to convert a UML diagram to Java code: I have an image of the uml document-

http://s1079.photobucket.com/albums/w513/user20121/?action=view&current=uml.jpg

I'll show you what I have so far:

Q1: Write a Java version of class Entry assuming it has this constructor: public Entry(String name) and that the method getSize is abstract. A:

public abstract class Entry {
    private String name;

    public Entry(String name){
        this.name =  name;
    }
    public String getName()
    {
        return name;
    }
    abstract long getSize();
}

Q2: Write a Java version of class File assuming it has this constructor: public File(String name, long size) A:

public class File extends Entry {
    private long size;

    public File(String name, long size){
        super(name);
        this.size = size;
    }

    public long getSize(){
        return size;
    }
}

Q3: A directory contains a collection of files and directories. Write a Java version of class Directory assuming it has this constructor: public Directory(String name) and the method getSize returns the total size of all the files in the directory and all its sub-directories (in this model the size of a directory itself is ignored).

A: This is where I get stuck, I don't know what to do about the getSize method. Can anyone tell me whether what I have done so far is correct? And also point me in the right direction for Q3?

Edit: okay I have attempted an answer but I really I don't know what I'm doing..

import java.util.ArrayList;

public class Directory extends Entry {

    ArrayList <Entry> entries = new ArrayList<Entry>();

    public Directory(String name)
    {
        super(name);
    }

    public long getSize(){
        long size;
        for(int i=0;i<entries.size();i++)
        {
        size +=  //don't know what to put here?
        }
        return size;
    }
}

Upvotes: 3

Views: 10232

Answers (3)

areont
areont

Reputation: 31

As pointed out first two items are correct.

Here's how i would implement your Directory class.

import java.util.ArrayList;

public class Directory extends Entry{


ArrayList <Entry> entries = new ArrayList<Entry>();

public Directory(String name)
{
    super(name);
}

public long getSize(){
    long size = 0;
    for(int i=0;i<entries.size();i++)
    {
    //Probably something like this
    size += entries.get(i).getSize();
    }
    return size;
}
}

If your Directory contains other directories, the getSize of the child directory will be called to get its Size.

You will have to add a control that you can't add a directory to itself, or you will have a infinite loop :)

An other option is

public long getSize(){
    long size = 0;
    for( Entry e : entries)
    {
    //Probably something like this
    size += e.getSize();
    }
    return size;
}

Upvotes: 0

UML GURU
UML GURU

Reputation: 1462

You can try the following 30 days evaluation build: http://www.uml2.org/eclipse-java-galileo-SR2-win32_eclipseUML2.2_package_may2010.zip

Just unzip and it works. You code your java and get your UML class diagram live at the same time. Easy to use and very efficient.

Upvotes: 0

Alp
Alp

Reputation: 29749

Your answers for Q1 and Q2 are looking fine.

Regarding Q3:

// A Directory is an Entry and can contain an arbitrary number of other Entry objects
public class Directory extends Entry {

    // you need a collection of Entry objects, like ArrayList<Entry>
    // ArrayList<Entry> entries = ...

    function getSize() {
        long size;
        // now we calculate the sum of all sizes
        // we do not care if the entries are directories or files
        // the respective getSize() methods will automatically do the "right thing"
        // therefore: you iterate through each entry and call the getSize() method
        // all sizes are summed up
        return size;
    }

}

Upvotes: 1

Related Questions