Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R#1-121 E-mail address:...

39
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R#1-121 E-mail address: [email protected] Group Email Addresses Post message: [email protected] Subscribe: [email protected] I will use this group to communicate and all the Slides will be posted on the group after each lesson CPCS-202 LAB -1

Transcript of Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R#1-121 E-mail address:...

Prepared by Uzma Hashmi

Instructor InformationUzma HashmiOffice: B# 7/ R#1-121 E-mail address: [email protected]

Group Email AddressesPost message: [email protected] Subscribe: [email protected]

I will use this group to communicate and all the Slides will be posted on the group after each lesson

CPCS-202LAB -1

Prepared by Uzma Hashmi

Learning Outcomes of Lab-11. Installation of jdk1.7 and IDE ECLIPSE2. Understanding the IDE3. Writing ,Running and Debugging the code4. Studying the structure of a Java Program5. Adding Comments6. White Spaces7. 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 BSee next slides for the installation steps

Prepared by Uzma Hashmi

PART A• 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 includes the JRE

Prepared by Uzma Hashmi

Step-2

Step-3

Prepared by Uzma Hashmi

7

Step-4 Step-5

Step-6 Step-7

Prepared by Uzma Hashmi

Step-8

Step-9

Step-10

Prepared by Uzma Hashmi

http://www.eclipse.org/downloads/PART B- ECLIPSE-IDE

Step-1

Step-2

Prepared by Uzma Hashmi

Step-3

Extract the files and there you get the files listed below

Step-4

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

RunDebug

Prepared by Uzma Hashmi

Use of RefactorOnce 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

public class MyProgram

{

}

// comments about the class

class header must be the sameAs the java program name MyProgram.java

class body

Comments can be placed almost anywhere

Prepared by Uzma Hashmi1-30

Java Program Structure

public class MyProgram

{

}

// comments about the class

public static void main (String[] args)

{

}

// comments about the method

method headermethod body

Prepared by Uzma Hashmi

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 Hashmi1-32

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 Hashmi1-33

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

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 Hashmi1-35

Reserved Words• The Java reserved words:

abstractassertbooleanbreakbytecasecatchcharclassconstcontinuedefaultdodouble

elseenumextendsfalsefinalfinallyfloatforgotoifimplementsimportinstanceofint

interfacelongnativenewnullpackageprivateprotectedpublicreturnshortstaticstrictfpsuper

switchsynchronizedthisthrowthrowstransienttruetryvoidvolatilewhile

Prepared by Uzma Hashmi1-36

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

//**************************************************// 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