How to returning multiple values from methods, JAVA Programming

Assignment Help:

How to Returning Multiple Values From Methods ?

It is not probable to return more than one value from techniques. You cannot, for example, return the licensePlate, speed and maxSpeed fields from a single technique. You could merge them within an object of a few type and return the object. Thus this would be poor object oriented design.

The right way to solve this problem is to describe three separate methods, getSpeed(), getMaxSpeed(), and getLicensePlate(), each of that returns its respective value. For example,
class Car {

String licensePlate = ""; // e.g. "New York 543 A23"
double speed = 0.0; // in kilometers per hour
double maxSpeed = 123.45; // in kilometers per hour

// getter (accessor) methods
String getLicensePlate() {
return this.licensePlate;
}

double getMaxSpeed() {
return this.maxSpeed;
}

double getSpeed() {
return this.speed;
}

// setter method for the license plate property
void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}

// setter method for the maxSpeed property
void setMaximumSpeed(double maxSpeed) {
if (maxSpeed > 0) this.maxSpeed = maxSpeed;
else this.maxSpeed = 0.0;
}

// accelerate to maximum speed
// put the pedal to the metal
void floorIt() {
this.speed = this.maxSpeed;
}

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;
}
}
}


Related Discussions:- How to returning multiple values from methods

Describe what is meant by a constructor in java, Question: (a) Describe...

Question: (a) Describe what is meant by a ‘constructor' in Java. Use a simple example to illustrate your answer. (b) Differentiate between a class method and an instance me

Boggle Game, any one out there with Boggle Source code?......i really need ...

any one out there with Boggle Source code?......i really need it guys please.

What is the use of wrapper class, What is the use of Wrapper class You...

What is the use of Wrapper class You can create an object of Wrapper class using a String or a primitive data type Integer num = new Integer (4); or Integer num = n

What is xmlbeanfactory, BeanFactory has many executions in Spring. But one ...

BeanFactory has many executions in Spring. But one of the most useful one is org.springframework.beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions ha

Area under the curve, Write a program to find the area under the curve y = ...

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. The area under a curve between two points can b

What is alternate text, What is Alternate Text ? The APPLET element ca...

What is Alternate Text ? The APPLET element can have an ALT attribute. An ALT attribute is used through a browser which understands the APPLET tag but for some purpose cannot

Arraya, what is arrays and how do they operate

what is arrays and how do they operate

State about the jvm, State about the JVM? JVM is the acronym which ...

State about the JVM? JVM is the acronym which stands for 'Java virtual machine'. JVM provides execution environment. JVM isn't platform independent. JVM is Java

Ejb container security?, EJB elements operate inside a container environmen...

EJB elements operate inside a container environment and rely heavily on the container to give security. The four key services needed for the security are: 1. Identification :

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