A method is a collection of statements that perform some specific task and return result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class which is different from languages like C, C++ and Python. Methods help us save time and reuse the code without retyping the code.
Methods are referred to by its name and can be called in the main class at any time after an object of the class is created. Methods are called using a (.) Separator; E.g ObjectName.MethodName().
This is a simple program that creates five methods and uses them to find the area of a triangle. The program has two classes, the method class and the Main class. The methods are created in the method class and then called in the Main class.
CODE BLOCK
METHODS CLASS
import javax.swing.*;
public class methods {
double base;
double height;
double Area;
public void setBase(double b) {
base = b;
}
public void setHeight(double h) {
height = h;
}
public double getBase() {
return base;
}
public double getHeight() {
return height;
}
public void Area() {
JOptionPane.showMessageDialog(null, "The Area of the Triangle is " + (0.5 * (getBase() * getHeight())));
}
}
MAIN CLASS
import javax.swing.*;
public class Main {
public static void main (String args[]) {
try {
methods Object = new methods();
String baseString = JOptionPane.showInputDialog("Enter Base Here");
if (baseString == null) {
JOptionPane.showMessageDialog(null, "You ended the program");
System.exit(0);
}
else {
String heightString = JOptionPane.showInputDialog("Enter Height Here");
if (heightString == null) {
JOptionPane.showMessageDialog(null, "You ended the program");
System.exit(0);
}
else {
double baseDouble = Double.parseDouble(baseString);
double heightDouble = Double.parseDouble(heightString);
Object.setBase(baseDouble);
Object.setHeight(heightDouble);
Object.Area();
}
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "You closed the program");
}
}
}
METHOD CLASS
import javax.swing.*;
This is an import statement. This statement allows us to import every method in the javax.swing Application Program Interface (API). Instead of importing one by one, we use the “*” to import all. We can then use anyone of the methods in the program.
public class methods {
This is just like creating a new file or document and saving it with a name. This is the name of the new class created.
double base;
This is a Variable Declaration of double type. double type variable declarations are decimal point numbers e.g 1.0, 2.4, 3.8, 4.234. This will hold the value assigned to base.
double height;
This is a Variable Declaration of double type. double type variable declarations are decimal point numbers e.g 1.0, 2.4, 3.8, 4.234. This will hold the value assigned to height.
double Area;
This is a Variable Declaration of double type. double type variable declarations are decimal point numbers e.g 1.0, 2.4, 3.8, 4.234. This will hold the value assigned to Area.
public void setBase(double b) {
This is a Method declaration with one argument.
public is a java keyword which means it can be accessed from anywhere inside the class or from another class.
Some Methods in Java returns a value, some methods don’t. The void keyword here means that this method will not return any value.
The method has one double type argument, this means it will hold a single double type value.
This method is called setBase. The method is created to hold the value that will be assigned to base.
public void setHeight(double h) {
Like the previous one, this is a Method declaration with one argument.
public is a java keyword which means it can be accessed from anywhere inside the class or from another class.
Some Methods in Java returns a value, some methods don’t. The void keyword here means that this method will not return any value.
The method has one double type argument, this means it will hold a single double type value.
This method is called setHeight. The method is created to hold the value that will be assigned to base.
public double getBase() {
This is also a Method declaration with no arguments, hence the curly braces at the end of the method name is empty.
Like the previous methods, this one is also public and can be accessed from anywhere.
Now this method is of type double. This means that the method will return a value of double type. Every Method with a type must return a value of the same type.
The method name is getBase, created to return the value of base.
return base;
This is the return statement for the getBase method. It returns a base value of double type.
public double getHeight() {
This method is similar to the last one. This is a Method declaration with no arguments as well, hence the curly braces at the end of the method name is empty.
Like the previous methods, this one is also public and can be accessed from anywhere.
This method is of type double. This means that the method will return a value of double type. Every Method with a type must return a value of the same type.
The method name is getHeight, created to return the value of base.
return base;
This is the return statement for the getHeight method. It returns a height value of double type.
public void Area() {
This method is quite different from the last ones. It is public and also void meaning it is accessed from anywhere and returns nothing.
This method is simply created to calculate the area of the Triangle and display the message on the screen.
The .showMessageDialog() inbuilt method helps us to display messages to the screen. The formulae for area of triangle 0.5baseheight is then interpreted here.
Since the getBase() method returns the base value, it is substituted for base in the formulae and the same is also done for height.
This method displays the Area of the triangle using the base and height the user provides.
MAIN CLASS
import javax.swing.*;
This is an import statement. This statement allows us to import every method in the javax.swing Application Program Interface (API). Instead of importing one by one, we use the “*” to import all. We can then use anyone of the methods in the program.
public class Main {
This is just like creating a new file or document and saving it with a name. This is the name of the new class created.
public static void main (String args[]) {
This is the program's Main Method. Every executable program must have a Main Method in order to execute.
try {
In Java, an Exception occurs when there is a problem during the normal flow of the program. This causes the program to end prematurely.
The process of preventing this from happening is called Exception Handling and it has two statements; try and catch. The try statement list the code in the normal working state while the catch statement states what should occur if a problem is encountered during execution.
methods Object = new methods();
In order to call the methods that were created in the Methods class, an Object of the methods class must be created. This line of code creates an object of the Methods class and the object name is called Object although it can be called anything the programmer wishes. The methods will now be called using this Object.
String baseString = JOptionPane.showInputDialog()
This is a variable declaration and assignment statement. The String variable is declared and is given a String value. This value will be stored as baseString.
if (baseString == null) {
This is an if Conditional Statement stating a condition.
This simply means "if the value stored in the variable baseString is null".
JOptionPane.showMessageDialog()
The .showMessageDialog() Method is called from the JOptionPane class. This method allows the user to display a pop up message to the screen.
System.exit(0)
This ends the program. Embedding this line of code in an if Statement shuts down the program if the condition stated in the if statement is true.
If the condition is false, the program proceeds to execute the next line of code which is usually an else Statement
else {
This is an else conditional statement. This statement is executed if the condition in the if statement is false.
String heightString = JOptionPane.showInputDialog()
This is a variable declaration and assignment statement. The String variable is declared and is given a String value. This value will be stored as heightString.
if (heightString == null) {
This is an if Conditional Statement stating a condition.
This simply means "if the value stored in the variable heightString is null".
JOptionPane.showMessageDialog()
The .showMessageDialog() Method is called from the JOptionPane class. This method allows the user to display a pop up message to the screen.
System.exit(0)
This ends the program. Embedding this line of code in an if Statement shuts down the program if the condition stated in the if statement is true.
If the condition is false, the program proceeds to execute the next line of code which is usually an else Statement.
else {
This is an else conditional statement. This statement is executed if the condition in the if statement is false.
Double.parseDouble()
The .parseDouble() method is used to convert String type variables to Double type variables. Here, the String variable baseString is being converted to a Double variable baseDouble.
Double.parseDouble()
The .parseDouble() method is used to convert String type variables to Double type variables. Here, the String variable heightString is being converted to a Double variable heightDouble.
Object.setBase(baseDouble)
The setBase() method is called here using the (.) separator. The Object of the methods class; Object is used to call the setBase method and a double argument baseDouble is given because the method was created with an argument of double type.
Object.setHeight(heightDouble)
The same thing happens here, The setHeight() method is called here using the (.) separator. The Object of the methods class; Object is used to call the setHeight method and a double argument baseDouble is given because the method was created with an argument of double type.
Object.Area()
Now that the two previous methods have been called and base and height have been set, the .Area method is then called to display the area of the triangle.
catch (Exception e) {
If during the course of executing the program an error is encountered, Exception catch handles the way the program behaves in response to the error.
JOptionPane.showMessageDialog()
The .showMessageDialog() Method is called from the JOptionPane class. This method allows the user to display a pop up message to the screen.
Putting this in an Exception catch statement causes the message to pop up if there is an error encountered.
To have a look at the codes, you can get it here, you can edit it if you want to try it out on your own.
User is prompted to enter base when program is run
User is prompted to enter height
The Area is then displayed on the screen
Source Code from GitHub Account
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for the approval. i put similar lines of code in code blocks, do you mean i put each line of code in code blocks?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This post has received a 0.45 % upvote from @drotto thanks to: @will-ugo.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @will-ugo I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit