Identifiers

advertisement
CPCS-202
LAB -1
Instructor Information
Uzma Hashmi
Office: B# 7/ R#1-121
E-mail address: uzma_a2001@yahoo.com
Group Email Addresses
Post message: cs202-lab@yahoogroups.com
Subscribe: cs202-lab-subscribe@yahoogroups.com
I will use this group to communicate and all the Slides will be posted on the
group after each lesson
Prepared by Uzma Hashmi
Learning Outcomes of Lab-1
1.
2.
3.
4.
5.
6.
7.
Installation of jdk1.7 and IDE ECLIPSE
Understanding the IDE
Writing ,Running and Debugging the code
Studying the structure of a Java Program
Adding Comments
White Spaces
Identifiers
Prepared by Uzma Hashmi
• In this semester we will learn JAVA
• The IDE (integrated development
environment)we will use will be Eclipse
• The compiler is jdk(java development kit)
• Now we will see how can we install both in
Part A and Part B
See next slides for the installation steps
Prepared by Uzma Hashmi
• First you need to install jdk for java language
compilation
• To do so we'll access this web link
http://www.oracle.com/technetwork/java/javase
/downloads/index.html
Prepared by Uzma Hashmi
Step 1:
http://www.oracle.com/technetwork/java/javase/downloads/
index.html
• JRE (Java Runtime Environment) on your
system to run Java applications and applets.
To develop Java applications and applets, you
need the JDK (Java Development Kit), which
includes the JRE
JRE (Java Runtime Environment) on your system to run Java
applications and applets. To develop Java applications and
applets, you need the JDK (Java Development Kit), which
Prepared by Uzma Hashmi
includes the JRE
Prepared by Uzma Hashmi
7
Prepared by Uzma Hashmi
Prepared by Uzma Hashmi
http://www.eclipse.org/downloads/
Prepared by Uzma Hashmi
Extract the files and there you get the
files listed below
Prepared by Uzma Hashmi
Using Eclipse
• The system will prompt you for a workspace.
The workspace is the place there you store
your Java projects (more on workspaces later).
Select a suitable (empty) directory and press
Ok.
Prepared by Uzma Hashmi
Click here
Prepared by Uzma Hashmi
Creating Java Project
Select from the menu File ->
New-> Java project.
Prepared by Uzma Hashmi
Prepared by Uzma Hashmi
Prepared by Uzma Hashmi
Creating Packages inside the work-space
Prepared by Uzma Hashmi
Prepared by Uzma Hashmi
Prepared by Uzma Hashmi
Prepared by Uzma Hashmi
Prepared by Uzma Hashmi
Another way of
Running your code
Debug Run
Prepared by Uzma Hashmi
Use of Refactor
Once you have created your file ,you can change
the name of the file using the refactor option
Prepared by Uzma Hashmi
Prepared by Uzma Hashmi
Prepared by Uzma Hashmi
Prepared by Uzma Hashmi
Error description in Problems
Prepared by Uzma Hashmi
Prepared by Uzma Hashmi
Structure of the program
• In the Java programming language:
– A program is made up of one or more classes
– A class contains one or more methods
– A method contains program statements
• These terms will be explored in detail throughout
the course
• A Java application always contains a method
called main,
• A Java application name must be the same as the
class name.
Prepared by Uzma Hashmi
Java Program Structure
//
comments about the class
public class MyProgram
{
class header must be the same
As the java program name
MyProgram.java
class body
Comments can be placed almost anywhere
}
Prepared by Uzma Hashmi
Java Program Structure
//
comments about the class
public class MyProgram
{
//
comments about the method
public static void main (String[] args)
{
method body
method header
}
}
Prepared by Uzma Hashmi
1-30
Program.java
//********************************************************************
// Program.java
Author: Lewis/Loftus
//
// Demonstrates the basic structure of a Java application.
//********************************************************************
public class Program
{
//----------------------------------------------------------------// Prints a presidential quote.
//----------------------------------------------------------------public static void main (String[] args)
{
System.out.println ( "A quote by Abraham Lincoln:” );
//System is a predefined class that provides access to the system.
//out is the output stream that is connected to the console{e.g. Monitor}.
//println() - Displays the String which is passed to it.
System.out.println ( "Whatever you are, be a good one.” );
}
}
Prepared by Uzma Hashmi
Comments
• Comments in a program are called inline documentation
• They should be included to explain the purpose of the
program and describe processing steps
• They do not affect how a program works
• Java comments can take three forms:
// this comment runs to the end of the line
/*
this comment runs to the terminating
symbol, even across line breaks
*/
/** this is a javadoc comment
Prepared by Uzma Hashmi
*/
1-32
Identifiers
• Identifiers are the words a programmer uses in a program
• An identifier can be made up of letters, digits, the underscore character ( _ ),
and the dollar sign
• Identifiers cannot begin with a digit
• Java is case sensitive - Total, total, and TOTAL are different
identifiers
• By convention, programmers use different case styles for different types of
identifiers, such as
– title case for class names - Lincoln
– upper case for constants – MAXIMUM
– Combination(Compound Word)opt. -Camel Notation
– E.g Class Name :MyProject
Prepared by Uzma Hashmi
1-33
Identifiers cont.
• Often we use special identifiers called
reserved words that already have a predefined
meaning in the language ( such as void )
• A reserved word cannot be used in any other
way
Prepared by Uzma Hashmi
Reserved Words
• The Java reserved words:
abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
false
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
strictfp
super
Prepared by Uzma Hashmi
switch
synchronized
this
throw
throws
transient
true
try
void
volatile
while
1-35
White Space
• Spaces, blank lines, and tabs are called white
space
• White space is used to separate words and
symbols in a program
• Extra white space is ignored
• A valid Java program can be formatted many
ways
• Programs should be formatted to enhance
readability, using consistent indentation
Prepared by Uzma Hashmi
1-36
//**************************************************
// Poem.java
//
// Prints a classic poem on four lines.
//**************************************************
public class Poem
{
public static void main(String[] args)
{
System.out.println("Roses are red");
System.out.println("Violets are blue");
System.out.println("Sugar is sweet");
System.out.println("And so are you!");
}
}
Prepared by Uzma Hashmi
Example for white spaces
//********************************************************************
// Lincoln3.java
Author: Lewis/Loftus
// Demonstrates another valid program that is poorly formatted.
//********************************************************************
public
class
Lincoln3
{
public
static
void
main
(
String
[]
args
)
{
System.out.println
(
"A quote by Abraham Lincoln:"
)
;
System.out.println
(
"Whatever you are, be a good one."
)
;
}
}
Prepared by Uzma Hashmi
Lab Assignment
Prepared by Uzma Hashmi
Download