Computer Science 252
Problem Solving with Java
Fall 2014, The College of Saint Rose
Lecture 1: Introduction and Overview
Date: Tuesday, August 26, 2014
Agenda
- Welcome to Computer Science 252!
- Syllabus and administrative details
- SubmissionBox setup
We will be using a system called "SubmissionBox," developed by
software engineering students here at Saint Rose, for the submission
of most of your work this semester. We'll start by making sure
everyone can log in and submit a test assignment before we move on to
the first in-class exercise.
First, everyone will need to come up to get their SubmissionBox
account set up (or updated, if you were in my CSC 202) to be used for
this course.
Once your SubmissionBox account is ready to go, create a document (a
plain text or Word document, for example) and save it somewhere on
your computer. Then log into SubmissionBox, and submit this document
under assignment "SBTest". Verify that you received an email
confirmation for this submission.
- In-class Exercise 1 - (15 lecture assignment
points) due before the end of class.
Before we get in to class examples, we will make sure everyone can run
Java programs in the BlueJ development environment that we will be
using all semester. There are two major types of programs we will be
developing this semester: Java applets and Java applications. More
soon on the differences between them and when we'll use each, but for
now we will work together through an exercise to run one of each in
BlueJ.
It is important to keep up - ask if you are having trouble at any
step. For today, everyone should use one of the lab computers. If
you would like to use your own computer later, we will see soon how to
set that up. It's all free software, available for any computer that
can run Java.
Here is the outline of our procedure:
- Create a folder where you will store your work for this course.
It is essential that you choose a location that is on your network
storage space (the "H" drive) so it will be available on any
computer in the lab (and in fact, on computers in labs all over
campus). I suggest calling your folder something like
"CS252" without any spaces.
- Launch BlueJ by clicking its icon on the desktop. A mostly
blank window, called the project window, with some greyed out
buttons, should appear.
- From the "Project" menu, choose "New Project". In the
window that pops up, navigate to the folder you created above, then
enter "InClass1", again with no spaces in the "File" box, then
click "Create". You should now see an icon in your BlueJ window
that looks like a sheet of lined paper, and the buttons on the left
side should now be active.
Important note: be sure to get into the habit of naming your
folders and files for this course without any spaces. Spaces in the
names can cause a variety of problems later.
- Click the "New Class..." button. In the "Class Name" field,
enter "HelloWorldWC" (remember: no spaces) and make sure that the
"Class Type" is selected as "WindowController Class", then click
"Ok". You should now see another icon in your project window
labeled "HelloWorldWC" and with some diagonal lines across it.
- Double-click on the new "HelloWorldWC" icon. This should bring
up an editor window with the skeleton of a program that we will use
as a starting point. You should see text displayed in a few colors
and some of it within shaded boxes. We will learn all about what's
going on there as we go forward.
- Click the "Compile" button at the top of the editor window.
If all goes well, you should see a message at the bottom of the
editor window: "Class compiled - no syntax errors".
- Now, go back to the project window. The "HelloWorldWC" icon
should no longer have the diagonal lines across it. And now we're
ready to run the program. Right-click on the "HelloWorldWC" icon,
which should bring up a menu. The last item in that menu is what
you want: "Run Controller". Choose this. Two things should
happen: a new red icon should appear at the bottom of the project
window, and a brand new window should appear, mostly if not
completely blank and white. This new window is one we will refer to
as the "canvas". Congratulations, you have run a Java applet,
albeit an incredibly boring and useless one. Close the canvas
window when you are done admiring it.
- To make our program slightly more interesting, we are going to
make it display a message on the canvas. Go back to the editor
window, the one with the program text. On the blank line between
the
{
and }
, type the following line, exactly as you
see it here, including all spacing and punctuation:
new Text("Hello, objectdraw world!", 100, 100, canvas);
- Now hit the "Compile" button again on the editor window, and
choose "Run Controller" again from the popup menu on the
"HelloWorldWC" icon on the project window. The canvas that comes up
should now have a message displayed.
- Now, go back to the editor window and change the paragraph at
the top to a message that briefly describes what you just did (a
sentence or two is sufficient here), and replace the "(your name)"
after the
@author
with your name. Re-compile (the
"Compile" button) and re-run (the "Run Controller" menu item)
the program to make sure you didn't break anything.
- Finally, to make sure your program looks pretty, go back to the
editor window and choose "Auto-layout" from the "Edit" menu.
This will indent all of your code nicely (more on this later).
Re-compile and re-run one last time to make sure everything still works.
- Now, we'll run a Java application in BlueJ. Go back to the
project window and click the "New Class" icon. In the "Class
Name" field, this time enter "HelloWorldApp" (still no spaces, of
course) and select the "Class Type" as "Application Class", then
click "Ok". Another icon should appear in your project window
labeled "HelloWorldApp".
- Double-click the new "HelloWorldApp" icon. Again you will be
brought into the BlueJ editor, but the starting text this time will
look a little different.
- Click the "Compile" button at the top of the editor window.
If all goes well, you should see a message at the bottom of the
editor window: "Class compiled - no syntax errors".
- Return again to the project window. The "HelloWorldApp" icon
should no longer have the diagonal lines across it. And now we're
ready to run the program. Right-click on the "HelloWorldApp" icon,
which should bring up a menu. The menu is different this time, and
you should choose the entry "void main(String[] args)". A window
will pop up. For now, we don't need to do anything with this window
beyond hitting "Ok". If all goes, well, your program will run and
you'll get back to the project window. Since the program doesn't
ask Java to do anything, this behavior is exactly right, though
incredibly boring.
- Now, we'll make this program slightly more interesting. Switch
back to the "HelloWorldApp" window. If you look more closely,
you'll see a familiar-looking skeleton of Java application. Add to
the main method a simple printout:
System.out.println("Hello, Java application world");
- Run the program again (that is, compile it, then choose the
"void main(String[] args)" menu item when you right-click on the
class icon in the project window). This time, a new window should
pop up with your program's output. This is the "terminal" window,
through which we will interact with our Java application programs.
- Like you did with the applet program, put your name into the
"HelloWorldApp" program, reindent your program with the
"Auto-layout" entry under "Edit", and re-run to make sure things
still work.
- Your last task is to create a single archive file that you can
submit through our SubmissionBox system. In Windows, you can do
this by choosing the "7z" option or the option to create a "Zip
Archive" when you right click on a folder. Navigate to your "H:"
drive in the Windows Explorer, and go into your
"CS252" folder. Create a Zip archive of your
"InClass1" folder that contains the Java programs you just wrote.
Please submit only the file
InClass1.zip in
Submission Box
under "InClass1" before you leave class.
- Event-driven programs
Lecture 1 Assignment
Due at the start of class, Thursday, August 28.
Please submit answers to these questions
in Submission
Box under "LA1" or in hard copy by the
start of our next class. We will discuss these questions at the
start of class, so no late submissions are accepted. Please be sure
that your name is clearly indicated in all submissions.
Most of these are review of some of the Java concepts and constructs
you should be familiar with from your previous programming experience.
- I would like to get a better sense of everyone's backgrounds
coming in. Please answer each of the following. (4 points)
- What is your name, year, and major?
- If you are not already one, are you considering
a major or a minor in CS or IT?
- Have you taken any other computer science, either before or
at Saint Rose?
- Do you have any programming experience other than the
courses listed in the previous question? If so, which
programming languages have you used and how complex were the
programs you developed?
- What was your high school and home town?
- What is your favorite restaurant in your home town and what
should I order there?
- What types of computers (e.g., PC running Windows, Mac,
Linux, tablets) have
you used?
- If you plan to use computers other than those in
Saint Rose labs for course work, what type of computer
do you plan to use?
- In an effort to get to know you and to make sure you know how to
find me, please stop by my office (Albertus Hall 400-2) and introduce
yourself as a Computer Science 252 student (some time this week). (3 points)
- Consider the following names that a programmer is considering
for use as variable names in a Java program. For each one, indicate
whether it is a valid Java identifier. If it is valid, does it
conform to the Java naming convention for variables? For all names
that are either invalid or do not conform to the naming convention,
suggest a similar name that would be appropriate to use instead. (5 points)
KNIGHT
252assignments
FavoriteColor
darth vader
BugsBunny!
WHEN_IS_LUNCH?
gotCash$$
jeter2
little Red Wagon
school_name
- Suppose your program contains the following variables and values:
int a = 5;
int b = 12;
double c = 3.0;
Evaluate each of the expressions below. (3 points)
b + c
a / b
a + b + c
a / c
a + b / c
(a + b) / c
- Assume you have a Java program that includes the following
construction of a random number generator object:
Random r = new Random();
Complete the following Java statements so that they accomplish the
task as described in the preceding comment. (1 point each)
// x should contain an integer between 0 and 999
int x =
// y should contain an integer between 1 and 50
int y =
// z should contain an integer between -10 and 10
int z =
// a should contain a floating-point value between 0.0 and 2.0
double a =
// b should contain a floating-point value between 1.0 and 2.0
double b =
// c should contain a floating-point value between 10.0 and 50.0
double c =
- Convert the following Java conditional to use a switch
statement that accomplishes the exact same thing. (3 points)
if ((day == 1)||(day == 2)||(day == 3)||(day == 4)) {
hours = 9;
}
else if (day == 5) {
hours = 8;
}
else {
hours = 0;
}
- Rewrite this for loop as a while loop. (3 points)
for (int x = 0; x < 40; x += 2) {
sum += x;
}
Examples
Late penalty graph: