Question

Download PartiallyFilledArray.java. Though we studied this in class, you should still take several minutes examining the...

Download PartiallyFilledArray.java. Though we studied this in class, you should still take several minutes examining the code so that you understand the methods of the class.

/**
* This is a solution to the lab, "partially filled array".
*
*
*
*/
import java.util.Scanner;

public class CalculateAverage
{
public static void main(String[] args){
PartiallyFilledArray data = new PartiallyFilledArray();
getInput(data);
printResults(data);
}
  
private static void getInput(PartiallyFilledArray data) {
Scanner kbd = new Scanner(System.in);
System.out.print("Enter a number (negative will end input):");
double value = kbd.nextDouble();
while (value >= 0.0) {
data.add(value);
System.out.print("Enter a number (negative will end input):");
value = kbd.nextDouble();
}
}   
  
private static void printResults(PartiallyFilledArray data) {
int numData = data.getNumberOfElements();
double sum = 0.0;
if (numData == 0) {
System.out.println("You entered no values, so average is 0.0.");
return;
}
  
System.out.print("You entered: ");
for (int i=0; i<numData; i++) {
System.out.print(data.getElement(i)+", ");
sum+=data.getElement(i);
}
System.out.println("\nThe average is "+(sum/(double)numData));
}
}




Create a class, CalculateAverage, that prompts the user for doubles until the user enters a negative number, and then prints all the values entered by the user (not including the negative value), followed by the average of those values. Your program should use a PartiallyFilledArray to store and access the values the user provides. You may assume that the user will not enter more than 10 doubles. If the user enters only a negative value, the "average" of the values entered should be 0.

You will want to use several methods of the PartiallyFilledArray class, including an appropriate constructor, getNumberOfElements(), add(), and getElement().

Submit your CalculateAverage.java file.

Example output1:

Enter a number (negative will end input):3
Enter a number (negative will end input):5
Enter a number (negative will end input):-1
You entered: 3.0, 5.0,
The average is 4.0

Example output2:

Enter a number (negative will end input):-1
You entered no values, so average is 0.0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Here is the completed code for this problem.  Let me know if you have any doubts or if you need anything to change.

Thank You !!

================================================================================================

public class PartiallyFilledArray {

    private double[] array;
    private int elementCount;


    public PartiallyFilledArray() {
        array = new double[1];
        this.elementCount = 0;
    }

    public void add(double value) {

        if (elementCount == array.length) {
            double[] tempArray = new double[elementCount * 2];
            for (int i = 0; i < elementCount; i++) tempArray[i] = array[i];
            array = tempArray;
        }
        array[elementCount++] = value;

    }

    public int getNumberOfElements() {

        return elementCount;

    }

    public double getElement(int i) {
        if (0 <= i && i < elementCount) {
            return array[i];
        } else {
            return -1;
        }
    }
}

=======================================================================================

Add a comment
Know the answer?
Add Answer to:
Download PartiallyFilledArray.java. Though we studied this in class, you should still take several minutes examining the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • You will need to think about problem solving. There are several multi-step activities. Design, compile and...

    You will need to think about problem solving. There are several multi-step activities. Design, compile and run a single Java program, StringEx.java, to accomplish all of the following tasks. Add one part at a time and test before trying the next one. The program can just include a main method, or it is neater to split things into separate methods (all static void, with names like showLength, sentenceType, lastFirst1, lastFirst), and have main call all the ones you have written...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • ******** IN JAVA ********* I have a program that I need help debugging. This program should...

    ******** IN JAVA ********* I have a program that I need help debugging. This program should ask a user to input 3 dimensions of a rectangular block (length, width, and height), and then perform a volume and surface area calculation and display the results of both. It should only be done using local variables via methods and should have 4 methods: getInput, volBlock, saBlock, and display (should be void). I have most of it written here: import java.util.*; public class...

  • specs for ComputeAverage ComputeAverage Write a class called ComputeAverage what it does: asks the user to...

    specs for ComputeAverage ComputeAverage Write a class called ComputeAverage what it does: asks the user to enter three double numbers (see examples below), it uses Scanner class to read from standard input each one of the doubles entered by the user. it then prints the average of the three numbers. Suggested steps: 1. prompt the user to enter each of the three doubles by printing. 2. read each of the three doubles using a Scanner. Remember you need to declare...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

  • This is the assignment..... Write a class DataSet that stores a number of values of type...

    This is the assignment..... Write a class DataSet that stores a number of values of type double. Provide a constructor public DataSet(int maxNumberOfValues) and a method public void addValue(double value) that add a value provided there is still room. Provide methods to compute the sum, average, maximum and minimum value. ​This is what I have, its suppose to be using arrays also the double smallest = Double.MAX_VALUE; and double largest = Double.MIN_VALUE;​ are there so I don't need to create...

  • ANS (2) 0R hat output is produced by the following code if user enters these values...

    ANS (2) 0R hat output is produced by the following code if user enters these values for a question asked by the progran .User entered 3 Enter a positive integer value! (enter -1 to end) :3 Enter a positive integer value! (enter -1 to end) five Enter a positive integer value! (enter -1 to end) :7 Enter a positive integer value! (enter -1 to end) :-1 User entered five -User entered 7 ...User entered -1 import java.util.Scanner: class Verify input...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT