Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Simple Problem

Thread view: 
Sonia - 09 Apr 2004 21:27 GMT
Hello guys,
Need help with the following problem.  This is ex 6.31 from Deitel and
Deitel - Java How to Program 4/e [ Not a homework problem , I promise]
This is the problem:
Create 2 integers and display the following question in the status bar:
How much is INT1 times INT2.
Then user types in the answer in the JTextField and the program responds
wither CORRECT or INCORRECT depending  if the answer is right or wrong.  It
says that all the drawing on the applet should be performed by the "paint"
method.
The problem that I am having is when I add the paint method, it overwrites
or overshadows the JTextField that I've created. - it is there but I cannot
see it.
How would I go about taking care of this. I would appreciate any comments
that could help me with this.

I wrote this code:
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;

public class Ex0631a extends JApplet implements ActionListener
{
JLabel answerLabel;
JTextField answerField;

public void init()
{
Container c = getContentPane();
c.setLayout (new FlowLayout());

answerLabel = new JLabel("Answer");
answerField = new JTextField(20);
answerField.setEditable(true);
answerField.addActionListener(this);

c.add(answerLabel);
c.add(answerField);

} //end init method

 public int generateNumbers()
{
 int x=0;
 x = 1 + (int)(Math.random() * 10);
 return x;

} //end generateNumbers

public void actionPerformed(ActionEvent e)
{
 String correct = new String("Correct ");
 int x = generateNumbers();
 int y = generateNumbers();
 showStatus("How much is " + x + " times " + y);

 int answer;
 answer = Integer.parseInt(answerField.getText());
 int product = x*y;
 if (product == answer){
  showStatus("Correct");
 }
 else
  showStatus("Incorrect");
} //end actionPerformed method

public void paint(Graphics g) {
} // end of paint method
} //end Ex0631a class
Andrew Thompson - 10 Apr 2004 00:35 GMT
> Need help with the following problem.  This is ex 6.31 from Deitel and
> Deitel - Java How to Program 4/e [ Not a homework problem , I promise]
> This is the problem:
..
> ...It
> says that all the drawing on the applet
> should be performed by the "paint" method.
> The problem that I am having is when I add the paint method, it overwrites
> or overshadows the JTextField that I've created. - it is there but I cannot
> see it.

Are you _sure_ that this book
is telling you to both.
a) Use Swing
b) Override 'paint()'?

That is not right AFAIU, in Swing
you override paintComponent() instead.

Not that either applets or overriding
paint/paintComponent are good things
to be doing at the first stages of
learning Java.

For a working eg of rendering in Swing, check this,
<http://www.physci.org/launcher.jsp#JAnimateFrame>
, else AWT..
<http://www.physci.org/launcher.jsp#AnimateFrame>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Alex Hunsley - 10 Apr 2004 02:32 GMT
>>Need help with the following problem.  This is ex 6.31 from Deitel and
>>Deitel - Java How to Program 4/e [ Not a homework problem , I promise]
[quoted text clipped - 21 lines]
> to be doing at the first stages of
> learning Java.

I agree with Andrew here... Sonia, I think perhaps you've misunderstood
the problem. From the sound of the problem, there is no reason at all
the be overriding paint! Maybe you've misunderstood the book? Yes, all
painting is handled by the paint method, which is turn calls things like
paintChildren and paintComponent, but wasn't it just telling you this
for info? I don't think it meant that you were to override paint...
And you can do what you're attempting by not touching paint at all!

(Btw, if you absolutely have to override paint.. the reason it's not
working the way you've done it is that your paint method is empty, and
by empty I mean it doesn't even call super.paint(g).

If you change your paint method to:

 public void paint(Graphics g) {
    super.paint(g);
 } // end of paint method
} //end Ex0631a class

... you'll find that it now paints things correctly. However, if your
paint method looks like this, it's no different to not overriding paint
at all, so don't bother!)

Btw, in your actionPerformed method, you're setting the status bar to
ask the question. Surely you want your applet to be setting the status
to this text much earlier?

If you can be more specific/exact about the  question asked by the book,
I can understand things better. For instance, there is no "submit"
button to press once you've typed your answer. Do they expect you to
just hit return after you've typed your answer? And so on....

alex
Sonia - 10 Apr 2004 03:06 GMT
> >>Need help with the following problem.  This is ex 6.31 from Deitel and
> >>Deitel - Java How to Program 4/e [ Not a homework problem , I promise]
[quoted text clipped - 55 lines]
>
> alex

Guys,
Here is the extact question from the book:
6.31) Computers are playing an increasing role in education. Write a program
that will help an elementary school student learn multiplication. 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 a 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 again repeatedly until the student finally
gets it right. A separate 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 know how to solve the problem without the pain method, but the question
askes for it, so i;ve tried but failed to do so.  If you can give me
pointers that would be great.  Then I will worry about the rest of the
program.

