What is methods in java explain with an example, JAVA Programming

Assignment Help:

What is Methods in java explain with an example?

Data types aren't much use unless you can do things along with them. For this reasons classes have techniques. Fields say what a class is. Methods say what a class does. The fields and methods of a class are collectively referred to as the members of the class.

The classes you've encountered up till now have mostly had a single method, main(). Therefore, in general classes can have several different methods that do several different things. For example the Car class might have a method to form the car go as fast as it can. For instance,

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

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

}
The fields are the similar as before, other than now there's also a method known as floorIt(). It starts along with the Java keyword void that is the return type of the method. Every techniques must have a return type which will either be void or a few data type like int, byte, float, or String. The return type says what type of the value will be sent back to the calling method while all calculations inside the method are finished. If the return type is int, for instance, you can use the method anywhere you use an int constant. If the return kind is void then no value will be returned.

floorIt is the name of this techniques. The name is followed through two empty parentheses. Any arguments passed to the techniques would be passed among the parentheses, but this techniques has no arguments. Finally an opening brace ( { ) starts the body of the method.

There is one statement inside the method
this.speed = this.maxSpeed;
Remember that within the Car class the field names are prefixed along with the keyword this to denotes that I'm referring to fields in the current object.

Finally the floorIt() methods is closed with a } and the class is closed with another }.

Question: what are a few other methods this class might need? Or, another way of putting it, what might you want to do with a Car object?


Related Discussions:- What is methods in java explain with an example

Explain the term array and scope of variable, Explain the term array and Sc...

Explain the term array and Scope of Variable? Defining the space in that a variable is effective is known as describing the scope of a variable. A variable can be either local

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

Imagemagick or same as text png generation, Imagemagick or same as text png...

Imagemagick or same as text png/jpg generation expert needed Project Description: Require someone who is experienced with generating text images. You will use any of the soft

What do mean by synchronized and synchronized statements, What do mean by ...

What do mean by synchronized methods and synchronized statements ?

the file system simulator , • Study and enhance the File System Simulator ...

• Study and enhance the File System Simulator - See the FS_sim slide deck • New features - blockinfo - a new application that displays the information about the blocks in the file

Program for randomly generated 3- digit number matches, Program for Randoml...

Program for Randomly Generated 3- Digit Number Matches This assignment demonstrates your ability to use basic Java syntax including selection and looping statements. You will

What is an exceptions, What is an Exceptions ? • What is an exception?...

What is an Exceptions ? • What is an exception? • try-catch • finally • The different kinds of exceptions • Multiple catch clauses • The throws clause • Throwing excepti

Simple functional website without any graphic work, Want simple functional ...

Want simple functional website without any graphic work, like panel Project Description: I want a very simple look website .It should be like a panel. Few tab, contact form,

Differentiate between smtp and pop3 server, Differentiate between SMTP and ...

Differentiate between SMTP and POP3 server? SMTP (Simple Mail Transfer Protocol) is a protocol used to send and receive eMail messages over a TCP/IP network. While POP3 (Post

Decision structures, for $9.95 per month, 10 hours of access are provided.A...

for $9.95 per month, 10 hours of access are provided.Additional hours are $2.00 per hour

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