Title: CS 1371 Introduction to Computing for Engineers
1CS 1371Introduction to Computing for Engineers
2Java Data and Structures
Learning Objectives Transition from Matlab data
to Java data
- Primitive data vs Objects
- Primitive data
- Numbers
- Characters
- Boolean
- Structures
- Defining a structure class
- Making structure objects
- Using structure objects
3Data Types
- Interacting with the real world requires
representing a wide variety of different types of
data - Integer, real and imaginary numbers
- Differing precision and range depending on
application - Characters, strings and other textual data
- True, false values
- On/Off
- Stop/Go
4Computer Numbers
- Integers (byte, short, int, long)
- whole numbers
- exact
- Relatively limited in magnitude (1019)
- Floating Point (float, double)
- fractional
- often approximations (0.33333)
- larger magnitude (10308)
- Actually hold signed mantissa exponent
- 6.023 x 1023
5Type Checking
What happens when data is changed from one
representation to another? Some languages (such
as Java) enforce a strict set of rules to try and
catch problems early This may seem annoying at
first Later, it will really get under your skin!
6Problems?
Let's say that you have a floating point number
in a variable f and you decide to store it in an
integer variable i i
f Any problems possible?
7Built-in Data Types
Java primitives (text) char (character, use
single quotes b) String Java primitives
(True/False) boolean As with Matlab strings,
String is not really a primitive well treat it
that way for now
8Built-in Data Types
Java primitives (whole numbers) byte short int
long Java primitives (real numbers) float
double
9Variable Declarations
Simple form ltdatatypegt ltidentifiergt Example int
total Optional initialization at
declaration ltdata typegt ltidentifiergt ltinit
valuegt Example int total 0
int counter int numStudents 583 float
gpa double batAvg .406 char gender char
gender f boolean isSafe boolean isEmpty
true String personName String streetName
North Avenue
10Primitive Type Facts
11Casting
Consider float f int i 5 int j 2 f
i/j Result? f 2.0
Consider float f int i 5 int j 2 f
(float)i/(float)j Result? f 2.5
We say "cast i to a float" (float)i Are we
actually changing i??? No, we are telling Java to
convert the representation of i to a float. We
are not actually changing the contents of
variable i.
12Questions?
13Background
- We have already used structures in Matlab
- Structures in Java are implemented as classes
with a Constructor but no methods . - Accessing structure components
- is identical in Matlab and Java,
- evidence that Matlab internals are migrating
towards Java. - Primary distinctions are
- creating structure instances (objects),
- strong typing in structures.
14We Could Use Structures for
People First Name, Last Name, Age,
Weight Buildings Name, Address, Height,
Residential or Business, Number of
occupants CDs Artist, Genre, Record
Company, Number of Songs Or maybe even
15Embarrassing Photographs
Can you believe it? This is one of the CoC
instructors!
16Our Structure Should Store
The First Name of the Individual involved, The
Last Name of the Individual involved, The Date
the picture was taken, and The Embarrassment
Level of that particular photograph
17The Photo Class
public class Photo public String
first public String last public String
date public int level public Photo( String
f, String l, String d, int lv )
first f last l date d level
lv // toString() and main
- Thought for later
- Is this the best way to store a date?
- What about a Date structure?
18Does This Actually Store Anything?
We now have the ability to store information
about bad photos. NOTE We have NOT stored any
information about any particular photo. All that
weve done is create a kind of template for how
we WILL store information about bad photos.
19Using Photo in Matlab
- gtgt dan Photo('Dan','Lerner','about 1974', 10)
- dan
- Photo of Dan Lerner taken about 1974
embarrassment level 10 -
- gtgt prof Photo('David','Smith','2001', 1)
- prof
- Photo of David Smith taken 2001 embarrassment
level 1 -
- gtgt album1 Photo('George','Burdell','1998', 3)
- album
- 1x1 Photo
- gtgt album2 dan
- album
- 1x1 Photo 1x1 Photo
- gtgt album3 Photo('Prof','Craig','Oct 2000', 2)
- album
- 1x1 Photo 1x1 Photo 1x1 Photo
20Still Using Photo in Matlab
- gtgt album
- album
- 1x1 Photo 1x1 Photo 1x1 Photo
- gtgt album4 prof
- album
- 1x1 Photo 1x1 Photo 1x1 Photo
1x1 Photo - gtgt album3
- ans
-
- Photo of Prof Craig taken Oct 2000 embarrassment
level 2 - gtgt
21What About Writing Functions?
- function answer worst(alb)
-
- worst will take in a cell array of photos
- and return the worst picture
-
- worstLevel -1 initialize to invalid value
- answer 'none' initialize answer
- for i 1length(alb)
- if albi.level gt worstLevel
- worstLevel albi.level get field
- answer albi get entire entry
- end
- end
gtgt worst(album) ans Photo of Dan Lerner taken
about 1974 embarrassment level 10 gtgt
22Questions?
23(No Transcript)