CS 1063 Week
1: Introduction to Computer Programming
Objectives
- Administrative
- Use the web to obtain class notes, assignments, projects, and
calendar dates
- Type in a simple program in JBuilder and execute it
Reading Week 1
Administrative
- Syllabus
- Fill out survey
- Binder and floppy disk
- Borland JBuilder software
- Course mechanics
- Labs (12)
- Quizzes (10)
- Projects (3)
- Tests (2 and a final)
- Course web page
Activity 1: Compiling a Simple Program
Complete instructions for JBuilder can be found at
http://www.cs.utsa.edu/~javalab/lab/gettingStartedJbuilder.html
Part 1:
Setup - Find JBuilder on your computer and execute it.
- Project/Package Name: helloworldpkg
- Add a new Main Class with name: HelloWorld.
Remember to check the box Generate main method.
- Save all
- You will need to type in the highlighted line below replacing the
one that is there.
- Java is case-sensitive. You must enter upper and
lowercase letters exactly as they appear in the program listing.
- Right click on the HelloWorld
tab and chose run using defaults. you should
see
Hello, World! in the output window.
public class HelloWorld {
public HelloWorld() {
}
public static void main(String[] args) {
System.out.println("Hello
World");
}
}
Part 2:
Construction
- Every Java application contains a class with a main method. When the
application starts, the
instructions in the main method are executed.
public static
void main(String[] args) {
.
. .
}
- Use comments to help human readers understand your program.
Add the following line to your program. run
public class
HelloWorld {
public HelloWorld() {
}
public static void main(String[] args) {
// display a greeting in the console window
System.out.println("Hello World");
}
}
- Any text enclosed between // and the end of the line is
completely ignored by the compiler.
- Comments are used to explain the program to other programmers
or to yourself.
- The compiler also ignores any text between a /* and */ If
you have a comment that is longer than one line then the /* . . . */
comment is simpler. For example
/* This is a simple
Java program
It will print a greeting to the console
*/
- Add the highlighted lines to your HelloWorld file and run
again. Was there any change?
- Instructions or statements in the body of the main method (statements
between {} ) are executed one at a time. Each statement ends in a
semicolon (;)
- Notice that our main
has only one statement - the System.out.println("Hello
World");
- The comment is ignored by the compiler
- Whenever you call a method in Java, you need to specify three
things
- The object that you want to use - System.out
- The name of the method you want to use - println
- A pair of parentheses, containing any other information the
method needs - ("Hello World")
Part 3: More on the System.out.println statement
and strings. Add the highlighted lines to your HelloWorld file and run.
- String - a sequence of characters enclosed in quotation marks.
- The println method
prints a string or a number and then starts a new line. The
following code prints out the two lines
Hello
World!
System.out.println("Hello");
System.out.println("World!");
- You can also print numerical values. The following code
prints the number 7.
System.out.println(3
+ 4);
- The print method can be used to print an item without starting a
new line afterward. The following code outputs the two statements
in the single line
007
System.out.print("00");
System.out.println(3
+ 4);
- Add the highlighted statements in part 3 to your HelloWorld file and varify that
your code works as intended.
Part 4: Escape
Sequences Add the highlighted lines to your HelloWorld file and run
- When printing some characters the compiler will report an
error. Print out the sequence Hello, "World"! The
following statement would generate such an error Why?.
System.out.println("Hello,
"World"!");
- If you precede the quotation marks inside the string with a
backslash character (\") the " will be printed. The backslash
character is used as an escape character, and the character sequence \"
is called an escape sequence. The backslash does not denote
itself; instead, it is used to encode other characters that would
otherwise be difficult to include in a string. The correct way to
do this is:
System.out.println("Hello,
\"World\"!");
- Another character that is difficult to print is the \ Print
the following message: C:\courses\cs1063
System.out.println("C:\\courses\\cs1063");
- The newline sequence is \n Each time it appears
between quotes the output will go to the next line. Print the
following characters.
*
**
***
System.out.println("*\n**\n***\n");
- The tab sequence is \t Each time it appears between quotes
the
output will space over before printing the next part of the
string.
Print the following characters.
* **
***
System.out.println("*\t**\t***");
- Add the highlighted statements in part 4 to your HelloWorld file and varify that
your code works as intended.
Last
Modified: August 21, 2005 at 2:51pm