4

我第一次为 Java 课程介绍创建 UML 图。我无法确定我的类是否正确指向彼此。

Guitar 类是否应该指向测试类?另外,吉他课应该在测试课的左边吗?任何指针都非常感谢,谢谢。

在此处输入图像描述 我在 ImageShack 上托管的 UML 图 (我没有足够的参考点来插入图像)

代码如下

package guitartest;

import java.util.Scanner;

public class Guitar {

// Declare variables 
private int numberOfStrings;
private String stringName;
private boolean isTuned;
private boolean isPlaying;
private boolean isPlucking;


// Construct a guitar and set boolean fields to "false"
public Guitar(){
    this.isTuned = false;
    this.isPlaying = false;
    this.isPlucking = false;
}   // end constructor


// Prompt user to enter number of strings 
public int getNumberOfStrings() {
    Scanner sc = new Scanner(System.in);

    // Loop continues until a positive integer is entered
    do {
        System.out.println("Enter number of strings in integer format");
        while (!sc.hasNextInt()) {
            System.out.println("Error! That's not a number!");
            sc.next();
        }    // end while
        numberOfStrings = sc.nextInt();
     }    // end do-while loop
     while (numberOfStrings <= 0); 
    return numberOfStrings;
}   // end method getNumberOfStrings

// Prompt user to enter string names
public String getStringName() {
    Scanner sc = new Scanner(System.in);

    // Loop continues until all strings are named
    for (int i = 0; i < numberOfStrings; i++){
        System.out.println("Enter string name"); 
        // Error if input is not a character
        while (!sc.hasNext("[A-Za-z]")) {
            System.out.println("Error! That's not a character!");
            sc.next();
         }
        stringName = sc.next();
    }   // end for loop
    return stringName;
}   // end method getStringName

// Verifies tuning status
public boolean tuneInstrument(){
    return isTuned;
}   // end method tuneInstrument

// Tune guitar
public void setTuned(boolean isTuned) {
    this.isTuned = isTuned;
}   // end method setTuned

// Verifies instrument is playing
public boolean playInstrument(){
    return isPlaying;
}   // end method playInstrument

// 
public void startPlayingInstrument(){
    isPlaying = true;
}   // end method startPlayingInstrument

public boolean pluckInstrument(){
    return isPlucking;
}   // end method pluckInstrument

public void startPlucking(){
    isPlucking = true;
}   // end method startPlucking

// Stop instrument
public void stopInstrument(){
    isPlaying = false;
}   // end method stopInstrument

}   // end class Guitar

测试类如下:

package guitartest;

import java.util.Scanner;
// Scanner is in the java.util package

public class GuitarTest {

public static void main(String[] args) {

    // Create 10 guitar objects
    Guitar[] guitar = new Guitar[10];

    for (int i = 0; i < guitar.length; i++){
        guitar[i] = new Guitar();


    // Call methods in Guitar class
    guitar[i].getNumberOfStrings(); 
    guitar[i].getStringName();

    System.out.println("Is the guitar tuned? " + guitar[i].tuneInstrument());
    System.out.println("Guitar is being tuned. Please wait...");
    guitar[i].setTuned(true);
    System.out.println("Is the guitar tuned? " + guitar[i].tuneInstrument());

    System.out.println("Is the guitar playing? " + guitar[i].playInstrument());
    System.out.println("Please wait for guitar to play...");
    guitar[i].startPlayingInstrument();
    System.out.println("Is the guitar playing? " + guitar[i].playInstrument());

    System.out.println("Is the guitar being plucked? " + guitar[i].pluckInstrument());
    System.out.println("Please wait for guitar to be plucked...");
    guitar[i].startPlucking();
    System.out.println("The guitar is playing and being plucked. " + guitar[i].pluckInstrument());

    System.out.println("Stopping guitar from playing...");
    guitar[i].stopInstrument();
    System.out.println("Is the guitar playing? " + guitar[i].playInstrument());
    }   // end for loop

}   // end method main


}   // end class GuitarTest
4

2 回答 2

2

GuitarTest 和 Guitar 类不应该有关联关系。

如果你有一个“GuitarCase”类,(和一个“Cover”类)定义如下:

public class GuitarCase{
    public Guitar g;
    public Cover  c = new Cover();
}

那么应该有一个从 GuitarCase 到 Guitar 的关联,关联端(在 Guitar 端)有角色“g”;从 GuitarCase 到 Cover 应该有一个 Composition,关联端(在 Cover 端)有角色“c”;

我什至不想在图表上显示 GuitarTest,因为您可以将主要功能放在 Guitar 本身中。

于 2013-07-08T10:31:09.567 回答
0

你有一个主要问题——方向不应该是从Guitar到,GuitarTest而是相反——从GuitarTestGuitar(因为你Guitar在测试中使用)。你也忘记了 GuitarTest.main 的论点(很明显所以不是很重要)。

@milesma 在评论中指出我最初是不正确的,我同意他的观点:

GuitarTest 确实使用了 Guitar,但那是“依赖”关系。(从 Client 到 Supplier 画虚线,在这个例子中,从 GuitarTest 到 Guitar,这意味着,如果 Guitar 改变了,GuitarTest 必须改变);但是,我不认为在函数体中使用 Guitar 是一个依赖项。(如果 Guitar 用作方法的参数、返回类型等,则保证依赖关系。)。恕我直言,两者之间没有任何关系。

最好查看@milesma 答案,哪个更正确。

于 2013-07-07T22:48:11.977 回答