Results 1 to 9 of 9
- 08-11-2011, 06:34 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
Can't understand textbooks question and there is no solution to view?
Hi everyone,
I'm new to the JAVA programming language and have recently begun teaching myself the fundamentals by following a book called Java: How To Program (7th edition) by Deitel. I'm at the end of Chapter 2 and I am stumped on a question that for some reason has no solution in the book. The earlier bits of the Chapter Review showed answers to the questions so I could at least compare.
Anyway here is the question -- could anyone tell me what it is asking for?
Write an application that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Write a program using the following techniques:
a) Use one System.out.println statement.
b) Use four System.out.print statement.
c) Use one System.out.printf statement.
Here are my answers ... but I am still unsure.
Java Code:a) System.out.println("12 34"); b) System.out.print("1"); System.out.print("2 "); System.out.print("3"); System.out.print("4"); c) System.out.printf("%d%d %d%d",1,2,3,4); Any help is appreciated, thanks.Last edited by metap0d; 08-11-2011 at 06:51 AM.
- 08-11-2011, 06:49 AM #2
a) and c) look ok but your answer for b) took 4 statements but were told to only use 1.
- 08-11-2011, 06:52 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
- 08-11-2011, 06:55 AM #4
Ok have you typed up the code, compiled and run it? Did you get the results you expected?
- 08-11-2011, 06:59 AM #5
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
The code has been typed and executed, that isn't the issue. I don't have any point of reference to tell whether or not I am correct. There is no answer/solution in the book so I do not know if I have understood the question?
- 08-11-2011, 07:06 AM #6
You know you are correct when the code produces the correct output.
- 08-11-2011, 02:27 PM #7
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
- 08-11-2011, 07:28 PM #8
your part B looks correct as well. I guess they were trying to get you to understand the difference between System.out.print and System.out.println
In part B you have: b) Use four System.out.print statement.
And that does produce "12 34". So it should be correct.Java Code:System.out.print("1"); System.out.print("2 "); System.out.print("3"); System.out.print("4");
If you have 2 System.out.println statements, 2 different lines are output to the console, whereas with the System.out.print in succession, you get the output on the same line.
Good job.Last edited by sehudson; 08-11-2011 at 07:30 PM.
- 08-11-2011, 08:23 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
List question, I don't understand why...
By jigglywiggly in forum New To JavaReplies: 14Last Post: 03-21-2010, 08:59 AM -
How enter this mode-gui/design-view/code-view
By lse123 in forum NetBeansReplies: 0Last Post: 02-28-2010, 09:09 PM -
Please I need the solution to this
By debobbt in forum New To JavaReplies: 4Last Post: 12-18-2009, 04:34 AM -
Updating a view using actions in a seperate view
By xcallmejudasx in forum EclipseReplies: 0Last Post: 10-24-2008, 09:24 PM -
Please need solution
By prithvi in forum New To JavaReplies: 4Last Post: 04-22-2008, 01:27 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks