Overview
Can I switch sections?
Sure. Bring me an add-drop form.
What are the group work policies for this course?
Labs: You can choose whether to work alone or have a randomly assigned partner. (I would recommend the latter.)
Homework: You get a randomly assigned partner.
Exams and quizzes: On your own.
Tell me more about for (type x: thing)
.
Here’s an example.
import java.io.PrintWriter;
public class Example {
public static void main(String[] args) {
int[] stuff = { 5, 1, 2, 4, 8 };
PrintWriter pen = new PrintWriter(System.out, true);
for (int x: stuff) {
pen.println(x + " squared is " + x*x);
} // for
} // main
} // class Example
Output
5 squared is 25
1 squared is 1
2 squared is 4
4 squared is 16
8 squared is 64
Why do you write pen.flush()
?
Output in Java is “buffered” for efficiency. The computer writes to memory (a buffer) until a lot is written, and then dumps everything from memory to output. What’s in the buffer generally doesn’t appear until (a) buffer is full or (b) you flush the buffer. When you use
println
, it does not flush.
How do newlines work in Java?
There are multiple options. You can use
println
, which adds the newline and (in many cases) flushes the buffer. You can use\n
, like in C. I don’t know whether or not that flushes.
Why are you making us use PrintWriter
objects rather than System.out
?
I find that
PrintWriter
objects lead to more object-like code. In particular, it makes it much easier to change your mind about where output goes.
PrintWriters are more general, you can make them write to stdout or to a file or to an Internet connection or …. System.out only goes to stdout.
What is System.out
?
A name for an object that is a lot like
stdout
in C.
Are there standard packages or classes that we use in Java, like we
used <stdlib.h>
and <stdio.h>
in C?
Nope. It depends on the program.
How do I edit my .bashrc
?
emacs ~/.bashrc
What did you update since this morning?
I updated the fizzbuzz problem. Please reload the lab when you get to that point.
What’s the writeup?
Send the code for your
rev
method tocsc207-01-grader
.
(I don’t need any of the other parts of that class.)
When’s it due?
Preferably before class on Monday.
public class ArrayProblems {
public static void main(String[] args) {
PrintWriter pen = new PrintWriter(System.out, true);
int[] vals = { 2, 3, 4, 5, 10, 11, 12 };
rev(vals);
...
} // main
public static void rev(int[] vals) {
...
} // rev
} // class ArrayProblems