10 Replies - 800 Views - Last Post: 10 August 2012 - 12:14 PM Rate Topic: -----

#1 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 154
  • Joined: 05-May 12

Java Self Tests

Posted 10 August 2012 - 07:27 AM

class Mammal{
  String name = "furry";
  String makeNoise() (return "generic noise";}
}
class Zebra extends Mammal{
   String name = "stripes";
  String makeNoise() (return "generic noise";}
}

public class ZooKeeper{
  public static void main(String[] args) { new ZooKeeper().go();}
  void go() {
    Mammal m = new Zebra();
    system.out.println(m.name + m.makeNoise());
  }
}




The above program prints out furry bray but does not print out the noise? Why is that? because i can figure it out.

I meant to say that i cant figure it out!

Is This A Good Question/Topic? 0
  • +

Replies To: Java Self Tests

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Java Self Tests

Posted 10 August 2012 - 07:32 AM

This code doesn't print anything - it doesn't compile. Once you fix the code's compilation errors, it will print the string "furrygeneric noise". I don't know what makes you say that it wouldn't print the noise part.
Was This Post Helpful? 1
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1980
  • View blog
  • Posts: 4,824
  • Joined: 10-September 10

Re: Java Self Tests

Posted 10 August 2012 - 07:33 AM

Why is the code you post always a mess? It won't run, because there are typos and syntax errors throughout, and your general style of coding hasn't improved much in over 100 posts. What gives?

And what does the subject (thread title) have to do with the code you posted? This is another exercise in inheritance.
Was This Post Helpful? 3
  • +
  • -

#4 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 154
  • Joined: 05-May 12

Re: Java Self Tests

Posted 10 August 2012 - 07:51 AM

Yes i thought you may mention the way the program is layed out, it taken from a java book with self tests to practice for the real exam and how ive put it up is exactly how it is in the book, they say they make it look confusing and messy and do this on purpose just to make it harder for you to better prepare you for whats in the exam.

as for the answer to what it prints out it says the correct answer is furry bray and that whats confused me, as like sepp2k said it should print out the noise aswel and thats what i thought but apparently i was wrong
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Java Self Tests

Posted 10 August 2012 - 07:58 AM

After fixing the errors, it definitely prints "furrygeneric noise" (without fixing the errors, it doesn't print anything at all on account of not compiling). It most certainly does not print "furry bray". The word "bray" does not even appear anywhere in your code.

If your book claims otherwise, it's lying to you (assuming the code you posted is exactly the same as the one in the book).

This post has been edited by sepp2k: 10 August 2012 - 07:59 AM

Was This Post Helpful? 4
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1980
  • View blog
  • Posts: 4,824
  • Joined: 10-September 10

Re: Java Self Tests

Posted 10 August 2012 - 08:02 AM

Quote

they say they make it look confusing and messy and do this on purpose just to make it harder for you to better prepare you for whats in the exam

I'm not thrilled with the book's approach, but for whatever reason, you're using it. You should therefore get the "benefit" of its approach by cleaning up and fixing the code they've given you before posting it. That alone may answer some of your questions.

And I'm skeptical of your explanation, because you represented the code posted as code you've run, and it obviously won't run. If you want help with understanding code you've run, post the actual code, not the mess you started with from the book. We can't help you understand code you've run if we don't know what you've run.

And how could "bray" possibly be included in a result of the code you posted? Think it through a bit. There's something you're not telling us, probably because you haven't come across it yet yourself.
Was This Post Helpful? 4
  • +
  • -

#7 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 154
  • Joined: 05-May 12

Re: Java Self Tests

Posted 10 August 2012 - 08:13 AM

class Mammal{
  String name = "furry";
  String makeNoise() {return "generic noise";}
}
class Zebra extends Mammal{
  String name = "stripes";
  String makeNoise() {return "bray";}
}
	 
public class ZooKeeper{
   public static void main(String[] args) { new ZooKeeper().go();}
   void go() {
    Mammal m = new Zebra();
    System.out.println(m.name + m.makeNoise());	
  }
	}




After going back i see i did make an error, when copy and pasting the one part of the program i forgot to change it to bray that why its not there, how embarrassing!....

Yes Greg, good idea there will definitely change the code to a more readable one before posting any more of them out of that book

My link

Here is a link to the book im studying from.

This post has been edited by BrendanH: 10 August 2012 - 08:07 AM

Was This Post Helpful? 0
  • +
  • -

#8 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,141
  • Joined: 31-August 09

Re: Java Self Tests

Posted 10 August 2012 - 08:17 AM

It prints out "stripesbray" because Zebra implemented it's own functionality as a mammal. if you created a new Mammal(), it would have printed "furrygeneric noise".
Was This Post Helpful? 1
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5427
  • View blog
  • Posts: 8,727
  • Joined: 19-March 11

Re: Java Self Tests

Posted 10 August 2012 - 08:22 AM

*
POPULAR

View PostBrendanH, on 10 August 2012 - 10:09 AM, said:

Yes Greg, good idea there will definitely change the code to a more readable one before posting any more of them out of that book



Please do. If for no other reason than as a demonstration of a degree of respect for these nice people who are taking time out of their work day to help you learn to program.
Taking the time to make the code legible will help you in two ways. First, it'll force you to review the code closely (and catch any mistakes you may have introduced). Second, it'll help guys like Greg and Sepp and Luckless go straight to the problem you're actually asking about, which will give them more time to actually help you with the issues you're concerned about.

This post has been edited by jon.kiparsky: 10 August 2012 - 08:54 AM
Reason for edit:: EDIT: missed out LuckLess the first time

Was This Post Helpful? 5
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5427
  • View blog
  • Posts: 8,727
  • Joined: 19-March 11

Re: Java Self Tests

Posted 10 August 2012 - 08:28 AM

Addendum: To be specific, we will all benefit greatly if you take these steps:

- Compile and run the code you're asking about. If it doesn't compile, fix the errors.
- Format the code in a consistent style, adhering to standard naming conventions and other stylistic conventions
- If the code does not act the way you expect, tell us what you expect and why, and what you see instead. The "why" is crucial here - it tells us that you have a hypothesis, that you're thinking about what this code means. That's a very useful signal for us, it helps us zoom in on the correct answer for your actual point of confusion.

This post has been edited by jon.kiparsky: 10 August 2012 - 08:29 AM

Was This Post Helpful? 3
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,521
  • Joined: 27-December 08

Re: Java Self Tests

Posted 10 August 2012 - 12:14 PM

In short, please get in the habit of asking questions the smart way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1