Mentions légales du service

Skip to content
Snippets Groups Projects
Commit e8c8e68a authored by ynn's avatar ynn
Browse files

ajoute deux exercices sur objets niveau4

parent 15e2ad67
No related branches found
No related tags found
No related merge requests found
package fr.manj.niveau4.ex01;
public class Ex1 {
public static void question1() {
// Creez un simple point de façon à ce que le code fonctionne :
Point p1 = null;
p1.afficherPoint();
}
public static Point question2() {
// 2. Ecrire un constructeur Point(x,y)
// qui permet d'initialiser l'état de l'objet
// Donnez la valeur x = 1 et y = 4
Point p2 = null;
p2.afficherPoint();
return p2;
}
public static void main(String[] args) {
question1();
question2();
}
}
package fr.manj.niveau4.ex01;
public class Point {
private double x;
private double y;
public Point() {
this.x = 0;
this.y = 0;
}
// Corrigez le constructeur Point(x,y) pour qu'il fonctionne :
// Expliquez pourquoi il y'a un pb.
public Point(int x, int y) {
this.x = x;
y = y;
}
public void afficherPoint() {
System.out.println("[" + this.x + "," + this.y + "]");
}
}
package fr.manj.niveau4.ex2;
public class Ex2 {
public static void main(String[] args) {
// Ajouter un constructeur a personne pour prendre le nom en paramètre.
Personne p = new Personne();
// Ecrire la méthode toString de personne pour qu'elle renvoie le nom.
System.out.println(p);
}
}
package fr.manj.niveau4.ex2;
public class Personne {
private String nom;
// Ecrire le constructeur Personne(nom).
// Ecrire la méthode toString()
}
......@@ -2,10 +2,13 @@ package fr.manj;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import org.junit.After;
import org.junit.Before;
import fr.manj.niveau4.ex01.Point;
public class TestUtil {
public static final String NL = System.getProperty("line.separator");
......@@ -26,4 +29,12 @@ public class TestUtil {
System.setOut(this.originalOut);
System.setErr(this.originalErr);
}
public static Object getFieldValue(String name, Object instance)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Field f = instance.getClass().getDeclaredField(name);
f.setAccessible(true);
return f.get(instance);
}
}
package fr.manj.niveau4.ex01;
import static org.junit.Assert.*;
import java.lang.reflect.Field;
import org.junit.Test;
import fr.manj.niveau4.ex01.Ex1;
import fr.manj.niveau4.ex01.Point;
public class Ex1Test {
@Test
public void testQ1() {
try {
Ex1.question1();
} catch (NullPointerException e) {
fail("Vous n'avez pas intialisé le point");
}
}
@Test
public void testQ2()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
try {
Point p = Ex1.question2();
// On se donne l'accès au champs :
Field fx = Point.class.getDeclaredField("x");
fx.setAccessible(true);
Field fy = Point.class.getDeclaredField("y");
fy.setAccessible(true);
assertEquals(1.0, (double) fx.get(p), 0.001);
assertEquals(4.0, (double) fy.get(p), 0.001);
} catch (NullPointerException e) {
fail("Vous n'avez pas intialisé le point");
}
}
}
package fr.manj.niveau4.ex2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Constructor;
import org.junit.Test;
import fr.manj.TestUtil;
public class PersonneTest {
@Test
public void testConstructeur() {
try {
Constructor<Personne> c = Personne.class.getConstructor(String.class);
Personne p = c.newInstance("Paul");
assertEquals("Paul", TestUtil.getFieldValue("nom", p));
} catch (Exception e) {
fail("Vous n'avez pas implémenté le constructeur ou pas mis la bonne visibilité.");
}
}
@Test
public void testToString() {
try {
Constructor<Personne> c = Personne.class.getConstructor(String.class);
Personne p = c.newInstance("Paul");
assertEquals("Paul", p.toString());
} catch (Exception e) {
fail("Vous n'avez pas implémenté le constructeur ou pas mis la bonne visibilité.");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment