Question

JAVA - Without using any built in functions implement a menu based program with the following...

JAVA - Without using any built in functions implement a menu based program with the following array-based queue functions, ADD (at the end of array), INSERT (element at a given location), DELETE (element from a given location), SHOW (all array elements), COUNT (total number of elements), CLEAR (initialize array)

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

For this program, I've created a class to hold the array and it's methods (for better encapsulation). Refer to the comments to find the logic of each operation.  From what I've understood from the word clear, I've just reset it's length to 0. Hope it's right.

public class arrayProg {
public static void main(String args[]){
arr a = new arr();
a.add(10);
a.add(20);
a.show();
System.out.println();
a.add(30);
a.add(40);
a.show();
System.out.println();
a.insert(50,2);
a.show();
System.out.println();
a.delete(3);
a.show();
System.out.println();
System.out.println("Length of array is "+a.count());
a.clear();
a.show();
System.out.println();
}
}

class arr{
//length of the array
private int l;
//an empty array that holds all info
private int a[]=new int[100];
//add item at the end of the array
void add(int item){
a[l++]=item;
}
//insert item at position pos
void insert(int item,int pos){
if(pos>l){
//If position is not within length of the array
System.out.println("ERROR: Array not long enough");
return;
}
l++;
for(int j=l;j>pos;j--){
//move all elements after pos, one step ahead
a[j]=a[j-1];
}
a[pos]=item;
}
//delete element at given position
void delete(int pos){
if(pos>l){
//If position is not within length of the array
System.out.println("ERROR: Array not long enough");
return;
}
for(int j=pos;j<l;j++){
//move all element after pos, one position behind
a[j]=a[j+1];
}
l--;
}
//return number of elements
int count(){
return l;
}
//print the array
void show(){
if(l==0){
System.out.println("ERROR: Array empty!");
return;
}
for(int i=0;i<l;i++){
System.out.print(a[i]+" ");
}
}
//initialize array to 0s
void clear(){
l=0;
}
}

Here is the output:

10 20
10 20 30 40
10 20 50 30 40
10 20 50 40
Length of array is 4
ERROR: Array empty!

Add a comment
Know the answer?
Add Answer to:
JAVA - Without using any built in functions implement a menu based program with the following...
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
  • Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the...

    Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the max-heap data structure using an integer array of 10 cells. (Do not use Java in-built PriorityQueue class.) [In a max-heap, the root node and the intermediate node vales are always greater than their children.] First, take 10 integer values from the user and insert them in the max-priority queue. Then print the elements of the queue. After that, delete two elements from the queue...

  • Java Program

    Write a program in Java to implement the min-priority queue using min-heap data structure. Implement the min-heap data structure using a String array of 5 cells. (Do not use Java in-built PriorityQueue class.) [In a min-heap, the root node and the intermediate node vales are always smaller than their children.] First, take 5 names from the user and insert them in the min-priority queue. Then print the elements of the queue. After that, delete two elements from the queue and...

  • Using Java In this assignment we are working with arrays. You have to ask the user...

    Using Java In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...

  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • java Create a Queue class based on java.util.LinkedList class. Your Queue class should have an enqueue(),...

    java Create a Queue class based on java.util.LinkedList class. Your Queue class should have an enqueue(), dequeue(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods: A method to randomly generate a number of elements between two given values and save them in a queue A method to print a queue (10 elements per line). The original queue should remain as is after the print A method to return the number of elements on the...

  • Examine the line management routines in this dir. It handles any number of lines (or queues),...

    Examine the line management routines in this dir. It handles any number of lines (or queues), and returns a pointer to the line that is created. The header file (queueHeader.h), the line manager (queueManager.c) and a 'skeleton' driver program, lineMain.c, are given. Item 1 lineMain.c: Extend/expand lineMain.c so the expanded code exercises/invokes the various line management functions. The driver program should hep convince the reader that you understand these functions or routines! The driver should have comments @ top that...

  • JAVA - Without using build in functions Implement a program that will use a stack structure...

    JAVA - Without using build in functions Implement a program that will use a stack structure to check for correct placement of parentheses in an algebraic expression. Allow the use of ( ) [ ] { } characters as grouping symbols. Make sure that an error is reported for an expression of a form (...]. In addition report other possible parentheses related errors (too many levels, too many right paren., too many left paren.). Make sure to use 'silent error...

  • Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays,...

    Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays, and a random number generator. Upon program execution, the screen will be cleared and the menu shown below will appear at the top of the screen and centered. The menu items are explained below. Help Smallest Quit H or h ( for Help ) option will invoke a function named help() which will display a help screen. The help screen(s) should guide the user...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • Assignment Write a menu-driven C++ program to manage a class roster of student names that can...

    Assignment Write a menu-driven C++ program to manage a class roster of student names that can grow and shrink dynamically. It should work something like this (user input highlighted in blue): Array size: 0, capacity: 2 MENU A Add a student D Delete a student L List all students Q Quit ...your choice: a[ENTER] Enter the student name to add: Jonas-Gunnar Iversen[ENTER] Array size: 1, capacity: 2 MENU A Add a student D Delete a student L List all students...

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