Download CPE1001 - Object-Oriented Programming in Java

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

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

Document related concepts
no text concepts found
Transcript
Using Java without BlueJ
3.0
BlueJ projects
• A BlueJ project is stored in a directory on
disk.
• A BlueJ package is stored in several
different files.
• Some files store the source code, some
store the compiled code, some store
additional information.
• BlueJ uses standard Java format for some
files and adds some additional files with
extra information.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
2
The BlueJ directory structure
project: calculator
Calculator
UserInterface
CalcEngine
c:\bluej\calculator\
bluej.pkg
bluej.pkh
Calculator.java
Calculator.class
Calculator.ctxt
UserInterface.java
UserInterface.class
UserInterface.ctxt
CalcEngine.java
CalcEngine.class
CalcEngine.ctxt
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
3
The BlueJ file structure
• bluej.pkg - the package file. Contains
information about classes in the package.
One per package.
• bluej.pkh - backup of the package file.
• *.java - standard Java source file (text).
One per class.
• *.class - standard Java code file. One per
class
• *.ctxt - BlueJ context file. Contains extra
information for a class. One per class.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
4
Standard Java files
• source files: *.java
Java source files contain the source code
in readable form, as typed in by the
programmer.
• class files: *.class
Java class files contain byte code (a
machine readable version of the class).
They are generated by the compiler from
the source file.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
5
The edit-compile-execute
cycle
source file
class file
011010
110101
010001
011010
110101
1001
10
1
0111
0110110
editor
compiler
(javac)
virtual machine
(java)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
6
Editing
• A file can be edited in any text editor
– Notepad, emacs, jEdit, PFE, vi, ...
• Don't use Word: by default, Word
does not save in text format
• Make sure to save with a .java
filename before compiling!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
7
Command line invocation
• Compilation and execution of Java in
JDK are done from a command line
• On Microsoft systems: DOS shell
• On Unix: Unix shell
• Must make sure that the commands
for compiler and runtime are in the
command path.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
8
Compiling
• Name of the JDK compiler: javac
• To invoke:
javac <source name>
• compiles <source name> and all
classes it depends on
• Example:
cd C:\bluej\zuul
javac Game.java
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
9
Error messages
C:\bluej\zuul> javac Game.java
Game.java:22: ';' expected.
private Parser parser
^
1 error
C:\bluej\zuul>
The programmer has to open the file in the editor,
find the line number, fix the error and recompile.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
10
Execution
• C:\bluej\zuul> java Game
• “java” starts the Java virtual
machine.
• The named class is loaded and
execution is started.
• Other classes are loaded as needed.
• Only possible if class has been
compiled.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
11
Problem: Execute what?
• If we try:
C:\bluej\zuul> java Game
Exception in thread "main"
java.lang.NoSuchMethodError: main
• The problem: how does the system know
which method to execute?
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
12
The main method
• The answer: The java system always
executes a method called main with a
certain signature:
public static void main(String[] args)
{ ...
}
• For this to work, such a method must
exist!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
13
The main method (2)
“main” must exist
“main” must be public
“main” must be static (class method)
“main” must have a String array
parameter
• Only “main” can be invoked
•
•
•
•
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
14
Main method - example
public static void main(String[] args)
{
Game game = new Game();
game.play();
}
• The main method should
– create an object
– call the first method
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
15
Starting programs with main
• Can use the command line, but don’t
usually
• Can create an “executable jar” from
bluej which we just double-click to
execute.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
16
Interactive programs
• Some programs are ‘batch’ programs are
given all of their data at start of execution
– Payroll
– Weather forecasting
• Many programs are ‘interactive’ and
require continuous input during execution
– Word processors
– Games
– Web browsers
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
17
• So far our programs have had limited
interaction, but we could use
– Keyboard input
– Mouse input
– File input (interactive?)
–
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
18
Problems with interaction
• If we’re not running from a terminal,
what happens to System.out.println?
• If we try to read from a file what
happens if the file is not there?
• Do I have to write lots of graphics to
get a pull-down list to work?
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
19
Some answers
• Use Andy’s Console class to allow text
I/O from executable jars
• Need a good way of notifying and
dealing with errors (exceptions)
• The AWT (abstract windowing toolkit)
API offers help with lots of graphical
components
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
20
Related documents