Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I'm having trouble completing this question. My output is incorrect, especially the longest and shortest distance traveled, as well as average distance traveled and

Hi, I'm having trouble completing this question. My output is incorrect, especially the longest and shortest distance traveled, as well as average distance traveled and I'm unsure how I can correct it. The question and my current code are both attached. Any help is appreciated.

----TestCar----

import java.util.Scanner;

public class TestCar {

public static void main(String[] args){

Scanner scan = new Scanner(System.in);

String string;

int trip,sum = 0;

Car auto = new Car();

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

string = scan.nextLine();

auto.setName(string);

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

string = scan.nextLine();

auto.setRegistration(string);

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

string = scan.nextLine();

auto.setColor(string);

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

trip = scan.nextInt();

auto.setTrips(trip);

auto.setOdometer();

int[] odometer = auto.getOdometer();

System.out.println(auto.getName()+" | "+auto.getRegistration()+" | "+auto.getColor());

System.out.println("Number of trips: "+auto.getTrips());

int longest = odometer[1] - odometer[0];

int shortest = odometer[1] = odometer[0];

for(int i=1;i

{

sum += odometer[i];

if(longest

longest = odometer[i] - odometer[i-1];

if(shortest>(odometer[i] - odometer[i-1]))

shortest = odometer[i] - odometer[i-1];

}

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

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

System.out.println("Average distance travelled: "+ (sum / auto.getTrips()));

}

}

----Car----

import java.util.Scanner;

public class Car {

public String name;

public String registration;

public String color;

public int numberOfTrips;

int[] odometer = new int[10];

public Car(){

name = "";

registration = "";

color = "";

numberOfTrips = 0;

}

public void setName(String n){

name = n;

}

public void setRegistration(String r){

registration = r;

}

public void setColor(String c){

color = c;

}

public void setTrips(int t){

numberOfTrips = t;

}

public void setOdometer(){

Scanner scnr = new Scanner(System.in);

for(int i=0;i

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

odometer[i] = scnr.nextInt();

}

}

public String getName(){

return name;

}

public String getRegistration(){

return registration;

}

public String getColor(){

return color;

}

public int getTrips(){

return numberOfTrips;

}

public int[] getOdometer(){

return odometer;

}

}

image text in transcribed
\f

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

Students also viewed these Programming questions