Lab02: Getting Started with Java

advertisement
King Fahd University of Petroleum & Minerals
Information and Computer Science Department
ICS102: Introduction to Computing
First Semester 2008 (Term 081)
Lab01: Getting Started with Java
Objectives
Appreciate the importance of:
1.
2.
3.
4.
Comments,
Indentation,
Good Naming Style, and
Recognizing Syntax Errors.
2. Comments
Comments are English sentences inserted in a program to explain its purpose.
We use comments to explain the purpose of a class, a method or a variable.
There are two types of comments:
1) Block comment
/* … */
o Comments are ignored by the compiler.
o Any message enclosed inside /* … */ is treated as a comment.
o This type of comment can span more than one line.
2) Line comment
// …
o Any message following double forward slash, //, up to the end of the line is also
considered a comment.
o Notice that this type of comment cannot span more than one line.
o It is often used to comment variables.
You are expected to:
o Write brief information about yourself at the beginning of each program,
o Write comments explaining each program you write in this course, and
o Comment every variable whose purpose is not obvious from its name.
3. Indentation
Indentation involves using tabs, spaces, and blank lines to visually group related statements together.
You shall:

Push the statements inside a class, method, or structural statements such as if, while,…
etc. by at least three or four spaces or a tab character;

Separate between variable declarations and method declarations by a blank line; and

Separate adjacent methods by a blank line.
KFUPM, ICS Department
ICS102: Introduction to Computing
2/6
First Semester 2008 (Term 081)
Lab01: Getting Started with Java
4. Naming Style
In programs, variable names should clearly suggest their meanings. This is called the naming style.
Good naming style makes your program easier to read and easier to correct errors.
For example, the A coefficient of an equation should be called: aCoefficient, coefficient_A,
or similar; but never a, b, x, or z.
The use of abbreviations is also discouraged.
5. Recognizing Syntax Errors
When you make syntax errors in your program the compiler gives error messages and does not create
the bytecode file. It saves time and frustration to learn what some of these messages are and what
they mean. Unfortunately, at this stage in the game many of the messages will not be meaningful
except to let you know where the first error occurred. Your only choice is to carefully study your
program to find the error.
Correcting Syntax Errors
Some things to remember:
 Java is case sensitive, so, for example, the identifiers public, Public, and PUBLIC are
all considered different. For reserved words such as public and void and previously
defined identifiers such as String and System, you have to get the case right.
 When the compiler lists lots of errors, fix the first one (or few) and then recompile—often
the later errors aren't really errors in the program, they just indicate that the compiler is
confused from earlier errors.
 Always remember to close opened brackets, braces, and quotes.
 It is always important to remember to end every statement in Java with a semicolon ‘;’.
 Read the error messages carefully, and note what line numbers they refer to. Often the
messages are helpful, but even when they aren't, the line numbers usually are.
 When the program compiles cleanly, run it.
KFUPM, ICS Department
ICS102: Introduction to Computing
3/6
First Semester 2008 (Term 081)
Lab01: Getting Started with Java
Exercises
Exercise 1:
(Hello.java)
Write the following Java program.
/*
* By: Al-Mahmodi, Ali Y.
* ID: 251234
* KFUPM, CCSE, ICS Dept.
*/
/*
* Print a message to the screen
*/
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World”);
}
}
Compile and run the program to see what it does, then make the changes below and answer the
questions as you go.
Write down the resulting error messages in the following steps
in a piece of paper and submit it to your lab instructor.
1. Class name different from file name. Delete one l (el) from the name of the class (so the first
non-comment line is public class Helo), save the program, and recompile it. What was the
error message?
2. Misspelling inside string. Correct the mistake above, then delete one l from the Hello in the
message to be printed (inside the quotation marks). Save the program and recompile it. There
is no error message—why not? Now run the program. What has changed?
3. No ending quotation mark in a string literal. Correct the spelling in the string, then delete the
ending quotation mark enclosing the string Hello, World!. Save the program and recompile it.
What error message(s) do you get?
4. No beginning quotation mark in a string literal. Put the ending quotation mark back, then
take out the beginning one. Save and recompile. How many errors this time? Lots, even
though there is really only one error. When you get lots of errors always concentrate on
finding the first one listed!! Often fixing that one will fix the rest. After we study variables
the error messages that came up this time will make more sense.
KFUPM, ICS Department
ICS102: Introduction to Computing
4/6
First Semester 2008 (Term 081)
Lab01: Getting Started with Java
5. No semicolon after a statement. Fix the last error (put the quotation mark back). Now
remove the semicolon at the end of the line that prints the message. Save the program and
recompile it. What error message(s) do you get?
Exercise 2:
(Arithmetic.java)
The following Java program declears and assigns 2 variables then prints the followings:
 The sum of the 2 variables,
 The difference of the 2 variables, and
 The product of the 2 variables.
Type in the following program, correct all errors, then run it.
/*
* By: Al-Mahmodi, Ali Y.
* ID: 251234
* KFUPM, CCSE, ICS Dept.
*/
/*
* Compute the sum, difference, and product of two integer numbers.
*/
public Class Arith {
Public Static Void Main(string[] args) {
int number1 = 15;
// 1st integer
int number2 = 19
// 2nd integer
variable
variable
system.out.println("Number 1: " + number1);
system.out.println("Number 2: " + number2)
system.out.println();
system.out.println("Sum: " + (number1 + number2))
system.out.println("Difference: " + (number1 - number2));
system.out.println("Product: " + (number1 * number2))
}
}
Exercise 3:
(Printing.java)
Design then implement a Java program that displays the numbers 1 to 4 on the same line, with each
pair of adjacent numbers separated by one space. Write the program using the following techniques:
a. Use only 1 System.out.println statement
b. Use 4 System.out.print statements
KFUPM, ICS Department
ICS102: Introduction to Computing
5/6
First Semester 2008 (Term 081)
Lab01: Getting Started with Java
Exercise 4:
(Square.java)
Design then implement a program to draw a square-like shape that has its 4 sides composed of 6
asterisks (*) in length as follows:
******
*
*
*
*
*
*
*
*
******
Exercise 5:
(X.java)
Although the following program, which computes the average of 3 numbers, running fine; it doesn’t
follow the objectives of this lab (i.e., comments, indenation, and naming style). Type it then enhance
it so that it follows all the lab objectives.
public class X {
public static void main(String[] args) {
int x1=1,x2=2,x3=3;int x4=(x1+x2+x3)/3;System.out.println(x4);
}
}
KFUPM, ICS Department
ICS102: Introduction to Computing
6/6
Exercise 6:
First Semester 2008 (Term 081)
Lab01: Getting Started with Java
(Guess.java)
In the provided box below, write down the expected output of the following program. Then, verify
your answer by typing and running the program.
Line 1 
Line 2 
Line 3 
Line 4 
Line 5 
Line 6 
Line 7 
public class Guess {
public static void main(String[] args) {
System.out.println(“2 + 2 = 2 + 2”);
System.out.println(“2 + 2 = ” + 2 + 2);
System.out.println(“2 + 2 = ” + (2 + 2));
System.out.print(“Two + Two”);
System.out.println(“=”);
System.our.println(“Four”);
}
}
Download