Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By Junky
  • 1 Post By sehudson

Thread: Can't understand textbooks question and there is no solution to view?

  1. #1
    metap0d is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Question 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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    a) and c) look ok but your answer for b) took 4 statements but were told to only use 1.

  3. #3
    metap0d is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    a) and c) look ok but your answer for b) took 4 statements but were told to only use 1.
    Sorry the book said use (4) statements for b) but I wrote one. So you interpret the question the same way I do?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Ok have you typed up the code, compiled and run it? Did you get the results you expected?

  5. #5
    metap0d is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    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?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You know you are correct when the code produces the correct output.
    DarrylBurke likes this.

  7. #7
    metap0d is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You know you are correct when the code produces the correct output.
    Yes but what I am saying in both the post and thread title is I don't know what the correct output is supposed to be.

  8. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    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.

    Java Code:
    System.out.print("1");
        System.out.print("2 ");
        System.out.print("3");
        System.out.print("4");
    And that does produce "12 34". So it should be correct.

    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.
    JeffGrigg likes this.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Junky View Post
    You know you are correct when the code produces the correct output.
    Don't be so existentialistic.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. List question, I don't understand why...
    By jigglywiggly in forum New To Java
    Replies: 14
    Last Post: 03-21-2010, 08:59 AM
  2. How enter this mode-gui/design-view/code-view
    By lse123 in forum NetBeans
    Replies: 0
    Last Post: 02-28-2010, 09:09 PM
  3. Please I need the solution to this
    By debobbt in forum New To Java
    Replies: 4
    Last Post: 12-18-2009, 04:34 AM
  4. Updating a view using actions in a seperate view
    By xcallmejudasx in forum Eclipse
    Replies: 0
    Last Post: 10-24-2008, 09:24 PM
  5. Please need solution
    By prithvi in forum New To Java
    Replies: 4
    Last Post: 04-22-2008, 01:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •