home—info—lects—labs—exams—hws
D2L—tutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
primitive values and types
Today we will learn some basic types of values the computer can use,
and evaluate some simple expressions in BlueJ.
Your H: drive and you
On your H: drive, make a folder to keep all your ITEC120 work in;
It is strongly recommended that you do all your ITEC120
work on your H: drive.
Even if you live off-campus, you can access your H: drive
(by first running VPN on your computer; see a PI for advice).
Not only does this keep you from accumulating multiple versions
of your homework or lab work on different computers,
but you can pull up your homework in a professor's office hours,
no problem (and they can verify the date your file was last modified,
should that ever be an issue).
To do:
create a folder ITEC120 on your H: drive,
where you'll store future coursework.
BlueJ, and its Code Pad
BlueJ is an Integrated Development Environment
for Java --
a fancy way of saying, a program which lets you write your own Java
programs.
BlueJ is installed in
the Davis Hall labs (1st and 2nd floors),
although not other labs.
If you want to download and run BlueJ on your own computer (it's free!),
it is an easy install from BlueJ.org.
To evaluate expressions written in Java:
-
Start BlueJ.
-
On windows: do this by selecting Start,
and typing
BlueJ
into the entry-blank,
then clicking on the BlueJ icon.
- On Mac: BlueJ can be found inside the Applications folder
on the hard drive
(which is shown on the desktop, alongside your H: drive).
If, as BlueJ tries to start,
it asks for which version of Java to use, select the highest one
(e.g. 1.8.0).
This number might be rather embedded in the name.
-
Choose Project > New Project...,
navigate to My Computer > H:
and enter a name (perhaps
lab01a
).
BlueJ will ask you where to save it; put it on your H: drive,
inside your itec120 folder.
-
Select the menu item View… » Show Code Pad
-
To make sure everything is running as expected,
in the lower-right pane (the code pad), type
2+3
and hit return,
and confirm that it responds with the answer 5 (int)
.
Types
We will discuss, as a group, types of data, and some simple expressions/arithmetic.
Type these values into BlueJ’s code pad.
There are a few more basic types (notably booleans for true/false),
and also ways to add new types to the language
— there are thousands of those already, and we'll make more this semester!
Don't worry, we'll get to those, but we're already covering an awful lot for today!
Exercise:
What type is…
-
4.0
- "xvii"
-
17
-
"17"
-
The result of 1.5 + 2.5?
Exercise:
What type is appropriate for representing…
-
The number of students in this class?
-
The price of a textbook, in dollars?
-
The price of a textbook, in cents?
-
How far a car has traveled, in miles?
-
A student's name?
-
A zip code?
converting between tyhpes
Yes it is possible (and necessary) to convert values from one type to another —
convert a int into a STring so that you can print it,
or even convert an int into a double.
We can explicitly tell Java to do such conversions,
and sometimes Java will do them for us (implicitly — w/o us having to say anything explicitly).
Even if the conversions are done for us (implicitly, by the language),
as programmers we need to be aware that they are happening.
That is all we’ll say for now, but we'll definitely revisit this!
Expressions (simple)
You can use
arithmetic operators like + and * on numbers:
2 + 3, and 3.14 * 2.0.
In addition, the operator + can also mean "string concatenation":
"hi t" + "here"
evaluates to "hi there"
terminology:
We say that an "expression" evaluates to a "value".
You don't need to fully-understand or know these terms,
but I'll use them and in a few weeks you should be using them too.
Variables
You can also use a variable to hold a value — for example
we can have pi to hold 3.14159265,
or numStudents to hold 17,
or profName to hold "Doc B.".
We think of variables as a “bucket with a name written on it”.
When making variables, the name should describe that the value represents:
so numStudents is a good name, but num not so much (yeah it holds a number,
but what does that number represent? The number of students in the class, or the number of hairs on my head?).
Use good variable-names which are “self-documenting”.
Declaring a variable
You can make your own variables.
That is, you can tell Java
“Hey, I'm going to use the word ‘numStudents’ in my code, and that's not a typo;
that's a variable.”
When you declare a variable, you must give Java exactly two pieces of information:
the variable's name,
and what type of value it will hold.
Examples:
int numStudents;
String myFaveBand;
double MILES_PER_KM;
double earthCircumference; // in miles
|
The general syntax is:
type identifier;.
Hint: I will ask on quizzes,
not only for examples of declaring variables,
but for the syntax of declaring them!
The only time you mention a type in Java, is when you are declaring a new variable.
Assigning (initializing) a variable
We can use “=” to assign an initial value to a variable:
numStudents = 14;
myFaveBand = "Modest Mouse";
MILES_PER_KM = 1.609;
earthCircumference = 10000000 * 4 * MILES_PER_KM; // N.B. 10million km from equator to pole
|
The last example shows that the right-hand-side can be any expression.
You do not repeat the variable's type, when you're assigning to it!
You already told Java that MILES_PER_KM will hold a double, so don't tell it again.
(In fact, it is an error to do so.)
The only time you mention a type in Java, is when you are declaring a new variable.
Using a variable
This part is easy: to use a variable, just write its name.
For example, you can type (in the code pad) 5.75 * (numStudents - 2),
or "I " + myFaveGand.
(We (still) don't repeat the variable's type, of course.)
Task
Suppose you are opening a small business.
Type the following into the code pad:
-
- Declare a variable to hold the number of people you need, to work a shift.
- Declare a variable to hold the hourly wage you'll pay. [All employees will be paid the same.]
- Declare a variable to hold the name of your business.
- Now initialize each of the above (that is, assign an initial value to them — put a value into that labeled bucket!)
- Write an expression which represents your labor cost for a 40-hour weekly shift.
(When you type this in the code pad, you'll get a numeric answer. What type is the answer?)
-
Evaluate each of 13.0 / 5.0 and 13 / 5.
They give different answers because / is actually the name for two different
divisionlike-functions!
The first, which divides doubles, might be called “ ”
and the second, which divides ints, is commonly called “ ”.
- Optional/Challenge:
Write an expression as above, except it assumes that one of the shift-members is the manager,
who gets paid
$3/hr more
than all the other shift-members.
- Optional/Challenge: Write an expression the generates text along the lines of:
"Barland's Broom Emporium has 3 people in a shift, for weekly cost of $1200.0".
Hints:
- The result of this expression isn't just a number, but an entire string!
- Remember to use + to append (“concatenate”) two strings.
-
Your expression should include your variables, but no raw numbers!
-
Mine started out: myBizName + " has " + shiftSize + " people in a shift….
Review
We have covered a lot of things already!
Each one is simple, but that doesn't mean it's easy to keep them all straight,
and right now some of them are still a bit fuzzy.
You should review these terms to yourself (perhaps explain them to a roommate) between now and the next lecture.
We'll be using these terms every single day for the rest of the semester!
- values
- types
- expressions
- variable
- declaring a variable
- assigning to a variable
home—info—lects—labs—exams—hws
D2L—tutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
 This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland radford.edu |
 |