Algorithms and OOD (CSC 207 2014F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [Learning Outcomes] [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Student-Curated Resources] [Java 8 API] [Java 8 Tutorials] [Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2014S (Rebelsky)] [CSC 207 2014F (Walker)] [CSC 207 2011S (Weinman)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Summary: In this laboratory, you will start using Eclipse. In particular, you will ccreate your first project in Eclipse.
a. Make sure you have completed and understood the reading on IDEs and Eclipse.
b. Start Eclipse by typing the following in a terminal window.
$/glimmer/bin/eclipse &
You may recall that the ampersand means that the program will run in the background and you can continue using the terminal window.
We are using the version of Eclipse in /glimmer/bin because it is much newer than the version of Eclipse shipped with Debian.
You may also find it usful to create a startup item for Eclipse.
a. Make a new Java project in Eclipse and call it HelloEclipse.
From the menu, select ,
and then .
b. Create a package in the class that will hold your class files.
Name your package introduction.
If you are not sure how to create a new package, use
>
> .
(You may also find it useful to right click on the project and use
> .)
c. Create a class in the package called HelloJava. If you are
not sure how to create a new class, use
>
> .
(You may also find it useful to right click on the package and choose the
obvious menu items.)
d. Copy and paste this code into your class. Make sure that you use a package that corresponds to the one you just created.
package introduction;
/**
* A simple introductory Java class.
*/
public class HelloJava
{
/**
* Print a silly message.
*/
public static void main (String[] args)
throws Exception
{
System.out.print ("I am the 1337 h4x0r. Phear me!");
} // main (String[])
} // class HelloJava
e. Run the program and marvel at the words that appear in the console!
a. Create a new package named
edu.grinnell.csc207.username.intro.
(You should be using package names like this, so you might as well
get used to it.)
b. Drag HelloJava from one package to another in the
Package Explorer.
c. Observe what happens.
You've already seen that Java looks a bit like C, except that it's also
a bit more verbose. For example, instead of printf with
a pattern, you use System.out.println. (In a few days,
you will stop using System.out and explore other ways to
do output.)
a. Determine what happens if you have multiple calls to
System.out.println.
b. Determine what happens if you use println with an
integer or real number rather than a string.
c. Determine what happens if you use System.out.print rather
than System.out.println.
d. Determine what happens when you try to print System.out.
That is, determine the result of
System.out.println(System.out);
a. Make another class in your fully qualified package and call it
SimpleMath
b. Using the skeleton from above write a program that computes and prints out a simple sum. For example, your output might be
Adding 3 and 4 gives us 7.
Here's a sketch.
int x = 3;
int y = 4;
System.out.print("Adding ");
System.out.print(x);
...
System.out.print(x+y);
System.out.println();
Presumably, you wrote this program using a sequence of calls to
System.out.print and System.out.println. Can
we pack it into a single instruction?
c. Java “overloads” the + operation.
If you combine two strings with +, you will get a string.
For example, determine the result of the following instruction.
System.out.println("Hello" + "World");
d. We saw earlier that System.out.println accepts things other
than strings, such as integers and reals (and even
System.out). Can + also take different
types of parameters? Determine what happens if you combine a string
and an integer with +. What about combining an integer
and a string?
e. Rewrite your instructions to print a sum using just one call to
System.out.println.
In this course, we will be using a variant of the GNU C code conventions, adapted for Java. Of course, one could also use other conventions, such as the standard Sun (now Oracle) Java Code Conventions. Eclipse can help if you configure it correctly. We'll explore each.
First, select > .
Next, select > > .
Next, select under Active Profile.
Finally, save the new preferences.
Hit Shift-Ctrl-F.
But those are the Java code conventions. I find them painful. So let's use the course conventions. (Note: those conventions are under development, so I may ask you to update the version you use.)
Select > again.
Click the button
Navigate to /home/rebelsky/share and select
CSC207.xml.
Follow the natural steps.
Hit Shift-Ctrl-F.
a. Hover the cursor over one of the instances of System
and note what happens.
b. Hover the cursor over one of the instances of out and
note what happens.
c. Hover the cursor over one of the instances of print or
println and observe what happens.
d. Hover the cursor over main or HelloJava and
observe what happens.
In one of the exercises, you configured Eclipse to format code in a certain way. Eclipse is highly configurable, so you might explore other preferences. Go back to the Formatter preferences and click . In the dialog box that appears, select the course conventions as the initialization profile and then name your preferences. Click and then explore the various preferences that you can set.
You'll note that Eclipse has a . Figure out what at least one item in that menu does.