Skip to content
Snippets Groups Projects
Commit 0d795e98 authored by LABOUREL Arnaud's avatar LABOUREL Arnaud
Browse files

First version of the template

parent 047d15d6
Branches
No related tags found
No related merge requests found
...@@ -9,10 +9,10 @@ version '1.0-SNAPSHOT' ...@@ -9,10 +9,10 @@ version '1.0-SNAPSHOT'
repositories { repositories {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2' testImplementation ('org.junit.jupiter:junit-jupiter-api:5.8.1',
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2' 'org.assertj:assertj-core:3.21.0')
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
} }
test { test {
......
rootProject.name = 'students' rootProject.name = 'array-list'
import java.util.ArrayList;
import java.util.List;
/**
* A group of students.
*/
public class Cohort {
private final String name;
private final List<Student> students;
/**
* Constructs a cohort with a name equals to the specified {@code name} and no students.
* @param name the name of the constructed Cohort
*/
public Cohort(String name) {
this.name = name;
this.students = new ArrayList<>();
}
/**
* Add the specified {@code student} to the students of the cohort.
* @param student the student to be added to the cohort
*/
public void addStudent(Student student){
// TODO : add code
}
/**
* Returns the list of students of the cohort.
* @return the list of students of the cohort.
*/
public List<Student> getStudents(){
// TODO : change code
return null;
}
/**
* Print via the standard output the name of the cohort and all results associated to the students with their average
* grade.
*/
public void printStudentsResults(){
// TODO : add code
}
/**
* Returns the name of the cohort.
* @return the name of the cohort
*/
@Override
public String toString() {
// TODO : change code
return null;
}
}
import java.util.List;
/**
* A grade with a float value comprised between 0 and 20.
*
*/
public class Grade {
/**
* The maximum value of a grade.
*/
private static final int MAXIMUM_GRADE = 20;
private final double value;
/**
* Constructs a grade with a value equals to the specified {@code value}.
*
* @param value the value of the constructed grade
*/
public Grade(double value) {
this.value = value;
}
/**
* Returns the value of the grade as a double.
*
* @return the value of the grade
*/
public double getValue() {
// TODO : change code
return 0.;
}
/**
* Returns a string representation of the grade in the format X.X/20.
* @return a string representation of the grade
*/
@Override
public String toString() {
// TODO : change code
return null;
}
/**
* Returns a grade with a value equals to the arithmetic mean of the values of the grade in
* the specified list.
*
* @param grades a list of grades
* @return a grade corresponding to the mean of grade in {@code grades}
*/
public static Grade averageGrade(List<Grade> grades){
// TODO : change code
return null;
}
/**
* Determines whether or not two grades are equal. Two instances of Grade are equal if the values
* of their {@code value} member field are the same.
* @param o an object to be compared with this Grade
* @return {@code true} if the object to be compared is an instance of Grade and has the same value; {@code false}
* otherwise.
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Grade grade = (Grade) o;
return Double.compare(grade.value, value) == 0;
}
/**
* Returns a hash code value for the object.
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
long temp = Double.doubleToLongBits(value);
return (int) (temp ^ (temp >>> 32));
}
}
public class Main { public class Main {
public static void main(String[] args){ public static void main(String[] args){
// TODO: add code. // TODO: add code to use MyArrayList.
} }
} }
public class MyArrayList {
}
import java.util.ArrayList;
import java.util.List;
/**
* A students with results.
*/
public class Student {
private final String firstName;
private final String lastName;
private final List<TeachingUnitResult> results;
/**
* Constructs a student with the specified first name and last name and no associated results.
*
* @param firstName the first name of the constructed student
* @param lastName the last name of the constructed student
*/
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.results = new ArrayList<>();
}
/**
* Add a grade associated to a teaching unit to the results of the student.
*
* @param teachingUnitName the name of the teaching unit of the added result
* @param grade the grade of the added result
*/
public void addResult(String teachingUnitName, Grade grade){
// TODO : add code
}
/**
* Returns a string representation of the student in the format first name last name.
* @return a string representation of the student
*/
@Override
public String toString() {
// TODO : change code
return null;
}
/**
* Returns the grades of the student.
*
* @return the grades of the student
*/
public List<Grade> getGrades(){
// TODO : change code
return null;
}
/**
* Returns the average grade of the student.
*
* @return the average grade of the student
*/
public Grade averageGrade() {
// TODO : change code
return null;
}
/**
* Print via the standard output the name of the student, all results associated to the students and
* the average grade of the student.
*/
public void printResults(){
// TODO : add code
}
/**
* Determines whether or not two students are equal. Two instances of Student are equal if the values
* of their {@code firtName} and {@code lastName} member fields are the same.
* @param o an object to be compared with this Student
* @return {@code true} if the object to be compared is an instance of Student and has the same name; {@code false}
* otherwise.
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (!firstName.equals(student.firstName)) return false;
return lastName.equals(student.lastName);
}
/**
* Returns a hash code value for the object.
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
int result = firstName.hashCode();
result = 31 * result + lastName.hashCode();
return result;
}
}
/**
* A result corresponding to a grade associated with a teaching unit.
*/
public class TeachingUnitResult {
private final String teachingUnitName;
private final Grade grade;
/**
* Constructs an instance of TeachingUnitResult with a grade equals to the specified {@code grade}
* and a teaching unit name equals to the specified {@code teachingUnitName}.
*
* @param teachingUnitName the name of the teaching unit of the constructed TeachingUnitResult
* @param grade the grade of the constructed TeachingUnitResult
*/
public TeachingUnitResult(String teachingUnitName, Grade grade) {
this.teachingUnitName = teachingUnitName;
this.grade = grade;
}
/**
* Returns the grade associated to the result.
*
* @return the grade associated to the result
*/
public Grade getGrade() {
// TODO : change code
return null;
}
/**
* Returns a string representation of the result in the format Name of the teaching unit : X.X.
* @return a string representation of the result
*/
@Override
public String toString() {
// TODO : change code
return null;
}
/**
* Determines whether or not two results are equal. Two instances of TeachingUnitResult are equal if the values
* of their {@code teachingUnitName} and {@code grade} member fields are the same.
* @param o an object to be compared with this TeachingUnitResult
* @return {@code true} if the object to be compared is an instance of TeachingUnitResult and has the same grad and
* teaching unit name; {@code false} otherwise.
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TeachingUnitResult that = (TeachingUnitResult) o;
if (!teachingUnitName.equals(that.teachingUnitName)) return false;
return grade.equals(that.grade);
}
/**
* Returns a hash code value for the object.
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
int result = teachingUnitName.hashCode();
result = 31 * result + grade.hashCode();
return result;
}
}
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
public class MyArrayListTest {
@Test
void testToString(){
assertThat(new MyArrayList().toString()).isEqualTo("");
}
}
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
public class StandardOutputSandbox implements Runnable {
static String NEW_LINE = System.getProperty("line.separator");
private Runnable runnable;
private OutputStream outputStream;
StandardOutputSandbox(Runnable runnable) {
this.runnable = runnable;
}
public void run(){
outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);
System.setOut(printStream);
runnable.run();
PrintStream originalOut = System.out;
System.setOut(originalOut);
}
String getProducedOutput() {
return outputStream.toString();
}
}
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
class TestCohort {
private static Cohort cohort = new Cohort("L2 informatique");
@BeforeAll
static void addStudentsToCohort(){
Student paulCalcul = new Student("Paul", "Calcul");
Student pierreKiroul = new Student("Pierre", "Kiroul");
pierreKiroul.addResult("Programmation 2", TestGrade.ten);
pierreKiroul.addResult("Structures discrètes", TestGrade.zero);
paulCalcul.addResult("Programmation 2", TestGrade.ten);
paulCalcul.addResult("Structures discrètes", TestGrade.twenty);
cohort.addStudent(paulCalcul);
cohort.addStudent(pierreKiroul);
}
@Test
void testGetStudents(){
assertEquals(List.of(TestStudent.paulCalcul, TestStudent.pierreKiroul), cohort.getStudents());
}
@Test
void testPrintStudentsResults() {
StandardOutputSandbox standardOutputSandbox = new StandardOutputSandbox(() ->cohort.printStudentsResults());
String expectedOutput = "L2 informatique" + StandardOutputSandbox.NEW_LINE + StandardOutputSandbox.NEW_LINE
+ "Paul Calcul" + StandardOutputSandbox.NEW_LINE
+ "Programmation 2 : 10.0/20" + StandardOutputSandbox.NEW_LINE
+ "Structures discrètes : 20.0/20" + StandardOutputSandbox.NEW_LINE
+ "Note moyenne : 15.0/20" + StandardOutputSandbox.NEW_LINE + StandardOutputSandbox.NEW_LINE
+ "Pierre Kiroul" + StandardOutputSandbox.NEW_LINE
+ "Programmation 2 : 10.0/20" + StandardOutputSandbox.NEW_LINE
+ "Structures discrètes : 0.0/20" + StandardOutputSandbox.NEW_LINE
+ "Note moyenne : 5.0/20" + StandardOutputSandbox.NEW_LINE + StandardOutputSandbox.NEW_LINE;
standardOutputSandbox.run();
assertEquals(expectedOutput, standardOutputSandbox.getProducedOutput());
}
}
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
import java.util.List;
class TestGrade {
static Grade twenty = new Grade(20);
static Grade zero = new Grade(0);
static Grade ten = new Grade(10);
private static List<Grade> grades = List.of(zero, twenty, ten);
private static List<Grade> gradesZero = List.of(zero, zero);
@Test
void testGetValue() {
assertEquals(20, twenty.getValue());
assertEquals(0, zero.getValue());
}
@Test
void testToString() {
assertEquals("20.0/20", twenty.toString());
assertEquals("0.0/20", zero.toString());
}
@Test
void testAverageGrade(){
assertEquals(ten, Grade.averageGrade(grades));
assertEquals(zero, Grade.averageGrade(gradesZero));
}
}
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.List;
class TestStudent {
private static Student arnaudLabourel = new Student("Arnaud", "Labourel");
static Student paulCalcul = new Student("Paul", "Calcul");
static Student pierreKiroul = new Student("Pierre", "Kiroul");
@BeforeAll
static void addResultsToStudents(){
arnaudLabourel.addResult("Programmation 2", TestGrade.twenty);
arnaudLabourel.addResult("Structures discrètes", TestGrade.twenty);
pierreKiroul.addResult("Programmation 2", TestGrade.ten);
pierreKiroul.addResult("Structures discrètes", TestGrade.zero);
paulCalcul.addResult("Programmation 2", TestGrade.ten);
paulCalcul.addResult("Structures discrètes", TestGrade.twenty);
}
@Test
void testToString() {
assertEquals("Paul Calcul", paulCalcul.toString());
assertEquals("Pierre Kiroul", pierreKiroul.toString());
}
@Test
void testGetGrades() {
assertEquals(List.of(TestGrade.twenty, TestGrade.twenty), arnaudLabourel.getGrades());
assertEquals(List.of(TestGrade.ten, TestGrade.zero), pierreKiroul.getGrades());
assertEquals(List.of(TestGrade.ten, TestGrade.twenty), paulCalcul.getGrades());
}
@Test
void testGetAverageGrade() {
assertEquals(TestGrade.twenty, arnaudLabourel.averageGrade());
assertEquals(new Grade(5), pierreKiroul.averageGrade());
assertEquals(new Grade(15), paulCalcul.averageGrade());
}
@Test
void testPrintResults() {
StandardOutputSandbox standardOutputSandbox = new StandardOutputSandbox(() ->arnaudLabourel.printResults());
String expectedOutput =
"Arnaud Labourel" + StandardOutputSandbox.NEW_LINE
+ "Programmation 2 : 20.0/20" + StandardOutputSandbox.NEW_LINE
+ "Structures discrètes : 20.0/20" + StandardOutputSandbox.NEW_LINE
+ "Note moyenne : 20.0/20" + StandardOutputSandbox.NEW_LINE;
standardOutputSandbox.run();
assertEquals(expectedOutput, standardOutputSandbox.getProducedOutput());
}
}
\ No newline at end of file
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class TestTeachingUnitResult {
private static TeachingUnitResult twentyAtProg =
new TeachingUnitResult("Programmation 2", TestGrade.twenty);
private static TeachingUnitResult zeroAtStructDiscrete =
new TeachingUnitResult("Structures discrètes", TestGrade.zero);
@Test
void testGetGrade() {
assertEquals(TestGrade.twenty, twentyAtProg.getGrade());
assertEquals(TestGrade.zero, zeroAtStructDiscrete.getGrade());
}
@Test
void testToString() {
assertEquals("Programmation 2 : 20.0/20", twentyAtProg.toString());
assertEquals("Structures discrètes : 0.0/20", zeroAtStructDiscrete.toString());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment