我是第一次为Java课程介绍创建UML图。我无法弄清楚我的课程是否正确指向对方。
Guitar类是否应该指向测试类?另外,吉他课应该在考试的左边吗?任何指针都非常感谢,谢谢。
My UML diagram hosted on ImageShack (我没有足够的ref点来插入图像)
代码低于
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
答案 0 :(得分:2)
GuitarTest和Guitar课程不应该有关联关系。
如果您有“GuitarCase”类,(和“Cover”类)定义如下:
public class GuitarCase{
public Guitar g;
public Cover c = new Cover();
}
然后应该有一个从GuitarCase到Guitar的关联,关联结束(在吉他结束时)有角色“g”;应该有一个从GuitarCase到Cover的组合,关联结束(在封面端)有角色“c”;
我甚至不想在图表上显示GuitarTest,因为你可以将主要功能放在吉他本身。
答案 1 :(得分:0)
您有一个主要问题 - 方向不应该从Guitar
到GuitarTest
,而是相反 - 从GuitarTest
到Guitar
(因为您使用Guitar
在测试中)。你也忘记了GuitarTest.main的论点(显然不那么重要)。
GuitarTest确实使用了吉他,但这就是“依赖”关系。 (在此示例中,从客户端到供应商绘制虚线) GuitarTest to Guitar,这意味着,如果吉他改变,GuitarTest必须 更改);但是,我不认为在功能体中使用吉他是一种 依赖。 (如果使用Guitar作为方法的参数,返回类型等, 然后保证依赖性。)恕我直言,没有关系 之间。
更好地查看更正确的@milesma答案。