Computer Science 252
Problem Solving with Java
Spring 2016, The College of Saint Rose
Lecture 1: Introduction and Overview
Date: Tuesday, January 19, 2016
Agenda
- Welcome to Computer Science 252!
- Syllabus and administrative details
- 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 (that's the icon with your username on the Macs, and
will be the "H" drive in Windows) 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. To connect to your
network storage from the Macs, double-click the "Macintosh HD"
icon to open the Finder. In the sidebar on the window that pops up,
find the "SAN" entry and click it. That should bring up a large
group of folders. Find the one that matches your username and click
it to connect to your network drive.
- Launch BlueJ by typing its name in the Spotlight search tool in
the upper right corner on the Macs, or by clicking its icon on the
desktop in Windows. 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 in the names.
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 and replace its class comment with
something more descriptive, 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. You can do this by
right-clicking on the folder, then choosing "Compress" on the Mac,
or the "7z" option or the option to create a "Zip Archive" in
Windows. Navigate to your network drive, 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 by email before you leave class. Email submissions should use a meaningful
subject line, clearly indicating the course number and assignment
name.
- Lab 1: Java Review, Part 1 out
Lecture 1 Assignment
Due at the start of class, Thursday, January 21.
Please submit answers to these questions
either as a hard copy (typeset or handwritten are OK) or by email to
terescoj AT strose.edu by the start of class. Please use a clear subject line
when submitting by email (e.g., CSC 252 Lecture
1 Assignment , Joe Student). We will discuss these
questions at the start of class, so no late submissions are
accepted.
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 your names and to make sure you know how to
find me, please stop by my office (Albertus Hall 400-6) and introduce
yourself as a Computer Science 252 student (no later than January 26). (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
- Under what circumstances would you use the int data type,
and when would you use the double data type? The double
data type seems to be able to store everything you can store in an
int and more, so why not always use a double? (4 points)
- What is printed by the following? Please work through it by
hand rather than typing in and running the program. (3 points)
public class PrintOuts {
public static void main(String args[]) {
int a = 5;
int b = 12;
double c = 3.0;
System.out.println("" + (b + c));
System.out.println("" + (a / b));
System.out.println("" + (a + b + c));
System.out.println("" + (a / c));
System.out.println("" + (a + b / c));
System.out.println("" + ((a + b) / c));
}
}
- Complete the following Java program so that it assigns random
values to the variables as described in the preceding comment. (1
point each)
public class RandomNumbers {
public static void main(String args[]) {
Random r = new Random();
// 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 =
System.out.println("x=" + x + ", y=" + y + ", z=" + z +
", a=" + a + ", b=" + b " + ", c=" + 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: