problem in java question

R

roohbir

I am relatively new to the java programming language and at the moment
am trying Java How to Program by Deitel. There is a question in the
book in the Methods chapter which asks:

Use Math.random to produce two positive one-digit integers. The program
should then display a question in the status bar, such as "How much is
6 times 7?"
The student then types the answer into the JTextField. Next, the
program checks the student's answer. If it is correct, draw the string
"Very Good!" on the applet and ask another multiplication question. If
the answer is wrong, draw the string "No, Please try again." on the
applet and let the student try the same question repeatedly until the
student finally gets it right. A seperate method should be used to
generate each new question. This method should be called once when the
applet begins execution and each time the user answers the question
correctly. All drawing on the applet should be performed by the paint
method.

I have a rough version which I have copy-pasted below. As a first step,
I shall be very thankful if somebody could guide me how to get the
question and the text field at the same time when I run the code. I
shall work on the rest of the code after getting that into my head!

Also if there is any fundamental problem with the code, please point it
out.

The code:

// Question 6.31 Author: Roohbir Singh MBCS
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class KidsEducation extends JApplet implements ActionListener
{
final int SIZE = 2;
JTextField inputField;
JLabel prompt;

// set up GUI components
public void init()
{
prompt = new JLabel( "Enter answer:" );
inputField = new JTextField( SIZE );
inputField.addActionListener( this );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( prompt );
container.add( inputField );

} // end method init

public int question()
{
int value1, value2;

// pick random integer between 1 and 9
value1 = 1 + ( int ) ( Math.random() * 9 );
value2 = 1 + ( int ) ( Math.random() * 9 );
showStatus("How much is " + x + " times " + y + " ?");

}

// call method question if user input is within range
public void actionPerformed( ActionEvent actionEvent )
{
int inputNumber = Integer.parseInt( inputField.getText() );
question();
if ( inputNumber == 0 )
showStatus( "Point is: " );

} // end method actionPerformed

} // end class KidsEducation
 
A

Andrew Thompson

roohbir said:
I am relatively new to the java programming language and at the moment
am trying Java How to Program by Deitel.

There are aspects to the code you posted that
lead me to think you should dump that book
immediately.

Does it really attempt to teach about methods, from within an..
....
public class KidsEducation extends JApplet implements ActionListener

...applet?!

'methods' are something that should be covered while
the learner is still doing command line applications.

Andrew T.
 
A

Andrew Thompson

roohbir wrote:

Oh, but what I forogt to add was that ..

Sub: problem in java question

...is a very poor title for a post.

Most 1st posts here, are due to a problem that a
developer encounters while writing a Java application.
If there is a problem, there will almost certainly be
a question. Further, if it is *not* about a Java based
application, it is off topic.

So.. presuming Java and guessing there is a problem
that will lead to a question, leaves the only active word
of the title as ..'is'.

I suggest that 'How to get input?' might be one
example of a better title for this thread.

If you title threads more appropriately, they are more
likely to attract the attention of the people who are
most interested/knowledgable in that area.

Andrew T.
 
O

Oliver Wong

[snip description of program: applet which does multiplication]
I have a rough version which I have copy-pasted below. As a first step,
I shall be very thankful if somebody could guide me how to get the
question and the text field at the same time when I run the code. I
shall work on the rest of the code after getting that into my head! [snip most of the code]
public int question()
{
int value1, value2;

// pick random integer between 1 and 9
value1 = 1 + ( int ) ( Math.random() * 9 );
value2 = 1 + ( int ) ( Math.random() * 9 );
showStatus("How much is " + x + " times " + y + " ?");

}

I didn't really understand your implied question, "how to get the
question and the text field at the same time", so I tried to run your
program, but was faced with a compilation error: "x" and "y" are not
defined. Perhaps you meant value1 and value2?

Also, I don't think you're implementing the program the way the book
intended. The book says you're supposed to display the question to the user
using the paint() method, but you seem to be using the showStatus() method
instead. I agree with Andrew that it's very strange that the book would
expect you to understand how to override the paint method and how the applet
life cycle works when you haven't even learned how to use methods yet.

Consider learning Java using the tutorial at
http://java.sun.com/docs/books/tutorial/

- Oliver
 
R

roohbir

Thanks for the reply Andrew. Much appreciated. Can you recommend any
book that teaches Java the way you are saying. I tried the Java
Tutorial but it is not very beginner-friendly. I have 2 other books -
Wu's OOP introduction and Java for students by Bell & Parr and both use
Applets. I didn't see a single program which ran on command line.
So if you know of a book, please let me know.
Cheers
Roohbir
 
A

Andrew Thompson

roohbir said:
Thanks for the reply Andrew. Much appreciated.

You are welcome. But the best way to express
that appreciation is to not top-post to replies.
...Can you recommend any
book that teaches Java the way you are saying. I tried the Java
Tutorial but it is not very beginner-friendly.

I am no expert on books, and relied heavily on
the Java tutorial in my early learning . As such,
I think there are many other folks here that might
be able to make better (or *any*) book recommendations.

For the folks who just tuned in, I was criticising
books that jumped directly into GUI based
applications, and *especially* applets.

Andrew T.
 
R

roohbir

Andrew said:
You are welcome. But the best way to express
that appreciation is to not top-post to replies.

Sorry. I'll try to post properly in future.
I am no expert on books, and relied heavily on
the Java tutorial in my early learning . As such,
I think there are many other folks here that might
be able to make better (or *any*) book recommendations.

For the folks who just tuned in, I was criticising
books that jumped directly into GUI based
applications, and *especially* applets.

Actually, a lot of books are being marketed on the notion that studying
GUI based applications directly, and specifically applets, is the best
way to learn Java. So when you mentioned in the post about
command-line, even I was surprised.
Now I'll refer to the Sun's tutorial as much as possible. Though I
shall stick to this book for a while! Have become attached to it !!

Thanks
Roohbir
 
R

roohbir

Andrew said:
..is a very poor title for a post.

Most 1st posts here, are due to a problem that a
developer encounters while writing a Java application.
If there is a problem, there will almost certainly be
a question. Further, if it is *not* about a Java based
application, it is off topic.

So.. presuming Java and guessing there is a problem
that will lead to a question, leaves the only active word
of the title as ..'is'.

I suggest that 'How to get input?' might be one
example of a better title for this thread.

If you title threads more appropriately, they are more
likely to attract the attention of the people who are
most interested/knowledgable in that area.

Even I felt that the title for the thread was not appropriate. Thanks
for pointing it out. Shall be careful in future.
Roohbir
 
R

roohbir

Thanks for the reply Oliver.
I didn't really understand your implied question, "how to get the
question and the text field at the same time", so I tried to run your
program, but was faced with a compilation error: "x" and "y" are not
defined. Perhaps you meant value1 and value2?

It was value1 and value2. I corrected it.
Also, I don't think you're implementing the program the way the book
intended. The book says you're supposed to display the question to the user
using the paint() method, but you seem to be using the showStatus() method
instead.

The guidelines of the exercise state that the Question appears in the
status bar and after studying the question the student types the answer
and then the program check if the answer is correct.
Consider learning Java using the tutorial at
http://java.sun.com/docs/books/tutorial/

I have started consulting the Sun Tutorial and it is a great help. But
as I mentioned earlier, it is not very beginner-friendly; well, at
least I didn't think so. But after studying a few chapters, the
tutorial is starting to make sense.

Thanks for the advice.
Roohbir
 
A

Andrew Thompson

roohbir wrote:
....
Actually, a lot of books are being marketed on the notion that studying
GUI based applications directly, and specifically applets, is the best
way to learn Java.

Actually, that closely reflects what I was seeing
the last time I had cause to look for Java books**.
It is a pity that is *still* the case.

