0% found this document useful (0 votes)
64 views9 pages

Chapter0 PDF

Uploaded by

mike
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views9 pages

Chapter0 PDF

Uploaded by

mike
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

hapter

introduction and Output statements


Introduction:
Java-Jive introduces computer science through Java programming to high school students. The
book is organized in a way to facilitate a logical progression of computer science education for
beginners. A three-pronged approach is used:
1. Computer science concepts, patterns, and techniques are presented.
2. Questions are asked and should be discussed in class.
3. Programming labs are given to test students' abilities to apply the concepts taught. The
more challenging programs have an asterisk (*) at the end of their name.
Simple standard input and output are used with Java-Jive. Standard console output to the monitor
is used as it has been built into the Java language. Standard keyboard input, though, has not
been built into Java, so the chn.util open-source toolkit created by Chris Nevison of Colgate
University will be used. The Java educational environment software used with this text is JJ2 by
PSVM (see www.psvm.org), which builds in the chn.util toolkits as well as several other useful
open-source libraries.
This book has been created with the College Board's Advanced Placement Computer Science A
and AB Exams in mind. Upon successful completion of a computer science course using this
textbook, students will have the background to begin a rewarding instruction in either AP
Computer Science A at the high school level or CS1 at the university level. Many universities now
teach a first computer science course, CS0, which is comparable to this course.

Computer Programs:
A computer program is a set of instructions that "tell" the computer to do a certain task, such as
play a game of Diablo II; compose, format, and print a letter; or predict the survival of cockroaches
over several generations. People who used the earliest computers had to know how to write their
own programs. There were no stores where they could buy ready-to-use software packages.
Today, most people who use computers are not programmers. Instead, they use application
software such as a word processor, a spreadsheet, an Internet browsers, games, and graphics
programs. You can become a very sophisticated computer user and know nothing about writing
programs.
Yet even if you have no intention of becoming a software developer or writing complex software
applications, you can still learn to program and enjoy solving your own problems in your own way.

Java-Jive
Chapter 0

kweiser 04
Page 0

There are several reasons to learn the foundations of computer science and how to program:

Acquire training and practice in logical thinking.

Get a better understanding of how computers work (Everything a computer does


boils down to programmed instructions.)

Create your own solutions to problems.

Explore a new career field.

Just have fun!

Java Programming Language:


Java is the fastest growing programming language in the world today. It was created by James
Gosling of Sun Microsystems in 1994 and commercially released by Sun in 1995. Many
technology companies have chosen Java as their major application development language.
There are several reasons for this.
1. Java is a modern object-oriented programming (OOP) language. An OOP language
uses objects to construct software. The designers of Java spent much time studying the
features of classical object-oriented languages, integrating the beneficial features of
these languages and excluding the less desirable ones. OOP is a programming
approach in which objects are manipulated.
2. Java has several important features in addition to OOP, such as:

Portable Java programs can be run on different computer platforms without


needing any changes in the program.

Secure Java permits the creation of programs that are virus-free and tamper-free.

Robust Java programs do not overwrite memory.

Getting Started:
The software we will use to write Java programs is called JJ2.
1. Open JJ2, select Demo User and then select Open Selected JJ Project from Server.
2. Select: File New. The white window that opens is called the edit window.
3. Type HELLO into the edit window. Press the triangle button at the top right of the edit
window. This button compiles (translates Java code into a language the computer can
understand Java byte code see page 7) and runs the program. However, if the Java
code is incorrect, it cannot be translated. Instead, the compiler will report an error
message.

Questions:
1. Select Help About in JJ2 at the top right of the screen. Who is the third from the last
person listed who assisted in the development of this software?
2. What error message does the Java compiler report from #3 ("Getting Started") above?

Java-Jive
Chapter 0

kweiser 04
Page 1

Your First Java Code:


Go back into the edit window and correct the statement (line of programming code) so that it
now says:
System.out.println(Hello);

System.out is an object that prints characters on the console window (black window
on the screen).

println is a method, or behavior, that the System.out object carries out.

The characters enclosed in double quotation marks and are called a string. The string is
printed in the console window.

Each statement in a program ends with a semicolon (;).

Press the triangle button to compile this Java code. If it doesnt compile but shows another
error message, fix the error and compile again. Eventually, when all errors are corrected, the
console window will show the run of your Java code.
> compiling code snippet ... ... Compilation successful.
> running code snippet
BEGIN: snippet
Hello
END: snippet

The line between BEGIN:


code.

snippet and END:

snippet is the run of your Java

Lab # 00:
1.

Read over the Java code below. What do you guess it will do when you run it?
Discuss this with your desk partner or someone nearby.
System.out.print("A" + "B");
System.out.println("C" + "D");
System.out.println();
System.out.println("E");

Write your guess below:


> compiling code snippet ... ... Compilation successful.
> running code snippet
BEGIN: snippet
_______________________________________________
_______________________________________________
_______________________________________________
_______________________________________________
END: snippet

2.

Copy the program above into the editing window.

Java-Jive
Chapter 0

kweiser 04
Page 2

3.

Compile and run this program. Show the actual output below:
> compiling code snippet ... ... Compilation successful.
> running code snippet
BEGIN: snippet
_______________________________________________
_______________________________________________
_______________________________________________
_______________________________________________
END: snippet

