compiling with eclipse

25
Copyright © 2012, Oracle. All rights reserved. Compiling with Eclipse

Upload: satriahelmy

Post on 11-Nov-2014

408 views

Category:

Education


2 download

DESCRIPTION

Bagaimana cara menggunakan Eclipse? yuk cek disini

TRANSCRIPT

Page 1: Compiling With Eclipse

Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Page 2: Compiling With Eclipse

2Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

What Will I Learn?ObjectivesIn this lesson, you will learn how to: • Identify components of Eclipse• Compile an application• Test to ensure application is complete• Write the code for GalToLit.java• Continue coding until error free compilation and

execution is reached• Modify the program to compute a different number of

gallons into its equivalent number of liters

Page 3: Compiling With Eclipse

Copyright © 2012, Oracle. All rights reserved. 3

Compiling with Eclipse

Why Learn It?Purpose Eclipse is an efficient and easy to use a development environment for creating Java programs. It is a well known tool by many Java programmers and is used in many development environments.

Learning Eclipse in this lesson will give you additional skills with tools used in the Java programming space.

Page 4: Compiling With Eclipse

4Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Eclipse Community & RequirementsFacts about Eclipse: • Eclipse is created by an Open Source Community• The Eclipse project is governed by the Eclipse

Foundation, a non-profit organization.• Eclipse requires an installed Java Runtime

Environment (JRE).• Eclipse contains its own development tools, i.e., a

Java compiler.

Page 5: Compiling With Eclipse

5Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Java JRE and Java JDKJRE and JDK differences: • Java Runtime Environment (JRE) contains only the

necessary functionality to start Java programs.• Java Development Kit (JDK) contains functionality to

start Java programs as well as develop them.• At a minimum, the Java JRE is required to run Eclipse.

To check if Java is already installed on your computer: • Windows or Ubuntu operating systems: Enter java

-version in a command window.• Mac operating system: Use the Software Update

option from the Apple menu.

Page 6: Compiling With Eclipse

6Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Launching Eclipse To launch Eclipse: 1. On a computer with Windows double-click on the file

eclipse.exe. On a Linux or Mac computer double click on the file eclipse.

2. When prompted, enter the pathname for the workspace into which you will store your Java projects and click the OK button.

Page 7: Compiling With Eclipse

7Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Launching Eclipse (cont.)To launch Eclipse (cont.): 3. Eclipse will start and display the Welcome page.4. Close the Welcome page by clicking the X next to the

Welcome tab name.– There are valuable resources available from the

Welcome page– You can return to the Welcome page by choosing

Welcome from the Help menu

Page 8: Compiling With Eclipse

Copyright © 2012, Oracle. All rights reserved. 8

Compiling with Eclipse

Eclipse Edit Area and ViewsEclipse provides an edit area and several views.

• An editor is where you type in your Java source code• Views are sub-windows that provide information about

your project

Page 9: Compiling With Eclipse

Copyright © 2012, Oracle. All rights reserved. 9

Compiling with Eclipse

Eclipse Edit Area and Views (cont.)The edit area uses tabs when more than one file is open.

Page 10: Compiling With Eclipse

Copyright © 2012, Oracle. All rights reserved. 10

Compiling with Eclipse

Eclipse Edit Area and Views (cont.)The edit area can have multiple windows occupy the space.

Page 11: Compiling With Eclipse

11Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Eclipse Edit Area and Views (cont.)Details on edit areas and views: • A combination of views and editors are referred to as a

perspective.• All projects are developed and modified in a

workspace.

Page 12: Compiling With Eclipse

12Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Steps to Creating a ProgramTo create a program in Eclipse: 1. Create a Project2. Create a Package (inside the src folder of the project)3. Create a Class (inside the package)4. Create and run Java code

Page 13: Compiling With Eclipse

13Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Step 1: Create a ProjectIn Eclipse, all programs must reside inside a project for proper compilation. You may have one or multiple programs in one project. To create a project:• Choose File → New → Java

Project• Enter a project name and

click Finish• All project values are set to

default by clicking the Finish button

Page 14: Compiling With Eclipse

14Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Step 1: Create a Project (cont.)The project: • Is created and displayed as a folder• Displays in the Package view to the left of the edit area

Page 15: Compiling With Eclipse

15Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Step 2: Create a PackageTo create a package (note: create the package in the src folder):1. Select the src folder in the Package view2. Right click the src folder and choose New → Package

A package, in Java, is a mechanism for organizing Java classes into namespaces. In Eclipse, packages are created inside projects.

Page 16: Compiling With Eclipse

16Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Step 2: Create a PackageA good convention is to name the top package the same name as the Project using lower camel case.

Camel case is the practice of stringingCapitalizedWords together with no spaces. Lower camel case does not capitalize the lead word.

Page 17: Compiling With Eclipse

17Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Step 3: Create a ClassTo create a class:1. Right click on the project name to create a new class

named StudyPage in the studyTool package.2. Select the option to create the main method.

A class, in Java, is a construct that is used as a blueprint to create objects. It can also be a construct in which objects are created.

Page 18: Compiling With Eclipse

18Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Step 3: Create a Class (cont.)Notice the following:• The StudyPage.java class appears in the studyTool

package in the Package Explorer View.• The StudyPage.java class is created with the main

method.

A main method, in Java, is the method inside a class that runs when the class is compiled and ran.

Page 19: Compiling With Eclipse

19Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Step 4: Create and Run Java CodeTo implement the main method and run the program:1. Enter the following information into the main class:

2. Right click on StudyPage.java in the package view.

3. Choose Run As → Java Application.4. Save the class when prompted.5. Note results display in the Console

View.

public class studyPage {

public static void main(String[] args) {

System.out.println(“Enter a study term”);

}

}

Page 20: Compiling With Eclipse

20Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Step 4: Create and Run Java Code (cont.)Continue implementing the main class to accept a term, prompt for a definition, accept a definition, and finally, display the term and definition.package studyTool;import java.util.Scanner;

public class StudyPage { public static void main(String[] args) {

Scanner scanterm = new Scanner(System.in);

String termvar;

System.out.println("Enter a study term");

termvar = scanterm.nextLine();

Continued on next slide...

Page 21: Compiling With Eclipse

21Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Step 4: Create and Run Java Code (cont.)Continue implementing the main class to accept a term, prompt for a definition, accept a definition, and finally, display the term and definition.

Continued from previous slide...

Scanner scandef = new Scanner(System.in);

String termdef;

System.out.println("Enter a definition");

termdef = scandef.nextLine();

System.out.println(termvar+": "+termdef);

}

}

Page 22: Compiling With Eclipse

22Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

Program to Convert Gallons to LitersWhat is the formula to convert gallons to liters? • 1 US gallon is equivalent to 3.78541178 Liters

What is the math calculation to complete the conversion?• Pseudocode would look like:

– Use Scanner to read in the number of gallons.– Store the number of gallons in a variable.– Multiply that variable by 3.785 (variable*3.785) and

store this new value into a second variable (Hint: use double for the variables, not int).

– Display the output.

Now try it on your own. Create the program!

Page 23: Compiling With Eclipse

23Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

TerminologyKey terms used in this lesson:Camel caseEclipse edit and view areasJava classJava packageJava main methodOpen perspectiveSwitch workspace

Page 24: Compiling With Eclipse

24Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

SummaryIn this lesson, you learned how to: • Identify components of Eclipse• Compile an application• Test to ensure application is complete• Write the code for GalToLit.java• Continue coding until error free compilation and

execution is reached• Modify the program to compute a different number of

gallons into its equivalent number of liters

Page 25: Compiling With Eclipse

25Copyright © 2012, Oracle. All rights reserved.

Compiling with Eclipse

PracticeThe exercises for this lesson cover the following topics:• Creating a Java program using Eclipse