Created
January 12, 2012 07:41
-
-
Save mluukkai/1599284 to your computer and use it in GitHub Desktop.
Taulukon läpikäyntejä
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void main(String[] args) { | |
| int[] luvut = {1, 2, 3, 4, 5, 6}; | |
| int summa = (Integer) (new Summa(luvut).tulos() ); | |
| System.out.println(summa); | |
| double keskiarvo = (Double)new Keskiarvo(luvut).tulos() ; | |
| System.out.println(keskiarvo); | |
| System.out.println( new Merkkijono(luvut).tulos() ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract class Lapikayja { | |
| int[] taulukko; | |
| public Lapikayja(int[] taulukko) { | |
| this.taulukko = taulukko; | |
| } | |
| void lapikay(int[] taulukko, Lapikayja lapikayja) { | |
| for (int luku : taulukko) { | |
| lapikayja.operoi(luku); | |
| } | |
| } | |
| Object tulos(){ | |
| lapikay(taulukko, this); | |
| return muodostaTulos(); | |
| } | |
| abstract void operoi(int luku); | |
| abstract Object muodostaTulos(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Summa extends Lapikayja { | |
| public Summa(int[] taulukko) { | |
| super(taulukko); | |
| } | |
| private int summa = 0; | |
| @Override | |
| public void operoi(int luku) { | |
| summa += luku; | |
| } | |
| @Override | |
| public Object muodostaTulos() { | |
| return summa; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Keskiarvo extends Lapikayja { | |
| public Keskiarvo(int[] taulukko) { | |
| super(taulukko); | |
| } | |
| private double summa = 0; | |
| private int n = 0; | |
| @Override | |
| public void operoi(int luku) { | |
| summa += luku; | |
| n++; | |
| } | |
| @Override | |
| public Object muodostaTulos() { | |
| return (summa/n); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Merkkijono extends Lapikayja { | |
| public Merkkijono(int[] taulukko) { | |
| super(taulukko); | |
| } | |
| private String mj = ""; | |
| @Override | |
| void operoi(int luku) { | |
| mj += luku + ", "; | |
| } | |
| @Override | |
| Object muodostaTulos() { | |
| return mj.substring(0, mj.length()-2); | |
| } | |
| } |
Author
erittäin hyvä huomio! pitää fiksata myöhemmin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Voishan tossa olla myös tyyppiparametrit.