Вы находитесь на странице: 1из 13

Computer Science III

Computer Science III

Topic 3 Java introduction

Learning objectives
At the end of the topic you will be able to:

Define the syntax of variables in the Java language. Define the syntax of the main commands in the Java language. Define the syntax of the procedures and functions in the Java language. Id tif the Identify th characteristics h t i ti of f the th web b browsers. b Solve logical problems.

Derechos reservados TecMilenio, A.C.

Computer Science III

Introduction
Now that we have enough tools to structure real problems in representations that the Java language can understand, we will look more closely to the way Java language instructions are written. Have you ever wondered why there are so many programs to surf the Web? Have you wondered why, when we login in to our e-mail e mail account account, the page adjusts itself to fit in our screen? And the same goes for our PDA or cell phone.

Introduction
Do you know why some websites seem distorted or different when viewed from a Mac computer? In this topic you will find the answer to all of these questions as well as their relation with the Java language.

Derechos reservados TecMilenio, A.C.

Computer Science III

Introduction to Java

To create a Java program we need to generate a file that


when compiled, compiled it generates a new file that the computer can execute.

This file is called source file and it is a text file that you
can generate in any text or word processor such as Notepad or Microsoft Word.

Introduction to Java
Some things to consider when generating a source file:
Uppercase / lowercase sensitive The sentences end with a semicolon. Blocks of instructions delimited with curly brackets { } Comments of a line and multiline. The word class is not equal to the word Class. Any statement (sentence) in Java must end with a semicolon (;). The blocks of instructions define the beginning and the end of a module in our source file which is performed independently. Comments are instructions within a program that are not executed and which main task is to allow us to document the steps. o / / Comment of a line o / * multiline comment * /

Derechos reservados TecMilenio, A.C.

Computer Science III

Definition of variables
A variable is a space in memory where a program can store data and use it later. There are different types of data. We define a data type according to a variable to determine the amount of "space" that will be required in memory to store the data we require.

Definition of variables
1.

Primitive Type: a. byte b. char b c. short d. integer a. long b float b. c. double d. boolean

A. Whole numbers

There are the byte, short, integer and long type. The char type represents a character. B. Character The float and double types represent the C. Decimal numbers d i l numbers. decimal b The boolean type is the logical data and D. Logics its only two values are true or false.

2. Object type or referenced 3. Enumerated type

Derechos reservados TecMilenio, A.C.

Computer Science III

Definition of variables

Data type byte char short integer long float double boolean

Values it can save 125 S 31,425 217483647 92233720368575807 43.2421 6.3 x 10242 true / false

Executions of basic commands and sequences


A sequence is a set of instructions that are executed sequentially one after another sequentially, another. Within a Java program we can make different types of operations. The main ones are the arithmetic operations that allow us to perform mathematical operations that manipulate data in a program, like if we were using a calculator.

Derechos reservados TecMilenio, A.C.

Computer Science III

Executions of basic commands and sequence.

Arithmetic operators. Their function is the equivalent to


press the key with the plus sign (+), minus (-), multiplication (x) (x), and division () ( ) in your calculator. calculator Operator + * / % ++ -Description Adds two numerical values. Subtracts two numerical values. Multiplies two numerical values. Divides two numbers. C l l t the Calculates th result lt of f the th division di i i b between t two numbers (residuals). Increments a value by one. Decrements a value by one.

Executions of basic commands and sequence.


Arithmetic expressions It is a set of operands connected by operators to specify a particular operation operation. Example of an arithmetic expression: //Expression returned by the data1 * data2 multiplication of the value of d t 1 by data1 b data2 d t 2

Derechos reservados TecMilenio, A.C.

Computer Science III

Executions of basic commands and sequence.


If we need to perform more operations within the same expression we can organize them by parentheses to specify the order in which operations will be executed.

(data1 + data2) / data3

In this expression we can determine that the first operation performed is the sum of data1 and data2, and the result of this operation is divided by data3.

