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'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 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 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...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize these entries and post the closing entries 2. i need to prepare Dalhanis multi-step income statement and statement of owners equity for August 2010 3. i need to prepare the blance sheet at august 31,2010 4. i need to prepare a post-closing trial balance at august 31,2010 DALHANI makes all credit sales on terms 2/10 n/30 and uses the Perpetual Inventory System Aug 1...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize these entries and post the closing entries 2. i need to prepare Dalhanis multi-step income statement and statement of owners equity for August 2010 3. i need to prepare the blance sheet at august 31,2010 4. i need to prepare a post-closing trial balance at august 31,2010 DALHANI makes all credit sales on terms 2/10 n/30 and uses the Perpetual Inventory System Aug 1...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked...
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked to calculate the sum of all fines in the policeOfficer class from the arraylist i created. I modified the issueParking ticket method which i bolded at the very end to add each issued Parking ticket to the arrayList, i think thats the right way? if not please let me know. What I dont understand how to do is access the fineAmountInCAD from the arrayList...
Hello i am working on an assignment for my programming course in JAVA. The following is...
Hello i am working on an assignment for my programming course in JAVA. The following is the assignment: In main, first ask the user for their name, and read the name into a String variable. Then, using their name, ask for a temperature in farenheit, and read that value in. Calculate and print the equivalent celsius, with output something like Bob, your 32 degrees farenheit would be 0 degrees celsius Look up the celsius to farenheit conversion if you do...
I am working on a project that requires a measurement of acceleration. I have a ADXL345...
I am working on a project that requires a measurement of acceleration. I have a ADXL345 accelerometer, an aduino and a MAX7219 dot matrix display. I need help setting up a code for my aduino to display the acceleration from the ADXL345 accelerometer onto the MAX7219 display. Please help!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT