I had ask for some help before to get me started. It helped a lot.Thank you, but
ID: 3616880 • Letter: I
Question
I had ask for some help before to get me started. It helped a lot.Thank you, but I am still having problem getting it to work. Couldyou please help me. I need to understand what the code do and howthe driver implement it.I am trying to put all the constructor and methods in thearrayOperation and keep the driver clean with just theimplementation codes.
*/
import java.util.*;
public class ArrayDriver {
private static int sum;
private static int avg;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated methodstub
int[] array = null;
//int array[10];
int n=array.length;
int i;
int j ; // One of the numbers input by the userto insert.
int p = 0; // One of the numbers input by theuser to delete.
int k = 0; // One of the numbers input by theuser to replace.
Scanner kbd = new Scanner(System.in);
for (i=0; i< n; i++) {
//System.out.print("Enter the elements in anarrry" +array[i]);
System.out.print("Enter a number for your arraysize: ");
array[i] = kbd.nextInt();
System.out.println("The array is:"+array[i]);
sum=sum+array[i];
//Select the location in the array to insert thesum of the array
System.out.print("In what location would youlike to insert?"
+ "/n pleaseEnter a number to : ");
j = kbd.nextInt();
//Select the pth element in the array todelete
System.out.print("What location would you liketo delete?"
+ "/n please Enter anumber to : ");
p = kbd.nextInt();
//replace the kth element with the averagevalue
System.out.print("What location would you liketo delete?"
+ "/nplease Enter a number to : ");
k = kbd.nextInt();
}
}
}
import java.util.*;
import java.lang.reflect.Array;
/**
* @author classpc
* @param <newIndex>
*
*/
public class ArrayOperations {
private int[] data;// An array to hold the intdata.
int avg=0;
public ArrayOperations() {// Constructor.
data = new int[0]; // Array will grow asnecessary.
}
public int get(int j) { // Get the value fromthe specified position in the array.
if (j >= data.length)
return 0;
else
return data[j];
}
//Insert
public void insert(int j, int value) { // Storethe value the user specify for the position in the array.
if (j >=data.length);
intnewSize = 2 * data.length;
if (j >= newSize)
newSize = 2 * j;
int[] data = new int[newSize];
}
//delete
public static void deleteElt( int [] data, int p){
for ( int i = p ; i < data.length - 1 ; i++ ) {
data[ i ] = data[ i + 1 ] ;
}
}
//replace
public void replace(int [] data, int k, int avg){
k=0;
for( i=0; i< int [] data .length; i++ )
{
if( data [i] == k)
data.length(i,"k","avg");
}
//print array values.
System.out.println(" ");
for (i=0; i<n.length; i++) {
}
{
}
}}
Explanation / Answer
not quite right --try this and please rate - thanks class arrayOperations {private double[]list; private int size; private double sum; private double average; public arrayOperations() {list=new double[50]; size=0; } public arrayOperations(int count) {list=new double[count]; size=0; } public void setsize(int val) {size=val; } public int getSize() {return size; } public double getsum() {return sum; } public double getavg() {return average; } public void add(double val) { list[size]=val; size++; } public void delete(int loc) {int i; for(i=loc;i=max;i--) list[i]=list[i-1]; list[loc]=sum; size++; } public void print() {int i; System.out.println("The array:"); for(i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.