RETT PÅ JAVA
     
 



     Eksempler og løsninger   
     Java og easyIO   
     Retteliste   
     Undervisning   
     Hjelp   
     Om boka   
     Forsiden   
                      



/*
 * Løsning på oppgave 3 og 4  i kapittel 10.
 * Klassene legges i en fil med navnet: Oppgvave3.java
 */

 import java.util.*;

 class Punkt {
	 double x, y;

	 Punkt() {
		 x = 0.0;
		 y = 0.0;
	}

	Punkt(double x, double y) {
		this.x = x;
		this.y = y;
	}

	Punkt [] lagPunktArray(double[] x, double[] y) {
		Punkt [] ret = null;
		if ( x.length == y.length) {
			ret = new Punkt[x.length];
			for (int i = 0 ; i< ret.length; i++ ) {
				ret[i] = new Punkt(x[i],y[i]);
			}
		}
		return ret;
	}// end lagPunktArray

	ArrayList lagPunktListe(double[] x, double[] y) {
			ArrayList ret = null;
			if ( x.length == y.length) {
				ret = new ArrayList(x.length);
				for (int i = 0 ; i< x.length; i++ ) {
					ret.add(new Punkt(x[i],y[i]));
				}
			}
			return ret;

	}// end lagPunktListe

	HashMap lagPunktMap(double[] x, double[] y) {
			HashMap ret = null;
			if ( x.length == y.length) {
				ret = new HashMap(x.length);
				for (int i = 0 ; i< x.length; i++ ) {
					String s = ""+i;
					ret.put(s,new Punkt(x[i],y[i]));
				}
			}
			return ret;

	}// end lagPunktMap

 } // end class Punkt


 class Oppgave3 {

	 public static void main (String [] args) {

		 Punkt p = new Punkt();
		 boolean feil = false;

		 double [] x = { 2.1,3.5,5.55, 6.6};
		 double [] y = { 0, 1,33.2, 1025.2};

		 Punkt [] pArray = p.lagPunktArray(x,y);
		 ArrayList ap    = p.lagPunktListe(x,y) ;
		 HashMap hp      = p.lagPunktMap(x,y);

		 for (int i = 0; i < x.length ; i++) {
			 Punkt pa = pArray[i];
			 Punkt pl = (Punkt) ap.get(i);
			 Punkt pm = (Punkt) hp.get(""+i);

			 if (pa.x != pl.x || pl.x != pm.x )
			     feil = true;
		 }

		  if (feil) System.out.println(" FEIL");
		  else System.out.println(" Alle testet - OK");

	 } // end main


 } // end class Oppgave3


 
Tilbake