Download Java without BlueJ

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java without BlueJ
public static void main (String[] args)
Java without BlueJ
BlueJ is an Integrated Development Environment (IDE) for Java.
BlueJ provides an editor and access to the standard Java compiler
and run-time system. Its innovation lies in its capabilities for
creating objects and invoking methods on them interactively
(supporting the passing of arguments and return of results). This
works through a graphical representation of classes and object
instances and simple point-and-click operation. This lets us
immediately start testing newly written classes without having
to write any testing code. Inspectors let us see running results.
Additionally, BlueJ provides a mechanism for recording a series
of interactive tests on a class together with user-defined correct
results for those tests. These recordings can be re-run (with the
results checked automatically) on the class each time the class
code is changed – regression testing. As code is continually
6
r
te
being maintained, this is a huge help!
p
a
h
C
Java without BlueJ
But what if we don’t have BlueJ?
How do we get anything written, compiled and run?
How do we test stuff?
Write (using any editor
editor)) each Java class in a separate file,
whose name is the class name with the suffix: .java
For example, the Date class goes in the file Date.java
To compile, we need a command window (e.g. Command
Prompt in Windows Accessories – or any Unix window).
Change directory ((cd)
cd) to wherever the .java files are.
Java without BlueJ
But what if we don’t have BlueJ?
How do we get anything written, compiled and run?
How do we test stuff?
Change directory ((cd)
cd) to wherever the .java files are.
To compile, use the command: javac Date.java
If no errors, this produces the file: Date.class
.class files contain Java byte code
’t try to look at it
code.. Don
Don’t
– these files don
’t hold anything humans can read!
don’t
Java without BlueJ
But what if we don’t have BlueJ?
How do we get anything written, compiled and run?
How do we test stuff?
.class files contain Java byte code
’t try to look at it
code.. Don
Don’t
– these files don
’t hold anything humans can read!
don’t
How do we create class objects and execute their methods
?
methods?
The standard Java Development Kit (JDK) provides no way
(outside of a Java program) to construct objects
objects!! But it does
have a way to invoke a (very particular) static method –
for which, of course, we only need the class (not an object
).
object).
Java without BlueJ
class DemoMain {
public static void main (String[] args
)
args)
{
}
}
Only a static method called main
main,, with exactly the
above signature
signature,, can be run with the standard command.
Java without BlueJ
class DemoMain {
/**
* Print arguments supplied to this program.
*/
public static void main (String[] args
)
args)
{
for (String s : args
) {
args)
System.out.println (s);
}
}
}
Only a static method called main
main,, with exactly the
above signature
signature,, can be run with the standard command.
Java without BlueJ
class DemoMain {
/**
* Print arguments supplied to this program.
*/
public static void main (String[] args
)
args)
{
for (String s : args
) {
args)
System.out.println (s);
}
}
}
% javac DemoMain.java
% java DemoMain
compile
invoke main
Java without BlueJ
class
class DemoMain
DemoMain {{
/**
/**
** Print
Print arguments
arguments supplied
supplied to
to this
this program.
program.
*/
*/
public
)
public static
static void
void main
main (String[]
(String[] args
args)
{{
for
) {{
for (String
(String ss :: args
args)
System.out.println
System.out.println (s);
(s);
}}
}}
}}
compile
%% javac
javac DemoMain.java
DemoMain.java
%% java
java DemoMain
DemoMain
invoke main
%% java
-b ––three
three
java DemoMain
DemoMain one
one two:a
two:a-b
one
one
two:a
-b
two:a-b
––three
three
%%
Java without BlueJ
For example, the BlueJ project tech-support-complete:
SupportSystem
InputReader
Responder
With BlueJ
BlueJ,, we make a SupportSystem object, then click
the start() method.
Java without BlueJ
For example, the BlueJ project tech-support-complete:
With BlueJ
BlueJ,, we make a SupportSystem object, then click
the start() method. Without BlueJ
BlueJ,, we have to program:
class TechSupportMain {
/**
* Print arguments supplied to the program
*/
public static void main (String[] args
)
args)
{
new SupportSystem ().start ();
}
}
Java without BlueJ
class
class TechSupportMain
TechSupportMain {{
/**
/**
** Print
Print arguments
arguments supplied
supplied to
to the
the program
program
*/
*/
public
)
public static
static void
void main
main (String[]
(String[] args
args)
{{
new
new SupportSystem
SupportSystem ().start
().start ();
();
}}
}}
compile
%% javac
javac TechSupport.java
TechSupport.java
invoke main
%% java
java TechSupport
TechSupport
Welcome
Welcome to
to the
the DodgySoft
DodgySoft Technical
Technical Support
Support System.
System.
Please
Please tell
tell us
us about
about your
your problem.
problem.
We
We will
will assist
assist you
you with
with any
any problem
problem you
you might
might have.
have.
Please
Please type
type 'bye'
'bye' to
to exit
exit our
our system.
system.
>> Your
Your software
software is
is aa load
load of
of rubbish
rubbish ...
...
II need
need aa bit
bit more
more information
information on
on that.
that.
Java without BlueJ
w
e
i
v
e
R
javac
java
The compiler from Sun’s standard Java
Development Kit. This compiles .java
files to .class files.
Launches the Java Virtual Machine (JVM),
which invokes the main method of the
class to which it is applied. We give it the
name of the class only (no .class suffix).
The main method of the class to which java is applied must
have the header:
public static void main (String[] args
)
args)