Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java make sure it is work1. Vehicle class Create a class called Vehicle that simulates a car moving along a 40 block stretch of road.

java make sure it is work1. Vehicle class

Create a class called Vehicle that simulates a car moving along a 40 block stretch of road.

Your class will build a vehicle and keep track of its location on the road. Location values may range from -20 to 20. A location value of 0 represents block 0, a location value of 1 represents block 1, a location value of 2 represents block 2, etc. If the user tries to move the vehicle beyond block +20 or -20, set the location to +/- 20 respectively.

In previous exercises, we had a requirement that you name your class Main. In this exercise, you must name your class Vehicle.

To test your code before submission, download the file student_runner_Vehicle.java to the same folder that holds your Vehicle.java implementation. Run the main method in the student_runner_Vehicle class and verify that the printed output matches the sample run output below. Please feel free to change the runner to test different values to make sure your program fits the requirements. We will use a similar runner class to grade the program.

When you are done coding and testing, copy and paste your entire Vehicle class into the Code Runner and press "Run" to submit the exercise. Use the "Check Answer" button to get additional feedback on your work.

Variables

int location - An integer that holds the current block location of the car on the road, with possible values ranging from -20 to 20.

Methods

Vehicle() - Set location to 0.

Vehicle(int loc) - If loc is between -20 and 20 inclusive, set location to loc. Otherwise, set location to 0.

void forward() - Increments the Vehicle forward one block. Do not let the user move past block 20.

void backward() - Increments the vehicle backward one block. Do not let the user move past block -20.

int getLocation() - Should return an integer representing the block location of the car.

String toString() - Returns a String representation of the car's location. It should print out spaces for all of the blocks from -20 to the car's current location, then print the '@' character. For example if the car is at block -10, the method will return the following String: " @" (10 spaces then the '@').

Sample Run

 @ @ @ @ @ 20 @ @ @ @ @ @ @ @ @ 0 @ -19 -20 -20 -20 

here is the code

class Vehicle { int location;

Vehicle() { location=0; } Vehicle(int loc) { if(loc < -20 || loc >20) location=0; else location=loc; } void forward() { if(++location > 20) location=20; } void backward() { if(--location < 20) location=-20; } public String toString() { String str=""; for(int i=-20; i<=location;i++) { if(i==location) { str+="@"; break; } else str+=" "; }

return str; }

}

// Your student_runner_Vehicle class should look like this public class student_runner_Vehicle { public static void main(String a[]) { Vehicle c = new Vehicle(18); System.out.println(c); c.forward(); System.out.println(c); c.forward(); System.out.println(c); c.forward(); System.out.println(c); c.forward(); System.out.println(c); }

}

here is the error

Compilation Error Main.java:265: error: cannot find symbol System.out.println(v3.getLocation()); ^ symbol: method getLocation() location: variable v3 of type Vehicle Main.java:274: error: cannot find symbol System.out.println(v1.getLocation()); ^ symbol: method getLocation() location: variable v1 of type Vehicle Main.java:282: error: cannot find symbol System.out.println(v2.getLocation()); ^ symbol: method getLocation() location: variable v2 of type Vehicle 3 errors 

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

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions

Question

How does an organization know if it is pursuing optimal strategies?

Answered: 1 week ago