Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Copy the PartiallyFilledArray.java and GolfScoresVersion2.java to your Workshop

ID: 3689542 • Letter: C

Question

Copy the PartiallyFilledArray.java and GolfScoresVersion2.java to your Workshop 12 directory.

Add a private method called resize that will double the size of the array. The sets to double the size of the array are:

Create a new array whose size is double the current array size.

Loop through the current instance variable array called "a" and copy all its elements to the new array.

Reassign array "a" to the new array.  

Modify the add method to call resize() if the array is full.

Modify the fillArray() method of GolfScoresVersion2.java so it recieve more scores than the original array size.

Since the original file doesn't include javadoc, you don't need to include it in the solution

Explanation / Answer

public class PartiallyFilledArray
{
private int maxNumberElements; //Same as a.length
private double[] a;
private int numberUsed; //Number of indices currently in use
/**
Sets the maximum number of allowable elements to 10.
*/
PartiallyFilledArray( )
{
maxNumberElements = 10;
a = new double[maxNumberElements];
numberUsed = 0;
}
/**
Precondition arraySize > 0.
*/
PartiallyFilledArray(int arraySize)
{
if (arraySize <= 0)
{
System.out.println("Error Array size zero or negative.");
System.exit(0);
}
maxNumberElements = arraySize;
a = new double[maxNumberElements];
numberUsed = 0;
}
PartiallyFilledArray(PartiallyFilledArray original)
{
if (original == null)
{
System.out.println("Fatal Error: aborting program.");
System.exit(0);
}
maxNumberElements =
original.maxNumberElements;
numberUsed = original.numberUsed;
a = new double[maxNumberElements];
for (int i = 0; i < numberUsed; i++)
a[i] = original.a[i];
}
/**
Adds newElement to the first unused array position.
*/
public void add(double newElement)
{
if (numberUsed >= a.length)
{
System.out.println("Error: Adding to a full array.");
System.exit(0);
}
else
{
a[numberUsed] = newElement;
numberUsed++;
}
}
public double getElement(int index)
{
if (index < 0 || index >= numberUsed)
{
System.out.println("Error:Illegal or unused index.");
System.exit(0);
}
return a[index];
}
/**
index must be an index in use or the first unused index.
*/
public void resetElement(int index, double newValue)
{
if (index < 0 || index >= maxNumberElements)
{
System.out.println("Error:Illegal index.");
System.exit(0);
}
else if (index > numberUsed)
{
System.out.println(
"Error: Changing an index that is too large.");
System.exit(0);
}
else
a[index] = newValue;
}
public void deleteLast( )
{
if (empty( ))
{
System.out.println("Error:Deleting from an empty array.");
System.exit(0);
}
else
numberUsed--;
}
/**
Deletes the element in position index. Moves down all elements with
indices higher than the deleted element.
*/
public void delete(int index)
{
if (index < 0 || index >= numberUsed)
{
System.out.println("Error:Illegal or unused index.");
System.exit(0);
}
for (int i = index; i < numberUsed; i++)
a[i] = a[i + 1];
numberUsed--;
}
public boolean empty( )
{
return (numberUsed == 0);
}
public boolean full( )
{
return (numberUsed == maxNumberElements);
}
public int getMaxCapacity( )
{
return maxNumberElements;
}
public int getNumberOfElements( )
{
return numberUsed;
}
public boolean full(){
boolean b = false;
for(int i = 0; i < a.length; i++){
if(a[i] == null){
b = true;
}
}
}
private void resize(){
boolean b = false;
for(int i = 0; i < a.length; i++){
if(a[i] == null){
b = true;
}
}
if(!b){
double new_array[] = new double[a.length*2];
for(int i=0;i<a.length;i++){
new_array[i] = a[i];
}
a = new_array;
}
}
}

import java.util.Scanner;
/**
Demonstrates Using the class PartiallyFilledArray,
*/
public class GolfScoresVersion2
{
public static final int MAX_NUMBER_SCORES = 10;
/**
Shows the differences between each of a list of golf scores and their average.
*/
public static void main(String[] args)
{
PartiallyFilledArray score =
new PartiallyFilledArray(MAX_NUMBER_SCORES);
System.out.println("This program reads golf scores and shows");
System.out.println("how much each differs from the average.");
System.out.println("Enter golf scores:");
fillArray(score);
showDifference(score);
}
/**
Reads values into the PartiallyFilledArray a.
*/
public static void fillArray(PartiallyFilledArray a)
{
System.out.println("Enter up to " + a.getMaxCapacity( )
+ " nonnegative numbers, one per line.");
System.out.println("Mark the end of the list with a negative number");
Scanner keyboard = new Scanner(System.in);
double next = keyboard.nextDouble( );
while ((next >= 0) && (!a.full( )))
{
a.add(next);
next = keyboard.nextDouble( );
}
a.resize();
if (next >= 0)
System.out.println("Could only read in "
+ a.getMaxCapacity( ) + " input values.");
}
/**
Returns the average of numbers in the PartiallyFilledArray a.
*/
public static double computeAverage(PartiallyFilledArray a)
{
double total = 0;
for (int index = 0; index < a.getNumberOfElements( ); index++)
total = total + a.getElement(index);
if (a.getNumberOfElements( ) > 0)
{
return (total/a.getNumberOfElements( ));
}
else
{
System.out.println("ERROR: Trying to average 0 numbers.");
System.out.println("computeAverage returns 0.");
return 0;
}
}
/**
Gives screen output showing how much each of the
elements in the PartiallyFilledArray a differ from the average.
*/
public static void showDifference(PartiallyFilledArray a)
{
double average = computeAverage(a);
System.out.println("Average of the " + a.getNumberOfElements( )
+ " scores = " + average);
System.out.println("The scores are:");
for (int index = 0; index < a.getNumberOfElements( ); index++)
System.out.println(a.getElement(index) + " differs from average by "
+ (a.getElement(index) - average));
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote