10 Replies - 530 Views - Last Post: 07 March 2010 - 07:33 PM Rate Topic: -----

#1 Guest_fehn*


Reputation:

please write down the outputs of the following short program

Posted 07 March 2010 - 05:07 PM

on a sample java exam it says "please write down the outputs of the following short program and justify your answer" but I dont understand what it is supposed to do and get errors when i even try to run it. does anybody know what this is trying to teach?

class Sup {
void who ( ) {
System.out.println (“who() in Sup”);
}
}
class Sub1 extends Sup {
void who ( ) {
System.out.println (“who() in Sub1”);
}
}
class Sub2 extends Sup {
void who ( ) {
System.out.println (“who() in Sub2”);
}
}
class SupSubApp {
public static void main(String args[]){
Sub1 subOb1 = new Sub1();
Sub2 subOb2 = new Sub2();
subOb1.who();
subOb2.who();
}
}


Is This A Good Question/Topic? 0

Replies To: please write down the outputs of the following short program

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Re: please write down the outputs of the following short program

Posted 07 March 2010 - 05:12 PM

The errors you are getting are due to the double-quotes. I'm guessing you copied this code directly from a Microsoft Office application, which provides fancy quotes for the standard double-quotes. So for future reference, Microsoft applications are not safe to copy code from, which is why the Strings in the println() statements aren't displaying correctly inside the code tags. After I fixed the quotes, I ran your program and it worked fine.

Now as for your exam answer, take a look at the type of the Object calling the who() method. What is the output for that who method?

This post has been edited by macosxnerd101: 07 March 2010 - 05:12 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_fehn*


Reputation:

Re: please write down the outputs of the following short program

Posted 07 March 2010 - 05:30 PM

View Postmacosxnerd101, on 07 March 2010 - 04:12 PM, said:

The errors you are getting are due to the double-quotes. I'm guessing you copied this code directly from a Microsoft Office application, which provides fancy quotes for the standard double-quotes. So for future reference, Microsoft applications are not safe to copy code from, which is why the Strings in the println() statements aren't displaying correctly inside the code tags. After I fixed the quotes, I ran your program and it worked fine.

Now as for your exam answer, take a look at the type of the Object calling the who() method. What is the output for that who method?

really it ran for you? well yes the quotes were given just like that on the exam. is this a trick question that because of the void it wouldn't output anything?
Was This Post Helpful? 0

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Re: please write down the outputs of the following short program

Posted 07 March 2010 - 05:35 PM

After I deleted the quotes and put them back, it worked. Here is your code with the fixed quotes. Note how the Strings in the println() statements are highlighted accordingly now as String literals:
class Sup { 
void who ( ) { 
System.out.println ("who() in Sup"); 
} 
} 
class Sub1 extends Sup { 
void who ( ) { 
System.out.println ("who() in Sub1"); 
} 
} 
class Sub2 extends Sup { 
void who ( ) { 
System.out.println ("who() in Sub2"); 
} 
} 
class SupSubApp { 
public static void main(String args[]){ 
Sub1 subOb1 = new Sub1(); 
Sub2 subOb2 = new Sub2(); 
subOb1.who(); 
subOb2.who(); 
} 
}


As for your question about void, this is not a trick question. When you invoke the who() method for those two objects, the code in the method executes and outputs. Even non-void type methods can still output to the console or display JOptionPane windows, but they must return values or references to Objects rather than nothing at all, as is the case with void type methods.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_fehn*


Reputation:

Re: please write down the outputs of the following short program

Posted 07 March 2010 - 05:45 PM

View Postmacosxnerd101, on 07 March 2010 - 04:35 PM, said:

After I deleted the quotes and put them back, it worked. Here is your code with the fixed quotes. Note how the Strings in the println() statements are highlighted accordingly now as String literals:
class Sup { 
void who ( ) { 
System.out.println ("who() in Sup"); 
} 
} 
class Sub1 extends Sup { 
void who ( ) { 
System.out.println ("who() in Sub1"); 
} 
} 
class Sub2 extends Sup { 
void who ( ) { 
System.out.println ("who() in Sub2"); 
} 
} 
class SupSubApp { 
public static void main(String args[]){ 
Sub1 subOb1 = new Sub1(); 
Sub2 subOb2 = new Sub2(); 
subOb1.who(); 
subOb2.who(); 
} 
}


As for your question about void, this is not a trick question. When you invoke the who() method for those two objects, the code in the method executes and outputs. Even non-void type methods can still output to the console or display JOptionPane windows, but they must return values or references to Objects rather than nothing at all, as is the case with void type methods.

i guess the output would just be 2 names?
Was This Post Helpful? 0

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Re: please write down the outputs of the following short program

Posted 07 March 2010 - 05:47 PM

That doesn't seem right to me. Since you can now run the code, why don't you give it a try and see what comes out. :)
Was This Post Helpful? 1
  • +
  • -

#7 Guest_fehn*


Reputation:

Re: please write down the outputs of the following short program

Posted 07 March 2010 - 06:18 PM

View Postmacosxnerd101, on 07 March 2010 - 04:47 PM, said:

That doesn't seem right to me. Since you can now run the code, why don't you give it a try and see what comes out. :)

i dont have eclipse at home so i was using an online java compiler
Was This Post Helpful? 0

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: please write down the outputs of the following short program

Posted 07 March 2010 - 07:09 PM

View Postfehn, on 07 March 2010 - 07:18 PM, said:

View Postmacosxnerd101, on 07 March 2010 - 04:47 PM, said:

That doesn't seem right to me. Since you can now run the code, why don't you give it a try and see what comes out. :)

i dont have eclipse at home so i was using an online java compiler

So ?
You do not need an IDE to run a simple program
> javac Sup.java
> java Sup
Was This Post Helpful? 0
  • +
  • -

#9 Guest_fehn*


Reputation:

Re: please write down the outputs of the following short program

Posted 07 March 2010 - 07:28 PM

View Postpbl, on 07 March 2010 - 06:09 PM, said:

View Postfehn, on 07 March 2010 - 07:18 PM, said:

View Postmacosxnerd101, on 07 March 2010 - 04:47 PM, said:

That doesn't seem right to me. Since you can now run the code, why don't you give it a try and see what comes out. :)

i dont have eclipse at home so i was using an online java compiler

So ?
You do not need an IDE to run a simple program
> javac Sup.java
> java Sup

no it doesnt run on my command prompt at home either :(
Was This Post Helpful? 0

#10 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: please write down the outputs of the following short program

Posted 07 March 2010 - 07:29 PM

Do you have the JDK installed, and are in the correct directory when trying to compile?
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: please write down the outputs of the following short program

Posted 07 March 2010 - 07:33 PM

Then you need to either install the JDK or re-establish System Variables. Look at this video to learn to install. Note that the current version is Java 6

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1