Definition of procedure
Procedures are a set of statements grouped with its own y have a p particular g goal. name and they You can define as many procedures as you want. The use of procedures allows a program to be modular, which means that it is organized by different modules (procedures) that perform certain particular tasks, but when integrated they fulfill the ultimate objective of the application. application

Derechos reservados TecMilenio, A.C.

Computer Science III

Definition of procedure
You can write procedures in a Java program as follows:

static void Procedure_Name(){ Instructions }

Definition of procedure
Example: Lets suppose that you need to create a procedure that calculates the amount of days in 5 years years. Knowing that a year has 365 days, you just require an arithmetic operation that multiplies the value 365 by 5: static void amount_days(){ years=5; days=365; value1=(years * days) stores the result }

//value1

Derechos reservados TecMilenio, A.C.

Computer Science III

Characteristics of web browsers


The following table lists the most popular browsers and its main features: Web browser Microsoft Internet Explorer Mozilla Firefox Characteristics Most web pages are optimized for this browser. An advantage is that the handling of tabs is very fast. Does not handle pop-ups. It has interchangeable skins and integrated Google search. It is an open source browser. Apple Safari Rapid deployment of websites. JavaScript code runs much faster.

Logical problems
Logical problems show up in Java programs when we need to make a decision.

For example, why when you press the button in the right side of our cell phone the calendar is y and when y you press displayed the button in the left side it shows the contact list?

Derechos reservados TecMilenio, A.C.

Computer Science III

Logical problems
This is because your cell phone is making decisions to accomplish certain actions. Said in fewer words words, it could be defined as follows:

If you press the right button, then the "Calendar" application is run.

Logical problems

This is the kind of logical problems that run constantly in


a Java program and which are defined and solved using l i l operations. logical ti

Logical operations are made up of two conditions that


can have two values: true / false and when the result is true some instruction is executed.

For the operators that connect ttwo o or more conditions


are called logical operators.

Derechos reservados TecMilenio, A.C.

10

Computer Science III

Logical problems
In the following table are shown the logical operators and a brief description..
Logical operators Symbol

Description Returns a true result only when both conditions are true, otherwise returns false. Returns a false result only when both conditions are false, otherwise returns true. Affects only one condition and returns true if this is set to false and vice versa. Returns a true result if one condition is true and the other one is false, otherwise returns false.

AND OR NOT XOR

&& || ! ^

Logical problems
If: data1=true; data2=false; d t 2 f l The logical expression: data1 AND data2; Returns false because the data2 operator is false. For the expression: data1 OR data2; Returns true because there data1 is true. For the expression: p NOT data1; Returns false because data1 is true For the expression: data1 XOR data2; It is true because one value is true and the other is false.

Derechos reservados TecMilenio, A.C.

11

Computer Science III

Closing arguments

With the elements described in


this topic now we have a general overview on how to structure our programs in Java.

Remember that it is very


important to be aware that the computer t is i merely l at tool l th that t obeys the instructions that we give to it.

23

Closing arguments
We must forget those false ideas like the computer does not work or the computer was wrong. Rather, Rather we must pay more attention to warning or error messages generated by the computer and to be more cautious and more patient when trying to identify what is happening when we are confronted with messages of that nature. Now that y you know the structure and syntax y for Java programs we will go into developing them on the computer, run them and detect and solve errors during the development process.

24

Derechos reservados TecMilenio, A.C.

12

Computer Science III

Bibliography
Fundamentals of the Java Programming Language (SL-110-SE6) http://www.sun.com/training/catalog/courses/SL-110 SE6.xml

25

Credits
Content Author Ing. Javier Castro Ruiz, MATI Academic Coordinator Lic. Nelly Maldonado Gonzlez, MEA Translating Teacher Lic. Nelda Contreras Leal, MA Instructional Designer Lic Dalila de Len Bauelos Lic. Bauelos, MTE Graphic Designer Lic. Miguel ngel Reynosa Castro, MANM

26

Derechos reservados TecMilenio, A.C.

13

Вам также может понравиться