<< Chapter < Page Chapter >> Page >

Writing your own methods

As mentioned earlier, you can write your own methods in Java. I mention this here so you will know that it is possible. I will have more to say about writing your own methods in futuremodules.

Sample program

A complete listing of a sample program named SqRt01.java is provided in Listing 5 near the end of the lesson. A batch file that you can use to compile and run the program is provided in Listing 6 .

When you compile and run the program, the following output should appear on your computer screen:

5.049752469181039 6.0

As you will see shortly, these are the square root values respectively for 25.5 and 36.

Interesting code fragments

I will explain portions of this program in fragments. I will explain only those portions of the program that are germane to this module. Don't worry about the other details of the program. You will learn about those details infuture modules.

You may find it useful to open this lesson in another browser window so that you can easily scroll back and forth among the fragments while reading the discussion.

The first code fragment that I will explain is shown in Listing 1 .

Listing 1 . The price of beans.
double beans; beans = 25.5;

What is the price of beans?

The code fragment shown in Listing 1 declares a variable named beans and assigns a value of 25.5 to the variable. (I briefly discussed the declaration of variables in a previous module. I will discuss them in more detail in a future module.)

What is that double thing?

In an earlier module, I declared a variable with a type named int . At that time, I explained that only integer values could be stored in that variable.

The variable named beans in Listing 1 is declared to be of the type double . I will explain the concept of data types in detail in a future module. Briefly, double means that you can store any numeric value in this variable, with or without adecimal part. In other words, you can store a value of 3 or a value of 3.33 in this variable, whereas a variable with a declared type of int won't accept a value of 3.33.

Every method has a name

Every method, every variable, and some other things as well have names. The names in Java are case sensitive . By case sensitive, I mean that the method named amethod is not the same as the method named aMethod .

A few words about names in Java

There are several rules that define the format of allowable names in Java. You can dig into this in more detail on the web if you like, but if you followthese two rules, you will be okay:

  • Use only letters and numbers in Java names.
  • Always make the first character a letter.

A standard method named sqrt

Java provides a Math library that contains many standard methods. Included in those methods is a method named sqrt that will calculate and return the square root of a number that is passed as a parameter when the method iscalled.

The sqrt method is called on the right-hand side of the equal sign (=) in the code fragment in Listing 2 .

Listing 2 . Compute the square root of the price of beans.
double sqRtBns = Math.sqrt(beans);

Questions & Answers

what are the factors effecting demand sedule
Kalimu Reply
we should talk about more important topics, you can search it on Google n u will find your answer we should try to focus on how we can improve our society using economics
so good night
ways of improving human capital
kelly Reply
what is human capital
Capital can be defined as man made assets use in production .
What is the differences between central Bank And Commercial Bank ?. 2 for each
Two types of bank clearing house.
what are the most durable assets of a bank
What is Opportunity Cost?
Cephas Reply
may be defined as expression of cost in terms of forgone alternative.
Helloo, im new, can i get to know more?
Saniya Reply
You ask questions on any topics you find difficult.
What is opportunity cost?
is price elasticity of demand the same as elasticity of demand
Favour Reply
not really
i hope everyone be ok
please explain
explanations please
price elasticity of demand is the reaction of customers /demand to price changes(increase or decrease) elasticity of demand is the reaction of prices brought about by the change in demand
thank you
state the laws of demand and supply
dd: when price rises demand decreases whereas when price reduces dd rises ss: when ss rises the price rises and when ss decreases price also reduces. There is a positive relationship
Draw a demand curve graph
though price elasticity and elasticity are used interchangeably, the demand can respond to income changes and prices of related goods as well.
explain the difference between merit goods and public goods and show why it is possible for profit to be made in the supply of one of these types of good but not the other
Public goods are defined as products where, for any given output, consumption by additional consumers does not reduce the quantity consumed by existing consumers. Merit goods are, for example, education and to some extent the health-care. They are provided by state as "good for you".
The ladies are doing much better than the men
what happens when there is a shift in demand curve?
What is Specialization ? Explain in detail
any one ?
specialisation is a method of production whereby an entity focuses on the production of a limited scope of goods to gain a greater degree of efficiency.
It's ok
No. price elasticity of demand refers to the manna in which price of good demanded fluctuate mean while elasticity of demand explains the way consumer change in their willingness as they plan or purchase a good
diffirence between demand and supply
what is economic
Seray Reply
It is a social science which studies human behavior as a relationship between ends and scarce which have alternative uses
what is norminal wage
Demba Reply
is the wages measured in money as distinct from actual purchasing power
what is demand curve
Azeez Reply
this is a curve that slop downward from left to rich
different between capital and wealth
Samuel Reply
Wealth refers to the amount of asset you have, while, capital is the amount of cash money you have with you now and willing to invest in any business.
What is scale of reference?
Finda Reply
What is monopoly?
It is the control of market by single seller or producer
the exclusive possession or control of the supply or trade in a commodity or services
what is scarcity
Bonny Reply
scarcity means that the resources which we can produce goods and services relatives to wants for them.
what is demand
Sophia Reply
demand means that's good demand according to your needs is called demand
needs of people ar called demand
what's the difference between opportunity cost and production possibility curve?
apportunity cost means a goods which can be replace by other goods without any ease of saticfaction
different between capital and wealth
apportunity cost means the profit lose when one alternative is selected over other
what is economocs
Bonny Reply
Economics is a science which studies human behaviour as a relationship between ends and scarce means which have alternative uses.
It deals with making choices in the face of scarcity
what is perfect complements?
Bilal Reply
explain the return to scale with the help of mathematical expression
what is scarcity
difference between fixed policy and monetary policies
Doris Reply
Difference between extinct and extici spicies
Amanpreet Reply
While the American heart association suggests that meditation might be used in conjunction with more traditional treatments as a way to manage hypertension
Beverly Reply
in a comparison of the stages of meiosis to the stage of mitosis, which stages are unique to meiosis and which stages have the same event in botg meiosis and mitosis
Leah Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?