Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Question 4(coding part) of Dr. Stonedahl's test

+12 votes
Hey, here is my code for those 2 coding problems. Can anyone let me know if I am correct?

1. public static int harvestEgg(){

               return BirdLivestock.getEggCountToday();



2. public class BirdLivestock {

        private Date brithday;

    public static int getAge() {


    public static int getEggCountToday() {


asked Sep 22, 2015 in Fall 15-16 by Amiao Gao (100 points)

2 Answers

+5 votes
Best answer
By saying that we need to be "calling getEggCountToday() on each individual bird," Dr. Stonedahl means that birds is a List containing objects of type BirdLivestock and you have to call that method on each element in the List. You can only call the getEggCountToday() method on an object of type BirdLivestock. In this case you can return each BirdLivestock object by using a for loop to traverse the List and the .get(int index) method call of the List interface. I believe the code should look similar to this:

public int harvestEggs(){

    int sum = 0;

    for (int index = 0; index < birds.size(); index++){

        sum += birds.get(index).getEggCountToday();


    return sum;



As for your revised part two, that is what I have.
answered Sep 23, 2015 by Jeffrey Prior (100 points)
selected Sep 23, 2015 by Amiao Gao
Looks good.  Calling the variable "i" would probably be better than "index", since the longer name isn't really more informative, and it's conventional that "i" is an index variable.

(A "for-each" loop would be best in this situation, but I wouldn't take off points for not using it.)

Side note: I hope you didn't stay up until 6:20 in the morning... seriously, a good night's sleep is very important for exam-taking!
+3 votes

For Part 1, you can't just call BirdLivestock.getEggCountToday() because the getEggCountToday() method is an instance method, not a static method, so you need to call it on individual instances of the BirdLivestock type. 

Note, also, that the BirdFarm class that you are writing in has a LIST of BirdLivestock.  So for Part 1, you need to write a FOR loop that goes through that list and sums up the result of calling getEggCountToday() on each individual bird...

For Part 2, there are a few issues:

a) none of the methods are static.  (FYI static methods appear as "underlined" in a UML diagram, but we haven't talked much about that, since we tend to focus mostly on instance methods in our class modeling.)

b) one of the methods should be *abstract*.  See the italics?  

c) Furthermore, the whole class itself is abstract...

answered Sep 23, 2015 by Forrest Stonedahl (100 points)
I redid part 1, here is my code:
public int harvestEgg() {
      int sum = 0;
      for (int brid:birds ) {
      sum += bird;
       return sum;

I don't get that I should "calling getEggCountToday() on each individual bird"
Am I still missing a part in the for loop?
And here is my part 2 code:

public abstract class BirdLivestock {
       private Date brithday;
   public int getAge() {
   public abstract int getEggCountToday() {


Please let me know if I still have any mistakes here.