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

Prepare a java look and feel theme from html template, Prepare a Java Look ...

Prepare a Java Look and Feel Theme from HTML Template Project Description: For this project you would be needed to create a Java LAF (Look and Feel) Theme from this HTML Y

What is object oriented programming explain with an example, What is Object...

What is Object Oriented Programming explain with an example? In Java everything is an object or a class (or a piece of one or a collection of various). Objects send messages t

To change c code and place into a java application, To change c code and pl...

To change c code and place into a java application Project Description: To change c code and place into a java application Skills required are C Programming, Java, Softwar

Confidentiality and privacy - security, The confidentiality and privacy may...

The confidentiality and privacy may be accomplished through encryption. Encryption may be: Private-key or symmetric: This is based on a single key. This needs the sender and the

Describe logical operators in java, Describe Logical Operators in Java ? ...

Describe Logical Operators in Java ? The relational operators you've learned so far ( , >=, !=, ==) are enough while you only required to check one condition. Therefore what if

Program, Write a program called Power that displays the positive powers of ...

Write a program called Power that displays the positive powers of 2. When the user enters the exponent at a prompt, the program displays 2 to that power. The program halts when the

Explain drawing rectangles in java, Explain Drawing Rectangles in java ? ...

Explain Drawing Rectangles in java ? Drawing rectangles is easy. Begin with a Graphics object g and call its drawRect() method: public void drawRect(int x, int y, int width, i

Java developer with spring framework prerequisite, Java developer with spri...

Java developer with spring framework prerequisite from anywhere Let me know that you have proven experience in java with spring frame work. Send some sample data and provide inf

Difference between boolean & operator and && operator, How can we define th...

How can we define the difference between the Boolean & operator and the && operator ?

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