Question

In: Computer Science

I'm working in Java and am working on a project where I need to find an...

I'm working in Java and am working on a project where I need to find an average. The catch is that for some of the values there is no data because they will be entered at a later date. I have variables assigned so that for each entry if there is an input I'll have it say _____available = 1, otherwise the variable will equal 0. I'll use an example to make this more clear. Let's say I am trying to find the average time of a car in a race. The car is set to run 5 races. He has run races 1, 3, and 5 but not 2 or 5. Or maybe he has run all 5. It would change depending on the user's input. How can I write an equation in Java that will find the average, no matter how many races the racer has run? I was thinking I would use an if statement, maybe a series of them that say if race1Available = 1 then add the time for race 1 to total time, if not don't add it. And I would need to do that for all 5 races. I would also need to do that for the total number of races so that if I only run 3 races, then I would divide the total time by 3.

Solutions

Expert Solution

Update: pls see new code added at bottom. carAvg2.java

We don't need to have two separate variables to keep track of
availability and time

we can only store Time, if its 0 means not available
as follows

Thanks

//------- carAvg.java
import java.util.Scanner;

public class carAvg {
        
        public static void main (String args[]) {
                
                int count = 0;
                double total = 0;
                double[] times = {0,0,0, 0,0};
                
                Scanner sc = new Scanner(System.in); //to read input
                System.out.println("Enter race time, otherwise enter 0 if race NOT run");
                
                for (int i = 0; i < times.length ; i++)
                {
                        System.out.print("enter Race " + (i+1) + ": ");
                        times[i] = sc.nextDouble();
                        
                        if (times[i] > 0)
                        {
                                //increase count if race time entered
                                count++;
                                
                                //also add time to the total
                                total = total + times[i];
                        }                                                                       
                }
                System.out.println("Average time for "  + count + " races= " + (total/count));          
                
                //Use following to display only 2 decimal places like 35.41 instead of 35.41456789
                //String strAvg = String.format("%.2f", total/count);           
                //System.out.println("Average time for "  + count + " races= " + strAvg);               
        }
}

//------- end of carAvg.java

Output

Added following code to Ask before entering race time

//------- carAvg2.java
import java.util.Scanner;

public class carAvg2 {
        
        public static void main (String args[]) {
                
                int count = 0;
                double total = 0;
                double[] times = {0,0,0, 0,0};
                
                Scanner sc = new Scanner(System.in); //to read input
                                
askNext:for (int i = 0; i < times.length ; i++)
                {
                        do {
                                System.out.print("Do you want to run Race" + (i+1) + "? y/n: ");
                                String ch = sc.next();
                                if ( ch.equals("n") )
                                {
                                        continue askNext;       // askNext is label of for loop 
                                }
                                else if ( ch.equals("y") )
                                {
                                        break;  //valid input so ask for time 
                                }                                                               
                        } while(true);
                        
                        do{
                                System.out.print("Enter time for Race" + (i+1) + ": ");
                                times[i] = sc.nextDouble();
                                
                                if (times[i] >= 0)
                                {
                                        //increase count if race time entered
                                        count++;
                                        
                                        //also add time to the total
                                        total = total + times[i];
                                }                                               
                        } while( times[i]<0 ); //repeat if -ve value entered
                }
                System.out.println("Average time for "  + count + " races= " + (total/count));          
                
                //Use following to display only 2 decimal places like 35.41 instead of 35.41456789
                //String strAvg = String.format("%.2f", total/count);           
                //System.out.println("Average time for "  + count + " races= " + strAvg);               
        }
}

//------- end of carAvg2.java

new output


Related Solutions

I am working java project. I need to write methods for text game. I'm working on...
I am working java project. I need to write methods for text game. I'm working on chest class. Description : You should start your work by implementing the Chest class (a skeleton is provided for you). It needs to store whether it is locked or not, a String describing the contents, and which key it was locked with. The method stubs for this class are provided with comments, be sure to implement them the way they are described. You should...
I'm working on a project where I need to design a transmission line tap for a...
I'm working on a project where I need to design a transmission line tap for a station generator. The parameters include sizing three switches for the tap where at least one is a loop break switch. From where to start? What kind of calculations are needed to size the switches? What are those switches?
I am working on a project where I need to close the solenoid valve (24DCV) at...
I am working on a project where I need to close the solenoid valve (24DCV) at a different rate of time (variable closer time). It needs close very fast (at least within 50ms). I am using NI DAQ: USB-6211. Which relay should I use? Will digital relay timer work? Looking forward to your suggestions.
i'm working on this assignment where we must simulate a bank account. I am having a...
i'm working on this assignment where we must simulate a bank account. I am having a few issues. The first issue is that for menu option 1 i am struggling to make the program so that I can add the name and balance of the account all at once. For example I would type "mike 350" and it would update the dictionary with a new account and balance, right now i have to open the account then deposit a balance...
I need this in java using textpad. I am missing a few lines where I added...
I need this in java using textpad. I am missing a few lines where I added in comments. I don't know what I need to add in. Here are the two programs as pasteable code.The comments in the code say what I need done. The two programs are below. I need it to work with the generic version of SLLNode. It is posted at the bottom. public class ListDemoHw { public static void printLinkedList(SLLNode node) { // display all elements...
I need this in java using textpad. I am missing a few lines where I added...
I need this in java using textpad. I am missing a few lines where I added in comments. I don't know what I need to add in. Here are the two programs as pasteable code.The comments in the code say what I need done. The two programs are below. public class ListDemoHw { public static void printLinkedList(SLLNode node) { // display all elements in the linked list while(node != null) { System.out.print(node.info + " "); node = node.next; // move...
in Java, I am writing a program where you need to pick a color. The color...
in Java, I am writing a program where you need to pick a color. The color must be red, blue, or green and is a user input. If they put in a different value I need the program to stop running. So I essentially need to write code that says, if variableColor does not equal "red", "blue", or "green" then I need to print the statement "invalid response" and exit the program. How can I write this?
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
I am working on a project where I have to create my own organization (stem cell...
I am working on a project where I have to create my own organization (stem cell research) I need to go further in depth about the program or organization in terms of describing the program implementation and the project evaluation...
How do I write this method correctly? What Ihave is a Java project assignment where I'm...
How do I write this method correctly? What Ihave is a Java project assignment where I'm supposed to create a Roshambo game. One of the classes that the project is supposed to have is a class called "RoshamboApp that let the user play 1 of 2 AI opponents, a "Bart" class, and a "Lisa" class. The instructions say "Create a class names RoshamboApp that allows player1 to play Bart or Lisa as shown in the console output. Rock should beat...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT