Oppgave 1
Her er det bare å "klippe og lime".
Oppgave 2
Metoden rangeCheck()
er kodet slik:
/* * Checks that fromIndex and toIndex are in * the range and throws an exception if they aren't. */ static void rangeCheck(int arrayLength, int fromIndex, int toIndex) { if (fromIndex > toIndex) { throw new IllegalArgumentException( "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); } if (fromIndex < 0) { throw new ArrayIndexOutOfBoundsException(fromIndex); } if (toIndex > arrayLength) { throw new ArrayIndexOutOfBoundsException(toIndex); } }
Oppgave 3 - føste del
public static int maks(int[] a, int fra, int til) { fratilKontroll(a.length,fra,til); if (fra == til) throw new NoSuchElementException ("fra(" + fra + ") = til(" + til + ") - tomt tabellintervall!"); int m = fra; // indeks til største verdi i a[fra:til> int maksverdi = a[fra]; // største verdi i a[fra:til> for (int i = fra + 1; i < til; i++) if (a[i] > maksverdi) { m = i; // indeks til største verdi oppdateres maksverdi = a[m]; // største verdi oppdateres } return m; // posisjonen til største verdi i a[fra:til> }
Oppgave 3 - andre del
Her er det satt opp flere mulige kall. For å få feilmeldinger av samme type som nedenfor, må ett og ett av kallene utføres:
public static void main(String[] args) throws IOException { int[] a = Tabell.randPerm(10); int[] c = null; Tabell.maks(a,-1,10); //Tabell.maks(a,0,11); //Tabell.maks(a,10,0); //Tabell.maks(a,0,0); //Tabell.maks(c,0,0); } // Feilmeldinger: // ArrayIndexOutOfBoundsException: fra(-1) er negativ! // ArrayIndexOutOfBoundsException: til(11) > tablengde(10) // IllegalArgumentException: fra(10) > til(0) - illegalt intervall! // NoSuchElementException: fra(0) = til(0) - tomt tabellintervall! // NullPointerException
Oppgave 4
public static int maks(int[] a, int fra, int til) { if (a == null) throw new NullPointerException ("parametertabellen a er null!"); fratilKontroll(a.length,fra,til); if (fra == til) throw new NoSuchElementException ("fra(" + fra + ") = til(" + til + ") - tomt tabellintervall!"); int m = fra; // indeks til største verdi i a[fra:til> int maksverdi = a[fra]; // største verdi i a[fra:til> for (int i = fra + 1; i < til; i++) if (a[i] > maksverdi) { m = i; // indeks til største verdi oppdateres maksverdi = a[m]; // største verdi oppdateres } return m; // posisjonen til største verdi i a[fra:til> }
Oppgave 5
public static void skriv(int[] a, int fra, int til) { fratilKontroll(a.length,fra,til); for (int i = fra; i < til; i++) System.out.print(a[i] + " "); } public static void skrivln(int[] a, int fra, int til) { fratilKontroll(a.length,fra,til); for (int i = fra; i < til; i++) System.out.print(a[i] + " "); System.out.println(); } public static void skriv(char[] c, int fra, int til) { fratilKontroll(c.length,fra,til); for (int i = fra; i < til; i++) System.out.print(c[i] + " "); } public static void skrivln(char[] c, int fra, int til) { fratilKontroll(c.length,fra,til); for (int i = fra; i < til; i++) System.out.print(c[i] + " "); System.out.println(); }
Oppgave 6
public static void snu(int[] a, int v, int h) { vhKontroll(a.length,v,h); while (v < h) bytt(a,v++,h--); } public static void snu(int[] a) { int v = 0, h = a.length-1; while (v < h) bytt(a,v++,h--); }
Oppgave 7
public static void snu(char[] a, int v, int h) { vhKontroll(a.length,v,h); while (v < h) bytt(a,v++,h--); } public static void snu(char[] a) { int v = 0, h = a.length-1; while (v < h) bytt(a,v++,h--); }