Warning This class is being recorded.
Getting started (this will be our normal start-of-class sequence)
Updated for Covid
Approximate overview
Academic
Cultural
Peer
Wellness
Given the illnesses going around campus, some of you may need to attend class remotely. That’s fine, but please try not to make it your norm.
a. Let me know via email that you will not be in class.
b. You are still responsible for the work. Plan to do the lab on your own.
c. Synchronous: If you wish, you can join the Teams Meeting during class. That gives you an opportunity to hear what I say and ask me questions.
d. Asynchronous: If not, I recommend that you check the recording and the eboard. (You may want to check those anyway.)
e. Let me know if you need extra time on any assignments.
BigInteger
objects.Fraction
objects (that we probably
should have called BigFraction
objects.Fraction
class in interesting ways which helped us
get a bit more accustomed to programming with objects and classes in Java.Counter
objects.What important lessons did we learn?
BigFraction
to simplify.BigFraction
objects from two ints, two BigIntegers,
a string, a floating point number (e.g, 2.5 -> 5/2), ….
a single integer (becomes n/1).toString
.What does it mean that “ All operations behave as if BigIntegers were represented in two’s-complement notation (like Java’s primitive integer types)”
Two’s complement is one of four or so ways to represent integers in binary.
These mechanisms differ in how they represent negative integers.
In two’s complement, … we flip all the bits and add 1.
A leading 0 represents positive, a leading 1 represents negative.
It’s important to know that to understand some of the constructors.
And, contrary to Sam’s minimalistic perspective, they provide a lot of bitwise operations.
Why do we have packages?
Reason one: It’s lets us put things that belong together together.
For example, the
BigFraction
and theBFCalculator
classes that you are writing will be working together, putting them in a package reinforces that.
On the other hand, the
Counter
that we built on Wednesday is unrelated, and should probably be in a different class.
Reason two: Helps us link things together. Things in the same package can refer to each other directly without giving the full package/path specification.
Reason three: There are only so many natural class names. Packages let us distinguish them.
For example, we’re building a
BigFraction
. But perhaps some data science library you’re using also has aBigFraction
. THe package lets us distinguish them. Ours will beedu.grinnell.csc207.fa2023.rebelsky.BigFraction
; theirs will beorg.cool.datasci.analyzer.BigFraction
.
Reason four: Information hiding. Things in the same package can access fields/methods that have the default protection level; things in other packages cannot.
We created a package,
foo
, with two classes,A
(fieldeh
) andMain
, with amain
method that accesses that field. It worked. Whoo!
We created another package,
bar
, which tries to reference theeh
field offoo.A
. It fails with a protection error. Yay! We have limited the availability of fields.
Our protection choices are
public
,protected
, nothing (package),private
.
In this class, we will generally restrict ourselves to public and package protection.
What is the difference between protected
and “package”?
protected
means “available to the package and to subclasses, even if they are in other packages”`.
Do we ever need to throw exceptions? If so, when?
Yes. Starting September 18.
Given that class A was public in the example above, why couldn’t we access the field?
We get to set different protections for classes and the things they contain. We often want other programs to be able to create new objects in the classes we design, but not to see/modify the internals.
We treat memory as one big address space (effectively, an array of bytes or words).
+--------------+
0 | |
+--------------+
1 | |
+--------------+
2 | |
+--------------+
3 | |
+--------------+
| |
.
.
.
| |
+--------------+
0xffff | | That's a made up address.
+--------------+
There are three kinds of things we need to store in memory (or reference from memory).
malloc
; in Java, with
new
).We generally arrange memory something like this.
+--------------+
| |
| |
| Static |
| |
+--------------+
| |
| |
| Heap |
| |
| |
+--------------+
| |
...
| |
+--------------+
| |
| |
| Stack |
| |
+--------------+
When we allocate something, it goes in the heap. The heap expands as necessary (fingers crossed).
When we call a procedure, we add a “stack frame” to the stack. It contains the local variables and parameters to the procedure.
If I have a procedure like the following
int foo(int x) {
int y;
y = x*x;
return y;
}
Our stack frame needs room for x
, y
, and some other housekeeping info.
As programmers, we need to remember that this is happening; it informs some of what we do, and it helps us understand why we get some errors.
We never explicitly free space in Java. What’s up with that?
Conceptually, the Java VM “cleans up” when it finds that there’s no/little space left. This is called Garbage Collection.
The things that can be removed are those that will no longer be used in the program.
We usually make the more conservative assessment of “this is not referenced by anything currently used in the program”
These days, garbage collection is done more incrementally (while your program is running), rather than all at once.
Your mentor says that we can configure all of this.
We’ll step back a bit from our pure representation of memory …
An expandable array provides three primary methods
String ExpandableArray(String default)
- Create a new expandable array
of the specified size.void set(int index, String val)
- Set the value at the given index to
val
.String get(int index)
- If we’ve set something at the given index using
set
, return that value. Otherwise, return default
.We want to implement these with chunks of memory (the underlying, fixed-size Java arrays).
Person closest to the board is the interviewer; person furthest from the board is the interviewee.
How do you implement these things? (in Java, conceptually, whatever)
Layout, Implementation, Assessment
get
?set
?