Skip to content

homework

Miguel Gamboa edited this page May 7, 2024 · 7 revisions

Share your code with user fmcarvalho in a private Github repository named isel-lae-2024-your_number. Create a new folder for each homework.

Homework 01 - Metadata and Objects Layout (28-2-2024)

On folder lesson03-TPC run javap -p apps/Amber.class to analyze the metadata of Amber.class.

  1. Write in Java an equivalent class to the output produced by javap

  2. Make a prediction about the expected layout and size of an instance of the Amber class in the JVM 64-bit model, 8-byte alignment and no compressed references. Save your design in a file amber-layout.txt

  3. Verify your expectations in point 2 comparing it to the result of the output of JOL for Hotspot Layout Simulation (JDK 15, 64-bit model, NO compressed references, NO compressed classes, 8-byte aligned). Run JOL with:

java -cp .;jol-cli-0.17-full.jar org.openjdk.jol.Main estimates apps.Amber

NOTE: Replace ; by : on -cp if you are using a linux-based terminal

Homework 02 - Virtual Methods (6-3-2024)

Consider the following definition of types A, B, C, and I. Given the use of those types in main, answer the following questions:

  1. Predict the output of the execution of the main function.
  2. What are the differences in resulting output if we remove the final keyword from virtualFoo in class B?
  3. Keeping the definition of types A, B, C, and I as it is, what would be the result of adding the following implementation in class C:
    • public void virtualFoo() { out.println("C"); }
  4. Provide an equivalent implementation in Kotlin for types A, B, C, and I, with only the method virtualFoo to achieve the same result as in question 1. Do not include the static method foo.
interface I { void virtualFoo(); }
class A {
    public static void foo() { out.println("A"); }
    public void virtualFoo(){ out.println("B"); }
}
class B extends A implements I {
    public static void foo() { out.println("B"); }
    public final void virtualFoo(){ out.println("B"); }
}
class C extends B {
    public static void foo(){ out.println("C"); }
}
public static void main(String[] args) {
    final C c = new C();
    final A a = c;
    final B b = c;
    final I i = c;
    a.foo();
    a.virtualFoo();
    b.foo();
    b.virtualFoo();
    c.foo();
    c.virtualFoo();
    i.virtualFoo();       
}

Homework 03 - Reflection (14-3-2023)

The program App.kt from lesson09-TPC is designed to inspect and print the members of accessible types found in the classpath.

The module lesson09-TPC contains some dummy classes in the resources folder to illustrate the functionality of this program.

  1. Analyze the program App.kt from lesson09-TPC and examine the output produced during its execution. Next, we present a portion of the expected output:
var SavingsAccount.annualInterestRate: kotlin.Double
var SavingsAccount.balance: kotlin.Long
fun SavingsAccount.accrueMonthlyInterest(): kotlin.Unit
fun SavingsAccount.deposit(kotlin.Long): kotlin.Unit
fun SavingsAccount.monthlyInterest(): kotlin.Long
fun SavingsAccount.withdraw(kotlin.Long): kotlin.Unit
fun SavingsAccount.equals(kotlin.Any?): kotlin.Boolean
fun SavingsAccount.hashCode(): kotlin.Int
fun SavingsAccount.toString(): kotlin.String
var Point.x: kotlin.Int
var Point.y: kotlin.Int
fun Point.print(): kotlin.Unit
...
  1. Implement the utility function callParameterlessMethod(owner: KClass<*>, func: KCallable<*>) that prints the result of invoking the given function func if it returns something other than Unit and if it has a single argument corresponding to the instance parameter.

In such cases, an instance of the owner class should be created and passed as the argument.

Next, we present a portion of the expected output:

var SavingsAccount.annualInterestRate: kotlin.Double
 >>>> 0.7
var SavingsAccount.balance: kotlin.Long
 >>>> 100
fun SavingsAccount.accrueMonthlyInterest(): kotlin.Unit
fun SavingsAccount.deposit(kotlin.Long): kotlin.Unit
fun SavingsAccount.monthlyInterest(): kotlin.Long
 >>>> 5
fun SavingsAccount.withdraw(kotlin.Long): kotlin.Unit
fun SavingsAccount.equals(kotlin.Any?): kotlin.Boolean
fun SavingsAccount.hashCode(): kotlin.Int
 >>>> 1807015220
fun SavingsAccount.toString(): kotlin.String
 >>>> SavingsAccount@7d9f158f
var Point.x: kotlin.Int
 >>>> 0
var Point.y: kotlin.Int
 >>>> 0
fun Point.print(): kotlin.Unit
fun Point.equals(kotlin.Any?): kotlin.Boolean

Homework 04 - Cojen Maker (15-4-202)

Replace the reflect invocation to mul in lesson20-cojen-maker with an interface call. To that end, make the dynamic type implement an interface with a method int mul(int)and then you may cast receiver to your interface and call mull directly.

Homework 05 - eagerDistinct (06-5-2024)

Implement eagerDistinct.