1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.io.File;
-
- class Ordliste {
- private ArrayList<Ord> ordliste = new ArrayList<Ord>();
-
- public Ordliste() {};
-
- public void lesBok(String filnavn) throws Exception {
- Scanner s = new Scanner(new File(filnavn));
-
- while (s.hasNextLine()) {
- leggTilOrd(s.nextLine());
- }
- }
-
- //This method should, according to the task, be private. However,
- //it's cruicial for TestOrdliste.java, which the task also told to write.
- //Changing 'public' to 'private' here works perfectly, as long as you don't
- //try to use it with TestOrdliste.java.
- //If that was an important part of the task, pretend this method is private.
- public void leggTilOrd(String tekst) {
- Ord ord = finnOrd(tekst);
-
- if (ord == null)
- ordliste.add(new Ord(tekst.toLowerCase()));
- else
- ord.oekAntall();
- }
-
- public Ord finnOrd(String tekst) {
- for (Ord ord: ordliste) {
- if (ord.toString().equals(tekst.toLowerCase()))
- return ord;
- }
-
- return null;
- }
-
- public int antallOrd() {
- return ordliste.size();
- }
-
- public int antallForekomster(String tekst) {
- Ord ord = finnOrd(tekst);
- if (ord == null)
- return 0;
- else
- return ord.hentAntall();
- }
-
- public Ord vanligste() {
- if (ordliste.size() == 0)
- return null;
-
- Ord vanligste = ordliste.get(0);
-
- for (Ord ord: ordliste) {
- if (ord.hentAntall() > vanligste.hentAntall())
- vanligste = ord;
- }
-
- return vanligste;
- }
-
- public Ord[] alleVanligste() {
- if (ordliste.size() == 0)
- return new Ord[0];
-
- ArrayList<Ord> vanligste = new ArrayList<Ord>();
- vanligste.add(ordliste.get(0));
-
- for (Ord ord: ordliste) {
- if (ord.hentAntall() > vanligste.get(0).hentAntall()) {
- vanligste = new ArrayList<Ord>();
- vanligste.add(ord);
- } else if (ord.hentAntall() == vanligste.get(0).hentAntall()) {
- vanligste.add(ord);
- }
- }
-
- Ord[] arr = new Ord[vanligste.size()];
- return vanligste.toArray(arr);
- }
- }
|