introduction to java. what is java? a computer programming language that can be run as either an...

26
Introduction to Java

Post on 21-Dec-2015

221 views

Category:

Documents


5 download

TRANSCRIPT

Introduction to Java

What is Java?• A computer programming language that

can be run as either an application or an applet.– What is the difference?

• It is considered to be a high-level language.• Designed in the early 90’s by a team

from ???• What is the difference between Java and

JavaScript ?• How did java get its name?

– http://www.javaworld.com/javaworld/jw-10-1996/jw-10-javaname.fullquote.html

Java is…• Parsimonious – that is it has a compact set of

commands without numerous versions or changes• Robust- it supports the development of programs

that do not accidentally overwrite data or corrupt data

• A Strongly typed language – in that its compiler provides extensive checking for potential problems

• Secure – its programs are easy to protect from viruses.

• Portability/platform independent – can be used on multiple operating systems

Why Learn Java?

• Good Paying Careers• It’s just a good fun language to kno

w

Java Components

• Program source• Compiler• Bytecode• Interpreter• Java Virtual Machine• Java Runtime Environment• IDE- Integrated Development

Environement

The Java Program Development Life Cycle

• Analyze the problem• Design the program• Code the program• Test the program• Formalize the solution• Maintain the program

Algorithms• A clear and unambiguous specification of

the steps needed to solve a problem.• Correct – using a logical constructs and

valid data in an organized way so that the steps will be carried out correctly and the program will make suitable responses to invalid data, ie warning messages

• Efficient – refers to the program’s ability to deliver a result in a short time frame to be useful and using as little memory as possible

Understanding FlowCharts

• Process Symbol• Input/Output Symbol• Flowline Symbol• Annotation Symbol• Decision Symbol• Terminal Symbol• Connecter Symbol• Predefined Process Symbol

Control Structures

• No matter what procedures (or progression of logical actions) that you wish the computer to perform they can be broken down into these three categories– Sequence Control– Selection Control

• Case Control

– Repetition Control• Nested

Java as an Object-Oriented Language

• Still has many different definitions, but the general concept is to package functions together while not restricting data types.– Ie the open desk example

Object- Speak

• Nouns– Object – anything real or abstract

about which you both store data and operations that manipulate the data

– Class- an object or set of objects that share a common structure and common behavior.• Subclass• superclass

Object Speak Cont• Generalization hierarchy

– An OO design tool used to show the relationships among classes

• Data abstraction– Process of creating new high-level data

structures based on a defined object through a set of interface operations instead of by directly manipulating the object

• Instance – unique object or specific occurrence of a class of objects – Ie a two-player button

Verbs

• Operation – activity that reads or manipulates the data of an object.

• Message defines the interaction of the object

• External trigger• Process that causes an operation

to occur is an event– Event diagram page 1.13

Adjectives

• Attributes are identifying characteristics of individual objects, such as name, size, or color.

Other terms• Encapsulation

– Process of hiding the implementation details of an object from its user by combining attributes and methods

– Inheritance• Concept that programmers can use a class along with

functions and data to create a descendent class or subclass

• Polymorphism– Allows instructions to be given to an object in a

generalized rather than specific detailed command, ie where specific actions are different, results are the same

Lets look at an Actual Java Program• /*• Project 1 Printing on the screen• Programmer: Carl Rebman• Date: 4 March 2003• Program Name Carl• */

• public class carl

• {• public static void main(String [] args) //method header• {• System.out.println("Carl's Antiques");• System.out.println("208 Olin Hall");• System.out.println("San Diego, CA 92110");• }• }

How do you run it?

• First step is to have the JDK loaded• Second step (after program is

written) is to compile the program– Go to command prompt – Set prompt to location of JDK compiler– In this case should be path=c:\jdk1.2.2\

bin– Next change to the directory your

program is in—should be A:\

Running the program cont.• Once path has been set and proper

directory has been established• Run compiler

– javac Program_Name.java• If successful you’ll get the command

prompt again• If not, you see a list of errors• When there are no errors, run program

by typing in – java Progam_Name

So what does the code mean?

• public class Carl {– A class must be defined in every java

program– Public is used as a modifer that describe

other classes’s access to this class• This becomes more important as your programs

get more complex

– Basically this line starts your program called Carl, and the brackets contain its guts

public static void main(String [] args)

• Methods are the groups of statements that are executed by your computer when instructed to do so.

• The main method actually drives the java applications– It is the first place the Java VM looks

when you run your application

Public versus static• Public makes this method accessible

from all other classes• Static ensures there is only one

reference to this method• Void means that the method does not

return any value when completed• The Strings[args] means that it is an

array list of command-line arguments

Comments

• Single line—begins with //• Multiple line begin with /* and end

with */

Moving up to an Applet

• Again what is an applet?

How to run an Applet

• Type at the command prompt– appletviewer FILE_NAME.html

Special Code• import java.awt.Graphics;

– This does actually import a class called Graphics– This is a previously defined class of which you

can call

• Extends java.applet Applet– This increases the import and allows for

extending the class• You get to reuse the functionality of the class• Which means when class A extend class B, class A

becomes a superclass, and class B becomes a subclass

Now lets write our first JAVA program

• Project 1 in book