Question

In: Computer Science

Question 1 (20 marks) Refer to the operations below: Add (10 + 5) Add (4+8) Add...

Question 1

Refer to the operations below:

Add (10 + 5)

Add (4+8)

Add (7*2)

Add (90 – 3)

Print list

Print peek

Remove an item from the list

Print list

1.1 Implement the operations above into a Queue structure called q1.

(10)

1.2 Implement the operations above into a Stack structure called s1.

(10

Guidelines: Please take note!

(a) Name your program Question1_1 for the queue structure and Question1_2 for the stack structure

(b) The Java Programmer is required to include screenshots from a JGrasp IDE which displays their commented code and the output and also indicates their laptop date and time of code execution, the screenshot of the code needs to be error-free and must not produce any logic errors!

Solutions

Expert Solution

Implementation in JAVA:

Question 1:

Implementation of queue using Array:

public class Question1_1 {

   public static void main(String[] args) throws Queuefullexception, Queueemptyexception {
      
       QueueusingArray qe= new QueueusingArray();
      
       System.out.println("Add(10+5) : ");
       qe.add(10+5);
       System.out.print("Queue is : ");
      
       for(int i=0;i<qe.size;i++) {
           System.out.print(qe.data[i]+" ");
       }
       System.out.println();
      
       System.out.println("Add(4+8) : ");
       qe.add(4+8);
       System.out.print("Queue is : ");
      
       for(int i=0;i<qe.size;i++) {
           System.out.print(qe.data[i]+" ");
       }
       System.out.println();
      
       System.out.println("Add(7*2) : ");
       qe.add(7*2);
       System.out.print("Queue is : ");
      
       for(int i=0;i<qe.size;i++) {
           System.out.print(qe.data[i]+" ");
       }
       System.out.println();
      
      
       System.out.println("Add(90-3) : ");
       qe.add(90-3);
       System.out.print("Queue is : ");
      
       for(int i=0;i<qe.size;i++) {
           System.out.print(qe.data[i]+" ");
       }
      
       System.out.println();
       System.out.println();
      
      
       System.out.println("List is : ");
       for(int i=0;i<qe.size;i++) {
           System.out.print(qe.data[i]+" ");
       }
      
       System.out.println();
      
       System.out.println("Peek element is : "+ qe.peek());
      
       System.out.print("Remove: ");
       System.out.println(qe.remove());
      
       System.out.print("List after Removing : ");
      
       for(int i=0;i<qe.size;i++) {
           System.out.print(qe.data[i]+" ");
          
       }
      
       System.out.println();
       System.out.println();
       System.out.println("Look here Insertion and deletion of elements are on Different ends ");
      
      
      
   }

}

//---- ----- classQueueusingArray


class QueueusingArray {

   int data[];
   int rear;
   int front;
   int size;
  
   public QueueusingArray() {
       data=new int[10];
       rear=-1;
       front=-1;
       size=0;
   }
  
   public QueueusingArray(int capacity) {
       data=new int[capacity];
       rear=-1;
       front=-1;
       size=0;
   }

   public int size() {
       return size;
      
   }
  
   public boolean isempty() {
       return size==0;
   }
  
   public int peek() throws Queueemptyexception {
       if(size==0) {
           Queueemptyexception e= new Queueemptyexception();
           throw e;
       }
       return data[front];
   }
  
   public void add(int elem) throws Queuefullexception {
       if(size()==data.length) {
//           Queuefullexception e= new Queuefullexception();
//           throw e;
           doublecapacity();
       }
       if(size==0) {
           front=0;
       }
       size++;
       rear++;
       if(rear==data.length) {
           rear=0;
       }
       data[rear]=elem;
      
      
   }
  
   public int remove() throws Queueemptyexception {
       if(size()==0) {
           Queueemptyexception e= new Queueemptyexception();
           throw e;
       }
       int temp=data[front];
      
      
       data=removeTheElement(data,0);
      
       size--;
      
       return temp;
   }
  
  
   public int[] removeTheElement(int[] arr,int index)
{


if (arr == null|| index < 0|| index >= arr.length) {

return arr;

}

// Create another array of size one less
int[] anotherArray = new int[arr.length];

// Copy the elements except the index
// from original array to the other array
for (int i = 0, k = 0; i < arr.length-1; i++) {

// if the index is
// the removal element index
if (i == index) {
continue;
}

// if the index is not
// the removal element index
anotherArray[k++] = arr[i];
}

// return the resultant array
return anotherArray;
}
  
   private void doublecapacity() {
       int temp[];
       temp=data;
       data= new int[data.length*2];
       for(int i=0;i<temp.length;i++) {
           data[i]=temp[i];
       }
   }
  
  
  
}

class Queuefullexception extends Exception {

}

class Queueemptyexception extends Exception {

}

SAMPLE OUTPUT:

QUESTION 2:

Implemantation of Stack using array in java

public class Question1_2 {

   public static void main(String[] args) throws Stackfullexception, Stackemptyexception {
      
       Stackusingarray st= new Stackusingarray();
      
       System.out.println("Add(10+5) : ");
       st.add(10+5);
       System.out.print("Stack is : ");
      
       for(int i=0;i<st.size();i++) {
           System.out.print(st.mydata[i]+" ");
       }
       System.out.println();
      
       System.out.println("Add(4+8) : ");
       st.add(4+8);
       System.out.print("Stack is : ");
      
       for(int i=0;i<st.size();i++) {
           System.out.print(st.mydata[i]+" ");
       }
       System.out.println();
      
       System.out.println("Add(7*2) : ");
       st.add(7*2);
       System.out.print("Stack is : ");
      
       for(int i=0;i<st.size();i++) {
           System.out.print(st.mydata[i]+" ");
       }
       System.out.println();
      
      
       System.out.println("Add(90-3) : ");
       st.add(90-3);
       System.out.print("Stack is : ");
      
       for(int i=0;i<st.size();i++) {
           System.out.print(st.mydata[i]+" ");
       }
      
       System.out.println();
       System.out.println();
      
      
       System.out.println("List is : ");
       for(int i=0;i<st.size();i++) {
           System.out.print(st.mydata[i]+" ");
       }
      
       System.out.println();
      
       System.out.println("Peek element is : "+ st.peek());
      
       System.out.print("Remove: ");
       System.out.println(st.remove());
      
       System.out.print("List after Removing : ");
      
       for(int i=0;i<st.size();i++) {
           System.out.print(st.mydata[i]+" ");
          
       }
      
       System.out.println();
       System.out.println();
       System.out.println("Look here Insertion and deletion of elements are on same ends ");

   }

}

// stack class

class Stackusingarray {
//   declaring mydata array
int mydata[];

// and top also
int top;

// no argument constructor
public Stackusingarray() {
    mydata=new int[10];
    top=-1;
}

// argument constructor with capacity defined
public Stackusingarray(int capacity) {
    mydata=new int[capacity];
    top=-1;
}

// will return size
public int size() {
    return top+1;
}

// will return either stack is empty or not
public boolean isempty() {
    if(size()==0) {
        return true;  
    }
    return false;
}

// will return top element of stack
// in case of if stack is empty will throw exception
public int peek() throws Stackemptyexception {
    if(top==0) {
        Stackemptyexception e= new Stackemptyexception();
        throw e;}
    return mydata[top];
}


// will remove topmost element from stack
// in case of stack is empty throw exception
public int remove() throws Stackemptyexception {
    if(size()==0) {
        Stackemptyexception e= new Stackemptyexception();
        throw e;
    }
    int temp=mydata[top];
    top--;
    return temp;
}


// will push element in stack
// in case of if capacity exceeds will throw stack full exception

   public void add(int i) throws Stackfullexception {
       // TODO Auto-generated method stub
       if(top==mydata.length-1) {
//        Stackfullexception e=new Stackfullexception();
//        throw e;
          
//           this method is implemented below
//           it double the capacity of array
           doublecapacity();
    }
       mydata[top+1]=i;
    top++;
   }


//   will double the capacity of array
   private void doublecapacity() {
       // TODO Auto-generated method stub
       int temp[]=new int[mydata.length];
       temp=mydata;
       mydata=new int[2*mydata.length];
       for(int i=0;i<=top;i++) {
           mydata[i]=temp[i];
       }
   }


  
}

class Stackemptyexception extends Exception {

}

class Stackfullexception extends Exception {

}

SAMPLE OUTPUT:

If you have any doubt regarding this question please ask me in comments

//THANK YOU:-)


Related Solutions

QUESTION 4 Regression Analysis Guide to marks: 20 marks – 5 for a, 10 for b,...
QUESTION 4 Regression Analysis Guide to marks: 20 marks – 5 for a, 10 for b, 3 for c, 2 for d Belinda, the accountant at Murray Manufacturing Company wants to identify cost drivers for support overhead costs. She has the impression that the staff spend a large part of their time ensuring that the equipment is correctly set up and checking the first units of production in each batch. Deborah has collected the following data for the past 12...
QUESTION 3 Regression Analysis Guide to marks: 20 marks – 5 for a, 10 for b,...
QUESTION 3 Regression Analysis Guide to marks: 20 marks – 5 for a, 10 for b, 3 for c, 2 for d Belinda, the accountant at Murray Manufacturing Company wants to identify cost drivers for support overhead costs. She has the impression that the staff spend a large part of their time ensuring that the equipment is correctly set up and checking the first units of production in each batch. Deborah has collected the following data for the past 12...
Question 4 (5 Marks) 0.00% Refer to Questions 2 and 3. The land for the factory...
Question 4 0.00% Refer to Questions 2 and 3. The land for the factory will cost $270,000 . The factory will cost $5,930,000 to build and construction will take two years with construction costs payable in equal installments at the start of each year. The factory will operate for 20 years; however, at the end of the fifth, tenth, and fifteenth year of operation, refurbishment costs will be $930,000 . At the end of its 20 year lifespan, the land...
Question 4 [20 marks] Analyze if the statements that are presented below are True or False....
Question 4 [20 marks] Analyze if the statements that are presented below are True or False. You MUST justify your answer to get credit. Answers without justification (even if they are correct) will be given zero marks. (a) In any Pareto-optimal allocation of a two-good economy, each consumer has to consume a positive amount of both goods. (b) A monopolist never produces on the elastic segment of its average revenue curve. (c) If a firm’s production exhibits increasing returns to...
Essay on Genetic Modification of food , 4-5 pages add sources (8-10)
Essay on Genetic Modification of food , 4-5 pages add sources (8-10)
QUESTION 4: [20 marks] Multichannel Analyser 160 (a) [10 marks] Describe the expected detector spectrum of...
QUESTION 4: [20 marks] Multichannel Analyser 160 (a) [10 marks] Describe the expected detector spectrum of Analyser. Co given by a MultiChannel (b) [5 marks] Calculate the energy difference between the photopeak and the Compton edge. (c) [5 marks] Calculate the position of the backscatter peak. Its Co 60 explain it properly specially b and c part of question
QUESTION 4 (2 + 1 + 2 + 1 + 4 =10 marks) It is believed...
QUESTION 4 (2 + 1 + 2 + 1 + 4 =10 marks) It is believed that cities tend to attract workers that are better educated. A sample of 610 people were classified by their highest education level (Primary and secondary school, Undergraduate and Postgraduate degree) and whether a person is working in a city or a town. The following information was obtained: Primary and secondary school: 89 people in total finished primary and secondary school as their highest qualification;...
QUESTION 4 CVP Analysis Guide to marks: 20 marks – 4 for a, 4 for b,...
QUESTION 4 CVP Analysis Guide to marks: 20 marks – 4 for a, 4 for b, 4 for c, 8 for d Show all calculations to support your answers. A manufacturer can make two products, A and B. The following data are available:B Product A B Total Sales price per unit $12 $15 Variable cost per unit $8 $10 Total fixed costs/month $5000 (a)Calculate the unit contribution margin for each product. (b)This month the manufacturer will specialise in making only...
Question 5 (5 Marks) 0.00 Refer to Questions 1 and 2. Richard has just received an...
Question 5 0.00 Refer to Questions 1 and 2. Richard has just received an unexpected bonus at work worth $9,250 and, given the J. Corp.'s reputation for excellent investment decision making, he will invest all of the bonus in J Corp. stock. Given the rates of return for stocks A, B, C, and D presented in Question 1 and the rates of return for J Corp. stock and the market presented in Question 2, as well as the cash amounts...
4. Refer to Table 10-1, which is based on bonds paying 10 percent interest for 20...
4. Refer to Table 10-1, which is based on bonds paying 10 percent interest for 20 years. Assume interest rates in the market (yield to maturity) decline from 16 percent to 6 percent. a. What is the bond price at 16 percent? Bond price b. What is the bond price at 6 percent? Bond price c. What would be your percentage return on investment if you bought when rates were 16 percent and sold when rates were 6 percent? (Do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT