-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTesterA5.java
247 lines (202 loc) · 7.87 KB
/
TesterA5.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import java.lang.reflect.*;
import java.util.ArrayList;
public class TesterA5
{
public static void main( String[] args )
{
//try { callAll(); }
//catch( Exception e ){};
System.out.println( "Testing class FileIO...\n" );
try
{
FileIO fio = null;
System.out.println( "Class found!" );
}
catch( Exception e )
{
System.out.println( "Class FileIO not found! Check the name of the class and the file!" );
}
try
{
System.out.print( "Testing readCharacter()... " );
Character monster = FileIO.readCharacter( "monster.txt" );
Character player = FileIO.readCharacter( "player.txt" );
System.out.println( "OK" );
System.out.print( "Testing readSpells()... " );
ArrayList< Spell > als = FileIO.readSpells( "spells.txt" );
System.out.println( "OK" );
System.out.print( "Testing writeCharacter()... " );
FileIO.writeCharacter( monster,"monster_copy.txt" );
System.out.println( "OK" );
}
catch( Exception e )
{
System.out.println( "\nFailed! Exception was raised. Check names of the methods and the assignment instructions!" );
}
System.out.println( "\n\nTesting class Spell...\n" );
try
{
Spell s = null;
System.out.println( "Class found!" );
}
catch( Exception e )
{
System.out.println( "Class Spell not found! Check the name of the class and the file!" );
}
try
{
System.out.print( "Testing constructor... " );
Spell s = new Spell( "crucify",2.0,10.0,0.3 );
System.out.println( "OK" );
System.out.print( "Testing toString()... " );
String ss = s.toString();
System.out.println( "OK" );
System.out.print( "Testing getName()... " );
String s0 = s.getName();
System.out.println( "OK" );
System.out.print( "Testing getMagicDamage()... " );
double d0 = s.getMagicDamage( 123 );
System.out.println( "OK" );
}
catch( Exception e )
{
System.out.println( "\nFailed! Exception was raised. Check names of the methods and the assignment instructions!" );
}
System.out.println( "\n\nTesting class Character...\n" );
try
{
Character c = null;
System.out.println( "Class found!" );
}
catch( Exception e )
{
System.out.println( "Class Character not found! Check the name of the class and the file!" );
}
try
{
System.out.print( "Testing constructor... " );
Character c = new Character( "soldier",2.0,100.0,1 );
System.out.println( "OK" );
System.out.print( "Testing increaseWins()... " );
c.increaseWins();
System.out.println( "OK" );
System.out.print( "Testing setSpells()... " );
ArrayList< Spell > als = new ArrayList< Spell >();
als.add( new Spell( "crucify",2.0,10.0,0.9 ) ) ;
als.add( new Spell( "woosha",1.0,8.0,0.5 ) ) ;
Character.setSpells( als );
System.out.println( "OK" );
System.out.println( "Testing displaySpells()... " );
Character.displaySpells();
System.out.println( "OK" );
System.out.print( "Testing getCurrHealth()... " );
double d1 = c.getCurrHealth();
System.out.println( "OK" );
System.out.print( "Testing toString()... " );
String s2 = c.toString();
System.out.println( "OK" );
System.out.print( "Testing getAttackDamage()... " );
{
Object[] results = { 1.8386881616119433,1.8339045217982881,1.8365382080197208 };
Object[] obtained = { c.getAttackDamage( 2 ),c.getAttackDamage( 123 ),c.getAttackDamage( 42 ) };
processResults( results,obtained );
}
System.out.print( "Testing castSpell()... " );
{
Object[] results = { 5.280646491937613,9.927191174217914,-1.0,0.0 };
Object[] obtained = { c.castSpell( "crucify",1 ),c.castSpell( "CRUCIFY",123 ),c.castSpell( "abrakadabra",456 ),c.castSpell( "woosha",123 ) };
processResults( results,obtained );
}
System.out.print( "Testing takeDamage()... " );
double d4 = c.takeDamage( 1.0 );
System.out.println( "OK" );
System.out.print( "Testing getName()... " );
String s3 = c.getName();
System.out.println( "OK" );
System.out.print( "Testing getAttackValue()... " );
double d5 = c.getAttackValue();
System.out.println( "OK" );
System.out.print( "Testing getMaxHealth()... " );
double d6 = c.getMaxHealth();
System.out.println( "OK" );
System.out.print( "Testing getNumWins()... " );
int i1 = c.getNumWins();
System.out.println( "OK" );
}
catch( Exception e )
{
System.out.println( "\nFailed! Exception was raised. Check names of the methods and the assignment instructions!" );
}
}
public static void processResults( Object[] results,Object[] obtained )
{
boolean passed = true;
for( int i = 0; i < results.length; i++ )
{
if( results[ 0 ] instanceof Double )
{
if( !String.format("%5.11f", results[ i ] ).equals( String.format("%5.11f", obtained[ i ] ) ) )
{
if( passed )
{
System.out.println( "Failed\n" );
passed = false;
}
System.out.println( "Expected: " + String.format("%5.11f", results[ i ] ) );
System.out.println( "Obtained: " + String.format("%5.11f", obtained[ i ] ) + "\n" );
}
}
else if( !results[ i ].equals( obtained[ i ] ) )
{
if( passed )
{
System.out.println( "Failed\n" );
passed = false;
}
System.out.println( "Expected: " + results[ i ] );
System.out.println( "Obtained: " + obtained[ i ] + "\n" );
}
}
if( passed )
{
System.out.println( "Passed" );
}
}
/*
public static void callAll() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
{
Character a = new Character( "Waldo",10.0,100.0,0 );
ArrayList< Spell > als = new ArrayList< Spell >();
als.add( new Spell ( "abraka",3.0,7.0,0.17 ) );
String[] names = { "setSpells","takeDamage","getAttackDamage" } ;
Object[] answers = { als,2.0,3 };
Method[] methods = a.getClass().getDeclaredMethods();
for( Method m : methods )
{
System.out.print( m.getReturnType() + " " + m.getName() + "( " );
Class[] params = m.getParameterTypes();
Object[] arguments = new Object[ params.length ];
for( int i = 0; i < params.length; i++ )
{
System.out.print( params[ i ].toString() + ", " );
}
System.out.println( " )" );
boolean parameter = false;
int whichParameter = -1;
for( int j = 0; j < answers.length; j++ )
{
if( m.getName().equals( names[ j ] ) )
{
parameter = true;
whichParameter = j;
}
}
if( parameter )
System.out.println( m.invoke( a,answers[ whichParameter ] ) );
else
System.out.println( m.invoke( a ) );
System.out.println();
}
}
*/
}