Java homework, book lacking information

Discussion in 'Java' started by dlittlebear@gmail.com, Aug 5, 2007.

  1. Guest

    I just reciently started a Java programming class, in our second week
    the book is lacking for telling us how to do a problem. I will write
    the problem out then say where I need help at the moment, the rest I
    think I have figured out though.

    Problem: Tell if a ticket number is valid

    The class displays an input dialog box that promts a ticket agent to
    enter a six-digit ticket number. Ticket numbers are designed so that
    if you drop the last digit of the number, the divide the number by 7,
    the remainder of the division will be identical to the last droped
    digit. This process is illustrated in the following example:

    Step 1 Enter the ticket number; for example, 123454
    Step 2 Remove the last digit, leaving 12345.
    Step 3 Determine the remainder when the ticket numer is divided by
    8. In this case 12345 divided by 7 leaves a remainder of 4.

    Step 4 Assign the boolean value of the comparision between the
    remainder and the digit droped from the ticket number.

    Step 5 Display the result -- true or false -- in a message box


    Test 123454 should be true
    test 147103 should be true
    test 154123 should be false.


    I am stuck at step 2, how do I go about droping that last digit, or
    putting it in a variable to match it to something later.

    Here is what I have so far to get the first 6 digits.


    import javax.swing.JOptionPane;

    public class Traveltickets
    {
    public static void main (String[] args)
    {
    String travelString;
    int travel;

    travelString = JOptionPane.showInputDialog(null, "Enter The ticket
    number.", "Tickets Dialog 1", JOptionPane.INFORMATION_MESSAGE);
    travel = Integer.parseInt(travelString);
    JOptionPane.showMessageDialog(null, travel);

    System.exit(0);
    }

    }


    Also there is supposed to be only an input of 6 characters, is there a
    way to limit the field to only allow 6?
     
    , Aug 5, 2007
    #1
    1. Advertising

  2. Mark Space Guest

    wrote:

    > Also there is supposed to be only an input of 6 characters, is there a
    > way to limit the field to only allow 6?
    >


    Hmm, I'm actually not sure. However, I'm pretty sure that only
    accepting 6 characters, then prompting for the 7th, would not fullfill
    your homework requirements.

    First, do you know how to convert a string to an integer (int)?

    Second, a hint: from your example above, what value do you get when you
    take the integer 123454 and divide it by integer 10?

    Try it and see. You're stuck on a math problem. You'll need to figure
    out algorithms like this when you write your programs. This one is
    pretty easy.
     
    Mark Space, Aug 5, 2007
    #2
    1. Advertising

  3. wrote:
    > I just reciently started a Java programming class, in our second week
    > the book is lacking for telling us how to do a problem. I will write
    > the problem out then say where I need help at the moment, the rest I
    > think I have figured out though.


    In addition to Mark's comments, reread your lecture notes. Often,
    homeworks use things that were discussed recently in class.

    Patricia
     
    Patricia Shanahan, Aug 5, 2007
    #3
  4. Guest

    On Aug 5, 12:29 am, Patricia Shanahan <> wrote:
    > wrote:
    > > I just reciently started a Java programming class, in our second week
    > > the book is lacking for telling us how to do a problem. I will write
    > > the problem out then say where I need help at the moment, the rest I
    > > think I have figured out though.

    >
    > In addition to Mark's comments, reread your lecture notes. Often,
    > homeworks use things that were discussed recently in class.
    >
    > Patricia


    I am actually trying to get ahead in class, as they do not start for
    another 2 weeks but during class time I have to take a lot of time out
    due to work, so just trying not to fall behind at his point. Oh, and
    they are online classes =(
     
    , Aug 5, 2007
    #4
  5. Hal Rosser Guest

    <> wrote in message
    news:...
    >I just reciently started a Java programming class, in our second week
    > the book is lacking for telling us how to do a problem. I will write
    > the problem out then say where I need help at the moment, the rest I
    > think I have figured out though.
    >
    > Problem: Tell if a ticket number is valid
    >
    > The class displays an input dialog box that promts a ticket agent to
    > enter a six-digit ticket number. Ticket numbers are designed so that
    > if you drop the last digit of the number, the divide the number by 7,
    > the remainder of the division will be identical to the last droped
    > digit. This process is illustrated in the following example:
    >
    > Step 1 Enter the ticket number; for example, 123454
    > Step 2 Remove the last digit, leaving 12345.
    > Step 3 Determine the remainder when the ticket numer is divided by
    > 8. In this case 12345 divided by 7 leaves a remainder of 4.
    >
    > Step 4 Assign the boolean value of the comparision between the
    > remainder and the digit droped from the ticket number.
    >
    > Step 5 Display the result -- true or false -- in a message box
    >
    >
    > Test 123454 should be true
    > test 147103 should be true
    > test 154123 should be false.
    >
    >
    > I am stuck at step 2, how do I go about droping that last digit, or
    > putting it in a variable to match it to something later.


    Take a look at the API - look up the String class and note the properties
    and methods.
    Note that the input dialog box will present its contents as a String object.
    Use methods of the String class to get the number of chars in it, and to get
    substrings from the input.
    Then look at the "%" operator.
    You may have all the pieces of the puzzle without knowing it.
     
    Hal Rosser, Aug 5, 2007
    #5
  6. Roedy Green Guest

    On Sun, 05 Aug 2007 03:52:08 -0000, wrote
    quoted or indirectly quoted someone who said :

    >Step 1 Enter the ticket number; for example, 123454


    You have a String and need an int.
    See http://mindprod.com/applet/converter.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Aug 5, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    696
  2. Anne Wangnick
    Replies:
    1
    Views:
    314
    Peter Hansen
    Aug 31, 2004
  3. James Stroud

    Curses sorely lacking an event loop?

    James Stroud, Feb 27, 2007, in forum: Python
    Replies:
    4
    Views:
    523
    James Stroud
    Feb 28, 2007
  4. castironpi

    lacking follow-through

    castironpi, Sep 7, 2008, in forum: Python
    Replies:
    30
    Views:
    785
    Paul Boddie
    Sep 13, 2008
  5. Alf P. Steinbach /Usenet
    Replies:
    1
    Views:
    366
    Sazzad Kamal
    Aug 29, 2010
Loading...

Share This Page