The Artima Developer Community
Sponsored Link

Java Answers Forum
Java noob

2 replies on 1 page. Most recent reply: Nov 10, 2006 12:05 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
santa laws

Posts: 1
Nickname: santalaws
Registered: Nov, 2006

Java noob Posted: Nov 9, 2006 7:20 PM
Reply to this message Reply
Advertisement
Build a GUI to accept integers (when the ENTER key is depressed) into an arr
ay of 15 int; have a button called AVERAGE; when AVERAGE is selected, there
is a display of the count and the current average; have a button called CLEA
R; when CLEAR is selected, count is set to 0 and so are all the array elemen
ts and then there is another display: count is 0 and average is 0.0.

package argui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ArrayGUIPanel extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel, countLabel, averageL
abel;

private JTextField nums;
private JButton average, clear;
private int count;
private double avg;

//-----------------------------------------------------------------
// Constructor: Sets up the main GUI components.
//-----------------------------------------------------------------
public ArrayGUIPanel()
{

count = 0;
avg=0.0;


inputLabel = new JLabel ("Enter integers: " );
resultLabel = new JLabel ("---");
countLabel = new JLabel ("Count: " + count);
averageLabel = new JLabel ("Average: " + avg);






average = new JButton ("AVERAGE");
clear = new JButton ("CLEAR");

AvgListener listener = new AvgListener();
average.addActionListener (listener);
clear.addActionListener(listener);

add (inputLabel);
add (average);
add (clear);
add (resultLabel);
add (countLabel);
add (averageLabel);

setPreferredSize (new Dimension(300, 75));
setBackground (Color.white);


}

am i going right direction here?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Java noob Posted: Nov 9, 2006 11:19 PM
Reply to this message Reply
Looks good to me.
You didn't use any Layout Manager, but that's no error.

Hint: If you'r using Java 1.5 you may consider to use java.util.ArrayList<Integer> instead of int[] to store your numbers.
There are two advantages:
1. You can drop the counter. Use list.size() for the number of entries
2. You can enter as much values as you want

The disadvantage is that you can't create ArrayList<int>, but must use ArrayList<Integer> instead. That means, that you add a new entry like this:
list.add(15);//using Java1.5's autoboxing
or
list.add(new Integer(15));//that's how the autoboxing feuture changes your code
and get the value like this:
list.get(2).intValue();


Hint: Make surea that you cast your sum from int to double before dividing.


Hint: You can also use a JFormattedTextField instead of JTextField . This way you can make sure the user types a number and not text.
If you use a JTextfield you have to parse the entry, catch the NumerFormatException ...
Using the JTextfield you can set it to accept only numbers and to ignore everything else (returns to it's previous value).


Hint: If you're using the ArrayList instead of the array, you can use the for-each feature of Java 1.5. That's a cool feature which makes code much more easier to read. It would also work for a array, but would ainclude all entries from that array, no matter how many are actually valid.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Java noob Posted: Nov 10, 2006 12:05 AM
Reply to this message Reply
And one more hint: use the Java tags when posting code to the forum!

Flat View: This topic has 2 replies on 1 page
Topic: Java noob Previous Topic   Next Topic Topic: LDAP

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use