0

Given the following example code, please help me answer the following questions with hints

 public class Coin
    {
    private String myColor;
    private int mySideOne;
    private double mySideTwo;


    public Coin(String Color, int SideOne, double SideTwo)
    {
    myColor= Color;
    mySideOne = SideOne;
    mySideTwo = SideTwo;
    }
    //accessors getColor(), getSideOne(), and getSideTwo()

}

public class Total
{
private int myNumCoins;
private Coin[] moneyList;

//constructor
public Total(int myCoins)

{

myNumCoins = numCoins;
moneyList = new Coins[numCoins]
String color;
int mySideOne;
double mySideTwo;
for (int i = 0; i<numCoins; i++)
{




}
}

**

Question:

**

//Returns total amount for Coins 
public double totalMoney()
{
double total = 0.0;
/* code to calculate 
return total;
}
}

Which represents correct / code to calculate amount */ in the totalMoney method?

 A. for (Coin t: moneyList)
    total+= moneyList.getSideTwo();

    B. for (Coin t: moneyList)
    total+=t.getSideTwo();

I think A is right because the "t" in B. doesn't exist in the code. How am I wrong?

5
  • actually, t does exist in the foreach loop statement. Commented Jan 13, 2015 at 21:45
  • 2
    A won't even compile, there isn't a getPrice() method on an array. Commented Jan 13, 2015 at 21:47
  • I might be wrong, but you dont have tickList and if you do make sure it is static Commented Jan 13, 2015 at 21:48
  • @JimW is right. Option A won't compile. Option B is the enhanced for loop where t is a local variable just the way i is in the regular for loop Commented Jan 13, 2015 at 21:50
  • @AmandaKelius tickList is a collection of Ticket objects. The collection has it's own properties like "how many objects do I have" and each item in the collection has it's own set of properties like "price" For a real world example think of a parking lot full of cars. The lot could be full, you could start one of the cars but you can't "start" the parking lot. Commented Jan 13, 2015 at 22:08

2 Answers 2

3

Let's evaluate the code using A.:

public double totalPaid()
{
    double total = 0.0;
    for (Ticket t:tickList)
        total+= tickList.getPrice();
    return total;
}

tickList is an array of Tickets. An array is an object which only has a static final field called length. So, tickList cannot have getPrice. This means, option A doesn't compile.

Let's evaluate the code using B.:

public double totalPaid()
{
    double total = 0.0;
    for (Ticket t:tickList)
        total+=t.getPrice();
    return total;
}

Here you state:

I think A is right because the "t" in B. doesn't exist in the code. How am I wrong?

In fact, t is a variable declared and used in the enhanced for loop statement. t is from type Ticket and it will take the value of each Ticket object reference stored in tickList. The enhanced for loop can be translated to this form for arrays:

for (int i = 0; i < tickList.length; i++) {
    Ticket t = tickList[i];
    //use t in this scope
    //in this case, it's used to accumulate the value of total
    total += t.getPrice();
}

Which makes B as the solution for this problem.

Sign up to request clarification or add additional context in comments.

2 Comments

@AmandaKelius tickList is not part of the constructor. tickList is a field declared in Transaction class. Check here: private Ticket[] tickList;. This field is initialized in the constructor of Transaction class: tickList = new Ticket[numTicks]. You cannot directly access to the methods of the elements of the array using the array variable only, you have to access to a single element in the array and then access to its elements, as shown in the code above.
@AmandaKelius no, t is a Ticket t, it's a single Ticket. You can see the difference in the last piece of code in my answer.
2

The answer is B because you declare t in your loop when you say Ticket t. The loop iterates the ticketList and t will stand for each Ticket in the list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.