Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Getting started with Eclipse and the Graphics
labs
Lab instructors: Dr Remi Barillec (M. Williams, R. Jones)
r.barillec@aston.ac.uk
http://wiki.aston.ac.uk/RemiBarillec
CS2150, Computer Graphics, Aston University, Birmingham, UK
http://wiki.aston.ac.uk/C2150
October 8, 2009Remi Barillec Graphics labs in Eclipse 1/26
Labs schedule and material
I The labs will running on:
• Mondays, 12:00-12:50 in MB370
• Fridays, 15:00-15:50 in MB372
I The material for the labs is now available from the wiki:
wiki.aston.ac.uk/CS2150/LabNotes
• Lab software and installation instructions
• Lab booklet
• Instructions for installation at home
I If you experience problems installing/running the labs
(especially at home), please let me know.
Remi Barillec Graphics labs in Eclipse 2/26
Aim of this lecture
1 Introduce the Eclipse development
environment
2 Show you how to set up the labs
3 Create a simple Java project in
Eclipse
Remi Barillec Graphics labs in Eclipse 3/26
Part I
Introducing Eclipse
Remi Barillec Graphics labs in Eclipse 4/26
Eclipse: a Java IDE
I Eclipse is an Integrated Development Environment (IDE)
I Integrated: The whole development process is centralised
inside a single application
I Eclipse brings together:
• A powerful code editor (syntax highlighting and validation)
• A compiler (runs automatically in the background)
• A console (shows the program output)
• A visual project/package manager
• A debugger (allows to inspect the state of a program while
running it)
• And many other features
I Fear not, we will only be using a small subset of these
features!
Remi Barillec Graphics labs in Eclipse 5/26
Why use Eclipse?
I Because it free
• You can download it online: www.eclipse.org
• Installation instructions are on the wiki
I Because it is used by many professional developers
I Because it is multi-platform (Windows, Linux, Mac)
I Because it can be extended (plug-ins for C++, PHP, etc.)
I Because we forgot to ask for alternatives to be installed on the lab machines...
Remi Barillec Graphics labs in Eclipse 6/26
Eclipse: The Workspace
1 Run Eclipse (shortcut in Start Menu)
2 Select the workspace you want to work in
I Eclipse requires a
workspace to work in
I The workspace is simply a
folder which “belongs” to
and is managed by
Eclipse
I The workspace contains
one (or possibly, several)
Java project(s)
Remi Barillec Graphics labs in Eclipse 7/26
Part II
Setting up the labs
Remi Barillec Graphics labs in Eclipse 8/26
Installing the labs code
I The Eclipse workspace for the labs has already been
created for you and packed as a ZIP file.
I All you need to do to get started is:
1 Go to http://wiki.aston.ac.uk/CS2150/LabNotes
2 Navigate to the Lab Software page
3 Download the ZIP file
4 Extract it to some sensible folder (e.g. H:\CS2150). This will
create a CS2150LabCode folder which is the labs Eclipse
workspace
5 Open the workspace in Eclipse
Remi Barillec Graphics labs in Eclipse 9/26
Demonstration
Let’s do it! (If the network allows...)
Remi Barillec Graphics labs in Eclipse 10/26
Eclipse: The Workbench
Remi Barillec Graphics labs in Eclipse 11/26
Eclipse: The Workbench views
I The Package Explorer shows:
• the projects in the current workspace (as grey folders)
• the packages in each project
• the classes in each package
• the external libraries used by the project (JRE)
I The Code Editor is where you write your Java source code
I The Class Outline
• shows a hierarchical representation of the class (fields,
methods...)
• can be used to navigate quickly to some part of the code
Remi Barillec Graphics labs in Eclipse 12/26
Eclipse: The Workbench views (continued)
I The Problems / Console view:
• lists current issues with the code (Problems tab)
• shows the output of the program at run-time (Console tab)
• shows TODO items (Task tab)
I The Toolbar provides quick access to the main features
(such as running the program, adding a new class, etc.)
I All the views can be moved around, resized, shown/hidden
to match your preferences
Remi Barillec Graphics labs in Eclipse 13/26
Part III
Creating a simple Java project in Eclipse
Remi Barillec Graphics labs in Eclipse 14/26
An example: the Patient class
I To show you how to use Eclipse, we consider the following
example:
Example
We want to write a class Patient to manage information about
patients in a hospital. For each patient, we want to store (and
be able to access/modify) the following information:
• their name
• their age
• their weight
I This should help reminding ourselves about Java
programming (although you will not need all this for the
Graphics labs, it might still be useful for other modules)
Remi Barillec Graphics labs in Eclipse 15/26
Starting with an empty workspace
Remi Barillec Graphics labs in Eclipse 16/26
Creating a new project
The first step is to create a new Java project in Eclipse.
1 In the File menu, select
New→ Project.
2 This brings up the “New
Project” Wizard
3 Select Java project in
the Java folder
4 Indicate the name of
your project:
PatientExample
5 Click Finish
Remi Barillec Graphics labs in Eclipse 17/26
Creating a new project
The first step is to create a new Java project in Eclipse.
1 In the File menu, select
New→ Project.
2 This brings up the “New
Project” Wizard
3 Select Java project in
the Java folder
4 Indicate the name of
your project:
PatientExample
5 Click Finish
Remi Barillec Graphics labs in Eclipse 17/26
Creating a new project
The first step is to create a new Java project in Eclipse.
1 In the File menu, select
New→ Project.
2 This brings up the “New
Project” Wizard
3 Select Java project in
the Java folder
4 Indicate the name of
your project:
PatientExample
5 Click Finish
Remi Barillec Graphics labs in Eclipse 17/26
Creating a new project
The first step is to create a new Java project in Eclipse.
1 In the File menu, select
New→ Project.
2 This brings up the “New
Project” Wizard
3 Select Java project in
the Java folder
4 Indicate the name of
your project:
PatientExample
5 Click Finish
Remi Barillec Graphics labs in Eclipse 17/26
Creating a new project
The first step is to create a new Java project in Eclipse.
1 In the File menu, select
New→ Project.
2 This brings up the “New
Project” Wizard
3 Select Java project in
the Java folder
4 Indicate the name of
your project:
PatientExample
5 Click Finish
Remi Barillec Graphics labs in Eclipse 17/26
Creating a new class
1 In the File menu, select
New→ Class.
2 This brings up the “New
Class” Wizard
3 Indicate the name of
the new class: Patient
4 Click Finish
Remi Barillec Graphics labs in Eclipse 18/26
Creating a new class
1 In the File menu, select
New→ Class.
2 This brings up the “New
Class” Wizard
3 Indicate the name of
the new class: Patient
4 Click Finish
Remi Barillec Graphics labs in Eclipse 18/26
Creating a new class
1 In the File menu, select
New→ Class.
2 This brings up the “New
Class” Wizard
3 Indicate the name of
the new class: Patient
4 Click Finish
Remi Barillec Graphics labs in Eclipse 18/26
Creating a new class
1 In the File menu, select
New→ Class.
2 This brings up the “New
Class” Wizard
3 Indicate the name of
the new class: Patient
4 Click Finish
Remi Barillec Graphics labs in Eclipse 18/26
Eclipse: The Workbench (new class)
Remi Barillec Graphics labs in Eclipse 19/26
Java reminder: Structure of a class
Identify the main parts of a Java class and explain their role
?
Remi Barillec Graphics labs in Eclipse 20/26
Java reminder: Structure of a class
Identify the main parts of a Java class and explain their role
I The fields
• contain the data handled by the class
• are almost always private
I The constructor(s)
• is called whenever an instance of the class is created
• initialises the state (= sets the fields) of the instance
I The methods
• perform operations involving the fields or otherwise relevant
to the class
• modifier methods are used to set the value of the fields
• accessor methods are used to get the value of the fields
• can be public or private
Remi Barillec Graphics labs in Eclipse 20/26
Java reminder: Structure of a class
Identify the main parts of a Java class and explain their role
I The fields
• contain the data handled by the class
• are almost always private
I The constructor(s)
• is called whenever an instance of the class is created
• initialises the state (= sets the fields) of the instance
I The methods
• perform operations involving the fields or otherwise relevant
to the class
• modifier methods are used to set the value of the fields
• accessor methods are used to get the value of the fields
• can be public or private
Remi Barillec Graphics labs in Eclipse 20/26
Java reminder: Structure of a class
Identify the main parts of a Java class and explain their role
I The fields
• contain the data handled by the class
• are almost always private
I The constructor(s)
• is called whenever an instance of the class is created
• initialises the state (= sets the fields) of the instance
I The methods
• perform operations involving the fields or otherwise relevant
to the class
• modifier methods are used to set the value of the fields
• accessor methods are used to get the value of the fields
• can be public or private
Remi Barillec Graphics labs in Eclipse 20/26
Java reminder: Structure of a class
public class RacingCar {
private int racerNumber; // The only field
// Constructor
public RacingCar(int number) {
racerNumber = number;
}
// Change the number of the car
public void setNumber(int newNumber) {
racerNumber = newNumber;
}
// Main method
public static void main(String [] args) {
// Create a new instance of a racing car
RacingCar herbie = new RacingCar (12);
// Wait a minute , that’s not Herbie ’s number!
herbie.setNumber (53);
}
}
Remi Barillec Graphics labs in Eclipse 21/26
The Patient example: fields
Write down the fields for the Patient class
Remember, we want to manage the patient’s name, age and
weight.
/**
* Fields
*/
private String name; // Name of the patient
private int age; // Age of the patient
private double weight; // Weight of the patient (in kg)
Remi Barillec Graphics labs in Eclipse 22/26
The Patient example: fields
Write down the fields for the Patient class
Remember, we want to manage the patient’s name, age and
weight.
/**
* Fields
*/
private String name; // Name of the patient
private int age; // Age of the patient
private double weight; // Weight of the patient (in kg)
Remi Barillec Graphics labs in Eclipse 22/26
The Patient example: constructor
Write down the constructor for the Patient class
Tip: Some fields might need to be initialised.
/**
* Constructor
*
* @param name The name of the new patient
* @param age The patient ’s age
* @param weight The patient ’s weight in kg
*/
public Patient(String name , int age , double theWeight) {
this.name = name;
this.age = age;
weight = theWeight;
}
Remi Barillec Graphics labs in Eclipse 23/26
The Patient example: constructor
Write down the constructor for the Patient class
Tip: Some fields might need to be initialised.
/**
* Constructor
*
* @param name The name of the new patient
* @param age The patient ’s age
* @param weight The patient ’s weight in kg
*/
public Patient(String name , int age , double theWeight) {
this.name = name;
this.age = age;
weight = theWeight;
}
Remi Barillec Graphics labs in Eclipse 23/26
The Patient example: some accessor/modifiers
Write down the accessor and modifier for the name field
/**
* Change the patient ’s name
*
* @param newName The patient ’s new name
*/
public void setName(String newName) {
name = newName;
}
/**
* Retrieve the patient ’s name
*
* @return The patient ’s name
*/
public String getName () {
return name;
}
Remi Barillec Graphics labs in Eclipse 24/26
The Patient example: some accessor/modifiers
Write down the accessor and modifier for the name field
/**
* Change the patient ’s name
*
* @param newName The patient ’s new name
*/
public void setName(String newName) {
name = newName;
}
/**
* Retrieve the patient ’s name
*
* @return The patient ’s name
*/
public String getName () {
return name;
}
Remi Barillec Graphics labs in Eclipse 24/26
The Patient example: further work
You can carry on with this example if you wish, adding:
I The other accessor/modifier methods
I A method to print out the patient’s record
Remi Barillec Graphics labs in Eclipse 25/26
Further information
I Some video tutorials on Eclipse/Java:
http://eclipsetutorial.sourceforge.net/
I The official Eclipse documentation
http://help.eclipse.org/
I The Java Tutorials - an excellent reference on Java
http://java.sun.com/docs/books/tutorial/
Remi Barillec Graphics labs in Eclipse 26/26