Skip to content

Commit

Permalink
Add quizz2 to the exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDubray committed Jan 4, 2021
1 parent 0925e89 commit 35121f8
Show file tree
Hide file tree
Showing 17 changed files with 771 additions and 65 deletions.
5 changes: 5 additions & 0 deletions ObservableAccount/feedback_settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Example of file
exercise_type: default
has_feedback: True
quorum: 1.0
feedback_kind: JavaGrading
68 changes: 68 additions & 0 deletions ObservableAccount/public/ObservableAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* This class implements a bank account.
* You can deposit and withdraw money from the account.
* Read carefully the comments of each method and fill the missing
* parts marked with "TODO".
* You can ADD any code you like in this class (new members, new methods, etc.).
* but do not add a package
*/

public class ObservableAccount {

public interface AccountObserver {
void accountHasChanged();
}

/**
* Get the account balance (franc. "solde")
* The initial balance of the account is 0.
*
* @return The balance
*/
public int getBalance() {
// TODO
return -100000;
}

/**
* Deposit an amount into the account
*
* @param amount The amount to deposit
*/
public void deposit(int amount) {
// TODO
}

/**
* Withdraw an amount from the account.
* An account cannot become negative.
* If you try to withdraw 1000 Euro from an account that has
* only 500 Euro, the withdrawal is NOT executed.
*
* @param amount The sum to withdraw
*/
public void withdraw(int amount) {
// TODO
}

/**
* Add an observer to the account.
* The observer will be notified if an amount is deposited or withdrawn
* that is greater than the specified maximum.
* The observer must NOT be notified if the withdrawal is not executed
* (see comment of the method 'withdraw')
*
* The user of this class can change the maximum for an added observer by calling
* this method again with the same observer. Example:
* account.addObserver(myObserver,1000);
* account.addObserver(myObserver,2000); // change maximum for this observer
*
* @param observer The observer to add.
* @param maximum The observer will be notified if the deposited or withdrawn
* amount is greater than "maximum".
*
*/
public void addObserver(AccountObserver observer, int maximum) {
// TODO
}
}
22 changes: 22 additions & 0 deletions ObservableAccount/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/python3
import importlib.util
import sys


