Probleme bei der Abgabe

Status
Nicht offen für weitere Antworten.

LauraHe

Mitglied
Hallo!
Ich bin leider totaler Java Neuling und komm noch sehr schwer damit zurecht.Ich habe jetzt eine Aufgabe Programmiert bei der bei Eclipse auch alles passt und das richtige Ergebnis rauskommt. Wenn ich die Aufgabe jedoch über PABS-Abgabesystem abgeb kommt folgende Fehlermeldung:

1Test(s) saved.
Compilation successful.
Testsuite: ex11.tests.TestInterestCalculation
Tests run:3, Failures: 0, Errors: 1, Time elapsed: 0.038 sec

Testcase: testCalculateInterest took 0.008 sec
Testcase: testCompareInterest took 0.001 sec
Testcase: testPrintInterestComparison took 0.003 sec
Caused an ERROR
3
java.lang.ArrayIndexOutOfBoundsException: 3
at ex11.InterestCalculation.printInterestComparison(InterestCalculation.java:76)
at ex11.tests.TestInterestCalculation.testPrintInterestComparison(TestInterestCalculation.java:173)


Test ex11.tests.TestInterestCalculation failed


Java:
Meine Lösung sieht so aus:

/*Aufgabe 11a) 
 */
 
public class InterestCalculation {
	public static double [] calculateInterest (double amount,int years, double interest, boolean isCompound) {
	
		double[] Betrag = new double[years + 2];
		double Zinssatz = 1 + interest;
	
		if (amount==0 || interest <=0 || interest>1||years<1) 
			   return null;
	  
	 if(isCompound == true){
		
		   Betrag[0] = -0.5;
		   Betrag[1] = amount;
		   for(int i = 2; i < (Betrag.length); i++) {
			   Betrag[i] = round(Betrag[i - 1] * Zinssatz);
		
		   }
		   
		}
		
	  else {		   
		  Betrag[0] = -1;
		  Betrag[1] = amount;
	
	   for( int i = 2; i < (Betrag.length); i++) {
		   Betrag[i] = round(Betrag[i-1] +  (Betrag[1] * interest));
		
	   }
	  }
	    return Betrag ;
     }
    

	private static double round(double guthaben){
		return Math.round(guthaben*Math.pow(10, 2))/Math.pow(10, 2);
	}
	
/*Aufgabe 11 b)
 */
	
	  public static double[][] compareInterests(double interest_1, int years_1, 
         boolean isCompound_1,double interest_2, int years_2, boolean isCompound_2 ,double amount){
		  double[][]Vergleich = new double [2][2];
		  Vergleich[0][0]=Vergleich[0][1]=amount;
		  if (amount == 0)
			  return null;
		  if (interest_1 <= 0.0||interest_1 > 1.0||interest_2 <= 0.0||interest_2 > 1.0)
			  return null;
		  if (years_1 < 1||years_2 < 1)
			  return null;
		  if (interest_1 > 0 & interest_1 <= 1 & interest_2 > 0 & interest_2 <= 1){
			  Vergleich[0] = calculateInterest(amount,years_1,interest_1,isCompound_1);
			  Vergleich[1] = calculateInterest(amount,years_2,interest_2,isCompound_2);
			  }

			  return Vergleich;



		  }
	  
 /*Aufgabe 11 c)
  */
	  
	  public static String printInterestComparison(double [][]values){
		String Ergebnis=("Anfangsbetrag: "+ values[0][1]+"\n"+
         "Zinsentwicklung einfache Verzinsung: "+values[0][2]+","+values[0][3]+","+values[0][4]
         +","+values[0][5]+","+"\n\n"+
	 "Anfangsbetrag: " + values[1][1]+"\n"+"Zinsentwicklung Zinseszins: "+values[1][2]+","
         +values[1][3]+","+values[1][4]+","+values[1][5]+",");
		return Ergebnis;   
	  }
	  public static void main(String[] args) { 
		  double[][]ct=compareInterests(0.1,4,false,0.1,4,true,500);
		  System.out.println(printInterestComparison(ct));		  
	}
	  }
Kann mir vllt jemand weiterhelfen?
Wär echt nett🙂
 
Zuletzt bearbeitet von einem Moderator:
Also bei mir läuft der Code mit folgender Ausgabe:
Code:
Anfangsbetrag: 500.0
Zinsentwicklung einfache Verzinsung: 550.0,600.0,650.0,700.0,

Anfangsbetrag: 500.0
Zinsentwicklung Zinseszins: 550.0,605.0,665.5,732.05,
 
Ja genau das soll am ende dabei rauskommen aber warum wird er nicht angenommen bei PABS.Was ist denn der Fehler?
 
> Testcase: testPrintInterestComparison took 0.003 sec

vielleicht werden beim automatischen Tests andere Daten übergeben, z.B. ein Array mit der Länge 2 statt 3,
bekommst du eine Ausgabe? prüfe genau, was als Parameter übergeben wurde
 
Also wenn ich anstatt:
double[][]ct=compareInterests(0.1,4,false,0.1,4,true,500);

double[][]ct=compareInterests(0.1,6,false,0.1,6,true,500);

eingeb müssten am ende eigentlich 6 beträge rauskommen.Passiert aber nicht.Weiß aber auch nicht wo da jetzt der Fehler liegt.Find ihn im mom nicht🙁
 
liegt an deiner printInterestComparison ...

da berücksichtigst du ja nur bis maximal values[0][5] und values[1][5]. hat das array nun mehr werte, werden die ja garnicht abgefragt.
besser wäre das in einer schleife.
 
Muss das ganze in 30 min abgeben.Kannst du mir ausnahmsweise helfen und mir sagen wie man so eine schleife schreibt?Ich hab erst vor ein paar wochen mit programmieren angefangen und muss mich da erst langsam einarbeite.Hab davor auch noch nie was mit programmieren zu tun gehabt.Wär also echt voll nett wenn du mir hilfst🙂
 
ähm, ich habs selber nur ebend überflogen und das war das was mir aufgefallen ist.

/edit: na da jetzt eh schon lösungen gepostet sind, kann ich mein [ c o l o r = white] auch wegmachen ;D


Java:
public static String printInterestComparison(double[][] values) {
		String Ergebnis = "";
		for (int i = 0; i < values.length; i++) {
			Ergebnis += "Anfangsbetrag: " + values[i][1] + "\n";
			for (int j = 2; j < values[i].length; j++) {
				Ergebnis += values[i][j] + "\t";
			}
			Ergebnis += "\n\n";
		}
		return Ergebnis;
	}
 
Zuletzt bearbeitet:
Java:
public static String printInterestComparison(double [][]values){
		
	        String Ergebnis = ("Anfangsbetrag: "+ values[0][1]+"\n"+
"Zinsentwicklung einfache Verzinsung: "+values[0][2]+","+values[0][3]+","+values[0][4]+","+
values[0][5]+","+"\n\n"+ "Anfangsbetrag: " + values[1][1]+"\n"+
"Zinsentwicklung Zinseszins: "+values[1][2]+","+values[1][3]+","+values[1][4]+","+values[1][5]+",");;
	        for (int i = 1; i < values.length; i++) {
	            for (int j = 1; j < values[i].length; j++) {
	                Ergebnis+=values[i][j]+" ";
	            }
	        }
	        return Ergebnis;

	        }
Hab das jetzt abgegeben und dann wir folgendes als Fehler gemeldet:
1Test(s) saved.
Compilation successful.
Testsuite: ex11.tests.TestInterestCalculation
Tests run:3, Failures: 0, Errors: 1, Time elapsed: 0.024 sec

Testcase: testCalculateInterest took 0.006 sec
Testcase: testCompareInterest took 0.001 sec
Testcase: testPrintInterestComparison took -0.002 sec
Caused an ERROR
3
java.lang.ArrayIndexOutOfBoundsException: 3
at ex11.InterestCalculation.printInterestComparison(InterestCalculation.java:77)
at ex11.tests.TestInterestCalculation.testPrintInterestComparison(TestInterestCalculation.java:173)


Test ex11.tests.TestInterestCalculation failed
 
Zuletzt bearbeitet von einem Moderator:
Ja naja is halt sau ärgerlich wenn man ewig an was arbeitet und am ende steht man aufm schlauch und dadurch ist dann alles umsonst.wenn die Aufgabe nicht angenommen wird von dem System bekommt man 0 Punkte von 26 und die aufgabe is nur 5 wert oder so
 
schreibe
Java:
public static String printInterestComparison(double [][]values){
		
	        String Ergebnis = "";
	        for (int i = 1; i < values.length; i++) {
	            for (int j = 1; j < values[i].length; j++) {
	                Ergebnis+=values[i][j]+" ";
	            }
	        }
	        return Ergebnis;

	        }

----

Ja naja is halt sau ärgerlich wenn man ewig an was arbeitet und am ende steht man aufm schlauch und dadurch ist dann alles umsonst.wenn die Aufgabe nicht angenommen wird von dem System bekommt man 0 Punkte von 26 und die aufgabe is nur 5 wert oder so
in einem solchen Fall wäre selbst
return "keine Ahnung";
besser

ist doch sehr leicht, da zumindest eine Exception zu vermeiden,
Pech allerdings, wenn die einfache auch nicht die Anforderung erfüllt
 
