How do I change the value of an attribute?

Suppose we have a class named as Train it has two constructors, two methods, and a single attribute each of wh?

  • Suppose we have a class named as Train it has two constructors, two methods, and a single attribute each of which described as follows: • One attribute: maxSpeed – an int value representing the maximum speed of the Train object. Note: Train speed of modern Trains may be something like 250 km per hour, so a value you could use for testing purposes is 250. • Two constructors: A default constructor: A constructor that initializes the attribute ms where ms is the maxSpeed. An overloaded constructor: A constructor that takes ms as an argument, where ms is the maxSpeed. • Two methods: getMaxSpeed() – return the maxSpeed value associated with the Train object. setMaxSpeed(ms) – change the maxSpeed value associated with the Train object, where ms is the new value. • Display the value through default constructor and overloaded constructor. • Display setter value through main function.

  • Answer:

    doing your homework for you won't help you learn the language. here is my rough thoughts, no compiler. public class Train{ private int maxSpeed = 250; public Train(){ } public Train(int maxSpeed){ this.maxSpeed = maxSpeed; } public int getMaxSpeed(){ return this.maxSpeed; } public void setMaxSpeed(int maxSpeed){ this.maxSpeed = maxSpeed; } } not sure what the two display notes mean

Imran Hameed ([email protected] at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.