Question

Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular a...

Hello!

I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP

Task is!

Improve the circular array implementation of the bounded queue by growing the elements array when the queue is full. Add assertions to check all preconditions of the methods of the bounded queue implementation.

My code is!

public class MessageQueue{

public MessageQueue(int capacity){

elements = new Message[capacity];

count = 0;

head = 0;

tail = 0;

}

/*

   @return the message that has been removed from the queue

   @precondition size() > 0

*/

public Message remove()

{

assert count > 0:"Empty Queue"; //check if elements are there

Message m = elements[head];

head = (head + 1) % elements.length;

count--;

return m;

}

public void add(Message aMessage)

{

if(isFull()){

Message[] new_elements = new Message[count + 1];//Create a new message array by increasing size 1

int i ,j,n;//copy the contents to the new array

for(i=head,j= 0,n=0;n<count; i=(i+1)%elements.length,j++,n++){

new_elements [j] = elements[i];

}

//add the new message at the end of new elements

new_elements [count]= aMessage;

//make elements point to new element

elements = new Message[new_elements.length];

elements = new_elements;

//update the tail,head and count

tail = (count +1)%elements.length;

head=0;

count++;

}

else{

elements[tail] =aMessage;

tail =(tail+1)%elements.length;

count++;

}

}

public int size()

{

return count;

}

public boolean isFull()

{

return count == elements.length;

}

public Message peek()

{

return elements[head];

}

private Message[] elements;

private int head;

private int tail;

private int count;

}

;=================================

public class Message { //greate class message

private String text; //declare of variable

public Message(String text){

this.text=text; //define the variable text

}

//define get text function

public String getText(){

return text;}}

=============================================

public class MessageQueueTest {

public static void main(String[] args) {

MessageQueue tex = new MessageQueue(4);

tex.add(new Message("Hello"));

tex.add(new Message("There !"));

tex.add(new Message("This"));

tex.add(new Message("Massege"));

tex.add(new Message("is :"));

System.out.println("The size is: "+ tex.size());

System.out.println("The head is:"+ tex.peek().getText()+" befor removing");

System.out.println("Remove the head: "+ tex.remove().getText());

tex.add(new Message("greater than the old orginal size"));

while(tex.size()>0)

System.out.println(tex.remove().getText());

}}

==================================

Regardes!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class MessageQueue{

public MessageQueue(int capacity){

elements = new Message[capacity];

count = 0;

head = 0;

tail = 0;

}

/*

@return the message that has been removed from the queue

@precondition size() > 0

*/

public Message remove()

{

assert count > 0:"Empty Queue"; //check if elements are there

Message m = elements[head];

head = (head + 1) % elements.length;

count--;

return m;

}

public void add(Message aMessage)

{

assert count == elements.length:"Queue is full"; //check if elements are there

if(isFull()){

Message[] new_elements = new Message[count + 1];//Create a new message array by increasing size 1

int i ,j,n;//copy the contents to the new array

for(i=head,j= 0,n=0;n<count; i=(i+1)%elements.length,j++,n++){

new_elements [j] = elements[i];

}

//add the new message at the end of new elements

new_elements [count]= aMessage;

//make elements point to new element

elements = new Message[new_elements.length];

elements = new_elements;

//update the tail,head and count

tail = (count +1)%elements.length;

head=0;

count++;

}

else{

elements[tail] =aMessage;

tail =(tail+1)%elements.length;

count++;

}

}

public int size()

{

return count;

}

public boolean isFull()

{

return count == elements.length;

}

public Message peek()

{
assert count == 0:"Empty Queue"; //check if elements are there

return elements[head];

}

private Message[] elements;

private int head;

private int tail;

private int count;

}

//=================================

class Message { //greate class message

private String text; //declare of variable

public Message(String text){

this.text=text; //define the variable text

}

//define get text function

public String getText(){

return text;}}

//=============================================

public class MessageQueueTest {

public static void main(String[] args) {

MessageQueue tex = new MessageQueue(4);

tex.add(new Message("Hello"));

tex.add(new Message("There !"));

tex.add(new Message("This"));

tex.add(new Message("Massege"));

tex.add(new Message("is :"));

System.out.println("The size is: "+ tex.size());

System.out.println("The head is:"+ tex.peek().getText()+" before removing");

System.out.println("Remove the head: "+ tex.remove().getText());

tex.add(new Message("greater than the old orginal size"));

while(tex.size()>0)

System.out.println(tex.remove().getText());
}
}

Add a comment
Know the answer?
Add Answer to:
Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private...

    public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count;   } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • *JAVA* Can somebody take a look at my current challenge? I need to throw a different...

    *JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> {    private int top, size;    private E arrS[];    private static final int MAX_STACK_SIZE = 10;    public ArrayStack() {        this.arrS = (E[]) new Object[MAX_STACK_SIZE];        this.top = size;        this.size = 0;...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • What is wrong with my code, when I pass in 4 It will not run, without...

    What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error   required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...

  • using Data Structures using Java. Modify it to make the ADT resizable. For this ADT, the...

    using Data Structures using Java. Modify it to make the ADT resizable. For this ADT, the MAX_SIZE will not be final, but rather will be increased whenever an new item needs to be added and the array is full (ie. size==MAX_SIZE). Whenever the array needs to grow, add the lesser of: (i) half the current size, or; (ii) 50 additional. Have your ADT report its old and new maximum size whenever the resize operation is called by printing “ADT resized...

  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT