The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

How to MAke a Buffer in this code?

Posted by Avin Sinanan on January 10, 2002 at 12:40 AM

Hello there.. can someone examine the code below...There is nothing wrong with the code below.. infact it works perfect. However I would like to extend the code to do a few extra things. Before I tell you the extra functions it is to perform let me tell you what the code does :

The code I have is actually very simple. If you look at my code you will see that there are 6 classes.
1) The main class.
2)The global class
3)Classes 1 to 3 are classes that each have a methods that genetares an interger.
4)The 6th class called Class4 each takes the intergers from classes 1 to 3 and print them.

The code as you can see is very simple.
So whats my problem? My problem is that Class4 takes each interger individally and prints them indiviually (one by one).
I would like to alter my code so that Class4 takes the 3 intergers from classes 1 to 3 , strore them and them add them all up then display the result.
I tried doing this.I came to the conclusion that Class4 would have to have some kind of buffer which stores values and when there are no more inputs it addes up all the values in the buffer. Note Class4 is only allowed one interger in its constructor.

Anyone knows how to do this?
The code --->

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

class AvinClassTry
{
public static void main(String[] args)
{
JFrame frame = new JFrame("This progam tests to see if the same class can be" +
"called more than once");

JPanel pane = new JPanel();
JButton button = new JButton("Click me");
button.setMnemonic(KeyEvent.VK_K);

button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Class1 class1 = new Class1();
int class1int = class1.class1Method();
Global.class4 = new Class4(class1int);


Class2 class2 = new Class2();
int class1int2 = class2.class2Method();
Global.class4 = new Class4(class1int2);


Class3 class3 = new Class3();
int class1int3 = class3.class3Method();
Global.class4 = new Class4(class1int3);

}
});



pane.add(button);
frame.getContentPane().add(pane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.pack();
frame.setVisible(true) ;

}
}

class Global
{
public static Class4 class4 ;
}


class Class1
{
public Class1()
{
}
public int class1Method()
{
int value1 = 100 ;
return value1 ;
}
}

class Class2
{
public Class2()
{
}
public int class2Method()
{
int value2 = 200 ;
return value2 ;
}
}

class Class3
{
public Class3()
{
}
public int class3Method()
{
int value3 = 300 ;
return value3 ;
}
}

class Class4
{
int intakeValue1 ;
int i =0 ;
public Class4(int intakeValue)
{
intakeValue1 = intakeValue;
System.out.println("The amount just entered is " + intakeValue1);

}

}





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us