ISE 582: Information Technology for Industrial Engineering

advertisement
ISE 582: Information Technology
for Industrial Engineering
Instructor: Elaine Chew
University of Southern California
Department of Industrial and Systems Engineering
Lecture 2-2
First cup of JAVA
Winston & Narasimhan: Chapt 1-5 (pp 1-33)
The Agenda for Today
•
•
•
•
HTML Forms
Introduction to Objects
Programming in General
Getting Started in JAVA
4 September 2003
ISE 582: Web Technology for IE
2
Introduction to Objects
• Object Instance
– Attributes (properties): associated data
– Operations: actions that change attributes
• Methods in JAVA
• Member functions in C++
• Object Class
– Category or set of objects
– Share same attributes, operations and
relationships to other objects
4 September 2003
ISE 582: Web Technology for IE
3
Example: Object Class
WIZARD
Class name
name
age
house
claimToFame
Attributes
changeName(…)
incrementAge()
calculateDOB()
Operations
4 September 2003
ISE 582: Web Technology for IE
4
Example: Object Instances
w1: Wizard
name = “Albus Dumbledore”
age = 161
house = “Gryffindor”
claimToFame = “Defeated
dark wizard Grindelwald”
4 September 2003
w2: Wizard
name = “Harry Potter”
age = 21
house = “Gryffindor”
claimToFame = “Survived
deadly curse by Voldemort”
ISE 582: Web Technology for IE
5
Objects and older constructs
• Operation/ Method = function/procedure
• Object Instance = FAT variable
containing multiple pieces of data and
its own functions
4 September 2003
ISE 582: Web Technology for IE
6
Naming Conventions
• Convention:
– Attributes/operation names begin with
lower-case characters
– Class and type names are capitalized
• Common Operations:
– get*, set*, is*
attributes:Type
Class name
4 September 2003
Class name
ISE 582: Web Technology for IE
operations
7
Programming in General
•
•
•
•
Algorithms
Pseudocode
Reusable Components
Testing and Debugging
4 September 2003
ISE 582: Web Technology for IE
8
Algorithms and Pseudocode
• Hardest part in designing methods: coming up
with a plan or strategy for carrying out the action
• Algorithm: set of instructions for solving a
problem. So precise that even a dumb
computer could follow the instructions and get
the same results
• Pseudocode: an algorithm written in a mixture of
English and some programming language
4 September 2003
ISE 582: Web Technology for IE
9
Reusable Components
• Most programs combine already
existing components
• Be sure to design classes so that they
are reusable
4 September 2003
ISE 582: Web Technology for IE
10
Testing and Debugging
• Syntax Error: a grammatical mistake
• Run-time Error:
detected when program is run
• Logic Error: incorrect but legal
4 September 2003
ISE 582: Web Technology for IE
11
Development Cycle
Write or edit source code
Compile source code
Fix bugs
that emerge
during
compilation
Fix bugs
that emerge
during
execution
Execute program
4 September 2003
ISE 582: Web Technology for IE
12
Getting Started in JAVA
•
•
•
•
•
A Little JAVA History
Compile and Execute a Simple Program
Declare Variables
Arithmetic Expressions, the Math Class
Define a Simple Method
4 September 2003
ISE 582: Web Technology for IE
13
JAVA: Definition
• 1.
An island of Indonesia separated
from Borneo by the Java Sea, an arm of
the western Pacific Ocean. Center of an
early Hindu Javanese civilization, Java
was converted to Islam before the arrival
of the Europeans (mainly the Dutch) in
the late 16th century.
4 September 2003
ISE 582: Web Technology for IE
14
JAVA: Definition
• 2.
A trademark used for a
programming language designed to
develop applications, especially ones
for the Internet, that can operate on
different platforms.
• 3.
n. Informal: Brewed coffee
4 September 2003
ISE 582: Web Technology for IE
15
A Little JAVA History
• 1991: Programming Language for
Home Appliances (Gosling)
• 1994: Coupled with HotJava webbrowser (Naughton and Payne)
• 1995: Netscape made browsers
capable of running Java programs
4 September 2003
ISE 582: Web Technology for IE
16
Source to Byte Code
• The Java compiler translates the class
definitions and programs into byte code
• Byte code is executed by an interpreter,
called a Java Virtual Machine
4 September 2003
ISE 582: Web Technology for IE
17
Demo Step 1
• Embed method in a class definition
accessibility
class
definition
public class Demonstrate {
...
}
• Explicit values are said to be literal
• Java is blank insensitive, but case sensitive
4 September 2003
ISE 582: Web Technology for IE
18
Demo Step 2
public class Demonstrate {
accessibility
class method,
not instance method
returns
no value
public static void main (String argv[]) {
System.out.print(“The movie rating is “);
System.out.println(6 + 9 + 8);
}
}
4 September 2003
ISE 582: Web Technology for IE
19
Demo Step 3: Compile & Execute
• At the command prompt:
– Type “javac Demonstrate.java”
– (this generates a file Demonstrate.class)
– Type “java Demonstrate”
• What will the output be?
• Data in this program is said to be
– Wired in or hard coded
4 September 2003
ISE 582: Web Technology for IE
20
The Quaffle
The Golden Snitch
Q ui ck Ti m e ™ an d a T I FF ( U nc om p r es se d) de co m pr e ss or ar e n ee de d t o se e t hi s p i ct ur e .
Qu i ck Ti m e ™ an d a T IF F ( Un co m p re ss ed ) d ec om pr es so r a r e ne ed ed to se e t hi s p ic tu r e.
Q ui ck Ti m e ™ an d a T I FF ( U nc om p r es se d) de co m pr e ss or ar e n ee de d t o se e t hi s p i ct ur e .
Quidditch
Quidditch is a game played in mid-air on broomsticks that is a cross between
rugby, basketball and the emergency room. Each team consists of seven
players; three Chasers, two Beaters, one Seeker and one Keeper. The Keeper
acts as a goalie against the other team making a score in one of their three
hoops. The Chasers work together to score, using a ball known as the Quaffle.
Scoring with the Quaffle is worth ten points. Two other balls known as Bludgers
are enchanted to try and knock the players from their brooms. It is up to the
Beaters to keep this from happening. Equipped with bats similar to those used
in Cricket, Beaters work furiously to keep their teammates from being injured by
Bludgers. Last and never least, the Seekers have the important task of catching
the Snitch. This ball is the size of a walnut with wings and flies around the Pitch
independantly. Capture of the Snitch is worth 150 points, and it automatically
ends the game. However, this doesn't mean the team that catches the Snitch
automatically wins. The game can also be ended with the mutual consent of
both team captains
From http://www.pensievemush.org/hogwarts.html
4 September 2003
ISE 582: Web Technology for IE
21
Declaring Variables in JAVA
• Identifiers are names of variables
– E.g. age, claimToFame, many$$$, no_way
• Variables are reserved chunks of
memory that contain values.
• Type of variable determines how much
memory is allocated to it.
4 September 2003
ISE 582: Web Technology for IE
22
Some Variable Types
•
•
•
•
char:2 bytes: characters
byte,short,int,long: increasing:integers
float,double: 4,8: floating point number
OTHERS: boolean, String, arrays etc
public class Demonstrate{
public static void main (String argv[]) {
int script, acting, direction
…
}
}
4 September 2003
ISE 582: Web Technology for IE
23
COMMENTING YOUR CODE
• // This is a one-line comment
• /* This comment
just goes on
and on and on…
*/
• NOTE: no nesting of comments.
4 September 2003
ISE 582: Web Technology for IE
24
ARITHMETIC EXPRESSIONS
•
•
•
•
•
The usual operators: +, -, *, /
Modulus: % (remainder in division)
The usual precedence rules apply
Mixing data types in expressions
Casting an expression
– Original data type remains unchanged
• Assignment operates from R to L
4 September 2003
ISE 582: Web Technology for IE
25
The Math Class
• public class Demonstrate {
•
public static void main (String argv[]) {
•
System.out.println(“natural log of 10:” +
Math.log(10));
•
System.out.println(“abs value of -10:” + Math.abs(10));
•
System.out.println(“max of 2 and 3:” + Math.max(2,3));
•
System.out.println(“5th power of 6:” + Math.pow(6,5));
•
System.out.println(“sqrt of 7:” + Math.sqrt(7));
•
System.out.println(“sine of 8 rad:” + Math.sin(8));
•
System.out.println(“Random no.(0,1):” +
Math.random());
•
}
• }
4 September 2003
ISE 582: Web Technology for IE
26
Defining a Simple Method
ARGUMENTS
public class Demonstrate {
public static void main (String argv[]) {
int script = 6, acting = 9, direction = 8;
System.out.print(“The rating of the movie is”);
System.out.println(movieRating(script,acting,direction);
}
public static int movieRating (int s, int a, int d) {
return s + a + d;
}
}
PARAMETERS
4 September 2003
ISE 582: Web Technology for IE
27
Defining Methods in Multiple Files
public class Movie {
public static int movieRating (int s, int a, int d) {
return s + a + d;
}
}
public class Demonstrate {
public static void main (String argv[]) {
int s = 6, a = 9, d = 8;
System.out.print(“The rating of the movie is”);
System.out.println(Movie.movieRating(s,a,d);
}
}
4 September 2003
ISE 582: Web Technology for IE
28
Other Things to Know
• You can define more than one class in the
same file
• To call a method in a different class,
preface the method name with the class
name and join with a dot.
• You can define multiple methods with the
same name in the same class as long as
they have different signatures (i.e.
arrangement of parameter data types)
4 September 2003
ISE 582: Web Technology for IE
29
Download