如何在股票之间建立关联?

huangapple go评论73阅读模式
英文:

How to make association between shares?

问题

[![在这里输入图片描述][1]][1]


  [1]: https://i.stack.imgur.com/mST3Z.png

我是 Java 的新手我需要绘制这个图表但是我遇到了很多问题在 Company 类中我有以下内容


      public class Company {
        private String name;
    
        public Company (String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

在 Share 类中


     public class Share  {
        private double value;
        private Company company;
    
        public Share(double value, Company company) {
            this.value = value;
            this.company = company;
        }
    
        public double getValue() {
            return value;
        }
    
        public void setValue(double value) {
            this.value = value;
        }
    
        public Company getCompany() {
            return company;
        }
    
        public void setCompany(Company company) {
            this.company = company;
        }
    }
    
然后在 Portfolio 类中
    
        public class Portfolio {
       private int noShares;
       Share[] shares = new Share[5];
       private int numberOfShares = 0;
    
        public Portfolio(int noShares) {
            this.noShares = noShares;
        }
    
        public void addShare(Share newShare) {
        if (shares == null) {
            shares = new Share[0];
        }
        else {
            Share[] newShares = new Share[numberOfShares+1];
            System.arraycopy(shares, 0, newShares, 0, numberOfShares);
            shares = newShares;
        }

        shares[numberOfShares++] = newShare;
        newShare.setValue(this);
        }
    public double computeSum() {
            double sum = 0;
            for(var i = 0; i < shares.length; i++) {
                sum += i;
            }
            return sum;
        }
    
        public int getNoShares() {
            return noShares;
        }
    
        public Share[] getShares() {
            return shares;
        }
    }
根据图像中的说明我需要计算我添加的股份的价值问题是当我尝试使用关键字 this所需的类型是 double但提供的类型是 Portfolio所以我的问题是为了能够像图像中那样正确地计算股份我需要做什么呢
英文:

如何在股票之间建立关联?

I'm new to Java and i have to do this diagram, but i have a lot of problems, in Company i have the following:

  public class Company {
private String name;
public Company (String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

In Share:

 public class Share  {
private double value;
private Company company;
public Share(double value, Company company) {
this.value = value;
this.company = company;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
}

And in Portofolio i have:

    public class Portofolio {
private int noShares;
Share[] shares = new Share[5];
private int numberOfShares = 0;
public Portofolio(int noShares) {
this.noShares = noShares;
}
public void addShare(Share newShare) {
if (shares == null) {
shares = new Share[0];
}
else {
Share[] newShares = new Share[numberOfShares+1];
System.arraycopy(shares, 0, newShares, 0, numberOfShares);
shares = newShares;
}
shares[numberOfShares++] = newShare;
newShare.setValue(this);
}
public double computeSum() {
double sum = 0;
for(var i = 0; i < shares.length; i++) {
sum += i;
}
return sum;
}
public int getNoShares() {
return noShares;
}
public Share[] getShares() {
return shares;
}
}

So how it is said in the image i need to compute the values that i added, the problem is when i try to use this keyword, the required type is double, but the provided is Portofolio. So my question is what i need to do in order to be able to sum the shares correctly like in the image?

答案1

得分: 1

addShare函数中删除以下行,它没有意义:

newShare.setValue(this);

你还应该删除numberOfShares的多余定义,并且始终使用noShares代替(实际上,它们都不是必需的,因为Java数组知道它们自己的长度 - 但是你的练习要求这样做,所以坚持下去)。

接下来,删除构造函数,它也没有意义。

此外,你的computeSum方法对索引求和,而不是股份价值。将其更改为:

public double computeSum() {
    double sum = 0;
    for (int i = 0; i < noShares; i++) {
        sum += shares[i].getValue();
    }
    return sum;
}

Portfolio类的最终版本应该类似于这样:

public class Portfolio {
    private int noShares;
    private Share[] shares;

    public void addShare(Share newShare) {
        if (shares == null) {
            shares = new Share[0];
        } else {
            Share[] newShares = new Share[noShares + 1];
            System.arraycopy(shares, 0, newShares, 0, noShares);
            shares = newShares;
        }

        shares[noShares++] = newShare;
    }

    public double computeSum() {
        double sum = 0;
        for (int i = 0; i < shares.length; i++) {
            sum += shares[i].getValue();
        }
        return sum;
    }
}
英文:

Remove the following line from addShare, it does not make sense:

newShare.setValue(this);

You should also remove the redundant definition of numberOfShares and consistently use noShares instead (in fact, neither of them are necessary because Java arrays know their own lengths — but your exercise requires it, so stick with it).

Next, remove the constructor, it also doesn’t make sense.

Furthermore, your computeSum method sums the indices, not the share values. Change it to

public double computeSum() {
double sum = 0;
for(var i = 0; i &lt; noShares; i++) {
sum += shares[i].getValue();
}
return sum;
}

The final version of the Portfolio class should look something like this:

public class Portofolio {
private int noShares;
private Share[] shares;
public void addShare(Share newShare) {
if (shares == null) {
shares = new Share[0];
} else {
Share[] newShares = new Share[noShares + 1];
System.arraycopy(shares, 0, newShares, 0, noShares);
shares = newShares;
}
shares[noShares++] = newShare;
}
public double computeSum() {
double sum = 0;
for(int i = 0; i &lt; shares.length; i++) {
sum += shares[i].getValue();
}
return sum;
}
}

huangapple
  • 本文由 发表于 2020年10月27日 19:03:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64553041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定