# Dynamically load modules we need
# Credits to https://stackoverflow.com/a/67692/6149867
# And for the explanation : http://www.blog.pythonlibrary.org/2016/05/27/python-201-an-intro-to-importlib/
def dynamically_load_module(module, path):
spec = importlib.util.spec_from_file_location(module, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod


#####################################
# Our import for common run file #
#####################################
sys.path.append("/course/common")

runfile = dynamically_load_module("runfile", "/course/common/runfile.py")
runfile.main()
86 changes: 86 additions & 0 deletions ObservableAccount/src/InginiousTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package src;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.github.guillaumederval.javagrading.GradingRunner;
import com.github.guillaumederval.javagrading.Grade;
import com.github.guillaumederval.javagrading.GradeFeedback;

import org.junit.Assert;

import templates.*;

@RunWith(GradingRunner.class)
public class InginiousTests {
int observerHasBeenNotified;

@Test
@Grade(value = 1, cpuTimeout=1000)
@GradeFeedback(message="Balance should be correct after multiple deposits and withdrawals", onFail=true)
public void testBalance() {
ObservableAccount account=new ObservableAccount();

account.deposit(1500);
account.deposit(500);
account.withdraw(400);
Assert.assertEquals(1600,account.getBalance());
}

@Test
@Grade(value = 1, cpuTimeout=1000)
@GradeFeedback(message="Observer should be notified of large deposits and withdrawals", onFail=true)
public void testObserver() {
ObservableAccount account=new ObservableAccount();
account.addObserver(() -> observerHasBeenNotified++, 1000);

observerHasBeenNotified=0;
account.deposit(1500);
Assert.assertEquals(1, observerHasBeenNotified);
}

@Test
@Grade(value = 1, cpuTimeout=1000)
@GradeFeedback(message="Withdrawals should not be executed if there is not enough money in the account", onFail=true)
public void testNotExecutedWithdrawal() {
ObservableAccount account=new ObservableAccount();
account.addObserver(() -> observerHasBeenNotified++, 1000);

observerHasBeenNotified=0;
account.deposit(500);
account.withdraw(1000);
Assert.assertEquals(0, observerHasBeenNotified);
}

@Test
@Grade(value = 1, cpuTimeout=1000)
@GradeFeedback(message="Multiple observers with different maximums should be possible", onFail=true)
public void testMultipleObserver() {
ObservableAccount account=new ObservableAccount();
account.addObserver(() -> observerHasBeenNotified++, 1000);
account.addObserver(() -> observerHasBeenNotified++, 500);

observerHasBeenNotified=0;
account.deposit(800);
Assert.assertEquals(1, observerHasBeenNotified);
observerHasBeenNotified=0;
account.deposit(1500);
Assert.assertEquals(2, observerHasBeenNotified);
}

@Test
@Grade(value = 1, cpuTimeout=1000)
@GradeFeedback(message="The maximum for a observer can be modified by calling addObserver() again", onFail=true)
public void testModifyObserver() {
ObservableAccount account=new ObservableAccount();
ObservableAccount.AccountObserver observer= () -> observerHasBeenNotified++;

observerHasBeenNotified=0;
account.addObserver(observer, 1000);
account.deposit(900);
account.addObserver(observer, 800);
account.deposit(900);
Assert.assertEquals(1, observerHasBeenNotified);
account.deposit(2000);
Assert.assertEquals(2, observerHasBeenNotified);
}
}
48 changes: 48 additions & 0 deletions ObservableAccount/src/StudentTestRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package src;

import com.github.guillaumederval.javagrading.GradingListener;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

import java.util.List;

public class StudentTestRunner {

public static void main(String [] args) {

JUnitCore runner = new JUnitCore();
runner.addListener(new GradingListener());
Result result = runner.run(InginiousTests.class);

if(result.wasSuccessful()){
System.exit(0);
} else {
determineProblem(result);
}
}

/*
* Makes the distinction between two different failure cases :
* - The student has a mistake in his/her code. (Sys exit 1)
* - The student used a forbidden instruction in his/her code. (Sys exit 2)
*/
private static void determineProblem(Result result){
boolean flag = false;

if(result.getFailureCount() != 0) {
List<Failure> failures = result.getFailures();

for (Failure fail : failures) {
flag |= fail.getMessage()!= null && fail.getMessage().contains("access denied");
}
}

if(flag) {
System.exit(2); // The student used a forbidden instruction
} else {
System.exit(1); // There's an error in your code etc etc...
}
}

}
105 changes: 105 additions & 0 deletions ObservableAccount/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
accessible: true
author: ''
categories: []
contact_url: ''
context: |-
La classe ObservableAccount implemente un compte bancaire. Vous pouvez déposer et retirer de l'argent. Vous pouvez également ajouter des observateurs au compte qui seront avertis en cas de transactions importants.
Lisez attentivement les commentaires de chaque méthode et remplissez les parties manquantes marquées par "TODO".
Vous pouvez ajouter tout code que vous souhaitez dans cette classe (nouveaux membres, nouvelles méthodes, etc.). Naturellement, vous ne devez pas modifier les signatures des méthodes existantes.
environment_id: java8scala
environment_parameters:
limits:
memory: '100'
time: '30'
hard_time: ''
run_cmd: ''
environment_type: docker
evaluate: best
file: ''
groups: false
input_random: '0'
name: ObservableAccount
network_grading: false
problems:
ObservableAccountMain:
name: ''
type: code
language: java
default: |
/**
* This class implements a bank account.
* You can deposit and withdraw money from the account.
* Read carefully the comments of each method and fill the missing
* parts marked with "TODO".
* You can ADD any code you like in this class (new members, new methods, etc.).
* You can also add imports.
*/
public class ObservableAccount {
public interface AccountObserver {
void accountHasChanged();
}
/**
* Get the account balance (franc. "solde")
* The initial balance of the account is 0.
*
* @return The balance
*/
public int getBalance() {
// TODO
return -100000;
}
/**
* Deposit an amount into the account
*
* @param amount The amount to deposit
*/
public void deposit(int amount) {
// TODO
}
/**
* Withdraw an amount from the account.
* An account cannot become negative.
* If you try to withdraw 1000 Euro from an account that has
* only 500 Euro, the withdrawal is NOT executed.
*
* @param amount The sum to withdraw
*/
public void withdraw(int amount) {
// TODO
}
/**
* Add an observer to the account.
* The observer will be notified if an amount is deposited or withdrawn
* that is greater than the specified maximum.
* The observer must NOT be notified if the withdrawal is not executed
* (see comment of the method 'withdraw')
*
* The user of this class can change the maximum for an added observer by calling
* this method again with the same observer. Example:
* account.addObserver(myObserver,1000);
* account.addObserver(myObserver,2000); // change maximum for this observer
*
* @param observer The observer to add.
* @param maximum The observer will be notified if the deposited or withdrawn
* amount is greater than "maximum".
*
*/
public void addObserver(AccountObserver observer, int maximum) {
// TODO
}
}
header: Paste here the content of the `ObservableAccount.java` file
stored_submissions: 0
submission_limit:
amount: -1
period: -1
weight: 1.0
2 changes: 2 additions & 0 deletions ObservableAccount/templates/ObservableAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package templates;
@@ObservableAccountMain@@
5 changes: 5 additions & 0 deletions ParallelCounting/feedback_settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Example of file
exercise_type: default
has_feedback: True
quorum: 1.0
feedback_kind: JavaGrading
29 changes: 29 additions & 0 deletions ParallelCounting/public/ParallelCounting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Optional;

/**
* You can ADD any code you like in this class (new members, new methods, etc.).
* You can also add imports.
*/

public class ParallelCounting {
/**
* Return the number of values equal to v using a parallel algorithm.
*
* @param values an array of numbers
* @param v the value that you want to count
* @param nThreads is a value between 1 and values.length
* @return the number of elements equal to v in values (or 0 if no values are provided)
*
* Example: For
* values = [4.5f,3.2f,5.0f,6.6f,7.2f,1.5f,3.7f,5.8f,6.0f,9.0f,1.3f,2.3f,4.5f,1.5f]
* and
* v = 4.5
* the method returns 2 because the value 4.5 appears two times in the array.
*
* Try to give all threads more or less the same amount of work!
*/
public static int parallelCount (Optional<float[]> values, float v, int nThreads) {
// TODO
return -1;
}
}
Loading

0 comments on commit 35121f8

Please sign in to comment.