Question

In: Computer Science

how do I write this function to check if it is close? below is checking if...

how do I write this function to check if it is close?

below is checking if the function is open. I want to write another if then statement to check when that function is closed and when the queue is empty. You can use any variables or declaration as a example to show me how it is done.

if (tx.isSenderStreamOpen())

{

}

Solutions

Expert Solution

/**
* Top level class available for the Client. In this example Main class is the client
*/
import java.util.Queue;
import java.util.LinkedList;;

public class Transaction {
   /**
   * Data member of type SenderStream
   */
   private SenderStream senderStream;
  
   /**
   * Opens the SenderStream with empty queue
   */
   public void open(){
       senderStream = new SenderStream();
   }
  
   /**
   * Opens the SenderStream with Queue elements
   * @param queue
   */
   public void open(Queue queue){
       senderStream = new SenderStream(queue);
   }
  
   /**
   * Closes the SenderStream
   */
   public void close(){
       senderStream = null;
   }
  
   /**
   * Checks whether a SenderStream is open or not
   * @return true or false
   */
   public boolean isSenderStreamOpen(){
       if(senderStream!=null)
           return true;
       return false;
   }
  
   /**
   * Checks whether the queue of empty or not. If SenderStream is closed, throws SenderStreamException
   * @return true or false
   * @throws SenderStreamException
   */
   public boolean isSenderStreamEmpty() throws SenderStreamException{
       if(senderStream==null)
           throw new SenderStreamException("Sender Stream is closed.");
       if(senderStream.getQueue().size()==0)
           return true;
       return false;
   }

}

/**
* SenderStream class with Queue
*
*/
class SenderStream{
   /**
   * This queue for the element
   */
   private Queue queue;

   /**
   * Queue constructor
   * @param queue
   */
   public SenderStream(Queue queue) {
       this.queue = queue;
   }

   /**
   * Queue default constructor
   */
   public SenderStream() {
       this.queue = new LinkedList<>();
   }

   /**
   * Retrieve the SendStream Queue
   * @return
   */
   public Queue getQueue() {
       return queue;
   }

   /**
   * Sets the SendStream Queue
   * @param queue
   */
   public void setQueue(Queue queue) {
       this.queue = queue;
   }
  
}

/**
* Exception class for throwing ClosedSenderStream exception
*
*/
class SenderStreamException extends Exception{

   public SenderStreamException(String message) {
       super(message);
   }
}

//Main class

public class Main {

   public static void main(String[] args) {
       Transaction tx = new Transaction();

       //Without opening the SenderStream
       if(tx.isSenderStreamOpen())
           System.out.println("Sender Stream is open");
       else
           System.out.println("Sender Stream is closed");
       //Output => Sender Stream is closed
      
       try {
           if(tx.isSenderStreamEmpty()){
               System.out.println("Sender Stream is empty");
           }
       } catch (SenderStreamException e1) {
           System.out.println("Sender Stream is closed");
           e1.printStackTrace();
       }
       //Outptut => Sender Stream is closed, followed by Exception
      
      
       //Openning the Sender Stream
       tx.open();
      
       if(tx.isSenderStreamOpen())
           System.out.println("Sender Stream is open");
       else
           System.out.println("Sender Stream is closed");
       //Output => Sender Stream is open
      
       try {
           if(tx.isSenderStreamEmpty()){
               System.out.println("Sender Stream is empty");
           }
       } catch (SenderStreamException e1) {
           System.out.println("Sender Stream is closed");
           e1.printStackTrace();
       }
       //Output => Sender Stream is empty
       tx.close();

   }

}

//Output

Sender Stream is closed
Sender Stream is closed
com.sandeep.SenderStreamException: Sender Stream is closed.
   at com.sandeep.Transaction.isSenderStreamEmpty(Transaction.java:54)
   at com.sandeep.TransactionMain.main(TransactionMain.java:16)
Sender Stream is open
Sender Stream is empty


Related Solutions

Palindrome Javascript I am trying to write this javascript function to check if a number is...
Palindrome Javascript I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. This is the code i have, but it doesnt work. Code: let convertButton = document.getElementsByClassName("btn")[0]; let userInput = document.getElementById("number").value; let results = document.getElementById("result").value; convertButton.addEventListener("click", function (event) { event.preventDefault(); console.log(userInput); if (validatePalidrome(userInput)) document.getElementById("result").innerHTML = "true"; else document.getElementById("result").innerHTML = "false"; }); function validatePalidrome(numbers) { let...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
Write a function for checking the speed of drivers. This function should have one parameter: speed....
Write a function for checking the speed of drivers. This function should have one parameter: speed. 1. If speed is less than 70, it should print “Ok”. 2. Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: "Points: 2". 3. If the driver gets more than 12 points, the function should print: “License suspended”...
How do I check something is analytic. C-R is necessary but not sufficient. i was told...
How do I check something is analytic. C-R is necessary but not sufficient. i was told that I must also check if the partial derivatives are continuous. How would I do that?
I need to write a resume and I do not know how to write it. Please...
I need to write a resume and I do not know how to write it. Please help me. My name is John Doe. I finished Associate of Science in (Electrical Engineering, Computer Engineering, Physics and Math) from Community College. I have work experience over 10 years at food company. Please help me write as completely as possible. Thanks a lot for your help!
How do I plug in the finite function in excel?
How do I plug in the finite function in excel?
How do I write out the new planned expenditure function after the change in planned investment
How do I write out the new planned expenditure function after the change in planned investment
How do I do  independent t test on the data set below and how do I know...
How do I do  independent t test on the data set below and how do I know if its pooled t test or unrolled t test? Health question for reference: to what extend does the age of MI patients vary by gender Standard deviation: male = 13.944 female= 13.939 mean: male 65.353 female=73.628 Male female 65   88 77   81 78   82 76   66 40   81 83   73 58   64 43   53 39   69 66   67 61   89 49   85 85   81...
How do I write an ethnography paper
How do I write an ethnography paper
Discuss checking and savings accounts. How do they work? How are they similar? How do they...
Discuss checking and savings accounts. How do they work? How are they similar? How do they differ? What are CDs? What are advantages of an ATM?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT