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

I want android expert, I want android expert Project Description: I h...

I want android expert Project Description: I have too many android project pending anyone who will start work right away, with me will bid here , only bid if you can work rig

Program with fibonacci sequence of first 20 terms, Program 1. Write a J...

Program 1. Write a Java program to display the first 20 Fibonacci numbers Program with Fibonacci sequence of first 20 terms Output 2. Write an applet progr

What is the role of the jms provider, The JMS provider handles security of ...

The JMS provider handles security of the messages, data conversion and the client triggering. The JMS provider verifies the level of encryption and the security level of the messag

Program to abstract syntax tree constructed, Please submit your answers    ...

Please submit your answers     In this homework, we are going to generate Java Virtual Machine codes by processing the abstract syntax tree constructed from the parsing process

Concurrent Programming, Problem 1 A savings account object holds a non-nega...

Problem 1 A savings account object holds a non-negative balance, and provides deposit(k ) and withdraw(k ) methods, where deposit(k ) adds k to the balance, and withdraw(k ) subtra

Translate four row solitaires from java swing to android, Translate Four Ro...

Translate Four Row Solitaires from Java Swing to Android. Project Description: There is a product known Four Row Solitaire listed in GitHub. I would like Swing GUI to be repl

Developing a prototype system in php and javascript, General Overview A...

General Overview A number of organisations are using micro-blogging as a cost-effective way of communicating with customers, building online communities and releasing informati

Collaboration diagrams, Collaboration diagrams are also communication diagr...

Collaboration diagrams are also communication diagrams. Collaboration diagrams give the same message as the sequence diagrams. But the collaboration diagrams depend on the object f

Develop a adobe air native extension, Develop a Adobe Air Native Extension ...

Develop a Adobe Air Native Extension Project Description: We are seeking someone that must create an adobe native extension for the subsequent SDK: Develop a Adobe Air Nat

Ajax- html- xml- css and tomcat used in java, AJAX- HTML- XML- CSS and  To...

AJAX- HTML- XML- CSS and  Tomcat used in Java: Project Title: Zee Ads   Role                       : Developer Domain                  : Web Ads Environment

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