• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make a buffer in this code?

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 prints them when the button is clicked once.
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 , stores them and then adds them all up then displays 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 ;

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

}

}
[ January 09, 2002: Message edited by: Avin Sinanan ]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Avin,
The simplest approach would be to use a static class variable that keeps track of the total value. Since you are constantly creating new Class4 objects this is the only approach left to you. The new Class4 would look like the following.

The you just need to add a single call at the bottom of your actionPerformed method:
Global.class4.printSum();
then it should work as you expect.
NOTE: your Global class is somewhat useless. It should not have a public static variable either. This violates OOP data hiding. When writing java classes you should make it a point to have all class variables be private unless you find a VERY GOOD reason to change it!
Regards,
Manfred.
 
Avin Sinanan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred,
You said I am constantly making new Class4 objects.. I thought by using the global class I could make one Class4 object and use that same one over and over. SO I guess my question is how do I make one Class4 object and always use that same Class4 object more than once?
Thanks in advance
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You do only have one Class4 object but its value is kept changing while you do this
Global.class4 = new Class4(class1int2);
By calling "new Class4(...)", you assign a new value to Global.class4.
If you just need ONE Global.class4, do this
then
Class4.addValue(class1int1); //class method
Class4.addValue(class1int2);
Class4.addValue(class1int3);
Class4.printSum();
and forget about the new Class4(...)
then add this method
static void addValue(int value)
{
sumOfValue += value;
}
and change the printSum method to static,
static void printSum()
This way the Class4 only has CLASS method, no instance method.
Ping
[ January 10, 2002: Message edited by: ping203 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic