Developer community
0 votes
16 views
asked by Guru (24.6k points) 31 57 92 | 16 views

1 Answer

0 votes
 
Parameters allow a method to be generalized by operating on a variety of data and/or be used in a number of slightly different situations.
Syntax
 
This is the general form of a method:
 
 
type methodName(parameterType variable,parameterType2 variable2,...) { 
   // body of method 
 
Example 1
 
A parameterized method can operate on a variety of data.
 
The new Rectangle class has a new method which accepts the dimensions of a rectangle and sets the dimensions with the passed-in value.
 
 
class Rectangle {
  double width;
  double height;
 
  double area() {
    return width * height;
  }
 
  void setDim(double w, double h) { // Method with parameters
    width = w;
    height = h;
  }
}
 
public class Main {
 
  public static void main(String args[]) {
    Rectangle mybox1 = new Rectangle();
    double vol;
    mybox1.setDim(10, 20);
 
    vol = mybox1.area();
    System.out.println("Area is " + vol);
 
  }
}
 
 
answered by Sr Member (3.9k points) 5 8 12

Related questions

0 votes
1 answer
asked by augustaina Sr Member (4.8k points) 10 26 32 | 97 views
0 votes
1 answer
asked by smith Geek (15.3k points) 18 43 68 | 96 views
0 votes
1 answer
asked by smith Geek (15.3k points) 18 43 68 | 19 views
0 votes
1 answer
asked by sara Guru (24.6k points) 31 57 92 | 14 views
0 votes
1 answer
asked by rock Expert (15k points) 15 23 32 | 32 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,754 questions
4,046 answers
117 comments
10,709 users