Computer Science 101 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Computer Science 101

Description:

Use algorithms to process the data. Output the results to the user ... Example: Circle Area. Input gets data from a field. Computation calculates the area ... – PowerPoint PPT presentation

Number of Views:474
Avg rating:3.0/5.0
Slides: 19
Provided by: KenLa2
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 101


1
Computer Science 101
  • More Java Programming

2
All About Java
  • Was developed at Sun Microsystems in the early
    1990s
  • A general-purpose language but
  • Programs could run in small devices (embedded
    systems)
  • Internet-ready and Web-ready
  • Standard support for multimedia applications

3
Compiling and Running a Java Program
  • A Java compiler translates a Java program to an
    equivalent program in byte code
  • A Java virtual machine (JVM) is a program that
    interprets a Java byte code program to execute it

User inputs
Java code
Program outputs
byte code
Compiler
JVM
4
Portability
  • A Java program translates to a standard byte code
  • A Java byte code can run on any JVM
  • A JVM, and thus a Java program, can run
  • In a PDA
  • In a desktop computer
  • In a Web browser

5
Resources
  • Suns Java Web site
  • http//java.sun.com/j2se/
  • Documentation for Java
  • http//java.sun.com/j2se/1.4.2/docs/api/index.html
  • Java programming environment
  • http//www.bluej.org/

6
A Design Template
  • Most simple problems have the form
  • Get input data from the user
  • Use algorithms to process the data
  • Output the results to the user

7
Example Circle Area
public void buttonClicked (JButton buttonObj)
double radius radiusField.getNumber() //
Input radius double area 3.14 radius
radius // Compute area
areaField.setNumber (area) //
Output area areaField.setPrecision (2)
// Format output
Input gets data from a field Computation
calculates the area Output sends data to a field
8
Input
double radius radiusField.getNumber() //
Input radius
  • There are three types of fields
  • IntegerField for ints (whole numbers)
  • DoubleField for doubles (decimal point numbers)
  • TextField for Strings (names, etc.)
  • Use the method getNumber() to obtain the number
    stored in a numeric field.
  • Use the method getText() to obtain the string
    stored in a text field.

9
Output
areaField.setNumber (area) //
Output area areaField.setPrecision (2)
// Format output
Use the method setNumber(lta numbergt) to display a
number in a numeric field. Use the method
setText(lta stringgt) to display a string in a text
field. Use the method setPrecision(ltan integergt)
to set a DoubleFields precision limit.
10
Customer Request
Modify the circle area program so that it can
also compute the radius from the area.
11
Analysis
  • The program uses
  • to compute the radius from the area
  • The program displays results with the maximum
    allowable precision

12
Proposed Interface
13
Design
  • Because we display the results with the maximum
    allowable precision, we use the most precise
    value of ? supported by the programming language

The constant Math.PI represents ? to 15 places of
precision. The method Math.sqrt(lta numbergt)
returns the square root of a number.
14
Pseudocode for buttonClicked
if the user selects compute area button get
the radius area Math.PI radius radius
display the area else get the area radius
Math.sqrt(area / Math.PI) display the radius
15
Implementation
public void buttonClicked(Button buttonObj)
double radius, area if (buttonObj
areaButton) radius radiusField.getNumber(
) area Math.PI radius radius
areaField.setNumber(area) else
area areaField.getNumber() radius
Math.sqrt(area / Math.PI)
radiusField.setNumber(radius)
16
Syntax of Compound Statements
ltstatement-1gt ltstatement-ngt
A compound statement is a sequence of zero or
more statements.
17
Syntax of Selection Statements
if (ltBoolean expressiongt) // One-way
decision ltstatementgt if (ltBoolean
expressiongt) // Two-way decision
ltstatementgt else ltstatementgt
18
Semantics of Selection Statements
false
if (ltBoolean expressiongt) ltstatementgt if
(ltBoolean expressiongt) ltstatementgt else
ltstatementgt
?
true
statement
false
?
true
statement
statement
Write a Comment
User Comments (0)
About PowerShow.com