Getting Started with Eclipse and Java Dr. Sebastian van Delden.

Post on 28-Dec-2015

230 views 5 download

Tags:

Transcript of Getting Started with Eclipse and Java Dr. Sebastian van Delden.

Getting Started with Eclipse and Java

Dr. Sebastian van Delden

What is “Eclipse” and “Java” Eclipse

Used world-wide; very popular. Software used to facilitate the creation of other

software. An “IDE” (Integrated Development Environment) or “SDK” (Software Development Kit)

Consists of: editor, syntax highlighting, debugging, navigator.

Java Programming Language Collection of commands and symbols that you can

use to formulate software. Most popular language in the world (according to

some studies)

Let’s think about this… What if in your English class you were

asked to type up an essay describing something. What would you use to type up the essay?

Microsoft Word? Notepad? OpenOffice? What language would you use?

English? Chinese? Spanish? The relationship between Microsoft

Word and English is like the relationship between Eclipse and Java.

Top Ten Most Popular Programming Languages in the World (2011)1. Java2. C3. C++4. PHP5. C#6. Objective-C7. Visual Basic8. Python9. Perl10. Javascript

FUN FACT:

“LISP” is number 14. It was designed by John McCarthy as the first “Artificial Intelligence” language.

Let’s think about a computer… What are the inputs, outputs, and the brain?

The System Unit (or computer). CPU = brains Random Access Memory (RAM) = short term

memory Hard Drive = long term memory

Inside a harddrive

Where is a program/software stored? When a program is first created it is in RAM. When the computer is turned off RAM is

erased. A hard drive can stored information “long

term” so that you can the computer off. For a program to run it must be in RAM The CPU is the “brains” of the computer. It

reads the program in memory and executed it.

But what language does a CPU speak? All CPUs speak “machine” or “binary” language. A sequences of 0s and 1s. 0101010001011101010101010100101…

But, Java is not 01010101101010s – as we’ll see later… So how does a CPU understand it????

Think. If you wrote an essay in English but you want somebody who onlyspeaks Chinese or Spanish to read it,

what would you have to do to your essay???

Compiling… Basic Idea:

Compiling is the process of translated the programming language code into 1010101010s so that the CPU can understand it.

class fun{private int x;public fun(){….

Java Code (people can understand this)

Compiler

0100101101111010111010010000101010101010101010101010….

Binary Code (CPU can understand this)

Back to Eclipse and Java Eclipse has all the tools need to develop

software using the Java language There is an editor (like MS Word) to type up

the program There is a compiler to translate it so that the

CPU can understand it. There is a “debugger” to help you figure out

where you errors (or bugs) are in your program. Fun Fact: In early computers, moths

used to get into them and chew on the components causing them to short out. The term “computer bug” came from moths.

Java is an “Object Oriented” language Main objective in Object Oriented (OO)

programming is to model “the objects” in the programs.

There are two things all objects need to be defined: What behaviors/actions can the object perform? What attributes/properties does the object

possess? The contents of the attributes represents the state of

the object.

The Object Oriented Approach

Example Consider a Mindstorm Robot Example Actions/behaviors:

Rotate a motor forward Rotate a motor backward Make a beep sound Display text on the LCD screen

Example Attributes: Is a motor moving forward What text is currently being displayed on the LCD.

The Object Oriented Approach Algorithm Design and Analysis

Once the objects are well defined, you make to make it “do something”.

An “Algorithm” is just a detailed step-by-step approach to solving a problem.

Example: Navigate through a Maze Walk in a jig saw pattern Etc…

Java Java is a programming language

A formal language that you can use to communicate with the computer.

Its syntax is quite complicated and really takes a while to fully understand...

I.e. Some of this will look really weird!! And, we just won’t have time to explain everything…

We’ll be covering: Basic Input/Output – getting data in and out of the

program Simple arithmetic operations and variables. Later on, we’ll also cover looping and decision control

structures.

Java Simple Input/Output Output:

This is the exact code to display text to the computer screen.

// My first line of Java Code

System.out.println(“whatever you want to print”);

System.out is an object that represents the console/screen.

println(..) is an action/method that the System.out object can do – print text to the console/screen.

All Java statements end with a semi-colon!!

This a comment. Must start with // and you can put them anywhere.

Java Simple Input/Output Input

Get input into the program with a “Scanner” object.

One way to create such an object and name it “scan” is:

Scanner scan = new Scanner(System.in);

You’ll need to import the Scanner code at the very beginning of your program (before the class definition):

import java.util.*;Now you can read in the next item from the console.

For example nextInt() reads in the next integer from the console.

scan.nextInt();

Variables in Java

A variable is a name for a location in memory

They are used to remember data in a program

A variable must be declared by specifying the variable's name and the type of information that it will hold

int total;

int count, temp, result;

Multiple variables can be created in one declaration

data type variable name

Variable – The idea Think about a variable like a box in which you

can store a number or word. You can see what’s in the box at any time and

also put something different in the box at any time.

pi

3.14 int pi = 3.14;

This box can only

hold an int

Variable Naming User defined names (like variables) must obey

the following rules: Must start with a letter or underscore, and

can contain letters, underscore, and digits. Variable names are case sensitive

Examples: X X1 I_Like_this_Course 5x (not valid!!!)

Variable Initialization

A variable can be given an initial value in the declaration

• When a variable is referenced in a program, its current value is used

int sum = 0;int base = 32, max = 149;

A variable can be given an value later in the program

int average, sum = 198, count = 11;

average = sum/count

Assignment

An assignment statement changes the value of a variable

The assignment operator is the = sign

total = 55;

• The value that was in total is overwritten

• You can only assign a value to a variable that is consistent with the variable's declared type

• The expression on the right is evaluated and the result is stored in the variable on the left

Data Types

There are several types of data that can be stored/used in Java. We’ll just use a couple of them:

Integers (-13, 45, 0, 14, etc):

int

decimal numbers (3.14, 7.0, etc):

Double

Sequences of alpha-numeric characters (“fun”, “Hi There”):

String

Let’s Bring it All Together!! A Scanner object can read data into a variable Variables can be printed to the console Example program (what does this program do?!?!):

Scanner scan = new Scanner(System.in);double F, C;

C = scan.nextInt();F = 9.0 / 5.0 * C + 32;

System.out.println(F);

Two Types of Errors that you’ll see… Syntax Errors

You disobeyed Java’s language rules. Eclipse shows you little red lines where the errors

are You won’t be able to compile or run the problem. Example (comma should be semi-colon): int pi =

3.14, Semantic Errors

The meaning of the program is wrong. You can compile and run it, but it just does the

wrong thing. Example (you wanted to print “hi there”):

System.out.println(“hi tehre”);

Using built-in Java Objects

Don’t reinvent the wheel!!

The Math Class The Math class is part of the java.lang.*

package

The Math class contains methods that perform various mathematical functions

These include:

absolute value

square root

exponentiation

trigonometric functions

The Math Class The methods of the Math class are static

methods (also called class methods)

Static methods can be used through the class name – no object of the Math class is needed

value = Math.cos(90) + Math.sqrt(delta);

Programming Using Eclipse

Installing Eclipse on you home computer Go to:

http://www.eclipse.org/downloads/ Download the latest version of “Eclipse

Classic” Save ZIP file to your computer UNZIP file on your C: drive Go to the folder and double click on the

eclipse program. Click “OK” to use the default

workspace.

You should get a welcome screen. Go ahead and close the Welcome tab.

To start a programming in Eclipse, you MUST first create a New Project

Choose “Java Project” and click Next >

Give the Project a name. Leave all the default

setting alone and just click Finish.

Right Click on the Project and navigate to New > Class.

Give the class a name. Main

Put a check mark where it says public static void main(String[] args). Don’t worry about

what all that stuff means….

This creates a “driver” program – the initial program in the project that gets things running.

Click Finish.

You are ready to start programming!

Insert Java Code Right here

Navigate through your project files here

The “Console” is here. Output is displayed here.

Type in some code and click the run button.

This code prints “Hello World!!” to the Console area.

See output displayed in the Console Area.

Putting it all together – Write a program that converts Celsius to Fahrenheit

Don’t forget imports….

Ready to start programming?!?! You are going to write several little programs. Just comment the old code before starting the code for a

new problem. I.e. Do not erase it…

Comment old code, do not erase it…

Start new code…

It’s Your turn! Write the following programs: All input/output data should be doubles. Covert Fahrenheit to Celsius Input to program is a circle radius, output is the circle’s

perimeter and area. Note there is a Math.PI attribute. Input to program is two 2D points (x1, y1) and (x2, y2),

output is the distance between these points and the formula for the line which connects the two points. Note: there is a Math.sqrt(a) which returns the square root of a.

A program that implements the quadratic equation: ex + x2-4x+15. Note: there is a Math.pow(a,b) which returns ab and a Math.E attribute. Don’t forget to import java.lang.*; Input to the program is x, output is f(x)

Given “a” and “b” of a right triangle, output: c, angle at A, angle at B, area, and perimeter.