Question

In: Computer Science

Could i get a walk trough how to solve these questions. Note that   is where the...

Could i get a walk trough how to solve these questions. Note that   is where the answer is meant to go.

Question 1

Say, instead of a separate variable (nItems), the number of items currently in the list are stored at index 0 in the array data, the actual values being stored starting at index 1. Complete the constructor that instantiates a list that can hold n values (excluding the item that holds the number of items currently in the list). That is, one should be able to add n values to the list before the need to grow it.

public class PackedArrayList {
public int[] data;

public PackedArrayList(int n) {
data = new int[  ];
data[0] =  ;
}
}

Question 2

Complete the body of method addEnsureCapacity in the following code:
public class MyArrayList {
public int[] data;
public int nItems;
public MyArrayList(int n) {
data = new int[n];
nItems = 0;
}
public void grow() {
//assume it increases capacity by 10
}
public boolean isFull() {
return (nItems == data.length);
}
public void addBasic(int val) {
data[nItems] = val;
nItems++;
}
public void addEnsureCapacity(int val) {
if(  == false) {
 ;
}
else {
 ;
addBasic(val);
}
}
}

Solutions

Expert Solution

Q1) Solution:

public class PackedArrayList {
public int[] data;

public PackedArrayList(int n) {
data = new int[ n+1 ]; //it should be n+1 as we are storing n value at index 0 and we want n values to store.
data[0] = 0 ; //as we are initializing the array this willl be 0 and should be adjusted with values in array.
}
}

Q2) Solution:
public class MyArrayList {
public int[] data;
public int nItems;
public MyArrayList(int n) {
data = new int[n];
nItems = 0;
}
public void grow() {
//assume it increases capacity by 10
}
public boolean isFull() {
return (nItems == data.length);
}
public void addBasic(int val) {
data[nItems] = val;
nItems++;
}
public void addEnsureCapacity(int val) {
if( isFull() == false) { //if isFull() returns false then we have space in array to store more
addBasic(val); //as isFull() is false we can call addBasic to store the val.
}
else {
grow(); //we reach else if isFull() returns true which means there is no more space left in array so we call grow()
addBasic(val);
}
}
}

If you find this helpful. Please upvote the answer and if you have any doubts please leave a comment.


Related Solutions

Could I please get explainations and working out as to how to get to the answers....
Could I please get explainations and working out as to how to get to the answers. Note that  is where part of the answer goes. 1. Consider the following ArrayList list: ArrayList list = new ArrayList(Arrays.asList(10,70,20,90)); //list = [10, 70, 20, 90] Complete the following statements so that list contains the items [50, 70, 20] Do NOT include spaces in your answers. list.remove(  ); list.  (  , 50); 2. Assume there exists an ArrayList list that...
Could I please get explainations and working out as to how to get to the answers....
Could I please get explainations and working out as to how to get to the answers. Note that  is where part of the answer goes. 1. Write a statement that removes the first occurrence of item 10 (if any) from an ArrayList of Integer objects, data. data.remove(  ); 2. Complete the following function that returns true if ArrayList list contains any positive (more than 0) item, false otherwise. public static  containsPositive(ArrayList list) { for(int i=0; i <...
Could I please get explainations and working out as to how to get to the answers....
Could I please get explainations and working out as to how to get to the answers. Note that  is where part of the answer goes. 1. Complete the following function that returns true if ArrayLists list1 and list2 are exactly the same in terms of contents (same items in same order), false otherwise. public static  sameSame(ArrayList<Integer> list1, ArrayList<Integer> list2) { if(list1 == null || list2 == null) return false; if(  !=  ) return false; for(int i=0;...
note (Please solve the questions in the language of operating systems) [2] (i) Define Timesharing.    ...
note (Please solve the questions in the language of operating systems) [2] (i) Define Timesharing.     (ii) In a one processor system, there is an interrupt clock which is set to a “time slice Q”, that is, every Q an interrupt occurs to stop the process. There is only one I/O device in the system    which is interrupted when the process needs an I/O. There are 3 processes A, B, C.           Process A executes an I/O interrupt every...
Hi I was wondering if I could get an explanation on how race, ethnic groups, multiracial...
Hi I was wondering if I could get an explanation on how race, ethnic groups, multiracial people, minority groups, dominanat groups, prejudice, discrimination and racisism all relate to one another?
Could I get an explanation of Henry’s Law including definition, examples, and how it was derivied...
Could I get an explanation of Henry’s Law including definition, examples, and how it was derivied if possible. I don't want the Wiki link, but I want to understand this law better.
I have two Biophysical chem questions that I have no idea how to get the answer....
I have two Biophysical chem questions that I have no idea how to get the answer. Please help and show work. Thank you. 1. The unit millimeters of mercury (mmHg) has been replaced by the unit torr: 1mmHg is defined as the pressure at the base of a column of mercury exactly 1 mm high when its density is 13.5951 g/cm3 and the acceleration of free fall is 9.806 m/s2. What is the relation between the two units? The answer...
Hello! Could anyone show me how to solve these questions in excel using the "solver" tool?...
Hello! Could anyone show me how to solve these questions in excel using the "solver" tool? CarPro is an automobile dealer selling only new cars. CarPro sells three types of vehicles: sedans, SUVs and trucks. CarPro places orders to the car manufacturers only when customers have decided to purchase. The ordering cost (per unit) of sedan, SUV and truck are $18,000, $20,500 and $19,000, respectively. The sales price (per unit) of sedan, SUV and truck are $20,000, $23,000, and $21,500,...
Please note that I tried a screen shot and scanned, and I could not paste this...
Please note that I tried a screen shot and scanned, and I could not paste this on this site because of the browser will not allow, and I called, and was told to type this. The problem and questio Analysis of Variance by hand. The average number of purchases in three different stores are compared to determine if they are significantly different. The following summary statistics is given for each store. State the null and alternative hypothesis, calculate the F-statistics,...
Bonus Problem: Find the number of spanning trees in Km,n. Note: I need how to get...
Bonus Problem: Find the number of spanning trees in Km,n. Note: I need how to get the right formula-not just oh this is the formula you need.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT