You are probably being recorded (and transcribed) (if the technology is working correctly). * Otter.ai now times out after ninety minutes, so I may pause after an hour to start a second recording. * My otter.ai monthly limit has been cut, so I’ll do my best to stop recording during lab. Hopefully, I’ll remember to turn it on for announcements. (That probably makes it more convenient, too.) * If you talk to me after class, please ensure that I’ve stopped recording.
Start of class instructions
Approximate overview
If you’d like to suggest token events, please let me know in advance of class.
Today is an exception.
Academic/Scholarly
Cultural
Multicultural
Peer
Wellness
Misc
Why are you making us create PrintWriter
objects rather than using
System.out
?
Because it makes your code more general (at least once things get more complex). It takes a moment to switch your
PrintWriter
from stdout to a file, while you’d have to change a lot of lines if you were usingSystem.out
.
Start with good habits, rather than bad.
It feels a bit more object-oriented, since you are explicitly creating an output object.
What if I want to print debugging output?
You are generally better off using the debugging tool.
However, I accept that some debugging output is useful. Please use
System.err.println
in such cases.
Could you explain more about static
keyword in java? In the reading, it says (static
indicates that the method can run without having an associated object created),, but I don’t quite understand.
“We’ll cover it in two weeks.”
The standard model in object-oriented languages is that you (a) create an object and then (b) ask the object to do things (by calling methods) (also “sending messages”).
Sometimes, there’s no natural object.
We use
static
for the “free-floating” procedures (and variables), even if they aren’t quite free floating. (Almost) everything must be in a class.
Static methods you can call (somewhat) like you call C functions; non-static method require a bit more work.
Can we call a static method on an object?
Yes, but it’s discouraged.
Why is it discouraged?
Because it can be confusing to the reader. We like to see static methods associated with classes and non-static methods associated with objects.
Could you make the for(String name: names)
thing more clearer because I don’t know exactly what name or names represent or String. For example, if String is just where the type goes then just clarify that it is the where the type of the array goes and the same for the rest.
Sure.
We often need to do something for each element in an array.
int[] vals = ...;
int sum = 0;
for (int i = 0; i < vals.length; i++) {
sum += vals[i];
} // for
We aren’t using
i
except to keep track of where we are in the array. Java will do that for us in the for-each form (“syntactic sugar”).
int[] vals = ...;
int sum = 0;
for (int val : vals) {
sum += val;
} // for
This does the same thing as the code up above.
It’s nicer because (a) we don’t have to worry about about accidentally changing the iteration variable; (b) we don’t have to come up with different iteration variables for nested loops; (c) we can actually use for-each loops for many kinds of collections, not just arrays, so it’s easy to switch the collection and not change the rest of our code.
How do Maven and other build tools (like Gradle or Ant) compare in terms of features and ease of use?
I have no idea.
Is a class interchangeable with a file? In other words, can one file only contain one class?
For the purposes of this course, we’ll (generally) expect a one-to-one correspondence. FILE.java will contain the class FILE. (Alternately, CLASS will be in the file CLASS.java)
In the input and output section where you were making an object of class PrintWriter, how did you know the parameters of that class?
Java reference pages are your friend.
Reference code is your friend.
Random internet pages may or may not be your friend.
Is there a difference between import in Java and #include in C?
Yes!
#include
explicitly puts the text of another file into the including file.
import
basically says “Here’s a shorthand.”
import java.io.PrintWriter
just says “When I writePrintWriter
, I meanjava.io.PrintWriter
.
Could you explain the difference between ADTs and Data Structures in a less abstract way? I’m still a bit confused about that.
Why we put static
on the main class?
So that we don’t need an object to start our program.
Please explain why we need to use ‘new’ in Java. What’s the functionality?
Wait until next Tuesday.
The person closer to the screen is partner A. The person further from the screen is partner B.
I used VSCode in CSC-161. Can I use VSCode for this lab?
Certainly.
What folder should I use?
I’d suggest something like
/home/USERNAME/CSC207/Labs/getting-started
.
Wrapping up …
Please finish the prior lab up to the min(int[] arr)
problem (on your own
or with your partner or with anyone).
Please do this one on your own (or with your partner or with anyone). There is nothing to turn in; this is a “practice to understand” lab.
Side note: Tab is your friend.