Variable size array Size Array

Use user input to set lenght of an array size

Page 1 of 1

2 Replies - 7925 Views - Last Post: 03 September 2009 - 01:16 PM Rate Topic: -----

#1 B00024949  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-September 09

Variable size array Size Array

Post icon  Posted 03 September 2009 - 03:45 AM

I am receiving and error on the part of the code below trying to set up a GUI
the error
[error]
europound is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public class europound extends Applet implements ActionListener
[/error]
Should what is in the code so far not compile?

 
/Euro to Pound conversion CA

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

 public  class europound extends Applet implements ActionListener
{
		// Declare fields and Buttons

		TextField Count;
		TextField Price;
		Button	NextPrice;
		Button	  Convert;
		Button	Display;

		// Setup the GUI for teh user

		public void init()
		{
		//create first textfield
		Count = new TextField(4);
		add (Count   );

		// create Button to add count
		NextPrice = new Button ( "Next Price" );
		add(NextPrice);
		NextPrice.addActionListener ( this );

		Convert = new Button ( "Convert Price" );
		add(Convert);
		Convert.addActionListener ( this );

		Display = new Button ( "Conversion" );
				add(Convert);
		Display.addActionListener ( this );
				}




}



Thanks

Charles

Is This A Good Question/Topic? 0
  • +

Replies To: Variable size array Size Array

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Variable size array Size Array

Posted 03 September 2009 - 04:43 AM

you say your class implements actionListener
 public  class europound extends Applet implements ActionListener
{


but you don't have the method actionListener(), looks like
 public void actionPerformed(ActionEvent e) { 
			  //code that reacts to the action... 
		  }


Was This Post Helpful? 0
  • +
  • -

#3 B00024949  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-September 09

Re: Variable size array Size Array

Posted 03 September 2009 - 01:16 PM

View Posthorace, on 3 Sep, 2009 - 03:43 AM, said:

you say your class implements actionListener
 public  class europound extends Applet implements ActionListener
{


but you don't have the method actionListener(), looks like
 public void actionPerformed(ActionEvent e) { 
			  //code that reacts to the action... 
		  }



Thanks I have gotten a bit further now This it what it looks like now and the error is gone.
I see my assumption was correct.
Thanks again.
 
//Euro to Pound conversion CA

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class europound extends Applet implements ActionListener
{
		//Global var's
		Integer i =0;
		Integer len=0;
		Double price_arr[] = new Double[len];
		Double price_input2;

		// Declare fields and Buttons
		Label CountLabel;
		Label PriceLabel;

		TextField Count;
		TextField Price;
		Button	NextPrice;
		Button	  Convert;
		Button	Display;

		// Setup the GUI for teh user

		public void init()
		{
		//create first textfield
		CountLabel = new Label("Count");
		Count = new TextField(4);
		add(CountLabel);
		add (Count   );
		Count.setText("0");

		PriceLabel = new Label("Price");
		Price = new TextField(10);
		add(PriceLabel);
		add(Price);
		
		// create Button to add count
		NextPrice = new Button ( "Next Price" );
		add(NextPrice);
		NextPrice.addActionListener ( this );

		Convert = new Button ( "Convert Price" );
		add(Convert);
		Convert.addActionListener ( this );

		Display = new Button ( "Conversion" );
		add(Display);
		Display.addActionListener ( this );

		}
 	public void getArray(){
		//start a new array
		i=0;
		String len_string = Count.getText();
	 	len = Integer.parseInt(len_string);
		System.out.println(len);

		//Declare Price Array double setting its length to the size (len)
		 price_arr = new Double[len];

		//collect price from gui
		String price_input = Price.getText();
		price_input2=Double.parseDouble(price_input);
		price_arr[i] = price_input2;
		
		//check if index is at the end - if it is reset the index to the beginning.
		if (i < len)
		i++;
		if (i>= len)
		i=0;
		Price.setText("");


		//print out prices on console
		for (int i=0; i<len; i++)
		System.out.println(price_arr[i]);
	}
		
	public void setArray(){
		//check if index i is at end if it is, reset the index to the beginning
		if (i>= len){
			i=0;
			for (int i=0; i<len; i++)
			price_arr[i]= 0.0;
			}

		// input for the price array
		String price_input = Price.getText();
		price_input2=Double.parseDouble(price_input);
		price_arr[i] = price_input2;
		if (i < len)
		i++;
		Price.setText("");


		//print out prices on console
		for (int i=0; i<len; i++)
		System.out.println(price_arr[i]);
	}
		//Declare Pound Array double




		// Creat method to take send copy from  Price array
		// convert to pounds and send to  Pound array
		// For loop

		// Display price array and pound array
public void actionPerformed(ActionEvent e){
if (e.getSource() == NextPrice){
		if (len == Integer.parseInt(Count.getText()))
		setArray();
		else if (len ==  Integer.parseInt(Count.getText()) && price_input2 !=Double.parseDouble(Price.getText()))
		getArray();
		
		else if (len != Integer.parseInt(Count.getText()))
		getArray();
		}
if (e.getSource() == Convert){}

if (e.getSource() == Display){}

}
										



}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1