Algorithms and OOD (CSC 207 2013F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [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.
$
eclipse &
You may recall that the ampersand means that the program will run in the background and you can continue using the terminal window.
Alternately, you can start Eclipse by finding the appropriate menu item in the
menu, which looks like a little creature over a blue X under Xfce4.
a. Make a new 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
>
> .
c. Create a class in the package called HelloJava
. If you are
not sure how to create a new class, use
>
> .
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) { 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 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.
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
).
What about +
? 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 trying to follow the standard Sun (now Oracle) Java Code Conventions. Eclipse can help if you configure it correctly.
First, select
> .Next, select
> > .Next, select Active Profile.
underFinally, save the new preferences.
Select all of your code and hit Ctrl-I
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 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.Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Copyright (c) 2013 Samuel A. Rebelsky.
This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this
license, visit http://creativecommons.org/licenses/by/3.0/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.