英文:
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 < 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 < shares.length; i++) {
sum += shares[i].getValue();
}
return sum;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论