Write some instructions that will ask the user to enter an integer and then use
ID: 3686618 • Letter: W
Question
Write some instructions that will ask the user to enter an integer and then use this integer to declare an array named myList which contains that many double precision numbers. Write some instructions that will read numbers from the keyboard and store them in the array myList from problem 3. Write a method that will combine the actions of problem 3 and 4. It should return this new array. Write some instructions that will print all of the numbers in the array myList. Rewrite this to use a method which accepts the array as a parameter and prints it. Write some instructions that will find the largest number in myList and print it. Rewrite this as a method that accepts the array as a parameter and returns the largest value. Write some instructions that w ill find the average of the numbers in myList and print it. Rewrite this as a method that accepts the array as a parameter and returns the average as a double. Consider the arrays declared below. double [] thisList = {2.5, 2.7,3.1}; double [] thatList = new double [3]; What does each of the following instructions do thatList = thisList; System.arraycopy (thisList, 0, thatList. 0. thisList.length); Write a loop that will do the same thing as the instruction System.arraycopy (thisList, 0, thatList, 0, thisList.length); Write a method which will accept an array of integers and return the Boolean value true if the numbers are in sequence from smallest to largest (duplicate values are allowed). The method returns the Boolean value false if any number the array is larger than a number which follows it.Explanation / Answer
Question 3,4,5:
public double[] getArray()
{
Scanner sc = new Scanner(System.in);
int arraySize = sc.nextInt();
double myList[] = new double[arraySize];
for(int i=0;i<myList.length;i++)
{
System.out.println("Please enter number "+(i+1)+": ");
myList[i] = sc.nextDouble();
}
return myList;
}
Question 6:
public void printArray(double[] array)
{
System.out.println("Array contents: ");
for(int i=0;i<array.length;i++)
{
System.out.println(array[i]+" ");
}
}
Question 7:
public void printLargest(double[] array)
{
double max = Double.MIN_VALUE;
for(int i=0;i<array.length;i++)
{
if(max < array[i])
max = array[i];
}
System.out.println("Largest number in the array is: "+max);
}
Question 8:
public double getAverage(double[] array)
{
double sum = 0;
for(int i=0;i<array.length;i++)
sum += array[i];
return (sum/array.length);
}
Question 9:
thatList = thisList;
//thatList is pointed to thisList array location
//any changes in thisList will also effect thatList
System.arraycopy(thisList,0,thatList,0,thatList.length);
//copies contents of thisList in to thatList
//changes to thisList will not effect thatList
Question 10:
for(int i=0;i<thisList.length;i++)
thatList[i] = thisList[i];
Question 11:
public boolean sequence(int[] array)
{
for(int i=0;i<array.length-1;i++)
{
if(array[i] > array[i+1])
return false;
}
return true;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.