Dies ist eine statische Kopie unseres alten Forums. Es sind keine Interaktionen möglich.
This is a static copy of our old forum. Interactions are not possible.

kyo_udon

Praktikant

  • "kyo_udon" started this thread

Posts: 10

Date of registration: Sep 16th 2009

1

Thursday, September 17th 2009, 12:02am

Hilfe Java

please forgive me if i'm not allowed to ask this question here.

i need help in writing a generic method. this question is from the previous exam:


Dann soll für ein gegebenes Fahrzeug im Vector gesucht werden, ob dort ein gleichschweres
Fahrzeug enthalten ist. Als Algorithmus zur Suche benutzen Sie die Binäre Suche, zum vergleichen
benutzen Sie istSchwerer(). Erstellen Sie in der Applikation eine generische statische Methode
binaereSuche (Vector<T> vec, T a, int links, int rechts).
Der Typparameter T soll Fahrzeuge und Unterklassen erlauben. Der Methode wird ein Vector von T
und ein Element vom Typ T sowie die Indizes des aktuellen Intervalls übergeben. Die Methode gibt
das gefundene Element aus oder meldet erfolglose Suche.
Testen Sie Ihr Programm auf erfolgreiche/erfolglose Suche, sowie Suchen von Elementen am Anfang
und am Ende des Vectors.
Binäre Suche funktioniert folgendermassen: Beim Aufruf der Methode binaereSuche() werden die
Indizes (rechts/links) übergeben, innerhalb derer gesucht werden soll, anfänglich von Null bis
Länge des Vectors. Dann wird das zu suchende Element X mit dem Element M in der Mitte des
Vectors (Index: (links+rechts)/2) verglichen.
Es gibt vier Fälle. 1. Ist X==M, gebe X aus, fertig. 2. Falls die komplette Liste durchsucht wurde (also
links==rechts), auch fertig. 3./4. Falls sich X in der unteren bzw. oberen Hälfte des Vectors
befindet, ruft man die binäre Suche mit den Indizes: (links, (links+rechts)/2) bzw.: (1+
(links+rechts)/2, rechts) auf.


and there isnt any sintax error but it didnt work quiet the way it suppose tobe :


public static <T extends Auto> void binaereSuche(Vector<T> vec,T a,int l,int r){

int m = (l+r) /2;
int x = a.getGewicht();

if (vec.get(m).getGewicht() == x){
System.out.println(a);
}

if (l == m){
System.out.println("Es gibt kein gleichschweres Auto.");

}


if (a.istSchwerer(vec.get(m))){
binaereSuche(vec, a, (1+((l+r)/2)), r);
}

else
binaereSuche(vec, a, l, (l+r)/2);
}


once again. if i'm not allowed to ask this here please forgive me.
:)

DrChaotica

Senior Schreiberling

  • "DrChaotica" is male

Posts: 714

Date of registration: Jan 22nd 2005

Location: SHG

Occupation: SW-Entwickler

2

Thursday, September 17th 2009, 9:48am

hi,
looks good. with your check 'if (l == m){...}' there could be one element l+1 that you forget, better try 'if (l >= r){...}'.

Posts: 60

Date of registration: Oct 10th 2005

3

Thursday, September 17th 2009, 8:56pm

Hi,
i made very few changes to your code to make it work properly.
First you should never forget to check wether an object is null. It is a common beginner mistake. Maybe it is not that critical in this case, but you will damn every NPE you get, when the projects grow.
The actual problem seems to be that you forgot to stop the method after an object has been found or there are no more objects to compare. So you should put at least to void "return"-Statements after your "System.out.println" to stop the method.

If you don't find an object, though it is in the vector, you should remember that this version of "binaereSuche" expects an ordered vector. Your code actually works only if vector is descending ordered.

So here goes your modified code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static <T extends Auto> void binaereSuche(Vector<T> vec, T a, int l, int r) {
		int m = (l + r) / 2;
    	//check for NPE
		Auto auto = vec.get(m);
		if (auto != null) {
			int x = a.getGewicht();
			if (auto.getGewicht() == x) {
				System.out.println(a);
				return;
			}
        	//DrChaotica already mentioned
			if (l == r) {
				System.out.println("Es gibt kein gleichschweres Auto.");
				return;
			}
        	//works only fine, when order is descending
			if (a.istSchwerer(vec.get(m))) {
				binaereSuche(vec, a, (1 + ((l + r) / 2)), r);
			} else {
				binaereSuche(vec, a, l, (l + r) / 2);
			}
		}
	}

kyo_udon

Praktikant

  • "kyo_udon" started this thread

Posts: 10

Date of registration: Sep 16th 2009

4

Friday, September 18th 2009, 11:05pm

thanks ShadowScout....well yes.. i've forgotten to check if the object is null...and what i realise also is the return statement...i've use "else if" instead of "if"...and it should also stop after an object is found....but thanks again...i've never known we could put just "return;" with a void methode....thanks alot....^^