What is constraints explain with example, JAVA Programming

Assignment Help:

What is Constraints explain with example?

One of the purposes to use constructors and setter techniques rather than directly accessing fields is to enforce constraints. For example, in the Car class it's significant to make sure in which the speed is always less than or equal to the maximum speed and in which both speed and maximum speed are greater than or equal to zero.

You've already seen one instance of this in the accelerate() method which will not accelerate a car past its maximum speed.
void accelerate(double deltaV) {

this.speed = this.speed + deltaV;
if (this.speed > this.maxSpeed) {
this.speed = this.maxSpeed;
}
if (this.speed < 0.0) {
this.speed = 0.0;
}
}
You can also insert constraints such as that in the constructor. For instance, this Car constructor makes sure in which the maximum speed is greater than or equal to zero:
Car(String licensePlate, double maxSpeed) {

this.licensePlate = licensePlate;
this.speed = 0.0;
if (maxSpeed >= 0.0) {
this.maxSpeed = maxSpeed;
}
else {
maxSpeed = 0.0;
}
}


Related Discussions:- What is constraints explain with example

Explain the char data type in java, Explain the char data type in java? ...

Explain the char data type in java? A char is a single character in which a letter, a digit, a punctuation mark, a tab, a space or something same. A char literal is a single on

What are the advantages of multimedia presentations, What are the advantage...

What are the advantages of multimedia presentations? Write any five. Multimedia presentation is a excellent tool for effective communication: Advantages: 1. Easy to make l

Explain the term - garbage collection, Explain the term - Garbage Collectio...

Explain the term - Garbage Collection Java  performs  garbage  collection  and  eliminates  the  need  to  free  objects  explicitly.  When an object  has  no references   to

OOP, differentiate between states and behaviors of n object

differentiate between states and behaviors of n object

Which are three elements of event handler, The event handler attribute cons...

The event handler attribute consists of three elements. Write about each of them? 1. The identifier of the event handler. 2. The equal sign. 3. A string consisting of JavaScrip

Midlet game, do you have example of a simple midlet game ?

do you have example of a simple midlet game ?

Board coloring, write a java program to board coloring

write a java program to board coloring

Combo box and Check box help, Sal?s Pizza wants to install a system to reco...

Sal?s Pizza wants to install a system to record orders for pizza and Buffalo chicken wings. When regular customers call Sal?s Pizza on the phone, they are asked their phone number.

Jdbc, creating connection which will authenciate for a password

creating connection which will authenciate for a password

Explain about java sdk, Java SDK is not so comfortable with people used to ...

Java SDK is not so comfortable with people used to command line interpreter. IDEs contain compilers, editors, debugging facilities, drag and drop methods, etc. This compiler strive

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd