Download Chapter 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSC 112
Java Programming I
Chapter 1: An Overview of Computers
and Programming Languages
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 problemsolving techniques
 Become aware of structured and object-oriented
programming design methodologies
Java Programming: From Problem Analysis to Program Design, Third Edition
2
Evolution of
Programming Languages
 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
Java Programming: From Problem Analysis to Program Design, Third Edition
3
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.
Java Programming: From Problem Analysis to Program Design, Third Edition
4
History Of Java
 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.
Java Programming: From Problem Analysis to Program Design, Third Edition
5
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.
Java Programming: From Problem Analysis to Program Design, Third Edition
6
So… How do we learn to program?
 Practice, practice and more 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 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 three
Java Programming: From Problem Analysis to Program Design, Third Edition
7
Basics of Java Environment




The environment
The language
Java applications programming Interface API
Various class libraries
Java Programming: From Problem Analysis to Program Design, Third Edition
8
Processing a Java Program
A 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 syntax
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 .
Java Programming: From Problem Analysis to Program Design, Third Edition
9
Processing a Java Program
Java Programming: From Problem Analysis to Program Design, Third Edition
10
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 .
Java Programming: From Problem Analysis to Program Design, Third Edition
11
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 .
Java Programming: From Problem Analysis to Program Design, Third Edition
12
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.
Java Programming: From Problem Analysis to Program Design, Third Edition
13
What does a Java program look like?
Example 1
 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!
Java Programming: From Problem Analysis to Program Design, Third Edition
14
What does a Java program look like?
Example 1
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.
Java Programming: From Problem Analysis to Program Design, Third Edition
15
What does a Java program look like?
Example 1
 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 .
Java Programming: From Problem Analysis to Program Design, Third Edition
16
What does a Java program look like?
Let’s make it work !
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
Java Programming: From Problem Analysis to Program Design, Third Edition
17
What does a Java program look like?
Let’s work it !
Java Programming: From Problem Analysis to Program Design, Third Edition
18
What does a Java program look like?
Remember !
 Upper case and lower case are very important! Java
is case-sensitive. ( A is NOT similar to a)
 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.
Java Programming: From Problem Analysis to Program Design, Third Edition
19
What does a Java program look like?
Example 2
public class ASimpleJavaProgram
Class name
{
public static void main(String[] args)
{
System.out.println("My first Java program.");
Java
System.out.println("The sum of 2 and 3 = " + 5);
o/p
System.out.println("7 + 8 = " + (7 + 8));
stmts
}
}
Body of class
Heading of method
main
Java Programming: From Problem Analysis to Program Design, Third Edition
20
What does a Java program look like?
Example 2
 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 .
Java Programming: From Problem Analysis to Program Design, Third Edition
21
What does a Java program look like?
Example 2
 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
Java Programming: From Problem Analysis to Program Design, Third Edition
22
Internet ,WWW and Java
 Internet : 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
Java Programming: From Problem Analysis to Program Design, Third Edition
23
Internet ,WWW and Java
 WWW 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 applets make the web responsive and interactive
Java Programming: From Problem Analysis to Program Design, Third Edition
24
Java Applet Example
Brush1.java
Brush1.class
Brush1.html
Java Programming: From Problem Analysis to Program Design, Third Edition
25
Problem-Analysis-CodingExecution Cycle
 Algorithm: a step-by-step, problem-solving process
in which a solution is arrived at in a finite amount of
time.
 Pseudocode: an “outline” of a program that could be
translated into actual code. It is a technique to show
the programming steps.
Java Programming: From Problem Analysis to Program Design, Third Edition
26
Example
Design an algorithm to find the perimeter and area
of a rectangle.
Steps:
1.
2.
3.
4.
Get the length of the rectangle.
Get the width of the rectangle.
Find the perimeter using: perimeter = 2 *(Length
Find the area using: area= Length * Width
Java Programming: From Problem Analysis to Program Design, Third Edition
+ Width)
27
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.
Java Programming: From Problem Analysis to Program Design, Third Edition
28
Problem-Analysis-Coding-Execution Cycle
Java Programming: From Problem Analysis to Program Design, Third Edition
29
Programming Methodologies
Two basic approaches to programming design:
 Structured design
 Object-oriented design
Java Programming: From Problem Analysis to Program Design, Third Edition
30
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 .
Java Programming: From Problem Analysis to Program Design, Third Edition
31
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.
Identify 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
Java Programming: From Problem Analysis to Program Design, Third Edition
32
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 .
Java Programming: From Problem Analysis to Program Design, Third Edition
33
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.
Java Programming: From Problem Analysis to Program Design, Third Edition
34