Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A challenge
Index -> Programming, Java -> Java Tutorials
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Jul 09, 2007 12:14 pm   Post subject: A challenge

What did I do wrong in the following code?

code:
class Foo {
   public int[] bar() {
      /* code omitted */
   }
}

class Baz {
   private Foo f;

   public Baz() {
      f = new Foo();
   }

   public void qux() {
      for (int i : f.bar()) {
         System.out.println(i);
      }
   }
}
Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Mon Jul 09, 2007 2:47 pm   Post subject: RE:A challenge

Are we sposted to reply by posting or PMing you :p. Also are there sposted to be more then one thing wrong?
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
wtd




PostPosted: Mon Jul 09, 2007 3:34 pm   Post subject: RE:A challenge

There is supposed to be one thing wrong. Or at least, I am looking for one thing specifically.

The code omitted is not the problem. Assume that it returns an array of ints via some mechanism that is irrelevant to the question.

Please post. First come, first serve.
Dan




PostPosted: Mon Jul 09, 2007 4:15 pm   Post subject: RE:A challenge

Well i could be wrong but public qux() needs a return type in it's deftion or has to have void.

I don't think this is what you meant tho. I have not seen a for loop done like that so i am not shure if it would work, i shall look at it more latter.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
wtd




PostPosted: Mon Jul 09, 2007 4:57 pm   Post subject: RE:A challenge

Indeed, it is not what I had in mind.

The loop is a 1.5+ for-each loop.
wtd




PostPosted: Mon Jul 09, 2007 7:17 pm   Post subject: RE:A challenge

For fun, in another language:

code:
class Foo {
   public def bar: Array[Int] = /* Code omitted */
}

class Baz {
   private val f: Foo = new Foo

   public def qux {
      for (val i <- f.bar) println(i)
   }
}
PaulButler




PostPosted: Mon Jul 09, 2007 9:37 pm   Post subject: RE:A challenge

I can't see anything that would prevent the code from compiling, is the mistake in the OOP design?

Should you have done the printing function inside the Foo class instead of the Baz class? Since you are printing the output of the Foo.bar method, it doesn't make sense to stick it in another class which is not used in the printing process.
wtd




PostPosted: Mon Jul 09, 2007 9:53 pm   Post subject: RE:A challenge

No. Good thoughts, but you're not even close. Smile
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Mon Jul 09, 2007 11:01 pm   Post subject: Re: A challenge

code:
   public void qux() {
      for (int i : f.bar()) {
         System.out.println(i);
      }
   }


Call to f.bar() can return null. Do not know if that is what you are looking for though.
wtd




PostPosted: Tue Jul 10, 2007 5:08 am   Post subject: RE:A challenge

It is not. Smile
Aziz




PostPosted: Wed Jul 11, 2007 12:00 pm   Post subject: RE:A challenge

The classes should be public...
Aziz




PostPosted: Wed Jul 11, 2007 12:02 pm   Post subject: RE:A challenge

And be in separate, properly named files. Though, again, perhaps not what you're looking for.
OneOffDriveByPoster




PostPosted: Wed Jul 11, 2007 2:27 pm   Post subject: Re: RE:A challenge

PaulButler @ Mon Jul 09, 2007 9:37 pm wrote:
Should you have done the printing function inside the Foo class instead of the Baz class? Since you are printing the output of the Foo.bar method, it doesn't make sense to stick it in another class which is not used in the printing process.

Maybe Baz should extend Foo instead of having a private Foo member?
Aziz




PostPosted: Wed Jul 11, 2007 5:15 pm   Post subject: RE:A challenge

I think it's more of a logical/syntax error or even theory, as there is no clue to the business rules that would apply to this model. There could be an Baz object that contains a Foo, and uses the qux() method the print it's child's (a Foo) property that is returned from bar() (which happens to be an array of int)
wtd




PostPosted: Wed Jul 11, 2007 7:11 pm   Post subject: RE:A challenge

Aziz is starting to think along the right lines.
Display posts from previous:   
   Index -> Programming, Java -> Java Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 39 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: