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

Need facebook developer, Project Description: i am seeking an experience...

Project Description: i am seeking an experienced Facebook page builder,fanpage builder, Header module Navigation menu module HTML module Widget module Image modul

Application for software tracking system, Talent agencies like ICM, CAA and...

Talent agencies like ICM, CAA and Paradigm represent writers, directors and actors (among other talent).  These agencies take in hundreds of millions of dollars a year collecting 1

HOW TO MAKE A TRANSCRIPT, Write a java program that computes the scores of ...

Write a java program that computes the scores of 10 students in the following units; 1.Java 2.PHP 3.Python 4.C++ 5.Perl Generate a transcript.

Web-based client program, Why are JSP pages the preferred API for creating ...

Why are JSP pages the preferred API for creating a web-based client program? Because no plug-ins or security policy files are required on the client systems (applet does). Also,

Explain difference between method overriding and overloading, What is the d...

What is the difference between method overriding and overloading? Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same met

Explain jsp technology?, Java Server Page is a standard Java extension that...

Java Server Page is a standard Java extension that is described on top of the servlet Extensions. The goal of JSP is the simplified formation and management of dynamic Web pages. J

How is method overriding different from method overloading, How is method o...

How is method overriding different from method overloading? Overriding involves the creation of two or more concepts with the same name and same signature in different classes

Write code in javascript to display the odd numbers, Write code in JavaScri...

Write code in JavaScript to display the odd numbers among 20 to 100 using FOR statement? Note: No required to write whole HTML program. Just JavaScript code of require elemen

Android app for evaluating of distance, Android app for evaluating of dista...

Android app for evaluating of distance Project Description: The request is to prepare an app for Android. The application needs to run on tablet (7 "or larger) and the layout

Determine the meaning of finalize, Determine the meaning of finalize W...

Determine the meaning of finalize When  a  finalize method  is  defined  in  a  class,  Java  run  time  calls  finalize() whenever  it's  about  to  recycle  an object  of  t

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