Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am not sure as how go about about creating a source component for the specifie

ID: 653926 • Letter: I

Question

I am not sure as how go about about creating a source component for the specified requirements below. In java

Create a source component named MemoryWatch for monitoring
memory. The component generates a MemoryEvent when the free
memory space exceeds a specified highLimit or is below a
specified lowLimit. The highLimit and lowLimit are customizable
properties in MemoryWatch.


Create an event set named MemoryEvent and MemoryListener. The
MemoryEvent simply extends java.util.EventObject and contains two
methods, freeMemory and totalMemory, which return the free memory
and total memory of the system. The MemoryListener interface
contains two handlers, sufficientMemory and insufficientMemory.
The sufficientMemory method is invoked when the free memory space
exceeds the specified high limit, and insufficientMemory is
invoked when the free memory space is less than the specified low
limit.

Develop a listener component that displays free memory, total
memory, and whether the memory is sufficient or insufficient when
a MemoryEvent occurs. Make the listener an applet with a main
method to run standalone.

Explanation / Answer

// Program for Memory event handling

import java.util.*;
import java.awt.event.*;
import java.awt.*;

class MemoryEvent extends EventObject //Creating MemoryEvent class
{
   MemoryEvent(Object obj) //Constructor with Object as parameter it can any class object as source
   {
       super(obj);
   }
   long freeMemory() //Method to return free memory
   {
       return Runtime.getRuntime().freeMemory();
   }
  
   long totalMemory() //Method to return total memory
   {
       return Runtime.getRuntime().totalMemory();
   }
}

interface MemoryListener extends EventListener //Creating Memory handler/listener
{
   public void sufficientMemory(MemoryEvent me); //Prototypes of function1
   public void insufficientMemory(MemoryEvent me); //Prototypes of function2
}


class MemoryWatch extends Frame implements MemoryListener //Frame to handle Memory Listener interface and MemoryEvent
{
   Label l1,l2;
   TextField txttotalM,txtFree;
   Button btnshow;
   MemoryWatch(String title)
   {
       super(title);
       setLayout(new FlowLayout());
       l1=new Label("Enter total memory space : ");
       txttotalM=new TextField(10);
       l2=new Label("Enter total free memory space : ");
       txtFree=new TextField(10);
       btnshow=new Button("Show");
       add(l1);add(txttotalM);
       add(l2);add(txtFree);
       add(btnshow);
       btnshow.addMemoryListener(this);
   }
   public void sufficientMemory(MemoryEvent me)
   {
   if(me.totalMemory()<Long.parseLong(txttotalM.getText()))
   System.out.println("Your total Memory required is > than System Memory : "+me.totalMemory());
   else
   System.out.println("Your total Memory required is available System Memory : "+me.totalMemory());
   }
  
   public void insufficientMemory(MemoryEvent me)
   {
   if(me.freeMemory()<Long.parseLong(txtFree.getText()))
   System.out.println("Your free Memory required is > than System Free Memory : "+me.freeMemory());
   else
   System.out.println("Your free Memory required is available, System Free Memory : "+me.freeMemory());
   }
  
   public static void main(String args[])
   {
       MemoryWatch m1=new MemoryWatch("Memory Monitor");
       m1.setSize(new Dimension(300,300));
       m1.setVisible(true);
   }
}
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote