Warning This class is being recorded. At least I think it is.
Approximate overview
lab-exceptions-vscode
.src
folder.Academic
Cultural
Peer
Wellness
Misc
You can ask questions about anything related to the class! Self gov says that you should ask those questions.
Do we get to choose which way I meet each LA?
If I have a paper-based LA, then you should at least try the first time on paper. If that fails, you can provide me with evidence.
Those may start appearing this weekend.
What should I get if I center boxed text?
It depends on the particulars. Let’s say you build
new Centered(new BoxedBlock(new TextLine("Hello")), 21)
and put it abovenew TextLine("012345678901234567890123456789")
.`
+-----+
|Hello|
+-----+
012345678901234567890123456789
On the other hand, if you built a block with an expression like
new BoxedBlock(new Centered(new TextLine("Hello"), 21))
, that should look more like
+---------------------+
| Hello |
+---------------------+
012345678901234567890123456789
Order of operations matters!
What should Centered
and RightJustified
do if the block is too
big (or the size is too small)?
This will also truncate.
What should new HorizontallyFlipped(new TextLine("Hello"))
look like?
olleH
What should new HorizontallyFlipped(new BoxedBlock(new TextLine("Hello")))
look like?
+-----+
|olleH|
+-----+
What should new VerticallyFlipped(new VerticallyCompose(new TextLine("Hello"), new TextLine("Goodbye")))
look like?
Goodbye
Hello
When centering with an odd number of surrounding spaces, do you care if we’re slightly left or slightly right?
No. But that may make my testing harder.
Can you give us some hints as to how we might write Truncate
?
Truncate will have two fields, *
TextBlock block
*int w
Focus on how you write
row
,height
,width
in terms of therow
,height
, andwidth
of your one field.
If I truncate a block, what’s the height of the new block? It’s the same.
If I truncate a block to width
w
, what’s the width of the truncated block? It’sw
.
What is row i of the truncated block? The first
w
characters or row i of the underlying block. (Usually)
How would you phrase “The first
w
characters or rowi
of the underlying block” in Java?
Write a for loop to grab the characters from row i of the
this.block.row(i)
.
return this.block.row(i).substring(0, w);
Special case: The width of the ith row may be less than w. So we need to pad it with spaces (at the right) in that case.
Putting it all together
/**
* Blocks of text truncated to a specified width.
*/
public class Truncated implements TextBlock {
// +--------+------------------------------------------------------
// | Fields |
// +--------+
/**
* The block we are truncating.
*/
TextBlock block;
/**
* The width of the truncated block.
*/
int width;
// +--------------+------------------------------------------------
// | Constructors |
// +--------------+
// +---------+-----------------------------------------------------
// | Methods |
// +---------+
public int width() {
return this.width;
} // width()
public int height() {
return this.block.height;
} // height()
public String row(int i) {
return ""; // STUB
} // row(int)
} // class Truncated
When testing, do I write one test method per method I’m testing or should I write multiple methods?
It depends on how much you like green checkmarks.
It’s harder to track down bugs if you put everything in one test method, so I generally recommend separate test methods, although I don’t always follow my own recommendation.
When testing, why would you have multiple test classes?
Most developers prefer one test class per normal class.
In today’s lab, we’re working with quadratic polynomials (of the form \(ax^2 + bx + c\)).
TPS
On paper, by yourself: What does class Quadratic
, which we use
to represent quadratic polynomials look like?
What I saw
a
, b
, and c
, or coeff1
, coeff2
, and coeff3
.int[] coeffs = new int[3];
int
, double
, BigInteger
,
BigDouble
, …
double
seems okay.evaluate(double x)
- find the value of the polynomialPolynomial derivative()
- find the derivativedouble smallerRoot()
- these are sometimes complex; we’ll
need to think of how to deal with that solution.double biggerRoot()
double[] realRoots()
- could return the empty array if there
are no real roots, a size-one array if there’s one root, and
a size-two array if there are two roots. Might also work with
a list, which could be easier to handle.void setA()
- should we be able to mutate our structures?void setB()
- perhaps we should just create a new onevoid setC()
double getA()
- Useful. May not be strictly necessary.double getB()
double getC()
String toString()
String toString(double x)
Quadratic add(Quadtratic other)
https://rebelsky.cs.grinnell.edu/Courses/CSC207/2023Fa/labs/exceptions.html
Compute square roots with Math.sqrt
.
Surprise! There’s nothing to turn in today.