MOOC.fi Java Programming course 1 - Exercise 13 "Exercises" Part 6 - Compilation error

48 Views Asked by At

I've been trying to submit this exercise for quite some time now. I just copied and pasted the previous snippets I wrote by following the example in the course referred to by the exercise.

The test run ok, they all pass. But when I change the "partsCompleted" in the MainProgram to "2" to state that I have completed the tasks and then I submit, I get a compilation error. This is my code:

Exercise:

public class Exercise {
    
    private String name;
    private boolean completed;
    
    public Exercise (String exerciseName) {
        this.name = exerciseName;
        this.completed = false;
    }
    
    public String getName() {
        return name;
    }
    
    public void setCompleted(boolean completed) {
        this.completed = completed;
    }
    
    public boolean isCompleted() {
        return completed;
    }
}

Exercise management:

import java.util.ArrayList;


public class ExerciseManagement {
    private ArrayList<Exercise> exercises;
    
    public ExerciseManagement() {
        this.exercises = new ArrayList<>();
    }
    
    public ArrayList<String> exerciseList() {
        ArrayList<String> list = new ArrayList<>();
        for (Exercise exes: exercises) {
            list.add(exes.getName());
        }
        return list;
    }
    
    public void add(Exercise exercise) {
        this.exercises.add(exercise);
    }
    
    public void markAsCompleted(String exercise) {
        
        for (Exercise exes: exercises) {
            if (exes.getName().equals(exercise)) {
                exes.setCompleted(true);
                break;
            }
        }
    }
    
    public boolean isCompleted(String exercise) {
        
        for (Exercise exes: exercises) {
            if (exes.getName().equals(exercise)) {
                return exes.isCompleted();
            }
        }
        return false;
    }    
}

Exercise Management test:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;


public class ExerciseManagementTest {
    private ExerciseManagement management;
    private Exercise exercise;
    
    public ExerciseManagementTest() {
    }
    
    @Before
    public void initialize() {
        management = new ExerciseManagement();
        exercise = new Exercise("Write here the exercise");
    }
    
    @Test
    public void exerciseListemptyAtTheBeginning() {
        assertEquals(0, management.exerciseList().size());
    }
    
    @Test
    public void addingExercisesGrowsListByOne() {
        management.add(exercise);
        assertEquals(1, management.exerciseList().size());
    }
    
    @Test
    public void addedExerciseIsInList() {
        management.add(exercise);
        assertTrue(management.exerciseList().contains(exercise.getName()));
    }
    
    @Test
    public void exerciseCanBeMarkedAsCompleted() {
        management.add(exercise);
        management.markAsCompleted(exercise.getName());
        assertTrue(management.isCompleted(exercise.getName()));
    }
    
    @Test
    public void ifNotMarkedCompletedIsNotCompleted() {
        management.add(exercise);
        management.markAsCompleted(exercise.getName());
        assertFalse(management.isCompleted("Some exercise"));
    }
}

Main Program:

public class MainProgram {

    // update here your exercise progress
    public static int partsCompleted() {
        return 2;
    }
}

And this is the error I get:


Compilation error: stdout: [INFO] Scanning for projects... [INFO]  [INFO] ----------------------< tkt:Part06_13.Exercises >----------------------- [INFO] Building Part06_13.Exercises 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ Part06_13.Exercises --- [INFO]  [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Part06_13.Exercises --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /app/src/main/resources [INFO]  [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ Part06_13.Exercises --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 3 source files to /app/target/classes [INFO]  [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Part06_13.Exercises --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /app/src/main/resources [INFO]  [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ Part06_13.Exercises --- [INFO] Nothing to compile - all classes are up to date [INFO]  [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Part06_13.Exercises --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /app/src/test/resources [INFO]  [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ Part06_13.Exercises --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to /app/target/test-classes [INFO] Some messages have been simplified; recompile with -Xdiags:verbose to get full output [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR :  [INFO] ------------------------------------------------------------- [ERROR] /app/src/test/java/ExerciseManagementTest.java:[27,21] incompatible types: java.lang.String cannot be converted to Exercise [ERROR] /app/src/test/java/ExerciseManagementTest.java:[33,21] incompatible types: java.lang.String cannot be converted to Exercise [ERROR] /app/src/test/java/ExerciseManagementTest.java:[39,21] incompatible types: java.lang.String cannot be converted to Exercise [ERROR] /app/src/test/java/ExerciseManagementTest.java:[45,21] incompatible types: java.lang.String cannot be converted to Exercise [INFO] 4 errors  [INFO] ------------------------------------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time:  3.207 s [INFO] Finished at: 2024-03-28T00:10:51Z [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:testCompile (default-testCompile) on project Part06_13.Exercises: Compilation failure: Compilation failure:  [ERROR] /app/src/test/java/ExerciseManagementTest.java:[27,21] incompatible types: java.lang.String cannot be converted to Exercise [ERROR] /app/src/test/java/ExerciseManagementTest.java:[33,21] incompatible types: java.lang.String cannot be converted to Exercise [ERROR] /app/src/test/java/ExerciseManagementTest.java:[39,21] incompatible types: java.lang.String cannot be converted to Exercise [ERROR] /app/src/test/java/ExerciseManagementTest.java:[45,21] incompatible types: java.lang.String cannot be converted to Exercise [ERROR] -> [Help 1] [ERROR]  [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR]  [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException stderr: WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release


I've been trying to figure out what's wrong even with the help of ChatGPT but without success. Point is, I pretty much followed what the example above asked me to do (even corrected some blatant syntax errors in one of the example's classes). This error should probably state that there is an incompatibility with the types in one or more of the methods, but can't understand how to solve it without changing drastically the whole exercise. If you need any other detail ask, Thanks!

0

There are 0 best solutions below