Using Primitive Data Types
Using Primitive Data Types
Objective
This lab will begin to introduce the Java syntax to the new Java programmer. You
will create your first Java program and experiment with using different primitive
data types.
Overview
In this lab you will create a new Java project and your first Java class. While the
details of object-oriented programming and Java class structure are yet to come,
this initial Java class will allow you to begin writing some basic Java code.
1. Launch your IDE and use the default workspace for your lab exercises. The
default workspace location should be left unchanged in this environment and
it is: /home/developer/eclipse-workspace, so just click the Launch button.
2. Create a new Java project for the first few exercises. Name the project
ClassExercises.
The new class wizard will appear. Enter the name of the class, check the box
for a main method, and check the generate comments method. Click the
Finish button.
5. The Lab2 class will open in the editor. It will contain a beginning curly brace
‘{‘and an ending curly brace ‘}’ for the class definition. Proceed with the lab
exercises that follow.
6. Remove the TODO comment. It is there only to remind you that the method
was automatically created for you and that you must implement what the
method is to do.
7. Add a print statement to your main() method that says, “Hello World!”. We
will cover the details of how this all works later but for now it will look like the
following:
8. Save your changes and execute your small Java program. In most IDEs this
can be accomplished by right-clicking on the class name and selecting Run
As > Java Application or by clicking the run button on the toolbar.
9. View the results in the console view. It should be similar to the following:
10. At the beginning of the main() method, declare the following variables with the
specified characteristics:
In the body of the main() method, perform the following variable assignments:
11. Near the bottom of the main() method, print the values of each variable using
System.out.println(). Use the following as a guide:
12. Execute your program. Your output should be similar to the following:
Define Arrays
13. Near the top of your main() method, define the following arrays:
14. Write 12 lines of code to assign the number of days in each month to the
daysInMonths[] array elements (do not worry about leap year!). See
output in step 16 below for days in months.
15. Write a print statement for each month that will display the name of the
month and the number of days it contains. Use the following as a guide:
If you are already familiar with loops, you may use them. If not, then use 12
output statements.
16. Execute your program. Your output should be similar to the following: