Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I was wondering how to output the 'average' in my code below to 2 decimal places if the user enters a double but to

Hi, I was wondering how to output the 'average' in my code below to 2 decimal places if the user enters a double but to 0 decimal places if the user enters an integer. Here is my code:

public class Car {

private String name;

private String registration;

private String colour;

private int numberOfTrips;

public Car() {

}

public Car(String name, String registration, String colour, int numberOfTrips) {

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getRegistration() {

return registration;

}

public void setRegistration(String registration) {

this.registration = registration;

}

public String getColour() {

return colour;

}

public void setColour(String colour) {

this.colour = colour;

}

public int getNumberOfTrips() {

return numberOfTrips;

}

public void setNumberOfTrips(int numberOfTrips) {

this.numberOfTrips = numberOfTrips;

}

}

import java.util.*;

public class TestCar {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

Car c = new Car();

System.out.print("Input name: ");

String name = input.nextLine();

c.setName(name);

System.out.print("Input registration: ");

String registration = input.nextLine();

c.setRegistration(registration);

System.out.print("Input colour: ");

String colour = input.nextLine();

c.setColour(colour);

System.out.print("Input trips: ");

int numberOfTrips = input.nextInt();

c.setNumberOfTrips(numberOfTrips);

int[] carArray = new int[c.getNumberOfTrips() + 1];

for (int i = 0; i <= c.getNumberOfTrips(); i++) {

System.out.print("Odometer reading " + i + ": ");

int odometer = input.nextInt();

carArray[i] = odometer;

}

int longestDistance = carArray[0];

int shortestDistance = carArray[c.getNumberOfTrips()];

double sum = 0;

double average = 0;

// make changes in below for loop

for (int i = 1; i <= c.getNumberOfTrips(); i++) {

if (longestDistance < (carArray[i] - carArray[i - 1])) {

longestDistance = carArray[i] - carArray[i - 1];

}

if (shortestDistance > (carArray[i] - carArray[i - 1])) {

shortestDistance = carArray[i] - carArray[i - 1];

}

sum += carArray[i] - carArray[i - 1];

// double division

average = sum / (double) (c.getNumberOfTrips());

}

if (c.getNumberOfTrips() == 0) {

longestDistance = 0;

shortestDistance = 0;

average = 0;

}

String stringAverage = Double.toString(average);

stringAverage.replaceFirst("\\.0*$|(\\.\\d*?)0+$", "$1");

String formatAverage = stringAverage.indexOf(".") < 0 ? stringAverage

: stringAverage.replaceAll("0*$", "").replaceAll("\\.$", "");

System.out.print(" ");

System.out.println(c.getName() + " | " + c.getRegistration() + " | " + c.getColour());

System.out.println("Longest distance travelled: " + longestDistance);

System.out.println("Shortest distance travelled: " + shortestDistance);

System.out.println("Average distance travelled: " + formatAverage);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

Explain the process of biochemistry

Answered: 1 week ago