I think the author's believe that helps keep the
student's interest, & the reason they push applets
is the same basic reasons Sun's examples also
(quite irritatingly) focus on applets.
1) 'everybody has a Java enabled browser'*
so the student can put their applet in a
web-page to boast to their friends.
2) Applets provide handy little convenience
methods for dealing with sounds, and images.
3) Applets have an 'inbuilt' message area
available through the 'showStatus(String)' -
which saves a little GUI coding.

* And no, 'everybody' does not.

** OTOH - even then I was able to find a couple of
books that I liked, a good example of which was.
"The Complete Reference: Java 2 (Third Edition)'
by Naughton/Schildt. (now quite old)

The chapter titles are..
1. Genesis of Java
2. Overview..
3. Data Types..
4. Operators
.....
10. Exception handling
11. Multi-threaded programming
12. I/O Applets and Other Topics.

...OK. It *still* goes for those (damnable) Applets,
but they are mentioned almost as an afterthought..
'Oh, and you can also wrap a cute GUI around this stuff'..

It is only chapter 19 before applets are revisited,
until chapter 21, where the AWT in general is
covered.

AFAIR - I skimmed the parts on applets and only
came back to them after I'd studied AWT.
...So when you mentioned in the post about
command-line, even I was surprised.

My opinions were not reflected in the majority
of books I found at the time. (Do not be surprised).
Now I'll refer to the Sun's tutorial as much as possible.

Not a bad idea, if you can make it work for you.
...Though I
shall stick to this book for a while! Have become attached to it !!

I understand the sentiment. It may have other
very good aspects, and having a number of
sources of information, is very useful.

Andrew T.
 
O

Oliver Wong

roohbir said:
Thanks for the reply Andrew. Much appreciated. Can you recommend any
book that teaches Java the way you are saying. I tried the Java
Tutorial but it is not very beginner-friendly. I have 2 other books -
Wu's OOP introduction and Java for students by Bell & Parr and both use
Applets. I didn't see a single program which ran on command line.
So if you know of a book, please let me know.

I haven't used this book myself, but a lot of people said good things
about "Thinking in Java" by Bruce Eckel, and it's available for free
(legally) at http://www.mindview.net/Books/TIJ/

- Oliver
 
O

Oliver Wong

roohbir said:
Thanks for the reply Oliver.


It was value1 and value2. I corrected it.

Okay, well the next problem it should show is that you're not returning
anything from a method which is declared to throw an int.
The guidelines of the exercise state that the Question appears in the
status bar and after studying the question the student types the answer
and then the program check if the answer is correct.

This seems to contradict the earlier instructions, but if that's what
the book says, I guess there isn't much we can do about it... I have a
solution in mind which I think complies with all of the book's instructions,
but it might be using techniques you haven't covered yet. Have you studied
objects, and private fields yet? And do you know the lifecycle of an applet?
initialized, started, stopped, destroyed? Do you know when the paint method
gets called in this lifecycle?

- Oliver
 
M

Martin Gregorie

Andrew said:
I am no expert on books, and relied heavily on
the Java tutorial in my early learning . As such,
I think there are many other folks here that might
be able to make better (or *any*) book recommendations.
I taught myself with Ivor Horton's "Beginning Java". It only uses GUI
examples when its illustrating GUI-related concepts.

As an added bonus, it assumes you'll learn with a minimal set of free
software: the Sun J2SE SDK, a command line console window and your
favorite programming source editor.

My main criticism is that it doesn't suggest that you download the Sun
documentation set at the same time as J2SE: an odd oversight seeing that
having the documentation set installed is near essential.
 
A

Andrew Thompson

Martin Gregorie wrote:
....
(sounds like a great book!)
My main criticism is that it doesn't suggest that you download the Sun
documentation set at the same time as J2SE: an odd oversight seeing that
having the documentation set installed is near essential.

(dismissive) Puhh.. Maybe they figured if
you were be smart enough to ..
- buy the book that works through Java
from the command line,
- download and install the SDK, and
- load a text editor and a command line,
...they didn't have to mention such 'no brainers' like..
- turn on your computer.
- download and install the description of
the Java core API (the JavaDocs)...

;-)

Andrew T.
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top