Computer Science 111 Fundamentals of Computer Programming I - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Computer Science 111 Fundamentals of Computer Programming I

Description:

Disciplined introduction to computer programming with the Java language. ... We'll begin with Terminal I/O and move later to GUIs ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 33
Provided by: tomwh
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 111 Fundamentals of Computer Programming I


1
Computer Science 111Fundamentals of Computer
Programming I
  • First Java

2
Course Goals
  • Disciplined introduction to computer programming
    with the Java language.
  • Introduction to object-oriented programming.
  • Problem solving algorithm development.

3
A Very Brief History
  • 1990 Development of a language for consumer
    electronics begins (Sun)
  • Requirements
  • Portable
  • Reliable
  • 1993 the World Wide Web appears, and the
    developers realize that Java has great potential
    in that sphere

4
Continued Development
  • 1994 Sun develops HotJava web browser which is
    able to execute Java programs retrieved across
    the Internet, and publicly releases it and Java
    1.0
  • Soon thereafter, Netscape makes its extremely
    popular browser able to execute Java programs
  • 1997 Java 1.1 hits the silicon superhighway

5
Whats special about Java
  • Platform independent
  • Write program once, run anywhere
  • Program translated to bytecode
  • Local computer has interpreter to run bytecode
  • Provides security
  • Provides tools for graphics, database and
    network programming
  • Avoids many pitfalls of C and others

6
Java Virtual Machine
  • Different types of computers have their own
    machine languages. The machine language for a
    type of computer consists of specially coded
    basic, fundamental instructions that the computer
    can execute.
  • A high-level language is more English like,
    making it easier to write programs. Examples
    would be C, C, FORTRAN, Java, Basic, etc.
  • For a given type of computer and high level
    language, we must have a compiler to translate
    the program from the high-level language to the
    machine language.

7
Java Virtual Machine (cont.)
Machine Language 1
C Program
Machine Language 2
Machine Language 3
8
Java Virtual Machine (cont.)
  • With Java, the high level program is translated
    to a common language called byte code which is
    similar to a real machine language.
  • Then for each type of computer, we have a
    program, called a Java Virtual Machine (JVM),
    that can take the byte code program and execute
    its commands on the specific type of computer.
  • Thus the single byte code program can be run on
    various computers without change. For example,
    browsers have JVMs built in to execute Java
    applets (web programs).

9
Java Virtual Machine (cont.)
Machine 1JVM
Java Program
Byte Code
Machine 2JVM
10
User Interfaces
  • Terminal I/O interface Program prompts user
    with text on the terminal screen. User responds
    by typing text at the keyboard.
  • Graphical User Interfaces User interacts with
    the program via buttons, menus, input boxes, etc.
  • Well begin with Terminal I/O and move later to
    GUIs
  • In both cases well make use of classes (special
    packages) provided by our textbook authors.

11
Disclaimer
  • With any Java program there is a lot of
    boilerplate that must be present for the
    program to work. This will be the same for most
    of our programs.
  • For now, well just put this in place and
    concentrate on the logic of the program at hand.
  • Well talk about the mumbo-jumbo as we move along
    in the course.

12
Hello World Program
  • A tradition is to have your first program in a
    given language simply print the message Hello
    World!
  • In Java we work with objects which are instances
    of classes.
  • Objects have properties (members) and actions
    (methods).
  • Well create a class called HelloWorld.
  • An instance of HelloWorld will be an object that
    has a writer object that can write characters on
    the terminal screen.

13
Hello World (cont.)
  • The primary statement to be executed
    iswriter.println(Hello World!)
  • Here we are sending a message to the writer
    object telling it to execute its println method.
    The characters that we want it to display are in
    quotes. This information that the method needs to
    do its job is called a parameter.
  • Note that Java statements end with a semicolon.

14
Hello World (cont.)
15
Hello World (cont.)
16
Hello World version 2
  • This example, taken from the text, is the same as
    the first except it makes a coffee cup with the
    words streaming out the top.
  • The only change is in the run method.

17
Hello World version 2
18
Hello World version 2 (cont.)
19
Temperature Conversion
  • This example, from the text, allows the user to
    enter a temperature in Fahrenheit, and the
    program prints out the corresponding temperature
    in Celsius.
  • Recall that the formula for this conversion is C
    (5/9)(F-32)

20
Temperature Conversion (cont.)
21
Temperature Conversion (cont.)
22
Conversion Program Explained
  • Here we will give more explanation of this
    program. Well still postpone some of the detail.
  • The basic idea is that we are creating a class
    called Convert. An instance of this class will be
    an object that can do the conversions for us.

23
Conversion Explained (cont.)
  • An instance of the Convert class will have four
    properties.
  • The line KeyboardReader reader new
    KeyBoardReader()says one property is to be a
    KeyboardReader (capable of reading whats typed
    at the keyboard) whose name is reader.
  • The line ScreenWriter writer new
    ScreenWriter()gives writer as a ScreenWriter to
    write to the terminal screen.

24
Conversion Explained (cont.)
  • The other two properties that a Convert object
    has are given by the lines double
    fahrenheit double celsius
  • These say that the Convert object has two
    properties (variables) that are of type double,
    meaning that they can store numbers with
    fractional parts.
  • Technically the variables reader and writer refer
    to objects defined in the TerminalIO class, and
    fahrenheit and celsius are variables of a basic
    Java type.

25
Conversion Explained (cont.)
fahrenheit
212.0
KeyboardReaderObject
readDouble
ScreenWriterObject
print
println
26
Conversion Explained (cont.)
  • In addition to the four variables, reader,
    writer, fahrenheit and celsius, a Convert object
    has two methods functions it is able to
    perform.
  • The main method is the starting point for any
    class that is to execute on its own. Typically it
    instantiates an instance of the class and starts
    its execution. In this case, it asks the object
    to perform its run method.
  • The run method does the work for our Convert
    object.

27
Conversion Explained (cont.)
  • writer.print(Enter degrees Fahrenheit)sends a
    message to the writer object to print those
    words.
  • fahrenheit reader.readDouble()
  • Sends message to the reader to read a double from
    the keyboard
  • Then assigns this number as the current value of
    the fahrenheit variable.
  • celsius (fahrenheit 32.0) 5.0 / 9.0
  • Causes the expression on the right hand side to
    be computed using the current value of the
    variable fahrenheit, and using for
    multiplication
  • This computed value is assigned as the current
    value of the celsius variable

28
Conversion Explained (cont.)
  • writer.print(The equivalent in Celsius is
    )writer.println(celsius)cause the prompt to
    be printed and then the value of the variable
    celsius to be printed. Note that celsius is not
    in quotes so the value of the variable is
    printed, not the name of the variable.

29
Conversion Explained (cont.)
  • import TerminalIO.informs the java compiler
    that were using classes that are defined in the
    TerminalIO package.
  • public class Convert..Our programs are
    objects as well. We are defining a class of
    objects. When we execute, we will need an
    instance of this class.

30
Conversion Explained (cont.)
  • public void run() ..This provides the code
    for the run method.
  • public static void main(String args)
    ..This provides the code for the main
    method. This is the class method that the JVM
    executes to get the program started.

31
Conversion Explained (cont.)
  • public static void main(String args)
    Convert tpo new Convert()
    tpo.run()This causes a new Convert object to
    be created with name tpo (the program object).
    Then this object is told to execute its run
    method.

32
Dang! Seems as if there's a tax on everything
these days.
Write a Comment
User Comments (0)
About PowerShow.com