Skip to content

Add solution for assignment 1 #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/main/java/edu/hm/hafner/java2/assignment1/Assignment.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,89 @@
* Representation of an assignment.
*/
public class Assignment {
private final int number;
private final boolean[] testCases;
private final int tests; // redundant as implicitly given by array length

/**
* Creates a new instance of {@link Assignment}.
*
* @param number
* the assignment number
* @param tests
* the number of test cases
*/
public Assignment(final int number, final int tests) {
this.number = number;
this.testCases = new boolean[tests];
this.tests = tests;
}

public int getNumber() {
return number;
}

public int getTests() {
return tests;
}

/**
* Returns the number of green tests.
*
* @return the number of solved tests.
*/
public int getGreen() {
int solvedAmount = 0;
for (boolean isTestGreen : testCases) {
if (isTestGreen) {
solvedAmount++;
}
}
return solvedAmount;
}

/**
* Get number of red tests.
*
* @return number of unsolved tests
*/
public int getRed() {
return tests - getGreen();
}

/**
* Get percentage of solved tests.
*
* @return percentage of solved tests
*/
public int getPercentage() {
int solvedAmount = getGreen();

return 100 * solvedAmount / getTests();
}

/**
* Solves a test with the given index.
*
* @param index
* index of test to solve
* @throws IndexOutOfBoundsException if the index is out of bounds
*/
public void solve(final int index)
throws IndexOutOfBoundsException {
if (index < 0 || index >= testCases.length) {
throw new IndexOutOfBoundsException(
"Index out of bounds: " + index);
}
testCases[index] = true;
}

/**
* Returns whether the percentage of solved tests is greater or equal than 50 percent.
*
* @return if achieved percentage is enough
*/
public boolean isSufficient() {
return getPercentage() >= 50;
}
}
92 changes: 92 additions & 0 deletions src/main/java/edu/hm/hafner/java2/assignment1/Exam.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,99 @@
package edu.hm.hafner.java2.assignment1;

import java.util.Arrays;

/**
* An exam contains several assignments.
*/
public class Exam {
private static final int LIMIT_ONE = 95;
private static final int LIMIT_TWO = 80;
private static final int LIMIT_THREE = 65;
private static final int LIMIT_FOUR = 50;
private static final int LIMIT_FIVE = 0;

private Assignment[] assignments = new Assignment[0];

/**
* Returns the number of assignments.
*
* @return the number of assignments
*/
public int getSize() {
return assignments.length;
}

/**
* Adds an assignment to this exam.
*
* @param assignment
* the assignment to add
*/
public void addAssignment(final Assignment assignment) {
assignments = Arrays.copyOf(assignments, assignments.length + 1);
assignments[assignments.length - 1] = assignment;
}

/**
* Returns the assignment at the given index.
*
* @param index
* index of the assignment
*
* @return assignment at the given index
*/
public Assignment getAssignment(final int index) {
return assignments[index];
}

/**
* Get total score of all assignments in the exam.
*
* @return score between 0 and 100
*/
public int getScore() {
var size = getSize();
if (size == 0) {
return 100; // Not specified: 0 or exception is possible as well
}

int percentageSum = 0;
for (Assignment assignment : assignments) {
percentageSum += assignment.getPercentage();
}
return percentageSum / size;
}

/**
* Returns whether the score of this exam is greater or equal than 50 percent.
*
* @return if achieved score is enough
*/
public boolean isSufficient() {
return getScore() >= LIMIT_FOUR;
}

/**
* Returns the grade for this exam between 1 and 6.
*
* @return grade for exam
*/
public int getGrade() {
if (getScore() >= LIMIT_ONE) {
return 1;
}
if (getScore() >= LIMIT_TWO) {
return 2;
}
if (getScore() >= LIMIT_THREE) {
return 3;
}
if (getScore() >= LIMIT_FOUR) {
return 4;
}
if (getScore() > LIMIT_FIVE) {
return 5;
}
return 6;
}
}
68 changes: 66 additions & 2 deletions src/test/java/edu/hm/hafner/java2/assignment1/AssignmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,72 @@
class AssignmentTest {
@Test
void shouldCreateEmptyAssignment() {
var assignment = new Assignment();
var assignment = new Assignment(18, 5);

assertThat(assignment).isSameAs(assignment); // This is a fake test that needs to be replaced
assertThat(assignment.getNumber()).isEqualTo(18);
assertThat(assignment.getTests()).isEqualTo(5);
assertThat(assignment.getGreen()).isEqualTo(0);
assertThat(assignment.getRed()).isEqualTo(5);
assertThat(assignment.getPercentage()).isEqualTo(0);
assertThat(assignment.isSufficient()).isFalse();
}

@Test
void shouldFixOneTest() {
var assignment = new Assignment(1, 10);

assignment.solve(1);

assertThat(assignment.getTests()).isEqualTo(10);
assertThat(assignment.getGreen()).isEqualTo(1);
assertThat(assignment.getRed()).isEqualTo(9);
assertThat(assignment.getPercentage()).isEqualTo(10);
assertThat(assignment.isSufficient()).isFalse();
}

@Test
void shouldFixAllTests() {
var assignment = new Assignment(1, 2);

assignment.solve(0);

assertThat(assignment.getGreen()).isEqualTo(1);
assertThat(assignment.getRed()).isEqualTo(1);
assertThat(assignment.getPercentage()).isEqualTo(50);
assertThat(assignment.isSufficient()).isTrue();

assignment.solve(1);

assertThat(assignment.getGreen()).isEqualTo(2);
assertThat(assignment.getRed()).isEqualTo(0);
assertThat(assignment.getPercentage()).isEqualTo(100);
assertThat(assignment.isSufficient()).isTrue();
}

@Test
void shouldNotSolveTests() {
var assignment = new Assignment(1, 3);

assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> assignment.solve(-1));
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> assignment.solve(3));
}

@Test
void shouldNotSolveSameTestTwice() {
var assignment = new Assignment(1, 3);

assignment.solve(0);

assertThat(assignment.getGreen()).isEqualTo(1);
assertThat(assignment.getRed()).isEqualTo(2);
assertThat(assignment.getPercentage()).isEqualTo(33);

assignment.solve(0);

assertThat(assignment.getGreen()).isEqualTo(1);
assertThat(assignment.getRed()).isEqualTo(2);
assertThat(assignment.getPercentage()).isEqualTo(33);
}
}
Loading