Zuletzt bearbeitet von einem Moderator:
so folgende Abgabe und diesmal folgender Fehler:
Java:
public class InterestCalculation {
    public static double [] calculateInterest (double amount,int years, double interest, boolean isCompound) {
    
        double[] Betrag = new double[years + 2];
        double Zinssatz = 1 + interest;
    
        if (amount==0 || interest <=0 || interest>1||years<1) 
               return null;
      
     if(isCompound == true){
        
           Betrag[0] = -0.5;
           Betrag[1] = amount;
           for(int i = 2; i < (Betrag.length); i++) {
               Betrag[i] = round(Betrag[i - 1] * Zinssatz);
        
           }
           
        }
        
      else {           
          Betrag[0] = -1;
          Betrag[1] = amount;
    
       for( int i = 2; i < (Betrag.length); i++) {
           Betrag[i] = round(Betrag[i-1] +  (Betrag[1] * interest));
        
       }
      }
        return Betrag ;
     }
    

    private static double round(double Betrag){
        return Math.round(Betrag*Math.pow(10, 2))/Math.pow(10, 2);
    }
    
/*Aufgabe 11 b)
 */
    
      public static double[][] compareInterests(double interest_1, int years_1, boolean isCompound_1,double interest_2, int years_2, boolean isCompound_2 ,double amount){
          double[][]Vergleich = new double [2][2];
          Vergleich[0][0]=Vergleich[0][1]=amount;
          if (amount == 0)
              return null;
          if (interest_1 <= 0.0||interest_1 > 1.0||interest_2 <= 0.0||interest_2 > 1.0)
              return null;
          if (years_1 < 1||years_2 < 1)
              return null;
          if (interest_1 > 0 & interest_1 <= 1 & interest_2 > 0 & interest_2 <= 1){
              Vergleich[0] = calculateInterest(amount,years_1,interest_1,isCompound_1);
              Vergleich[1] = calculateInterest(amount,years_2,interest_2,isCompound_2);
              }

              return Vergleich;



          }
      
 /*Aufgabe 11 c)
  */
      
      public static String printInterestComparison(double[][] values) {
            String Ergebnis = "";
            for (int i = 0; i < values.length; i++) {
                Ergebnis += "Anfangsbetrag: " + values[i][1] + "\n";
                for (int j = 2; j < values[i].length; j++) {
                    Ergebnis += values[i][j] + "\t";
                }
                Ergebnis += "\n\n";
            }
            return Ergebnis;
        }



  

          
      

      public static void main(String[] args) { 
          double[][]ct=compareInterests(0.1,4,false,0.1,4,true,500);
          System.out.println(printInterestComparison(ct));        
    }
      }

1Test(s) saved.
Compilation successful.
Testsuite: ex11.tests.TestInterestCalculation
[Zinsentwicklung einfache Verzinsung: 11.0,

Anfangsbetrag: 10.0
Zinsentwicklung Zinseszins: 11.0,12.1,]

> but was:<Anfangsbetrag: 10.0
[11.0

Anfangsbetrag: 10.0
11.0 12.1 ]

>)
Tests run:3, Failures: 0, Errors: 1, Time elapsed: 0.031 sec

Testcase: testCalculateInterest took 0.008 sec
Testcase: testCompareInterest took 0.006 sec
Testcase: testPrintInterestComparison took 0.004 sec
Caused an ERROR
expected:<Anfangsbetrag: 10.0
[Zinsentwicklung einfache Verzinsung: 11.0,

Anfangsbetrag: 10.0
Zinsentwicklung Zinseszins: 11.0,12.1,]

> but was:<Anfangsbetrag: 10.0
[11.0

Anfangsbetrag: 10.0
11.0 12.1 ]

>
[Zinsentwicklung einfache Verzinsung: 11.0,

Anfangsbetrag: 10.0
Zinsentwicklung Zinseszins: 11.0,12.1,]

> but was:<Anfangsbetrag: 10.0
[11.0

Anfangsbetrag: 10.0
11.0 12.1 ]

>
at ex11.tests.TestInterestCalculation.testPrintInterestComparison(TestInterestCalculation.java:173)


Test ex11.tests.TestInterestCalculation failed
 
Zuletzt bearbeitet von einem Moderator:
Ich hab jetzt nur noch ein Problem:
public static String printInterestComparison(double [][]values){

String Ergebnis = ("Anfangsbetrag: "+ values[0][1]+"\n"+"Zinsentwicklung einfache Verzinsung: "+values[0][2]+","+values[0][3]+","+values[0][4]+","+values[0][5]+","+"\n\n"+
"Anfangsbetrag: " + values[1][1]+"\n"+"Zinsentwicklung Zinseszins: "+values[1][2]+","+values[1][3]+","+values[1][4]+","+values[1][5]+",");;
for (int i = 1; i < values.length; i++) {
for (int j = 1; j < values.length; j++) {
Ergebnis+=values[j]+" ";
}
}
return Ergebnis;

}
Wie bekomm ich die for schleife jetzt ins "String Ergebnis" rein?
 
was bedeutet es, 'eine for-Schleife in einen String zu bekommen'?

und verwende doch die Java-Tags..
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben