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!
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());
}
}
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...
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...
Hey guys! Huge part of my grade !! My code works already but I need it so it references to my node list class instead of my array, what would I need to change? My assignment was to create a list from an assignment when we used an array, but I cant make it to work without the array, help! The professor said it was okay to either keep the array or to delete it altogether. package zipcode; import java.io.File; import...
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 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 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 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...
******Java Programming Hi guys, I really need you help. I created a code for my java course, but it keep giving me error messages. Majority of my code is fine but some keep display error on my console. I was hoping someone could pin points the problem. .There are three classes with the testCenter class being the main class. In the following is the assignment, and the bottom is my code. Please help! Assignment: Concepts: GUI User Design Graphics Deployment...
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...
I need help with my code when I run my code running the wrong thing like this After downSize() words.length=60003 wordCount=60003 vowelCount=206728 this is my code here import java.io.*; import java.util.*; public class Project02 { static final int INITIAL_CAPACITY = 10; public static void main (String[] args) throws Exception { // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) ...
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...