Creating Java console programs with Eclipse

Download Report

Transcript Creating Java console programs with Eclipse

Java
Looking at our first console
application in Eclipse
Copyright © 2004-2015 Curt Hill
A moment on paradigms
• Java is well suited for either the console or GUI
paradigm
• A console program views the keyboard/screen
as nearly a single device
– We both read and write to it using strings
• A GUI program has a WIMP interface:
–
–
–
–
Windows
Input
Menus
Pointer
• The console paradigm is easier for starters, but
we will do both
Copyright © 2004-2015 Curt Hill
Eclipse
• Eclipse is the Interactive
Development Environment that we
will use
• Originally developed by IBM
• Donated to public domain
• Now maintained by the Eclipse
Foundation
– www.eclipse.org
Copyright © 2004-2015 Curt Hill
Terminology for Eclipse
• Resource
– Things such as projects, directories
and files
• Editor
– A window pane that allows changes to
a resource, often Java code
• View
– A window pane that shows a resource
• Perspective
– A group of editors and views
Copyright © 2004-2015 Curt Hill
Eclipse Construction steps
•
•
•
•
Create new project
Create new class
Customize as needed
Debug and run
Copyright © 2004-2015 Curt Hill
Create new project
• Click File|New|Java Project
• Use the New Project dialog to set
some initial parameters
• Specify the project name
• This will become a directory tree
• The project is an XML file of name:
.project in the directory
• The screen shots follow
Copyright © 2004-2015 Curt Hill
Create New Project
Copyright © 2004-2015 Curt Hill
Enter Project Name
Copyright © 2004-2015 Curt Hill
A usually skipped screen
Copyright © 2004-2015 Curt Hill
Projects and Classes
• A project is just a tool to manage
pieces
• We now have to add the pieces
• Each Java program is a class
• Any class might use other classes as
well
• What we now do is add the main or
root class to our project
Copyright © 2004-2015 Curt Hill
Create new class
• Click File|New|Class
• This generates a new class
– This new class will be your program
• Use java.lang.Object as the
ancestor class
• Specify a main method
– Click on the box
Copyright © 2004-2015 Curt Hill
Add an initial class
Copyright © 2004-2015 Curt Hill
Name the class
Copyright © 2004-2015 Curt Hill
Specified names
• In the prior screen there were three
things that were specified:
– Package
– Class name
– Inclusion of the main method via
checkbox
• The package name is usually all
lower case
• The class name usually starts with a
capital letter and each subsequent
word is capitalized:
FirstCon Copyright © 2004-2015 Curt Hill
Generated Code
Copyright © 2004-2015 Curt Hill
Customize as needed
• At this point we have a Java console
program with proper form, which
does nothing
– Now add the desired functionality
• Insert into main:
System.out.println(“Hello world”);
• Java is case sensitive so System
must start with a capital
• That doesn’t sound like much work
– It will get worse
Copyright © 2004-2015 Curt Hill
Customized
Copyright © 2004-2015 Curt Hill
Debug and run
• Click the run button or use menu
Run|Run
• Run time properties are displayed
– Specify the new class name
– Search may be easiest
• Console output will appear in the
bottom pane
– First the command line
– Next the program output
Copyright © 2004-2015 Curt Hill
Start Run
Copyright © 2004-2015 Curt Hill
Run
Copyright © 2004-2015 Curt Hill
Code
package firstcon;
public class FirstCon {
/**
* @param args
*/
public static void main
(String[] args)
{
// TODO Auto-generated method stub
System.out.println("Hello World");
}
}
Copyright © 2004-2015 Curt Hill
Comments
• A comment explains things for
people
– Ignored by Java
• /* and */
– Enclose multiline comments
• //
– Makes the rest of the line a comment
• Documentation uses special
comments
– Program: Javadoc
Copyright © 2004-2015 Curt Hill
Class Declaration
• public class FirstCon {
• Objects in Java are called Classes
– A class binds together variables and
methods
• This class is named FirstCon
• Its visibility is public
– A private class in this case could not be
executed
Copyright © 2004-2015 Curt Hill
Classes and files
• A file is the compilation unit
– It must have an extension .java
– Each file must have one and only one
public class
• The file name must match the public
class name
– Even case, which may be a problem on
MS Operating Systems
• Most of these issues are handled by
Eclipse without our intervention
Copyright © 2004-2015 Curt Hill
Compilation Units
• Public class names must match file
name
• Other private or friendly classes
may be present in the file
• Each class will have its own object
(.CLASS) file after it is compiled
Copyright © 2004-2015 Curt Hill
Main Function
• Most classes have both variables
and methods
• Every application must have a
method called main, which is where
execution starts
• Main’s parameter is a string array
– Command line parameters
• Often, in a console program most of
the work occurs in the main method
Copyright © 2004-2015 Curt Hill
Main function
• public static void main(String args[
])
{ System.out.println("Hello
World!");
} // end of main
• void indicates main returns nothing
• It is public
– Accessible to system
Copyright © 2004-2015 Curt Hill
Main function
• It is static
– A static function has limited access to
class data
– Can be called without having a class
instance
• This main function has just one
statement
• We will cover static more later
Copyright © 2004-2015 Curt Hill
Writing to the console
• System.out.println("Hello
world");
• System is an instance of a class that
contains numerous predefined
classes and methods
• out is an output stream class with
methods print and println
Copyright © 2004-2015 Curt Hill
print and println
• Overloaded method names
• Accept one parameter
• There is a version for almost every
parameter type
Copyright © 2004-2015 Curt Hill
Classes and objects
• Sometimes these two terms are
used interchangably
• A class is a type
– Defines possible values and actions
• An object is an instance of a class
• Example
– A person name would be a class
– “Curt Hill” would be an object, an
instance of a class
Copyright © 2004-2015 Curt Hill
Finally
• There are some other details that
need to be considered:
– Handing in programs
• These will be covered in a
subsequent presentation
Copyright © 2004-2015 Curt Hill