Computer Science 202
Introduction to Programming
Fall 2012, The College of Saint Rose
This first lab assignment introduces you to the tools that you will use this semester. Your task is to type in and run a Java program that prints a simple message.
Setting Up Java and BlueJ
This assignment assumes that you either using a school computer that already has BlueJ installed or that you have already set up Java and BlueJ on your own computer.
Writing and Running a Java Program in BlueJ
We will use the program we looked at in class as a starting point.
/* A Hello World example in Java */ public class HelloWorld { public static void main(String[] args) { String message = "Hello, World!"; System.out.println(message); } }
So let's go through the steps to set up, type in, and run this program in the BlueJ environment.
We type our program in a text editor. The program we type in is called the source code. Usually, each Java class we write will be placed in its own source code file, with a .java extension.
We will use a text editor that is part of an integrated development environment called BlueJ. BlueJ is a free program, indended for use by beginning Java programmers. It is available for Windows, Mac, and others, so you will be able to develop your Java programs on any computer where you want to install BlueJ.
That's it - you have successfully written, compiled, and run a Java program.
Adding Comments
Documentation is an important part of writing a program. Comments are the mechanism we will use to provide that documentation. Documentation is essential to help the author of a program as well as others who may need to read or perhaps modify the program at a later time.
We have seen that Java allows us to write comments on a single line
that begins with //
or that span multiple lines when enclosed
in a /*
and */
pair.
Every program you write should include comments. Part of your grade for every program you submit this semester will depend on how well you comment your code.
You should always include at least these kinds of comments:
We will work together through the start of the semester to make sure everyone has a good idea of what is considered an appropriate level of commenting for our course.
For this simple program, you should include a class comment, a method comment for the main method, and a comment giving a brief description of the one variable we used. There should be no need to describe any other lines of code in this very short program.
Class examples will provide an appropriate a level of documentation.
Adding a Bit More Code
We haven't seen enough Java yet to do much more, but we can print more messages. Leave the existing message as is, but add in at least two more System.out.println statements that print more sentences.
Making it Look Nice
Java is very picky about the syntax of our Java programs, but is very forgiving when it comes to how we organize a syntactically correct Java program in terms of new lines and spaces. We could write an entire class definition on a single line and Java would not complain. However, it is very difficult for you to write and understand a program that looks messy. Therefore, you will be required to submit programs that follow strict formatting guidelines.
Fortunately, BlueJ helps us out here. It tends to indent things nicely, and as long as you don't try to put multiple statements on a single line, your programs should exhibit reasonably good formatting. To ensure this, before you submit any program (and periodically while you are developing it, if the formatting starts to look ugly), you should run the "Auto-layout" command from the "Edit" menu in BlueJ.
Lab Questions
Please answer the following questions, placing your responses in an additional comment at the end of your Java program. You may refer to the text or class notes to help answer as needed.
Submitting Your Work
Before 11:59 PM, Monday, September 10, 2012, submit your Java program for grading. Please upload your Java source file (the one with the .java file extension, and only that file), including the comment at the end that includes your responses to the lab questions, to Blackboard using the submission procedure in the "Labs" section. Note: if your computer does not show any files in your project folder with a .java file extension, you should change your folder's properties so it does show file extensions. You should be able to do this in either Windows or Mac environments, but don't hesitate to ask if you have trouble doing so.
Grading
This assignment is worth 15 points, which are distributed as follows:
> Feature | Value | Score |
HelloWorld.java original message | 3 | |
HelloWorld.java extra messages | 2 | |
HelloWorld.java comments | 3 | |
HelloWorld.java formatting | 1 | |
Lab questions | 6 | |
Total | 15 | |