tags:

views:

108

answers:

8

//create a method called costOfRecordCollection that will return the cost of all the CDs in the ArrayList

hi, i'm stumped on the above method at the moment (new to java programming) - this is what i've written so far but don't know if i'm on the right track. any assistance/direction appreciated. Pieter.

public void setcostofRecordCollection(String cost) {
    cost = cost;
}

public int getcostofRecordCollection() {
    return cost();
}
+2  A: 

Okay, you have three significant problems at the moment:

First, in the setter method, you have this line:

cost = cost;

This is a no-op - it's not going to be doing anything. Assuming you also have an instance variable called cost, you want:

this.cost = cost;

Second, in the getter method, you have this:

return cost();

That's trying to call a method called cost, whereas presumably you want to return the value of the variable, so it should just be:

return cost;

Third, you've got different types: you're trying to set with a String, but then return an int. Which do you actually mean?

A less immediately significant problem is your names. They should conventionally be getCostOfRecordCollection and setCostOfRecordCollection - at least in terms of capitalization. I'd probably reorder the words slightly to getRecordCollectionCost and setRecordCollectionCost, but that's very minor.

Jon Skeet
Thanks Jon, as it's a value I guess it should be int or double, i'll implement your other suggestions and report back. Pieter
Pieter
better practice is to not name your input parameters to a method the same as instance vars, then this. is not needed.
Joe
@Joe: But then either your parameter or your instance variable has a less-than-natural name. You could use something like `newValue` in the setter, possibly... but I have no problem with using `this.cost` instead. I don't think you'll find enough agreement about this within the community to just declare that one way is "better practice" without qualification.
Jon Skeet
No, not newCost, but rather aCost, aName, aFileMy reason for saying it is a better practice is:1. It is dangerous to name parameters the same as instance vars as now you *have to* use this. and if you forget you're back to the no-op issue. And if you miss one it can be a hard bug to find.2. Adding this. means: typing 5 extra characters *everywhere* this occurs and adds to the visual 'noise' that must be dealt with by a programmer. Both of these issues are completely eliminated by use of aParam naming.
Joe
5. This is the convention used in Smalltalk. Which is good enough for me.So, my personal preference is to name params aParam and avoid the unnecessary extra work.
Joe
@Joe: Using a convention from one language in another is generally a bad idea, IMO - especially if you're working in a team. Note that with a decent IDE, you get a warning if you forget to use `this`. As for visual noise, it's *only* on setters - whereas by adding `a` on every parameter, you really *have* got visual noise everywhere. Urgh.
Jon Skeet
Agree, in general. But, in this case, as Java is based (in part ) on Smalltalk, and Smalltalk is the mother of OO languages, I don't have an issue. Rest is style. I find get and set on methods to be unnecessary, as it can be inferred from method signature, but I gave this up a long time ago.
Joe
@Joe: Just because two languages have the same architecture doesn't mean they have the same *conventions*. (C# and Java started off being *very* similar, but had different naming conventions for example.) If you start deciding to mix and match conventions, you'll have very inconsistent code. Again, that doesn't matter so much if it's just you, but if you're working on a large body of code maintained by many people, consistency is very important.
Jon Skeet
+1  A: 

From your description, your RecordCollection class should have an ArrayList with CDs in it. You want to loop through each element of the list and keep a running total of the cost of each CD. You can do that with a for loop.

ArrayList<CD> records; // instantiating this is up to you

public double getcostofRecordCollection() {
     double cost = 0.0;

     for(CD record : records) {
         cost = cost + record.getCost();
     }

     return cost;
}

As you can see, there's a lot left for you to do. The major things you have to do are instantiate the ArrayList and define what a CD is. The CD class (or whatever a record is) should have a getCost method so you can return the cost of each record in the collection. I suspect this is homework, so you should refer back to your assignment (or one of the other answers here) for these details.

Bill the Lizard
+1  A: 

This sounds like a homework problem to me, so I won't give you the code, just the general idea of what it should do.

You will need an ArrayList to store the list of prices. The function should iterate through each CD in that list and add its cost to the total. Then return that total value.

Colin O'Dell
+1  A: 

... that will return the cost of all the CDs in the ArrayList

This indicates some data structure similar to this one (if we stick with integers to represent the cost, BigInteger/BigDecimal or something similar would be more generic here):


class Cd {
  private int cost = 0;
  public int getCost() {
    return this.cost;
  }
  public void setCost(int cost){
    this.cost = cost;
  }
}

Given that the requested method should process an ArrayList of Cds, your signature should probably look somehow like this:

public int costOfRecordCollection(ArrayList collection)

Now all you have to do, is iterate over that collection and create the sum of all the costs.

If you have your own RecordCollection implementation, the method mentioned above could also live without its parameter and just operate on instance variables.

Horst Gutmann
A: 

Probably this is what you want. This goes by the rules, that you should generally make the fields of a class private (in this case "cost"). And provide a getter and setter function so it can be accessed from "outside". I hope that tells you enough.

public class RecordCollection{

   private int cost = 100;

   public RecordCollection() {

   }

   public void setCost(int cost) {this.cost = cost;}
   public int getCost() {return this.cost;}
}
AudioDroid
A: 

I would suggest not using a Concrete type like Array List, but rather an interface for the type - then you can use any concrete type to instantiate. example: List records = ArrayList()... or any collection that supports the list interface. Other wise you are now creating a hard dependency to the concrete class.

Joe
A: 

Your code is useless, throw it away. Here's something to get you started.

A class called record:

public class Record{

    private final String name;
    private final String artist;
    private final int cost;

    public String getName(){
        return name;
    }

    public String getArtist(){
        return artist;
    }

    public int getCost(){
        return cost;
    }

    public Record(final String name, final String artist, final int cost){
        super();
        this.name = name;
        this.artist = artist;
        this.cost = cost;
    }

}

A method to calculate the cost of a record collection (no, this should not use int, but I've got to leave some work for you to do):

public static int calculateCostOfRecordCollection(
    final Collection<Record> records){
    int cost = 0;
    for(final Record record : records){
        cost += record.getCost();
    }
    return cost;
}

And some test code:

public static void main(final String[] args){
    final Collection<Record> records =
        Arrays.asList(
            new Record("The Beatles", "White Album", 25),
            new Record("Roxy Music", "Stranded", 27),
            new Record("Bjork", "Debut", 25),
            new Record("David Bowie", "ChangesBowie", 39)
        );
    System.out.println(calculateCostOfRecordCollection(records));
}

Output:

116

seanizer
A: 

This sounds like homework, and you are new to coding, so try to break the original statement down

create a method called costOfRecordCollection that will return the cost of all the CDs in the ArrayList

You will need to create an ArrayList which will store your CD's inside of the class. Since the Arraylist will be storing the cost of the cds, you will most likely instantiate your ArrayList as a double.

Next your method costOfRecordCollection, need to calculate the cost of of all the cd's in the list, you need to loop on all of the records in the ArrayList. Keep a calculation to determine the running total.

You can then return this calculated value to determine your solution

Sean
thanks for the suggestions, there's a lot of information provided so please bear with me whilst i work through it, will report back asap. P
Pieter