import java.io.*; class PowerIt1 { public static void main(String[] args) throws IOException { int n,m; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter number n : "); n = Integer.parseInt(in.readLine()); System.out.print("Enter number m : "); m = Integer.parseInt(in.readLine()); System.out.print(n+" to the power of "+m+" is: "); System.out.println(power(n,m)); } public static int power(int n, int m) // Calculates n to the power of m iteratively { int acc=1, count=0; while(count!=m) { acc=acc*n; count=count+1; } return acc; } }As explained in those notes, the important part of this code for the Algorithms and Data Structures course is the method called
power
.
The method called main
is just given because you always
need a method called main
with a single parameter of type
String[]
to start up a Java program. It includes some code
which causes text to be read from the window the command to start the
Java program was issued from. This is OF NO IMPORTANCE WHATSOEVER
to the Algorithms and Data Structures course, since this course is
about algorithms and data structures and not about the more detailed
aspects of the Java language.
What happens here is that the statement:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));causes a
BufferedReader
object to be created and referred
to by the name in
. We needn't have called it in
.
If we wanted to call it buf
we would have used the
statement:
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));Having declared and initialised our
BufferedReader
object
in this way, if it is called in
then the call
in.readLine()
will return as a Java String
the next line of text typed in the command window, up to but not including
the end-of-line. Obviously, if we had decided to call the
BufferedReader
variable by the name buf
rather than by the name in
, to read the next line of text
typed into the command window and return it as a string we would use the
call buf.readLine()
.
Since the type BufferedReader
is defined in the Java
library package called java.io
, to use it the line
import java.io.*;needs to be included in the file, before the word
class
which starts the definition of the class. Another point is that as
the call in.readLine()
can cause something called an
IOException
to be thrown, the header to a method which
calls it must include following the closing )
the
"throws declaration" written throws IOException
.
Actually, you could just put throws Exception
since
Exception
is a general term which includes the specialised
form of exception called IOException
. Also, instead of
having a "throws declaration", you could have "caught" the exception
using a try-catch
statement, but this is an aspect of Java
you won't have covered yet.
If the variable str
of type String
is a
string consisting of digits only, then Integer.parseInt(str)
returns the actual integer which the string referred to by str
holds. That is why:
n = Integer.parseInt(in.readLine());reads a line of text, converts it to an integer, and puts the integer in the variable
n
. There was no need to have a separate
variable which is used just to hold the value returned by
in.readLine()
and pass it as the argument to the call
to Integer.parseInt
. However, it would cause no harm to
have one. If we alredy had a variable called str
of
type String
declared at this point but not used for anything
else, we could use the code:
str = in.readLine(); n = Integer.parseInt(str);If we didn't have a variable called
str
, we could
declare one and set its value in one statement, making the code:
String str = in.readLine(); n = Integer.parseInt(str);
import javax.swing.*; class PowerGraphics1 { public static void main(String[] args) { int n,m; String input = JOptionPane.showInputDialog("Enter number n : "); n = Integer.parseInt(input); input = JOptionPane.showInputDialog("Enter number m : "); m = Integer.parseInt(input); System.out.print(n+" to the power of "+m+" is: "); System.out.println(power(n,m)); System.exit(0); } public static int power(int n, int m) // Calculates n to the power of m iteratively { int acc=1, count=0; while(count!=m) { acc=acc*n; count=count+1; } return acc; } }As you can see, it involves just as much gobbledygook as getting your input from the command window.
For some explanation of this, if str
is a string
value, then JOptionPane.showInputDialog(str)
causes
a separate small window to open with the wording referred to by
str
in it, and below that a box in which text may be typed.
The call returns that text into the program. In the above code,
rather than use a separate variable to hold the wording that is used
in the box, a string constant is passed as the argument. A new variable
of type String
called input
is declared
and set to refer to the text typed in the separate small window.
As with the previous example, we have to convert the string typed in to
an integer using Integer.parseInt
. The variable
input
is then reused to refer to a second string of
text types into another small window in the same way.
Note that the Java library the code for using these small windows is
in is called javax.swing
, hence the import statement at
the start of the code. Unlike calling readLine
on a
BufferedReader
, calling JOptionPane.showInputDialog
cannot cause a checked exception to be thrown, so a throws declaration
is not required. However, note that the statement System.exit(0);
is required at the end of the main
method. This is because
calling JOptionPane.showInputDialog
sets up some independent
code that would carry on executing even when the end of the
main
method was reached, unless it is explicitly halted
using System.exit(0)
.
Last modified: 12 December 2002