1/15
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Provide the generic syntax for Interfaces
interface MyInterface {
int VALUE = 10; // same as public static final int VALUE = 10;
public static final int X = 20; // explicit form
void methodA(); // public abstract
public abstract void methodB(); // explicit form
default void methodC() { }
// private
// static
}Provide the generic syntax for Abstract Classes
abstract class MyClass {
// Fields
public int fieldA;
// public, private
// static, final
//Constructor
MyClass(){}
// public, protected, private (only can be called in this class)
// Methods Abstract Concrete
public void methodA() { }
// public, private
// abstract, static, final
}
Provide the generic syntax for Records
record MyRecord(int fieldA, String fieldB) {
// Fields - Implicitly private final
private final int x;
private final String name;
// Additional Fields - Must be static
static int count = 0;
// public, private
// static, final
// Canonical Constructor - Auto-generated, must be same visibility as record
public MyRecord(int fieldA, String fieldB) {
this.fieldA = fieldA;
this.fieldB = fieldB;
}
// Compact
MyRecord {
if (fieldB == null) {
throw new NullPointerException();
}
}
// Additional
public MyRecord(int fieldA) {
this(fieldA, "default");
}
// Methods
public void methodA() { }
// public, private
// static, final
// Setter Method - returns new record
public MyRecord setX(int newX) { return new MyRecord(newX, name); }
//Getter Methods - auto-generated
public int x() { return x; }
public String name() { return name; }Provide the generic main method syntax
public class Test {
public static void main(String[] args) {
}
}Provide syntax for iterating through the values of a map
Map<String, Integer> mapName = Map.of(
"A", 1,
"B", 2,
"C", 3
);
for (Integer value : mapName.values()) {
System.out.println(value);
}Provide syntax for iterating through the keys of a map
Map<String, Integer> mapName = Map.of(
"A", 1,
"B", 2,
"C", 3
);
for (String key : mapName.keySet()) {
System.out.println(key);
}Provide syntax for iterating through the key-value pairs of a map
Map<String, Integer> mapName = Map.of(
"A", 1,
"B", 2,
"C", 3
);
for (Map.Entry<String, Integer> entry : mapName.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}Provide syntax for the two basic methods of iterating through a list
List<String> listName = List.of("A", "B", "C");
for (int i = 0; i < listName.size(); i++) {
System.out.println(listName.get(i));
}
for (String element : listName) {
System.out.println(element);
}Provide syntax for an unmodifiable list
var ns= List.of(1,2,3,4);
System.out.println(ns); //[1, 2, 3, 4]
ns.add(10);//fails -- unmodifiable collection! A useful kind of failure.What is the toString() output for a record called Person
Person[name=Bob, friends=[Alice, Cara]]What is the printed structure of a Map? If it includes objects?
{key1=value1, key2=value2, key3=value3}
{Point[x=0, y=0]=base, Point[x=0, y=1]=down, Point[x=1, y=0]=left}Overriding + Recursion - Method Call Output
class Pow {
public int pow(int base, int exp) {
if (exp == 0) { return 1; }
return base * this.pow(base, exp - 1);
}
}
class PowLog extends Pow {
public int pow(int base, int exp) {
System.out.println("LogMessage");
return super.pow(base, exp);
}
}
System.out.println(new PowLog().pow(4, 3));super.pow(...) starts in the superclass method
but inside that method, this.pow(...) still uses dynamic dispatch
so if the actual object is a PowLog, the recursive call goes back to PowLog.pow(...)
Give the syntax for forwarding a super
interface A{
default String mA(){ return "A"; }
}
interface B extends A{
default String mA(){ return "NotA"; }
default String mB(){ return "B" + A.super.mA(); }
}
record C() implements B{ //'implements A, B' would behave identically
public String mA(){ return "C" + B.super.mA(); }
}
System.out.println( new C().mA() ); //CNotA
System.out.println( new C().mB() ); //BAThe perfect case to use an interface
interface Length{
default double meters(){ return yards() * 0.91d; }
default double yards(){return meters() * 1.09d; }
default Length add(Length l){
return new Meters(l.meters() + this.meters());
}
}
record Yards(double yards) implements Length{}
record Meters(double meters) implements Length{}
// Since records auto generate getters for the parameters, with the same name as the parameter, this new method overrides the method in the Length interface enabling less code.The perfect case to use an abstract class
abstract class LibraryProperty{
private final static List<LibraryProperty> all= new ArrayList<>();
public LibraryProperty(){
all.add(this);
}
public static List<LibraryProperty> inventory(){
return Collections.unmodifiableList(all);
}
}
class Book extends LibraryProperty{}
//new Book() will automatically add it to the list!
class Magazine extends LibraryProperty{}
//new Magazine() will automatically add it to the list!
class Newspaper extends LibraryProperty{}
//new Newspaper() will automatically add it to the list!
//Java effectively does:
// - create a new Book object
// - call the LibraryProperty constructor
// - inside that constructor, run all.add(this)
// - At that moment, this refers to the actual object being created, which is a Book.Test Syntax
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class MyTestSuit{
@Test public void testDogBarks(){
Dog dog = new Dog(...);
String sound = dog.bark();
assertEquals("Woof", sound);
}
}
var re= RuntimeException.class;
assertThrows(re, ()->new MyDate(0,0,0) );