+ All Categories
Home > Technology > Java 101 @ chattahoochee

Java 101 @ chattahoochee

Date post: 03-Jul-2015
Category:
Upload: deanna-riddlespur
View: 229 times
Download: 0 times
Share this document with a friend
56
Learning Java DeAnna Riddlespur Spring 2014
Transcript
Page 1: Java 101 @ chattahoochee

Learning Java

DeAnna Riddlespur

Spring 2014

Page 2: Java 101 @ chattahoochee

Introduction to Computers, Introduction to Computers, Programs, And JavaPrograms, And Java

Objectives● To Understand computer basics, programs, and

operating systems

● To describe the relationship between Java and the World Wide Web

● The understand the meaning of Java language specification, AOI, JDK, and IDE

● To write a simple Java program

Page 3: Java 101 @ chattahoochee

Objectives ● To display output on the console

● To explain the basic syntax of a Java Program

● To create, compile and run Java programs

● To display output using the JOptionPane message dialog boxes

● To become famiiar with Java programming style and documentaton.

● To explain the differences between syntax errors, runtime errors and logic errors

Page 4: Java 101 @ chattahoochee

Introduction

The central theme we focus on is to learn how to solve problems by writing a program

● What is programming? The term programming means to create( or develop) software, which is also called a program.

● Software contains the instructions that tell a computer ( or device) what to do.

● Programming languages are powerful tools that software developers use to create software.

Page 5: Java 101 @ chattahoochee

● There are many programming languages

● Each language was invented for a specific purpose- to build on the strengths of a previous language or to give the programmer a new and unique set of tools.

● The key is to learn how to solve a problem using a programming approach.

Page 6: Java 101 @ chattahoochee

1.2 What is a computer? A computer is an electronic device that stores and processes data.

● A computer includes both hardware and software.

● Hardware - comprises the visible, physical elements of the compute

● Software - provides the invisible instructions that control the hardware and make it perform a specific task.

● Knowing computer hardware can help you better understand the effects that a program's instructions have on the computer and its components.

Page 7: Java 101 @ chattahoochee

Computer Components

● Central Processing unit(CPU)

● Memory ( main memory)

● Storage devices ( disks and CDs)

● Input devices ( mouse and keyboard)

● Output devices ( such as monitors and printers)

A computer's components are interconnected by a subsystem called a bus.

Page 8: Java 101 @ chattahoochee

● Motherboard is a circuit case that connects all of the parts of a computer together

● The bus is build into the computer's motherboard.

● The central processing unit (CPU) is the computer's brain.It retrieves instructions frommemory and executes them.

● The CPU usually has two components

● ---The control unit controls and coordinates the actions of the other components.

● ---The arithmetic/logic unit performs numeric operations ( addition, subtraction, multiplication, division) and logical operations ( comparisions )

Page 9: Java 101 @ chattahoochee

● CPUs are build on small silicon semiconductor chips tht contain millions of tiny electric switches called transistors for processing information.

● The internal clock emits electronic pusles at a constant rate. These pulses are used to control and synchronize the pace of operations.

● A higher clock speed enables more instructions to be executed in a given period of time.

● The unit of measurement of clock speed is the hertz (Hz), with 1 hertz equaling 1 pulse per second.

● In the 1990's computers measured clocked speed in megahertz(MHz)

● The clock speed of a computer is now usually stated in gigahertz (GHz)

Page 10: Java 101 @ chattahoochee

● CPUs were oriinally developed with only one core.

● The core is the part of the processor that performs the reading and executing of instructions.

● Multicore CPU is a single component with two or more independent processors.

● A computer is nothing more than a series of switches.

● Each switch exists in two states : on or off

● If the switch is on its value is 1

● If the switch is off the value is 0

● These 0s and 1s are interpreted as digits in the binary number system and are called bits ( binary digits)

Page 11: Java 101 @ chattahoochee

● Storing information in a computer is simply a matter of setting a sequence of switches on or off.

● The minimum storage unit in a computer is a byte

● A byte is composed of eight bits

● To store a number that cannot fit into a single byte, the computer uses several bytes.

● An encoding scheme is a set of rules that govern how a computer translates characters, numbers and symbols into data the computer can actually work with.

● Most schemes translate each character into a predetermined string of numbers

Page 12: Java 101 @ chattahoochee

● In ASCII encoding scheme the character c is represented by 01000011 in one byte.

Computer's storage capacity● Kilobyte(KB) 1,000 bytes● megabyte(MB) 1 million bytes● Gigabyte (GB) 1 billion bytes● Terabyte(TB) 1 trillion bytes

1 MB can store 50 pages of documents .

1 GB can store 50,000 pages of documents.

Two hour high resolution movie 8 GB

Page 13: Java 101 @ chattahoochee

● A computer's memory consists of an ordered sequence of bytes for storing programs as well as data that the program is working with

● Memory is like the computer's work area for executing a program

● A program and its data must be moved into the computer's memory before they can be executed by the CPU

● Every byte in the memory has a unique address

● The unique address is used to locate the byte for storing and retreiving data.

Page 14: Java 101 @ chattahoochee

● Memory is also referred to as random access memory(RAM)

● Because the bytes in the memory can be accessed in any order the memory is also referred to as random access memory

● The more RAM a computer has, the faster it can operate. But there are limits

● A memory byte is never empty, but its initial content may be meaningless to your program

● Current content of a memory byte is lost when new information is placed in it.

Page 15: Java 101 @ chattahoochee

● Memory is built on silicon semiconductor chips that have millions of transistors embedded on their surface.

● Compared to CPU chips, memory chips are less complicated, slower, and less expensive.

● RAM is a volatile form of data storage

● Volatile means any information that has been stored in memory si lost when the system's power is turned off

● Programs and data are permanently stored on storage devices

● Programs and data are moved to memory when the computer actually uses them.

Page 16: Java 101 @ chattahoochee

Three main types of storage devices:● Magnetic disk drives

● Optical disc drives (CD and DVD)

● USB flash drives

● --Drives are devices for operating a medium.

● ----A storage medium physically stores data and program instructions.

● Hard disks are used for permanently storing data and programs

● CD stands for compact disc

● CD-R is for read only permanent storage; the user cannot modify its contents once they are recorded

Page 17: Java 101 @ chattahoochee

● CD-RW can be used like a hard disk; you can write data onto the disc and then over wtite that data with new data

● DVD digital versatile disc or digital video disc

● Universal serial bus (USB) connectors allow the user to attach many kids of peripheral devices to the computer.

● A USB flash drive is a device for storing and transporting data .

● A flash drive acts like a protable hard drive that can be plugged intor your computer's USB port.

Page 18: Java 101 @ chattahoochee

● Input and output devices let the user communicate with the computer

● A keyboard is a device for entering input

● A keyboard consists of the keys for entering input

● Function keys are located across the top of the keyboard and are prefaced with the letter F.

● A modifier key is a special key that modifies the normal action of another key when the two are pressed at the same time

● The numberic keypad is located on the right side of most keyboards, it is a spearate set of keys styled like a claculator to use for entering numbers quickly.

● Arrow keys, are used to move the mouse on the screen in many kinds of programs

Page 19: Java 101 @ chattahoochee

● The insert delete page up and page down keys are used in word processing and other programs for inserting text and objects, deleting text and objects and moving up or down through a document one screen at a time

● A mouse is a pointing device

● A cursor is a graphical pointer ( usually in the shape of an arrow)

● The monitor displays information. The screen resolution and dot pitch determine the quality of the display

● The screen resloution specifies the number of pixels in horizontal and vertical dimensions of the display device.

Page 20: Java 101 @ chattahoochee

● Pixesl (picture elements) are tiny dots that form an image on the screen

● Dot pitch is the amount of space between mizels, measured in milimeters.

● The smaller the dotpitch the sharper the display.

● A modem uses a phone line and can transfer data at a speed up to 56,000bps ( bits per second)

● A digital subscriber line(DSL) connection also uses a standard phone line but it can transfer data 20 time faster than a standard dial up

● A cable modem uses the cable TV line maintained by the cable company and is generally faster than DSL

● A network interface card(NIC) is a device that connects a computer to a local area network .

Page 21: Java 101 @ chattahoochee

● Wireless networking is available in homes businesses and schools.

● A wireless adapter enables the computer to connect to a local area network and the internet.

Computer programs, known as software, are instructions that tell a computer what to do .Computers do not understand human languages so programs must be written in a language a computer can use. All programs must be converted into a language the computer can understand.

● Machine Language-a computer's native language – a set of built in primitive instructions

● These instructions are in the form of binary code.

Page 22: Java 101 @ chattahoochee

● Assembly language uses a short descriptive word known as a mnemonic to represent each of the machine language instructions.

● The mnemonic add typically means to add numbers and sub means to subtract the numbers

● Assembly languages were developed to make programming easier

● An assembler is a program used to translate assembly language programs into machine code.

● Assembly language is referred to as a low level language because assembly language is close in nature to machine language and is machine dependent.

Page 23: Java 101 @ chattahoochee

● High level languages are platform independent

● Platform indemendent means that you can write a program in a high level language and run it in different types of machines.

● Higl level languages are english like and easy to learn and use

● The instructions in a high level programming language are called statements.

● A program written n a high level language is called a source program or source code

● A source program must be translated into machine code for execution.

● Translation can be done using an interpreter or a compiler

Page 24: Java 101 @ chattahoochee

● An interpreter reads one statement from the source code translates it to the machine code or virtual machine code and then executes it right away.

Page 25: Java 101 @ chattahoochee

Popular Programming Languages

Ada

● Named for Ada lovelace, who worded on mechanical general- purpose computers. The Ada language was developed for the Department of Defense and is used mainly in defense projects

BASIC

● Beginner's All purpose Symbolic Instruction Code. It was designed to be learned and used easily by beginners.

Page 26: Java 101 @ chattahoochee

C

● Developed at Bell Laboraories.

● C combines the power of an assembly language with the ease of use and portability of a high level language

C++

● C++ is an object oriented language, based on C

C#

● Pronounced “C Sharp”

● it is a hybrid of Java and C++ and was developed by Microsoft.

COBOL

● Common Business Oriented Language

● Used for business applications

Page 27: Java 101 @ chattahoochee

FORTRAN

● FORmula TRANslation.

● Popular for scientific and mathematical applications.

Java

● Developed by Sun Microsystems

● Now a part of oracle

● It is widely used for developing platform independent internet applications

Pascal

● Named for Blaise Pascal who pioneered calculating machines in the seventeenth century

● It is a simple structured general purpose language primarly for teaching programming

Page 28: Java 101 @ chattahoochee

Python

● A sinple general purpose scripting language good for writing short programs

Visual Basic

● Visual Basic was developed by microsoft

● Enables the programmers to rapidly develop graphical user interfaces

Page 29: Java 101 @ chattahoochee

● An interpreter translates and executes a program one statement at a time

● A compiler translates the entire source program into a machine language file for execution

The operating system(OS) is the most important program that runs on a computer The OS manages and controls a computer's activities.

● The popular operating systems for general purpose computers are

● Microsoft Windows, Mac OS and Linus

● Application programs cannot run unless and operating system is installed and running on the computer.

Page 30: Java 101 @ chattahoochee

Major Tasks of an Operating system

● Controlling and monitoring system activities

● Allocating and assigning system resources

● Scheduling operations

Page 31: Java 101 @ chattahoochee

● Operating systems perform basic tasks

● An operating system must also ensure that different programs and users working at the same time do not interfere with each other.

● The OS is responsible for security, ensuring tht unauthorized users and programs do not access the system.

● The OS is responsible for determining what computer resources a program needs and for allocating and assigning them to run the program

● The OS is responsible for scheduling programs' activities to make efficient use of system resources.

● Multiprogramming allows multiple programs to run simultaneously by sharing the same CPU

● Multithreading allows a single program to execute multple tasks at the same time

Page 32: Java 101 @ chattahoochee

● Multiprocessing or parallel processing uses two or more processors together to perform subtasks concurrently and then combine solutions of the subtasks to obtain a solution for the entire task.

● It is like a surgucal operation where several doctors work together on one patient.

Page 33: Java 101 @ chattahoochee

Java, the World Wide Web and Beyond

Java is a powerful and versatile programming language for developing software running on mobile devices, desktop computers and servers.

Java was developed by a team ed by James Gosling at Sun Microsystems. Originally called Oak, Java was designed in 1991 for use in embedded chips in consumer electronic appliances. In 1995 , renamed Java it was redesigned for developing Web applications.

Page 34: Java 101 @ chattahoochee

● Java is popular particularly because of its promise that you can write a program once and run it anywhere

● Java is simple, object oriented, distributed, interpreted, robust, secure, architechure neutral, protable, high performance, multithreaded and dynamic.

● Java is a full- featured, general- purpose programming language that can be used to develop robust mission- critical applications.

● Java was used to develop the code to communicate with and control the robotic rover on Mars.

Page 35: Java 101 @ chattahoochee

● The World Wide Web is an electronic information repository that can be accessed on the Internet from anywhere in the world.

● The internet has been around for more than forty years.

● Java became attractive because Java programs can be run from a Web browser.

● Java programs run in a Web browser are called applets

● Applets employ a modern graphical interface with buttons, text fields, text areas, radio buttons and more

Page 36: Java 101 @ chattahoochee

● Applets make the Web responsive, interactive, and fun to use

● Applets are embedded in an HTML file

● HTML( Hypertext Markup Language) is a simple scripting language for laying out documents, linking documents on the internet and bringing images, sound and video alive on the Web.

● Java is very popular for developing applications of Web Servers

● Java is a versatile programming language

● You can use java to develop applications for desktop computers servers and small hand held devices

Page 37: Java 101 @ chattahoochee

● The software for Android cell phones is developed using Java

The Java Language Specification,Api,JDK, and IDE

Java syntax is defined in the Java language specification, and the Java library is defined in the

Java API, The JDK is the software for developing and running Java programs. An IDE is an integrated development environment to rapidly developing

programs

Page 38: Java 101 @ chattahoochee

● Computer languages have strict rules of usage

● If you do not follow the rules the computer will not be able to understand

● The Java language specification and the Java API define the Java standards.

● The Java language specification is a dechnical definition of the Java programming language's syntax and semantcs

● The application program interface ( API) also known as the library contains predefined classes and interfaces for developing Java programs.

Page 39: Java 101 @ chattahoochee

Three Editions of Java● Java Standard Edition (Java SE) to develop client-

side standalone applications or applets

● Java Enterprise Edition ( Java EE) to develop server-side applications, such as Java servelets, JavaServer Pages (JSP) and JavaServer Faces (JSF)

● Java Micro Edition ( Java ME) to develop applications for mobile devices.

Java SE is the foundation upon which all other Java technology is based.

● The Java Development Toolkit is called JDK

Page 40: Java 101 @ chattahoochee

● JDK consists of a set of separate programs, each invoked from a command line for developing and testing Java prorams.

● Java development tools

----NetBeans

----Eclipse

---Textpad

● Software that provides and integrated development enviorment ( IDE) for developing Java programs quickly

● Editing, compiling, building, debugging and online help are integrated in one graphical user interface

Page 41: Java 101 @ chattahoochee

A simple Java Program

A Java program is executed from the main method in the class.● Console- an old computer term that refers to the text

entry and display device of a computer

● Console input means to receive input from the keyboard

● Console output means to display output on the monitor

Page 42: Java 101 @ chattahoochee

1 public class Welcome {

2 public static void main ( String [] args) {

3 //Display message Welcome to Java! On the console

4 System.out.println(“Welcome to Java!”);

5 }

6}

Page 43: Java 101 @ chattahoochee

Note the line numbers are for reference purposes only!!!!

Line 1 defines a class.

● Every Java program must have at least one class

● Each class has a name

● Class names start with an uppercase letter.

● Out class name in the example is Welcome

Line 2 defines the main method

● The program is executed from the main method

● A class may contain several methods

● The main method is the entry point where the program begins execution

Page 44: Java 101 @ chattahoochee

● A method is a construct that contains statements

● The main method in our program contains the System.out.println statement

● This statements displays the string on the console

● String is a programming term meaning a sequence of characters

● A string must be enclosed in double quotation marks

● Every statement in Java ends with a semicolon(;) known as the statement terminator

● Reserved words or keywords have a specific meaning to the compiler and cannot be used for other purposes in the program

Page 45: Java 101 @ chattahoochee

● Class compiler understands that the word after class is the name for the class

● Public, static, and void are also reserved words in Java

● Line 3 is a comment that documents what the program is and how it is constructed

● Comments help programmers to communicate and understand the program

● Comments are ignored by the compiler

● Comments are preceded by two slashes (//) on a line called a line comment or enclosed between /* and */ on one or several lines

Page 46: Java 101 @ chattahoochee

● Block or paragraph comments are on several lines

● When the compiler sees // it ignors all text after // on the same line

● When it sees /* it scans for the next / and ignores any text between /* and*/

● A pair of curly braces in a program forms a block that groups the program's components.

● In Java each bock begins with an opening brace ({) and ends with a closing brace (})

● Every class has a class block that groups the data and methods of the class.

● Every method has a method block that groups the statements in a method

Page 47: Java 101 @ chattahoochee

● Blocks can be nested meaning that one block can be placed within another

● An opening brace must be matched by a closing brace

● Java source programs are case sensitive ● Main and main do not mean the same

thing.

Page 48: Java 101 @ chattahoochee

Special Characters

Page 49: Java 101 @ chattahoochee

● The most common errors made as one learns to program are syntax errors

● The multiplication operator in Java is *

Page 50: Java 101 @ chattahoochee

Create, Compiling and Executing a Java Program

● You save a Java program in a .java file and compile it into a .class file. The .class file is executed by the Java Virtual Machine.

● You have to create your program and compile it before it can be executed.

● If your program has errors you have to modify it to fix them then recompile it

● The rourse file must have the same exact name as the public class name

Page 51: Java 101 @ chattahoochee

● A java compiler translates a Java source file into a Java bytecode file

To compile Welcome.java:

javac Weclome.java● If you have no syntax errors, the compiler

generates a bytecode file with a .class extension.

Page 52: Java 101 @ chattahoochee

● The java language is a high level language

● Java bytecode is a low level language

● Bytecode is similar to machine instructions but is architecture neutral and can run on any platform that has a JVM

● The virtual machine is a program that interprets Java bytecode

● Java bytecode can run on a variety of hardware platforms and operating systems

● Java source code is compiled into Javabytecode and Java bytecode is interpreted by the JVM

● The JVM executes your code along with the code in the library

Page 53: Java 101 @ chattahoochee

● To extcute a java program is to run the program's bytecode

● You can execute the bytecode on any platfor with a JVM

● The JVM translates the individual instructions in the bytecode into the target machine language code one at a time rather than the whole program as a single unit

● Each step is executed immediately after it is translated.

● If you execute a file that does not exist a NoCLassDefFoundError will occur

● If you execute a class file that does not have a main method or you mistyped the main method a NoSuchMethodError will occur

Page 54: Java 101 @ chattahoochee

● The JVM first loads the bytecode of the class to memory using a program called the class loader

● If your program uses other classes the class loader dynamically loads them just before they are needed

● After a class is loaded the JVM uses a program called the bytecode verifier to check the validity of the bytecode and to ensure that the bytecode does not violate Java's security restrictions

Page 55: Java 101 @ chattahoochee

Displaying Text in a Message Dialog Box

You can display text in a graphical dialog box

● To display text in a message dialog box you need to use the showMessageDialog method in the JOptionPane class

● JOptionPane is one of the many predefined classes in the java library

Page 56: Java 101 @ chattahoochee

WelcomeInMessageDialogBox.java1 /* This application program displays Welcome to Java!

2 * in a message dialog box.

3 */

4 import javax.swing.JOptionPane;

5

6 public class WelcomeInMessageDialogBox {

7 public static void main(String[] args) {

8 // Display Welcome to Java! In message dialog box

9 JoptionPane.showMessageDialog(null,”Welcome to Java!”):

10}

11}


Recommended