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

Photo upload feature to android app with google app engine, Include Photo U...

Include Photo Upload feature to Android App with Google App Engine Project Description: I have one small Android App skeleton that requires some features added to and backed

State the significance of public and private modifiers, State the significa...

State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by

Plug-in for adobe after effects, Plug-in for Adobe after Effects Project...

Plug-in for Adobe after Effects Project Description: We are seeking software engineers to create a plug-in for Adobe after Effects (CS4 and above) that allows 3D extrusions f

What are application softwares, What are application softwares? List down t...

What are application softwares? List down the names of three application software's. Definition: Application Software are in which software then can interact directory along

Create objects of student class by calling default, Create objects of stude...

Create objects of student class by calling default Create objects of student class by calling default, parameterize and copy constructor Call student class different methods on

Area under curve., write a program to find the area under the curve y=f(x) ...

write a program to find the area under the curve y=f(x) between x=a and x=b,integrate y=f(x) between the limits of a and b.   #include float start_point,

OOP, differentiate between states and behaviors of n object

differentiate between states and behaviors of n object

Write a note on java beans, Question 1 Explain the data types in Java ...

Question 1 Explain the data types in Java Question 2 Write a note on StringBuffer Question 3 What is Exception? Question 4 Write a note on stream classes Questi

Modify user profile class, Modify your UserProfile class to meet the follow...

Modify your UserProfile class to meet the following requirements: It must include a field called wall to store an ArrayList of wall posting objects. It must also provide

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