Indledende programmering med Eclipse. HJælp til en programmerings opgave.
Jeg skal løse nedenstående opgave og kan ikke rigtig få det til at passe.Jeg har fået udleveret en testkode, som du kan se efter opgaven.
Helt til sidst har jeg prøvet at komme med et gæt til hvordan koden skal se ud.
Write a class SafeDivMult which contains methods
public static int safeIntDivide (int a, int b)
public static int safeIntMultiply (int a, int b)
This methods return the integer part of the division a=b respectively the product ab.
Both methods return 0 and write an error message in case that the respective operation
cannot be performed or would gives an incorrect result. Use exactly the names specied
above.
Test filen:
package aflop01_12;
public class SaveDivMultTest {
public static int[][] testDataDiv = {{9,2,4},{2,9,0},{-23, -4,5}};
public static int[][] testDataMul = {{-10,2,-20},{-7,-2,14},{1022333444, 2,2044666888}};
public static void main(String[] args) {
runTest();
}
private static void runTest() {
int count = 0;
for (int i = 0; i < testDataDiv.length; i++) {
count += testDiv(testDataDiv[i]);
}
System.out.println(count+" out of "+testDataDiv.length+" tests for division passed.");
System.out.println("---------------------------");
count = 0;
for (int i = 0; i < testDataMul.length; i++) {
count += testMul(testDataMul[i]);
}
System.out.println(count+" out of "+testDataDiv.length+" tests for multiplication passed.");
System.out.println();
}
private static int testDiv(int[] data) {
try{
int yourResult = SaveDivMult.safeIntDivide(data[0], data[1]);
if(yourResult == data[2]){
System.out.println("OK : "+data[0]+" / "+data[1] +" = "+data[2] );
return 1;
} else {
System.out.println("ERR : "+data[0]+" / "+data[1] +" = "+data[2]+" you = "+yourResult );
return 0;
}
}catch(Exception e){
System.err.println("ERR: Your program cused a runtime error");
return 0;
}
}
private static int testMul(int[] data) {
try{
int yourResult = SaveDivMult.safeIntMultiply(data[0], data[1]);
if(yourResult == data[2]){
System.out.println("OK : "+data[0]+" * "+data[1] +" = "+data[2] );
return 1;
} else {
System.out.println("ERR : "+data[0]+" * "+data[1] +" = "+data[2]+" you = "+yourResult );
return 0;
}
}catch(Exception e){
System.err.println("ERR: Your program cused a runtime error");
return 0;
}
}
}
Selve koden jeg har prøvet at skrive i eclipse:
package aflop01_12;
public class SaveDivMult {
public static int safeIntDivide (int a, int b) {
if (b==1){
return b;
} else {
return a;
}
}
public static int safeIntMultiply(int a, int b) {
if (a==1){
return a;
} else {
return b;
}
}
}
Håber der er en der kan hjælpe med at svare på opgaven.