Velocity Reviews > Java > Java homework, book lacking information

Java homework, book lacking information

dlittlebear@gmail.com
Guest
Posts: n/a

 08-05-2007
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?

Mark Space
Guest
Posts: n/a

 08-05-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

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.

Patricia Shanahan
Guest
Posts: n/a

 08-05-2007
(E-Mail Removed) 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.

homeworks use things that were discussed recently in class.

Patricia

dlittlebear@gmail.com
Guest
Posts: n/a

 08-05-2007
On Aug 5, 12:29 am, Patricia Shanahan <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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.

>
> 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 =(

Hal Rosser
Guest
Posts: n/a

 08-05-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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.

Roedy Green
Guest
Posts: n/a

 08-05-2007
On Sun, 05 Aug 2007 03:52:08 -0000, (E-Mail Removed) 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
--
The Java Glossary
http://mindprod.com