Lab # 01:
This is the Java code for Lab # 01:
/*
@Author:
Date:
Teacher:
Course:
Period:
Lab #:

Kathy Weiser
11/3/04
Weiser
Computer Science
4
01

Lab Description: This program draws a rectangle.


*/
System.out.println("*****************");
System.out.println("*
*");
System.out.println("*
*");
System.out.println("*
*");
System.out.println("*
*");
System.out.println("*
*");
System.out.println("*****************");
This is the run of the Java program above:

*****************
*
*
*
*
*
*
*
*
*
*
*****************

Do the following:
1.

Type the Java code above, modifying the documentation (the information between /* and
*/) to have your name, period, and the correct date.

2.

Correct all errors so that your program will compile.

3.

Make sure that the program runs correctly.

Java-Jive
Chapter 0

kweiser 04
Page 3

4. Copy the run into documentation added below your program. Your program should now look
like this:
/*
@Author:
Date:
Teacher:
Course:
Period:
Lab #:

Kathy Weiser
11/3/04
Weiser
Computer Science
4
01

Lab Description: This program draws a rectangle.


*/
System.out.println("*****************");
System.out.println("*
*");
System.out.println("*
*");
System.out.println("*
*");
System.out.println("*
*");
System.out.println("*
*");
System.out.println("*****************");
/*

RUN

*****************
*
*
*
*
*
*
*
*
*
*
*****************
*/

5. Save your program on the server with the filename Lab01 .


6. Print out your program containing a copy of the run (like the sample above).

Lab # 02:
1.

Write a program that prints your first and last name on the first line, your address on the
second line, skips the third line, and prints your phone number on the fourth line.

2.

After compiling and debugging (correcting the errors) the program, run it. Copy the run
into documentation below the program statements. (See lab # 01).

3.

Save this program with the name Lab02 on the server.

Java-Jive
Chapter 0

kweiser 04
Page 4

4.

Print out the program that includes documentation at the top


/*
@Author:
Date:
Teacher:
Course:
Period:
Lab #:
Lab Description:
my name,
number.

Berj Avenasyan
11/4/04
Weiser
Computer Science
4
02
This program prints
address, and phone

*/
and documentation at the bottom with a copy of your run
/*

RUN
Berj Avenasyan
12345 Something St.
(818) 558-4700

*/

Output and Special Characters:


TO END A LINE or TO PRODUCE A BLANK LINE use println
System.out.print("one");
System.out.println("two");
System.out.println("three");
System.out.println();
System.out.print("four");

produces the following output:


onetwo
three
four

so does this:
System.out.println("one" + "two");
System.out.println("three");
System.out.println();
System.out.print("four");

so does this:
System.out.println("onetwo");
System.out.println("three");
System.out.println();
System.out.print("four");

Java provides a facility for displaying special characters in a string. These special characters
are indicated by placing a backslash (\) in the string. For example, since double quotation
marks are used to indicate the start and end of a string, there is no obvious way to include
quotation marks within a string.
Java-Jive
Chapter 0

kweiser 04
Page 5

Therefore, the backslash must be used as in the statement:


System.out.println(You say \Goodbye,\ and I say \Hello.\);
which displays:
You say "Goodbye," and I say "Hello."
The double quotation marks that follow the backslashes are displayed rather than interpreted as
ending the string. The combination of symbols is called an escape character. The idea is that
it escapes from the normal interpretation of characters in a string.
Another special escape character is the end-of-line character. This is indicated by \n and can
replace the use of ln in println. For example:
System.out.print(This is the first line,\n);
System.out.print(and this is the second line.);
displays:
This is the first line,
and this is the second line.
The escape characters include:
Symbol
\n
\
\
\\
\t

Effect
Go to next line
Double-quotation mark
Single-quotation mark
Single backslash
Tab (move over eight spaces)

Lab # 03:
1.

Write a program using escape sequences that produces the following run output:

*
|\
| \
------|--\-\
/
~~~~~~~\
/~~~~~~~
------~~~~~~~~~~~~~~~~~~~~~~~~
the BHS sailboat

2.

After compiling and debugging your program, run it. Copy the run into documentation
below the program. Make sure you have added documentation at the top of your program
giving me your information and the program description.

3.

Save this program with the name Lab03 .

4.

Print out a listing of the program that includes documentation at the top and the run at the
bottom. Turn in the program.

Java-Jive
Chapter 0

kweiser 04
Page 6

Lab # 04:
/*
@Author:
Date:
Teacher: Weiser
Course:
Period:
Lab #: 04
Lab Description: Write a program that draws a house using
characters inside quotation marks. The two output statements
in Java for console applications are:
System.out.print("stuff goes here");
System.out.println("stuff goes here");

-- and --

Use at least 3 escape characters in your house.


*/

The Java Virtual Machine and Byte Code:


The Java compiler translates Java into a pseudo-machine language called Java byte code,
the machine language for an imaginary Java computer. To run Java byte code on any
computer, you must install a Java virtual machine on that computer. Thus a Pentium computer
has a different Java virtual machine than a Macintosh computer.
A Java virtual machine (JVM) is a program that behaves like a computer. Such a program is
called an interpreter. An interpreter has several advantages as well as some disadvantages.
The main advantage of an interpreter is that any computer can run it. Thus, Java byte code is
highly portable. For instance, many of the pages you look at on the web with a browser like
Internet Explorer contain small Java programs already translated into byte code. These are
called applets, and they are run in a JVM that is incorporated into your web browser. You can
see a Java applet in action at these URLs:

http://javaboutique.internet.com/AdvNewsScroll2/

http://javaboutique.internet.com/WebCalculator/

http://javaboutique.internet.com/PingPong/

The disadvantage of an interpreter is that a program pretending to be a computer runs


programs more slowly than an actual computer. Java virtual machines are getting faster every
day, however.

Java-Jive
Chapter 0

kweiser 04
Page 7

Java Vocabulary:
1.

program

2.

Java

3.

object-oriented programming (OOP)

4.

secure

5.

robust

6.

portable

7.

JJ2

8.

edit window

9.

compiles

10.

error message

11.

statement

12.

console window

13.

method

14.

string

15.

semicolon

16.

code

17.

snippet

18.

documentation

19.

debugging

20.

escape character

21.

compiler

22.

Java byte code

23.

java virtual machine (JVM)

24.

interpreter

Java-Jive
Chapter 0

kweiser 04
Page 8

You might also like