Thanks Guys
Andrew Thompson - 10 Apr 2004 04:39 GMT
> The student then types the answer into a JTextField. Next, the program
> checks the student’s answer. If it is correct, draw the string “Very good!”
[quoted text clipped - 10 lines]
> pointers that would be great.  Then I will worry about the rest of the
> program.

My newsreader is playing up, so I'll make this quick.

The program description indicates a
bad book, get rid of it.

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Casey Hawthorne - 10 Apr 2004 07:47 GMT
There have been some very negative and some positive comments/reviews
of the book "Deitel and Deitel - Java How to Program" on the
amazon.com website.

>http://www.amazon.com/exec/obidos/ASIN/0131016210/qid=1081579507/sr=2-1/ref=sr_2
_1/104-8207624-7736758

you might try instead

"Core JAVA - Volume 1 - Fundamentals" by Cay S. Horstmann, Gary
Cornell

"JAVA for Programmers" by Douglas A. Lyon

Regards,
Casey
Ryan Stewart - 10 Apr 2004 12:30 GMT
> There have been some very negative and some positive comments/reviews
> of the book "Deitel and Deitel - Java How to Program" on the
[quoted text clipped - 11 lines]
> Regards,
> Casey

I tried a Deitel and Deitel book for Visual Basic long ago. It was a very
short time before I gave up on it and got a real book. Since then I've
glanced through a few others (including the Java one) and have seen nothing
that would give me any desire to use them. To the list above, I'd add "Java
2 Primer Plus" from Sams Publishing. I only remember the authors' first
names: Stephen and Steven. Hard to forget.
Alex Hunsley - 10 Apr 2004 13:33 GMT
>>There have been some very negative and some positive comments/reviews
>>of the book "Deitel and Deitel - Java How to Program" on the
[quoted text clipped - 20 lines]
> 2 Primer Plus" from Sams Publishing. I only remember the authors' first
> names: Stephen and Steven. Hard to forget.

What is it with Deitel and Deitel, anyway? How do they manage to publish
so many utterly crap books? Are all their authors totally clueless?
Shouldn't the people at the top know if authors are any good or not?

I wonder how most of these books get done anyway - does the publisher
decide to commision one, or do people just suggest an idea to the
publisher, who then inidcates interest or not?

lex
Alex Hunsley - 11 Apr 2004 02:29 GMT
>>> There have been some very negative and some positive comments/reviews
>>> of the book "Deitel and Deitel - Java How to Program" on the
[quoted text clipped - 32 lines]
>
> lex

Oops - to answer myself here - Deitel and Deitel write the books
themselves, of course.

Their website has such gems as:

>Top corporations pay Harvey and Paul Deitel thousands of dollars a day
>to train programmers in Visual Basic, C, C++, Java and object-oriented
>programming techniques. And they're worth every penny.

This blatant bragging wouldn't be so bad if it there *were* worth every
penny, but given they state of the books they publish, I have my doubts...!

>Dr. Harvey Deitel is one of the world's leading computer science
>instructors and seminar presenters, and author of more than a dozen
>books.

Quantity not quality, obviously.
Sonia - 10 Apr 2004 16:19 GMT
.
> There have been some very negative and some positive comments/reviews
> of the book "Deitel and Deitel - Java How to Program" on the
[quoted text clipped - 11 lines]
> Regards,
> Casey

Thanks Casey, I will definitively give it a try.
Alex Hunsley - 10 Apr 2004 11:49 GMT
>>>>Need help with the following problem.  This is ex 6.31 from Deitel and
>>>>Deitel - Java How to Program 4/e [ Not a homework problem , I promise]
[quoted text clipped - 87 lines]
>
> Thanks Guys

Hi Sonia
I have no experience with this books, but it sounds very bad! The fact
that it says "All drawing on the applet should be performed by the paint
method" at the end is completely irrelevant (although true) and
misleading. Also I notice it gives you no hint as to maybe using a
button to submit the answer; and I must say that the status line is
*not* a good place for normal user interaction to take place; that
should happen in the applet itself.

I would find a different book, as this one sounds like it's been written
by someone who doesn't know what they're talking about!

Try "Thinking in java" perhaps? It's free to download and it's very good:

http://mindview.net/Books/TIJ/DownloadSites

alex
Sonia - 10 Apr 2004 16:31 GMT
> >>>>Need help with the following problem.  This is ex 6.31 from Deitel and
> >>>>Deitel - Java How to Program 4/e [ Not a homework problem , I promise]
[quoted text clipped - 73 lines]
> > The student then types the answer into a 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
[quoted text clipped - 28 lines]
>
> alex


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.