From e0845968a17e2dc3ff7e7634d9fc2c4a21ca15d4 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 16 Apr 2024 13:02:44 +0200 Subject: [PATCH] Checkpoint 2 --- app/src/main/java/hatelace/IntWord.java | 30 +++++++------------ app/src/main/java/hatelace/Word.java | 8 ++--- .../main/java/hatelace/instructions/Add.java | 3 +- .../main/java/hatelace/instructions/Mul.java | 3 +- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/hatelace/IntWord.java b/app/src/main/java/hatelace/IntWord.java index 7473907..d77a068 100644 --- a/app/src/main/java/hatelace/IntWord.java +++ b/app/src/main/java/hatelace/IntWord.java @@ -7,31 +7,23 @@ public class IntWord extends Word { this.value = value; } - // public T getValue() { - // return value; - // } - public Integer getValue() { return value; } - public IntWord multiply(Word value) { + public Word add(Word other) { + return new IntWord(value + (Integer) other.getValue()); + } + + public Word subtract(Word other) { + return new IntWord(value - (Integer) other.getValue()); + } + + public Word multiply(Word value) { return new IntWord(this.value * (Integer) value.getValue()); } - public T add(Word other) { - return (T) new IntWord(value + (Integer) other.getValue()); - } - - public T subtract(Word other) { - return (T) new IntWord(value - (Integer) other.getValue()); - } - - // public T multiply(Word other) { - // return (T) new IntWord(value * (Integer) other.getValue()); - // } - - public T divide(Word other) { - return (T) new IntWord(value / (Integer) other.getValue()); + public Word divide(Word other) { + return new IntWord(value / (Integer) other.getValue()); } } diff --git a/app/src/main/java/hatelace/Word.java b/app/src/main/java/hatelace/Word.java index 5f6d3bc..c4e19c1 100644 --- a/app/src/main/java/hatelace/Word.java +++ b/app/src/main/java/hatelace/Word.java @@ -2,8 +2,8 @@ package hatelace; public abstract class Word { public abstract T getValue(); - public abstract T add(Word other); - public abstract T subtract(Word other); - public abstract T multiply(Word other); - public abstract T divide(Word other); + public abstract Word add(Word other); + public abstract Word subtract(Word other); + public abstract Word multiply(Word other); + public abstract Word divide(Word other); } diff --git a/app/src/main/java/hatelace/instructions/Add.java b/app/src/main/java/hatelace/instructions/Add.java index 979b0d7..e17c023 100644 --- a/app/src/main/java/hatelace/instructions/Add.java +++ b/app/src/main/java/hatelace/instructions/Add.java @@ -3,7 +3,6 @@ package hatelace.instructions; import hatelace.Instruction; import hatelace.Memory; import hatelace.ProgramCounter; -import hatelace.IntWord; import hatelace.Word; import hatelace.Address; @@ -19,7 +18,7 @@ public class Add extends Instruction { } public void execute(Memory memory, ProgramCounter PC) { - memory.write(this.dest, new IntWord(op1.add(op2))); + memory.write(this.dest, op1.add(op2)); PC.incPC(); } diff --git a/app/src/main/java/hatelace/instructions/Mul.java b/app/src/main/java/hatelace/instructions/Mul.java index 23c69b8..6c587ed 100644 --- a/app/src/main/java/hatelace/instructions/Mul.java +++ b/app/src/main/java/hatelace/instructions/Mul.java @@ -5,7 +5,6 @@ import hatelace.Word; import hatelace.Instruction; import hatelace.Memory; import hatelace.ProgramCounter; -import hatelace.IntWord; public class Mul extends Instruction { private Word op1; @@ -19,7 +18,7 @@ public class Mul extends Instruction { } public void execute(Memory memory, ProgramCounter PC) { - memory.write(this.dest, new IntWord(op1.multiply(op2))); + memory.write(this.dest, op1.multiply(op2)); PC.incPC(); }