Java Reference
In-Depth Information
You'll now convert the example of the course administration system to an object‐oriented Java pro-
gram. The first thing you need to do is define the concepts—the blueprints, templates, and types—
that will be used in your application. These are the classes . In this example, you can introduce two
class definitions: one for a Course and one for a Student .
Course and Student Classes 
try it out
To create some simple Java classes representing students and courses, follow these steps:
1.
If you haven't opened a project in Eclipse, or just prefer to follow along and create a new one, you
should do so by navigating to File New and then selecting Java Project in Eclipse. A dialog win-
dow will open asking you to fill in a project name. Choose CourseAdministration . You can then
press Finish to create the project.
2.
You will create two classes within the src folder: Course.java and Student.java . You do so by
right‐clicking the src folder in the package explorer in Eclipse and then selecting New Class. A
window will pop up asking you to fill in some details. The number of options offered might seem
a bit daunting at first, but remember that the only necessary element you need to provide is the
class name. All other aspects can be modified directly in the class' source code, or changed by mov-
ing the class around. Let's start with the Student class. In the wizard screen, you should enter the
class name without the .java suffix, as Eclipse will add this for you (you will receive a warning
when you do add the file suffix). Do not pay attention for now to the "The use of the default
package is discouraged" warning. You're just starting out, and can thus afford to be a bit
sloppy. You will learn about packages later.
3.
The Student.java file is created and opened in the Eclipse code editor (if it is not, you can double‐
click the file to open it in the Eclipse editor), showing a bare‐bones class definition:
public class Student {
}
4.
Edit Student.java to look like the following:
class Student {
int id;
String firstName;
String lastName;
int birthYear, birthMonth, birthDay;
}
5.
Save your file. You have now created a bare‐bones class definition for the Student concept,
containing variables for the ID, first name, last name, and date of birth. (For now, you'll use a
combination of three integers to represent the date of birth. There is also a Date class that will be
discussed later.)
6.
Similarly, create a Course.java class with the following source code:
class Course {
int id;
String name;
}
Search WWH ::




Custom Search