Question

Write a class called Taxicab that has three int fields (data members) to store its current...

Write a class called Taxicab that has three int fields (data members) to store its current x- and y-coordinates and the total distance it has driven so far (the actual distance driven by the Taxicab, not the Euclidean distance from it's starting point). The class should have a constructor that takes two parameters and uses them to initialize the coordinates, and also initializes the distance traveled to zero. The class should have a default constructor that sets all three fields to zero. The data members of this class do not need to be set after they are initialized, so this class doesn't need any set methods - therefore the constructors will directly assign values to the data members instead of calling set methods to do it. The class should have a get method for each data member. It should have a method called moveX that takes an int parameter that tells how far the Taxicab should shift left or right. It should have a method called moveY that takes an int parameter that tells how far the Taxicab should shift up or down. For example, the Taxicab class might be used as follows:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey, below is your class TaxiCab, i have mentioned comments where-ever necessary, also i have included TestTaxiCab class to test TaxiCab class.

TaxiCab.java

public class Taxicab {

private int currentX;

private int currentY;

private int totalDistance;

//Default constructor that sets all three fields to zero

public Taxicab() {

this.currentX = 0;

this.currentY = 0;

this.totalDistance = 0;

}

// Constructor which sets all current values of X and Y and also total distance as 0.

public Taxicab(int currentX, int currentY) {

this.currentX = currentX;

this.currentY = currentY;

this.totalDistance = 0;

}

public int getCurrentX() {

return currentX;

}

public int getCurrentY() {

return currentY;

}

public int getTotalDistance() {

return totalDistance;

}

public void moveX(int x) {

this.currentX = this.currentX + x;

this.totalDistance = this.totalDistance + Math.abs(x);

}

public void moveY(int y) {

this.currentY = this.currentY + y;

this.totalDistance = this.totalDistance + Math.abs(y); // add absolute value of y to total distance covered, as distance covered is independent of the direction of taxi.

}

}

TestTaxiCab.java

public class TestTaxiCab {

public static void main(String[] args) {

Taxicab taxicab = new Taxicab();

taxicab.moveX(14);

taxicab.moveX(-16);

taxicab.moveY(1);

taxicab.moveX(8);

taxicab.moveX(3);

taxicab.moveY(-6);

System.out.println(taxicab.getCurrentX());

System.out.println(taxicab.getCurrentY());

System.out.println(taxicab.getTotalDistance());

}

}

output

Thanks!!

Add a comment
Know the answer?
Add Answer to:
Write a class called Taxicab that has three int fields (data members) to store its current...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class named Taxicab that has three private data members: one that holds the current...

    Write a class named Taxicab that has three private data members: one that holds the current x-coordinate, one that holds the current y-coordinate, and one that holds the odometer reading (the actual odometer distance driven by the Taxicab, not the Euclidean distance from its starting point). The class should have an init method that takes two parameters and uses them to initialize the coordinates, and also initializes the odometer to zero. The class should have get members for each data...

  • Write a class called Player that has four data members: a string for the player's name,...

    Write a class called Player that has four data members: a string for the player's name, and an int for each of these stats: points, rebounds and assists. The class should have a default constructor that initializes the name to the empty string ("") and initializes each of the stats to -1. It should also have a constructor that takes four parameters and uses them to initialize the data members. It should have get methods for each data member. It...

  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

  • Write a class called Student. The specification for a Student is: Three Instance fields name -...

    Write a class called Student. The specification for a Student is: Three Instance fields name - a String of the student's full name totalQuizScore - double numQuizesTaken - int Constructor Default construtor that sets the instance fields to a default value Parameterized constructor that sets the name instance field to a parameter value and set the other instance fields to a default value. Methods setName - sets or changes the student name by taking in a parameter getName - returns...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • C++, entry level no pointers or vectors. Write a class called Person that has two data...

    C++, entry level no pointers or vectors. Write a class called Person that has two data members - a string variable called name and a double variable called age. It should have a constructor that takes two values and uses them to initialize the data members. It should have get methods for both data members (getName and getAge), but doesn't need any set methods. Write a separate function (not part of the Person class) called stdDev that takes two parameters...

  • List of Candles Create a class called CandleNode which has fields for the data (a Candle)...

    List of Candles Create a class called CandleNode which has fields for the data (a Candle) and next (CandleNode) instance variables. Include a one-argument constructor which takes a Candle as a parameter. (For hints, see the PowerPoint on "Static vs. Dynamic Structures”.) public CandleNode (Candle c) { . . } The instance variables should have protected access. There will not be any get and set methods for the two instance variables. Create an abstract linked list class called CandleList. This...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • Write a class definition for a Rectangle. The data fields should be: • An integer for...

    Write a class definition for a Rectangle. The data fields should be: • An integer for the value of the width of the Rectangle • An integer for the value of the height of the Rectangle An integer for the value of the area of the Rectangle It must have: . A default constructor A constructor that has a parameters for width and height and assigns them to the member variables. • The class should have mutators for all of...

  • Write a class named HalfOpenCylinder that has two data members: a double for the height in inches and a double for the radius in inches. It should have a constructor that takes two parameters and uses...

    Write a class named HalfOpenCylinder that has two data members: a double for the height in inches and a double for the radius in inches. It should have a constructor that takes two parameters and uses them to initialize the data members. It should have a default constructor that initializes the height to 10 and the radius to 2. It should have a method named surfaceArea that returns the the surface area (in square inches) of a cylinder with that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT