Top Banner
CSC 111 CSC 111 Java Programming I Java Programming I Chapter 1: An Overview of Chapter 1: An Overview of Computers and Programming Computers and Programming Languages Languages
34

CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Jan 17, 2016

Download

Documents

Charles Peters
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

CSC 111CSC 111Java Programming IJava Programming I

Chapter 1: An Overview of Chapter 1: An Overview of Computers and Programming Computers and Programming LanguagesLanguages

Page 2: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 2

Chapter Objectives

Learn about the evolution of programming languages Examine high-level programming languages Discover what a compiler is and what it does Examine how a Java program is processed Learn what an algorithm is and explore problem-

solving techniques Become aware of structured and object-oriented

programming design methodologies

Page 3: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 3

Computers have greatly affected our daily lives, helping us complete many tasks.

Computer program (software) is created with Programming languages which is easier for humans to read and write.

Programming languages (High-level languages) are a bridge between human languages and computer instructions (low level languages).

Basic, FORTRAN, COBOL, C/C++ and Java all are examples of programming languages

Evolution of Programming Languages

Page 4: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 4

History Of Java

Developed by Sun Microsystems – a company known for its workstations .

Java is well known for developing internet applications. it is used to : Create web pages with dynamic and interactive content. Develop large –scale enterprise applications. Enhance the functionality of WWW servers . Provide applications for customer devices ( ex . Cell phones) .

Java programs can run from a web browser.

Page 5: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 5

Java is portable. Your programs will run on your home computer and the Uni computers without changes.

Rich library. You can take advantage of code that is already written to do many commonly needed tasks and write more complex programs earlier.

History Of Java

Page 6: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 6

So… What is programming?

What do you think programming is?

Programming is, basically, solving a problem using a computer.

More formally: Programming is the process of translating a problem we want solved into software that runs (successfully) on a computer.

Page 7: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 7

So… How do we learn to program? PracticePractice, , practice practice and more and more practice !!!practice !!!

Like learning to swim … You wont learn by reading the book! You wont learn by copying from other students! You wont learn by not doing it

You need to get confidence inYou need to get confidence in 1. Using the environment

Your computer, editors, compilers, running programs 2. How to construct programs • Understanding the features of the language • This is what mostly what we cover in lectures • Planning (i.e., thinking!) 3. Getting the program to work • Compiling, testing, debugging

Need to get good at all threeNeed to get good at all three

Page 8: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 8

Basics of Java Environment

The environment The language Java applications programming Interface API Various class libraries

Page 9: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 9

Processing a Java ProgramA Java program undergoes several stages :1. Editing: use Java code and save in a text file named className .java ( source program ).

2. Compiling : the compiler checks the source program for any syntaxsyntax errors then translates the program into code understood by interpreter called bytecode saved in a file named className.class

3. Loading : the .class file is loaded into computer main memory for execution, and connected to all classes.

4. Verifying : to validate and secure against damage .

5. Interpreting :the Interpreter reads and translates each bytecode instruction into machine language and then executes it , one instruction at a time .

Page 10: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 10

Processing a Java Program

Page 11: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 11

Processing a Java Program

Java Virtual Machine (JVM): A hypothetical computer developed to make Java programs machine independent ( i.e run on many different types of computer platforms ).

Bytecode is the machine language for the JVM .

Page 12: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 12

Processing a Java Program

Two types of Java programs:

Applications : standalone programs stored and executed on a local computer .

Applets : small programs stored on remote computers that users connect to via a WWW browser. Applets are loaded into the browser , executed then discarded .

Page 13: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 13

What does a Java program look like? Example 1

A simple Java application:

an application executes using the Java interpreter.

The basic unit of a Java program is a class. Every Java program must have at least one class . Each class begins with a class declaration that

defines data and methods for the class . We’ll talk about this more later.

Page 14: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 14

Here’s a class called Welcome: public class Welcome { // This is a comment. } It’s a convention that the name of a class starts with a capital letter. You’ll meet other conventions along the way!! At the moment, we’ve defined a class that does nothing. The line with the // in front of it is not translated by the compiler - it’s called a

comment and it’s ignored. So let’s make our program do something!

What does a Java program look like? Example 1

Page 15: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 15

public class Welcome { public static void main(String [] args) { System.out.print(“Welcome to Java”); } }

We’ve now added a main method to our class. We’ve also used a number of words that have a special meaning to Java: class, public, static, void and String. The line System.out.print(“Welcome to Java ”) is an

instruction to print the sentence Welcome to Java on the screen. The double quotes (“) are not printed out as they are used to inform the

compiler that Welcome to Java is a String. Then our program exits.

What does a Java program look like? Example 1

Page 16: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 16

public static void main (String args[]) is a part of every Java application program.

Java applications automatically begin executing at main()

The void before main() means that main will not return any info .

A Java class must contain one main method if it is an application .

What does a Java program look like? Example 1

Page 17: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 17

1. Type the program into a text editor

2. Save as Welcome.java

3. Compile into byte codes

javac Welcome.java

4. Execute byte codes

java Welcome

What does a Java program look like? Let’s make it work !

Page 18: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 18

What does a Java program look like? Let’s work it !

Page 19: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 19

Upper case and lower case are very important! Java is case-sensitive. ( AA is NOT similar to aa)

Your class name MUST MATCH YOUR FILE NAME.

You only use the class name when you invoke Java but you use the file name when you invoke the compiler (Javac).

A file cannot contain two public classes.

What does a Java program look like? Remember !

Page 20: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 20

public class ASimpleJavaProgram { public static void main(String[] args) { System.out.println("My first Java program."); System.out.println("The sum of 2 and 3 = " + 5); System.out.println("7 + 8 = " + (7 + 8)); } }

Class name

Java

o/p

stmts

Body of class

Heading of method main

What does a Java program look like? Example 2

Page 21: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 21

A Java output statement causes the program to evaluate whatever is in the parentheses and display the result on screen .

+ is used to concatenate the strings . The system automatically converts the number 5 into a string ,joins that string with the first string ,and displays it .

What does a Java program look like? Example 2

Page 22: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 22

The parentheses around 7+8 causes the system to add the numbers 7 and 8 ,resulting in 15 .

The number 15 is then converted to string 15 and joined with string “7+8”= “ .

Sample Run:

My first Java program.

The sum of 2 and 3 = 5

7 + 8 = 15

What does a Java program look like? Example 2

Page 23: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 23

Internet ,WWW and Java InternetInternet : is an interconnection of networks that allows computers

around the world to communicate with each other .

In 1970’s , the US DOD developed techniques to interlink networks , i.e communication protocols so that networked computers could communicate

Page 24: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 24

Internet ,WWW and Java

WWWWWW uses s/w programs that enable users to view documents on any computer over the internet

The primary language of web is HTML , a simple language for laying out and linking documents .

HTML is not capable of interacting with users except to collect info via simple forms .

Java appletsJava applets make the web responsive and interactive

Page 25: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 25

Java Applet Example

Brush1.java

Brush1.class

Brush1.html

Page 26: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 26

Problem-Analysis-Coding-Execution Cycle

AlgorithmAlgorithm: a step-by-step, problem-solving process in which a solution is arrived at in a finite amount of time.

PseudocodePseudocode: an “outline” of a program that could be translated into actual code. It is a technique to show the programming steps.

Page 27: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 27

ExampleDesign an algorithm to find the perimeter and areaof a rectangle.

Steps:

1. Get the length of the rectangle.2. Get the width of the rectangle.3. Find the perimeter using: perimeter = 2 *(Length + Width)4. Find the area using: area= Length * Width

Page 28: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 28

Problem-Solving Process

1. Analyze the problem: Outline solution requirements and design an algorithm.

2. Implement the algorithm in a programming language (Java) and verify that the algorithm works.

3. Maintain the program: Use and modify if the problem domain changes.

Page 29: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 29

Problem-Analysis-Coding-Execution Cycle

Page 30: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 30

Programming Methodologies

Two basic approaches to programming design:

Structured design

Object-oriented design

Page 31: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 31

Structured Design

1. A problem is divided into smaller sub-problems.

2. Each sub-problem is analyzed, solved and a solution for this sub-problem is obtained.

3. The solutions of all sub-problems are combined to solve the overall problem.

4. Is called structured programming , top-down design approach, or modular programming .

Page 32: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 32

Object-Oriented Design (OOD)

In OOD, a program is a collection of interacting objects.

An object consists of data and operations.

Steps in OOD:

1.1. Identify the objectsIdentify the objects which form the basis of the solution , then determine how these objects interact with each other .

Example : write a program that automates the video rental process for a video store .

The two main objects are : 1- video

2- customer

Page 33: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 33

Object-Oriented Design (OOD)

Steps in OOD:

2. Specify the relevant data for each object and the possible operations to be performed on that data .

Example : for the video object

o the data might be :

movie name ,Starring actors ,and Number of copies in stock.

o The operations on video object might include :

checking the name of the movie , reducing the # of copies in stock by 1 after renting a copy .

Page 34: CSC 111 Java Programming I Chapter 1: An Overview of Computers and Programming Languages.

Java Programming: From Problem Analysis to Program Design, Third Edition 34

Object-Oriented Design (OOD)

Each object consists of data and operations on those data

The final program is a collection of interacting objects.

More on that in CSC113.