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
  • PLEASE LET ME KNOW WHERE I AM GOING WRONG IN THIS CODE. Allow a user to...

    PLEASE LET ME KNOW WHERE I AM GOING WRONG IN THIS CODE. Allow a user to enter any number of double values up to 20. The user should enter 99999 to quit entering numbers. Display an error message if the user quits without entering any numbers; otherwise, display each entered value and its distance from the average. My code is below: import java.util.Scanner; public class DistanceFromAverage { public static void main(String[] args) { // declaration of values as double type...

  • 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...

  • 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();      ...

  • 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...

  • 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...

  • Write a Java method that should take an ArrayList as a parameter, print its element in...

    Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen {   public static void main(String[] args) {     Scanner input = new Scanner(System.in);       System.out.println("How many numbers do you want to input?");       int num = input.nextInt();       ArrayList<Double> d = new ArrayList<Double>(num);       for(int i = 0 ; i < num; i++){           System.out.print("Enter...

  • ******** 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...

  • Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the...

    Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the mean and standard deviation of these numbers I already have the source code all done. I just need this translated into a flowchart. Again I just need the flowchart, not any source code. I have already provided the source code below. Need Flowchart version of my code Here is the source code: public class Mean_Standard_Deviation {    public static void main(String[] args) {   ...

  • 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...

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