Quantcast

Maximum PC

It is currently Tue Jul 22, 2014 4:18 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Stupid Easy Java question
PostPosted: Sun Apr 17, 2005 5:48 pm 
8086
8086

Joined: Sun Apr 10, 2005 2:58 pm
Posts: 22
Location: Chicago
OK I'm just starting to learn Java and I'm stuck on probably a very simple question. The questions is:

Given integer variable i and a floating point variable f , write a statement that writes the values of i and f to standard output in the format:

i = value-of-i f = value-of-f

Hopefully this is enough info

Any help?

Thanks :D


Top
  Profile  
 
 Post subject: Re: Stupid Easy Java question
PostPosted: Sun Apr 17, 2005 8:31 pm 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
Brian B wrote:
OK I'm just starting to learn Java and I'm stuck on probably a very simple question. The questions is:

Given integer variable i and a floating point variable f , write a statement that writes the values of i and f to standard output in the format:

i = value-of-i f = value-of-f

Hopefully this is enough info

Any help?

Thanks :D


Are you trying to send the values in the variables to the Console? If so, check out how to output to Console/Standard out. No one here will give you the answer, your best bet is to figure it out for yourself, write code, post code if you're having trouble and let us critique it - this way, you'll know what's going on and we don't get stuck doing your homework.


Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 17, 2005 9:05 pm 
8086
8086

Joined: Sun Apr 10, 2005 2:58 pm
Posts: 22
Location: Chicago
I'm getting errors when I put this:


System.out.println(" i = f = ");


Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 5:59 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
Brian B wrote:
I'm getting errors when I put this:


System.out.println(" i = f = ");


Two things:

1) That won't print the values of i and j, it will print "i=j=" since you encapsulated it the statement into a string.

2) Instead of using float, use double instead. The only difference between float and double is precision - float is 32-bits, double is 64-bits. Sun recommends using double instead of float and most compilers, IIRC, will enforce that.

BTW, post your code so we can see what it is you're trying to do exactly.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 19, 2005 6:56 pm 
8086
8086

Joined: Sun Apr 10, 2005 2:58 pm
Posts: 22
Location: Chicago
Well I got the answer...here it is

System.out.print("i="+i+" f="+f);
System.out.println();

thanks for the help


Top
  Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 11:37 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Brian B wrote:
Well I got the answer...here it is

System.out.print("i="+i+" f="+f);
System.out.println();

thanks for the help

Don't just 'I got the answer'... understand why that works.

You should be able to answer the following questions:

1. What is System? (A class, package, or something else?)
2. If a class, in what package is System located?
3. What is System.out, what args does it take, and what does it return?
4. What other methods besides println are available?
5. What types of variables are you passing to sout (for short), are any of them being caste? If so, how and why?
6. How could you have reduced the output from two lines to one line of code. And why would you want to? Why would you not want to?
7. Is the String literal mutable or immutable? How does the jvm handle String literals? Can you modify an immutable object?
8. How many different string concantenations does your main output line perform?
9. If it were in a loop iterating to say 1000, what should you do instead? And why? What is the relative cost in performance (both cpu and memory) of sout versus string concatenation versus string buffers.

And I won't even get started on unicode, locales, etc. Understanding is very (very, very, very) important - it is what seperates those who excel in the industry (and academia) from those who never get a job or just get by. It happens all the time - people who have been programming for 5+ years that take a serious spanking with the clue-by-four because they don't understand anything.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 5:55 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
Gadget wrote:
Brian B wrote:
Well I got the answer...here it is

System.out.print("i="+i+" f="+f);
System.out.println();

thanks for the help

Don't just 'I got the answer'... understand why that works.

You should be able to answer the following questions:

1. What is System? (A class, package, or something else?)
2. If a class, in what package is System located?
3. What is System.out, what args does it take, and what does it return?
4. What other methods besides println are available?
5. What types of variables are you passing to sout (for short), are any of them being caste? If so, how and why?
6. How could you have reduced the output from two lines to one line of code. And why would you want to? Why would you not want to?
7. Is the String literal mutable or immutable? How does the jvm handle String literals? Can you modify an immutable object?
8. How many different string concantenations does your main output line perform?
9. If it were in a loop iterating to say 1000, what should you do instead? And why? What is the relative cost in performance (both cpu and memory) of sout versus string concatenation versus string buffers.

And I won't even get started on unicode, locales, etc. Understanding is very (very, very, very) important - it is what seperates those who excel in the industry (and academia) from those who never get a job or just get by. It happens all the time - people who have been programming for 5+ years that take a serious spanking with the clue-by-four because they don't understand anything.



Good post. I should print this out and give it to other students I tutor. Knowing how to code and knowing what makes the code work is the difference between those who excel and those who don't. Fortunately enough, Reference books exists so questions like yours are easily answered as well. :P


Top
  Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 8:17 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
DJSPIN80 wrote:
Good post. I should print this out and give it to other students I tutor. Knowing how to code and knowing what makes the code work is the difference between those who excel and those who don't. Fortunately enough, Reference books exists so questions like yours are easily answered as well. :P

Watch it - I copywrite all my posts. =)


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group