简体   繁体   English

Java解密UML图

[英]Java deciphering UML diagram

在此处输入图片说明

I have to write a program which computes the prime factors of a number. 我必须编写一个程序来计算数字的素数。 I have the algorithm done already, I just don't see how to use the methods hasMoreFactors and nextFactor. 我已经完成了算法,只是看不到如何使用hasMoreFactors和nextFactor方法。 Here's my algorithm inside the constructor 这是构造函数中的我的算法

int i = 2;

while (num > 1)
{
    if (num % i == 0)
    {
      System.out.println(i); // test if algorithm works
      factor.add(i);        // adds factor to array list
    }
else
{
  i++;
} 

My hunch is that I have to use hasMoreFactors in the algorithm, so I would replace this: 我的直觉是我必须在算法中使用hasMoreFactors,所以我将其替换为:

if (num % i == 0)

with hasMoreFactors(). 使用hasMoreFactors()。

if (hasMoreFactors() == true)

What you have there is the iterator pattern. 您拥有的是迭代器模式。 You are supplying an iteratable data type Factors . 您正在提供可迭代的数据类型Factors That means the client of your code will be passing it a number to get all the calculated factors and then will retrieve them. 这意味着您的代码的客户端将为其传递一个数字,以获取所有计算出的因子,然后将其检索。

The two methods you have questions about are not for you to use per-se ; 您有疑问的两种方法不适合您本人使用; they need not be used in the code calculating the factors. 无需在计算因子的代码中使用它们。 They are part of the interface the client uses to get those calculated numbers. 它们是客户端用来获取这些计算数字的界面的一部分。

You do need to implement them which is another question. 确实需要实现它们,这是另一个问题。 The primary hint given to you is that you need to keep track of the current index the user is at. 给您的主要提示是,您需要跟踪用户所在的当前索引。

Taking a look at the Java Iterator interface may be of some help. 看一下Java Iterator接口可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM