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

Bluej, how to write a program to assign value of variable age and print if ...

how to write a program to assign value of variable age and print if person is major.

Converting strings to numeric primitive data types, Converting Strings to N...

Converting Strings to Numeric Primitive Data Types To convert a string which is containing digits to a primitive data type, wrapper classes can help. parseXxx method can be u

Develop a new e- payment gateway, Project Description: I need a new paym...

Project Description: I need a new payment gateway of my own. I mean new payment gateway not any integration. So please bid only if you had made a payment gateway former like

Polymorphism, Polymorphism - defines the ability of a single variable of ...

Polymorphism - defines the ability of a single variable of a provided type to be used to reference objects of different types, and automatically call the method that is specific

Build KNN Classifier in Java, Can you help? this assignment I will need to ...

Can you help? this assignment I will need to build a very straightforward and fully functional machine learning classifier with the K-Nearest Neighbor (KNN) algorithm. The KNN mode

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

Explain the reading documentation for a class in the class, Explain the Rea...

Explain the Reading documentation for a class in the class library For instance, let's suppose you need to use the URL class in the java.net package. Through reading the docume

Explain nested classes (or outer and inner classes) in java?, In Java not a...

In Java not all classes must be described separate from each other. You may put the definition of one class under the definition of another class. The class inside class is called

Intro to Programming, Overall Requirements Every phone number is broken up ...

Overall Requirements Every phone number is broken up into sections as shown below: Country Code Area Code Prefix Line Number 1 919 882 5000 Write a program to separate out a ph

JAVA Programming, Im not in the line of computer science.But i would like t...

Im not in the line of computer science.But i would like to have some knowledge about the computer language "JAVA".I aldredy learns the computer languages "C,C++".So kindly help me

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