In this workshop you will modify the PartiallyFilledArray to resize the array if
ID: 3690362 • Letter: I
Question
In this workshop you will modify the PartiallyFilledArray to resize the array if it is full. Using files PartiallyFilledArray.java and GolfScoresVersion2.java (copied below)
1. Add a private method called resize that will double the size of the array. The sets to double the size of the array are:
1. Create a new array whose size is double the current array size.
2. Loop through the current instance variable array called "a" and copy all its elements to the new array.
3. Reassign array "a" to the new array.
2. Modify the add method to call resize() if the array is full.
3. Modify the fillArray() method of GolfScoresVersion2.java so it receives more scores than the original array size. (you don't need to include it in the solution)
import java.util.Scanner;
public class GolfScoresVersion2
{
public static final int MAX_NUMBER_SCORES = 10;
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);
}
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( );
}
if (next >= 0)
System.out.println("Could only read in "
+ a.getMaxCapacity( ) + " input values.");
}
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));
}
}
*********************************************************************************************
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;
}
}
Explanation / Answer
Please find the required solution:
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.
*/
// 2. Modify the add method to call resize() if the array is full.
public void add(double newElement) {
if (numberUsed >= a.length) {
/*
* System.out.println("Error: Adding to a full array.");
* System.exit(0);
*/
resize();
maxNumberElements = a.length;
} else {
a[numberUsed] = newElement;
numberUsed++;
}
}
// private method
private void resize() {
// 1. Create a new array whose size is double the current array size.
double[] newArray = new double[2 * a.length];
// 2. Loop through the current instance variable array called "a" and
// copy all its elements to the new array.
for (int i = 0; i < a.length; i++) {
newArray[i] = a[i];
}
// 3. Reassign array "a" to the new array.
a = new double[2 * a.length];
for (int i = 0; i < newArray.length; i++) {
a[i] = newArray[i];
}
}
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;
}
}
import java.util.Scanner;
public class GolfScoresVersion2 {
public static final int MAX_NUMBER_SCORES = 10;
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);
}
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();
boolean resize = true;
// modified the method to be resized once
while ((next >= 0) && (!a.full()) && resize) {
a.add(next);
next = keyboard.nextDouble();
if (a.full() && resize) {
resize = false;
}
}
if (next >= 0)
System.out.println("Could only read in " + a.getMaxCapacity()
+ " input values.");
keyboard.close();
}
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));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.