.jar
files contain archives of directory trees that contain .class
files and other files..class
files on the filesystem.jar
files that contain archives of directory trees that contain .class
files.foo.bar.Baz
?
src/foo
src/bar/foo
src/main/java/foo/bar
switch
statements?
if-else
statements.String
s for the switch variable.default
clauses.Give a case where a comment is helpful.
What is the most important consideration in writing a function?
Tests should be F.I.R.S.T. Explain each F.I.R.S.T. rule with one sentence or phrase.
Write a unit test method for the static method public static int abs(int n)
in java.lang.Math
, which returns the absolute value of an integer n
. Assume java.lang.Math
has been imported. Include an assert for each equivalence partition.
Apply the principles of clean code to rewrite these code snippets the spaces below them. If the code is in Java, write your solution in Java. If the code is in Scala, write your solution in Scala. If the code is valid in both Java and Scala, write your solution in Scala.
1.
public class Part {
private String m_dsc; // The textual description
public void set(String s) {
m_dsc = s;
}
}
2.
// returns 1 if the file named by s exists, 0 otherwise.
public int file(String s)
3.
// Check to see if the employee is eligible for full benefits
if ((employee.flags & HOURLY_FLAG) && (employee.age > 65))
4.
def mean(xs: Seq[Double]): Double =
if (xs.isEmpty)
throw new ArithmeticException("mean of empty list undefined")
else
xs.sum / xs.length
Given the following two designs for representing geometric shapes:
Design A:
public class Rectangle {
public Point topLeft;
public double height;
public double width;
}
public class Circle {
public Point center;
public double radius;
}
public class Geometry {
public double area(Object shape) throws NoSuchShapeException {
if (shape instanceof Rectangle) {
Rectangle r = (Rectangle)shape;
return r.height * r.width;
}
else if (shape instanceof Circle) {
Circle c = (Circle)shape;
return Math.PI * c.radius * c.radius;
}
throw new NoSuchShapeException();
}
}
Design B:
public interface Shape {
public double area();
}
public class Rectangle implements Shape {
private Point topLeft;
private double height;
private double width;
public double area() { return height * width; }
}
public class Circle implements Shape {
private Point center;
private double radius;
public double area() { return Math.PI * radius * radius; }
}
Give an advantage of Design A.
Give an advantage of Design B.
Given:
public class RealBillingService {
public Receipt chargeOrder(PizzaOrder order, CreditCard creditCard) {
PaypalCreditCardProcessor processor = new PaypalCreditCardProcessor();
// Card charging code ...
}
}
List any “smells” in the design or the code.
State any principles or design patterns you would use to refactor the code.
Write an updated version of the code which applies the principles or design patterns in the previous question. You may need to write additional classes or interfaces. Only write code that is necessary to show the design, that is, dont’ write detailed implementations for methods.
Given the following UML class diagram, write a stubbed out SalariedEmployee
class in Scala using Scala idioms. Complete the constructor (if you need one) but don’t give the code inside the curly braces of other methods. Do not include anything you don’t need in this class.
Given the following polymorphic class hierarchy:
trait Feline {
def dinner: Food
}
final class Lion() extends Feline {
def dinner: Food =
Antelope
}
final class Tiger() extends Feline {
def dinner: Food =
TigerFood
}
final class Panther() extends Feline {
def dinner: Food =
Licorice
}
final class Cat(val favouriteFood: String) extends Feline {
def dinner: Food =
CatFood(favouriteFood)
}
Rewrite these classes as an algebraic data type such that you don’t intend ever to add another subclass of Feline
. Also write the Food
classes as an algebraic data type.