Java homework, book lacking information

D

dlittlebear

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?
 
M

Mark Space

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

Patricia Shanahan

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
 
D

dlittlebear

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

Hal Rosser

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top