Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this question, you will be working within the Person class. public class Person { public Car car; public Person(Car c) { this.car = c;

For this question, you will be working within the Person class.

public class Person { public Car car; public Person(Car c) { this.car = c; } }

A Person has a field called car that refers to a Car object.

public class Car { // number of miles the car can go with a full tank private int totalMiles; // number of miles left before we need to get gas private int milesUntilGas; // initially has a full tank of gas public Car(int miles) { this.totalMiles = miles; this.milesUntilGas = miles; } /** * Drivess car the car the specified number of miles. * We can't drive more miles than we have gas for. * * @param miles, int number of miles to drive * @return true if car successfully drives, otherwise false */ public boolean drive(int miles) { if (miles <= this.milesUntilGas) { this.milesUntilGas = this.milesUntilGas - miles; return true; } return false; } /** * Represents filling up the gas tank of the car. */ public void fillUpGasTank() { this.milesUntilGas = totalMiles; } }

This Car class has two notable methods.

  • A drive method that represents driving the number of miles passed as a parameter (if possible)
  • a fillUpGasTank method that resents the number of miles the car can drive to its maximum.

For example, if we created a car object that could drive a maximum of 50 miles before needing gas, running drive(10) would mean we could drive 40 more miles before we need to get gas. We can see this is a boolean method where if we can drive the inputted amount, the method returns true.

If we tried to go further than the car could drive, though, this method would return false.

Car c = new Car(50); // the car can drive a maximum of 50 miles boolean firstDrive = c.drive(30); // returns true, can now drive 20 more miles boolean secondDrive = c.drive(30); // returns false - drive unsuccessful.

For this question, we will be creating a goToStore() method that should attempt to drive the number of miles passed in as a parameter.

Create a conditional to support the following logic:

  • If the first drive to the store is not successful (meaning didDrive is false), get gas and try to drive again

Your Answer:

public void goToStore(int milesToStore) { boolean didDrive = this.car.drive(milesToStore); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

To complete the goToStore method and implement the logic described you can modify the code as follow... 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

Organizational Behavior

Authors: Andrzej A. Huczynski, David A. Buchanan

8th Edition

273774816, 273774815, 978-0273774815

More Books

Students also viewed these Programming questions

Question

Calculate the missing values

Answered: 1 week ago