@matfah/

AP Line

Java

No description

fork
loading
Files
  • Main.java
  • APLine.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.lang.reflect.*;

public class Main {
  public static void main(String[] args) {
    
    checkAllInstanceVariablesArePrivate(APLine.class);
    
    for(int i = 0; i < 10000; i++)
      testAPLine();
    
    System.out.println("All tests passed!");
  }
  
  public static void checkAllInstanceVariablesArePrivate(Class c) {
    Field[] fields = c.getDeclaredFields();
    for(Field f: fields) {
      if((f.getModifiers() & Modifier.PRIVATE) != Modifier.PRIVATE)
      {
        System.out.println("Not all fields of " + c.getName() + " are private!");
        System.exit(0);
      }
    }
  }
  
  public static void testAPLine() {
    
    Constructor<APLine> con = null;
    
    try {
      con = APLine.class.getConstructor(int.class, int.class, int.class);
    }
    catch(Exception e) {
      System.out.println("APLine does not have a 3-parameter constructor that takes int's!");
      System.exit(0);
    }
    
    int a = (int)(Math.random()*10) + 1;
    int b = (int)(Math.random()*10) + 1;
    int c = (int)(Math.random()*10);
    
    if(Math.random() < 0.5) a = -a;
    if(Math.random() < 0.5) b = -b;
    if(Math.random() < 0.5) c = -c;
    
    APLine line = null;
    
    try {
      line = con.newInstance(a, b, c);
    }
    catch(Exception e) {
      System.out.println("Error trying to instantiate an APLine object");
      System.exit(0);
    }
    
    boolean ok = false;
    
    //make sure at least one field gets initialized
    Field[] fields = APLine.class.getDeclaredFields();
    for(Field f: fields) {
      f.setAccessible(true);
      
      Object value = null;
      try {
        value = f.get(line);
      }
      catch(Exception e) {
        System.out.println("Error accessing instance variables of APLine object");
        System.exit(0);
      }
      
      if(f.getType().isPrimitive()) {
        if(f.getType().equals(int.class)) {
          if(value != null && ((Integer)value).intValue() != 0) 
            ok = true;
        }
        if(f.getType().equals(double.class)) {
          if(value != null && ((Double)value).doubleValue() != 0.0) 
            ok = true;
        }
      }
      else {
        if(value != null) {
          ok = true;
        }
      }
      
      if(!ok) {
        System.out.println("Are you sure you initizlied your instance variables correctly?");
        System.exit(0);
      }
    }
    
    Method slopeMethod = null;
    
    try {
      slopeMethod = APLine.class.getMethod("getSlope");
    }
    catch(Exception e) {
      System.out.println("APLine does not have a getSlope() method with no parameters");
      System.exit(0);
    }
    
    Class returnType = slopeMethod.getReturnType();
    if(!returnType.equals(double.class)) {
      System.out.println("APLine's getSlope() method does not have a double return type");
      System.exit(0);
    }
    
    double slope = 0.0;
    
    try {
      slope = (Double)slopeMethod.invoke(line);
    }
    catch(Exception e) {
      System.out.println("Error calling getSlope() method");
      System.exit(0);
    }
    
    double realSlope = -1.0*a/b;
    
    if(Math.abs(slope - realSlope) > 0.00001) {
      System.out.println("APLine's getSlope() method does not return the right value");
      System.exit(0);
    }
    
    
    Method isOnlineMethod = null;
    
    try {
      isOnlineMethod = APLine.class.getMethod("isOnLine", int.class, int.class);
    }
    catch(Exception e) {
      System.out.println("APLine does not have an isOnLine() method with two int parameters");
      System.exit(0);
    }
    
    returnType = isOnlineMethod.getReturnType();
    if(!returnType.equals(boolean.class)) {
      System.out.println("APLine's isOnLine() method does not have a boolean return type");
      System.exit(0);
    }
    
    boolean online = false;
    
    int x = (int)(Math.random() * 10) - 5;
    int y = (int)(Math.random() * 10) - 5; 
    
    try {
      online = (Boolean)isOnlineMethod.invoke(line, x, y);
    }
    catch(Exception e) {
      System.out.println("Error calling isOnLine() method");
      System.exit(0);
    }
    
    if((a * x + b * y + c == 0) != online) {
      System.out.println("isOnLine() method returns the wrong value");
      System.exit(0);
    }
    
    
  }
}