Title: Computer Science 101
1Computer Science 101
2All 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
3Compiling 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
4Portability
- 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
5Resources
- 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/
6A 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
7Example 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
8Input
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.
9Output
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.
10Customer Request
Modify the circle area program so that it can
also compute the radius from the area.
11Analysis
- The program uses
- to compute the radius from the area
- The program displays results with the maximum
allowable precision
12Proposed Interface
13Design
- 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.
14Pseudocode 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
15Implementation
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)
16Syntax of Compound Statements
ltstatement-1gt ltstatement-ngt
A compound statement is a sequence of zero or
more statements.
17Syntax of Selection Statements
if (ltBoolean expressiongt) // One-way
decision ltstatementgt if (ltBoolean
expressiongt) // Two-way decision
ltstatementgt else ltstatementgt
18Semantics of Selection Statements
false
if (ltBoolean expressiongt) ltstatementgt if
(ltBoolean expressiongt) ltstatementgt else
ltstatementgt
?
true
statement
false
?
true
statement
statement