合并两个对象 (java)

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

Combining Two Objects (java)

问题

新手学习 Java对如何合并这两个对象感到困惑如果我表达不清楚或者这个问题以前已经被问过我很抱歉

我需要将 one.PiggyBank 添加到 two.PiggyBank

我们不允许修改用于输出代码的程序

public class PiggyBank {

    double pennies, nickels, dimes, quarters, totalValue, bankTotal;

    public PiggyBank(int p, int n, int d, int q) {
        pennies = p;
        nickels = n;
        dimes = d;
        quarters = q;
        totalValue = pennies + nickels + dimes + quarters;
    }

    public void addPenny() {
    }

    //访问器
    public double getP() {
        return pennies;
    }

    public double getN() {
        return nickels;
    }

    public double getD() {
        return dimes;
    }

    public double getQ() {
        return quarters;
    }

    public double combinePiggy(double bank2) {
        two.
        bankTotal = bank1 + bank2;
    }

    public static void main(String[] args) {
        PiggyBank one = new PiggyBank(5, 5, 5, 5);
        PiggyBank two = new PiggyBank(2, 3, 4, 1);

        System.out.println("Account 1: " + one + "\n");
        System.out.println("Account 2: " + two + "\n");

        one.combinePiggy(two);
        System.out.println("Account 1: " + one + "\n");
        System.out.println("Account 2: " + two + "\n");
    }
}
英文:

New to java and I'm confused as to how I can combine these two objects. Sorry if I am not clear / if this has been asked before.

I need to add one.PiggyBank to two.PiggyBank

We are not allowed to change the program used to output the code

    public class PiggyBank {
doubles pennies, nickels, dimes, quarters, totalValue, bankTotal;
public PiggyBank(int p, int n, int d, int q)
{
pennies = p;
nickels = n;
dimes = d;
quarters = q;
totalValue = pennies + nickels + dimes + quarters;
}
public void addPenny()
{
}
//accessors
public double getP()
{
return pennies;
}
public double getN()
{
return nickels;
}
public double getD()
{
return dimes;
}
public double getQ()
{
return quarters;
}
public double combinePiggy(double bank2)
{
two.
bankTotal = bank1 + bank2;
}
public static void main(String[] args) {
PiggyBank one = new PiggyBank(5, 5, 5, 5);
PiggyBank two = new PiggyBank(2, 3, 4, 1);
System.out.println(“Account 1: “ + one + “\n”);
System.out.println(“Account 2: “ + two + “\n”);
one.combinePiggy(two);
System.out.println(“Account 1: “ + one + “\n”);
System.out.println(“Account 2: “ + two + “\n”);
}
}

答案1

得分: 3

你需要创建那个 combinePiggy(PiggyBank) 方法,然后将参数的值添加到你调用它时相应的值上。

以下是一个相对抽象和简化的示例,可供您开始(为了获得学习效果,您应该自己完成真实的操作):

class Thing {
  int x;
  int y;

  void combine(Thing otherThing) { 
    x += otherThing.x; 
    y += otherThing.y;
  }
}
英文:

You need to create that combinePiggy(PiggyBank) method and then add the values of the parameter to the corresponding values you call this on.

Somewhat abstract and simplified example to get you started (you should do the real thing yourself to have a learning effect):

class Thing {
int x;
int y;
void combine(Thing otherThing) { 
x += otherThing.x; 
y += otherThing.y;
}
}

答案2

得分: 0

这有很多问题。首先,将这些属性列为双精度类型是没有意义的。
例如:pennies = 0.5; 这是没有意义的,因为在存钱罐里不能有半分钱。所有的访问器和修改器方法都需要进行调整。

public class PiggyBank {
int pennies, nickels, dimes, quarters;
double bankTotal;
.
.
.
public int getP()
{
return pennies;
}
public int getN()
{
return nickels;
}
public int getD()
{
return dimes;
}
public int getQ()
{
return quarters;
}
}

此外,combinePiggy 方法不应该有返回类型,因为你没有返回任何内容。而且,根据主方法的建议,combinePiggy 方法应该有另一个存钱罐作为参数。该方法应该将两个存钱罐中的每个类型的数量相加,以及每个存钱罐的总额。

public void combinePiggy(PiggyBank bank2)
{
pennies += bank2.pennies;
nickels += bank2.nickels;
dimes += bank2.dimes;
quarters += bank2.quarters;
bankTotal += bank2.bankTotal;
}

另外,根据主方法,你的类需要一个 toString 方法toString 方法的作用正如其名,它将对象转换为字符串。 (我会留给你编写适合你主方法的适当 toString 方法)

  public String toString(){
return "This piggy bank has " + pennies + " pennies, " + nickels + " nickels, " + 
dimes + " dimes, " + quarters + " quarters.";
}

另外,你不能简单地将硬币数量相加以得到银行总额。例如:你有一分钱、一枚五分硬币、一枚十分硬币和一枚二十五分硬币。0.01+0.05+0.10+0.25=0.41。你目前的方法只会将硬币的数量相加。

  public PiggyBank(int p, int n, int d, int q)
{
pennies = p;
nickels = n;
dimes = d;
quarters = q;
bankTotal = pennies*0.01 + nickels*0.05 + dimes*0.1 + quarters*0.25;
}

我认为我已经不仅回答了你的问题。关于 addPenny 方法以及设置器方法,我会留给你自己完成。看起来你对Java很陌生。在你着手完成这个任务之前,我建议你观看一些视频,阅读一些关于这门语言的文档。这里有一个视频 大致总结了这门语言。

英文:

There is so much wrong with this. First of all, it does not make sense to have those attributes listed as type double.
For example: pennies = 0.5; this makes no sense, as you cannot have half a penny in a piggy bank. This needs to be adjusted for all accessor and mutator methods.

public class PiggyBank {
int pennies, nickels, dimes, quarters;
double bankTotal;
.
.
.
public int getP()
{
return pennies;
}
public int getN()
{
return nickels;
}
public int getD()
{
return dimes;
}
public int getQ()
{
return quarters;
}
}

Additionally, the combinePiggy method should not have a return type, as you are not returning anything. Furthermore, as the main method suggests, the combinePiggy method should have another piggybank as a parameter. The method should then add the number of each piggy bank together, as well as the total of each bank.

public void combinePiggy(PiggyBank bank2)
{
pennies += bank2.pennies;
nickels += bank2.nickels;
dimes += bank2.dimes;
quarters += bank2.quarters;
bankTotal += bank2.bankTotal;
}

Another thing, judging by the main method, your class needs a toString method. A toString method is exactly what the name implies. It converts an object into a string. (I'll leave it for you to write the appropriate toString to fit your main method)

  public String toString(){
return "This piggy bank has " + pennies + " pennies, " + nickels + " nickels, " + 
dimes + " dimes, " + quarters + " quarters.";
}

Also, you cannot just add the number of coins in order to get a bank total. Example: You have a penny, a nickel, a dime and a quarter. 0.01+0.05+0.10+0.25=0.41. The way you are doing it, you're bank total would just add up the number of coins.

  public PiggyBank(int p, int n, int d, int q)
{
pennies = p;
nickels = n;
dimes = d;
quarters = q;
bankTotal = pennies*0.01 + nickels*0.05 + dimes*0.1 + quarters*0.25;
}

I think I've done more than answer your question. I will leave the addPenny as well as the setter methods to you. It seems you are very new to Java. I suggest you watch a few videos and read some documentation about the language before you tackle this assignment. Here is a video that more or less sums up the language.

huangapple
  • 本文由 发表于 2020年9月26日 02:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64069856.html
匿名

发表评论

匿名网友

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

确定