Java II - 1 0HUDRPJUlQVVQLWW (WWJUlQVVQLWWJDUDQWHUDUHQYLVV IXQNWLRQDOLWHWPRWGHVVRPJLYQLQJ *UlQVVQLWW (WWUHDOLVWLVNWH[HPSHOJava II - 2 6RUWHULQJ (QVRUWHULQJVDOJRULWPNDQLQWHVMlOY DYJ|UDYLONHWNULWHULHVRPHQPlQJGVND VRUWHUDVHIWHU *UlQVVQLWW 2EMHNW ,PSOHPHQWDWLRQ DY JUlQVVQLWWHW (WWDYREMHNWHQ VRPVNDVRUWHUDV ,PSOHPHQWDWLRQ DY JUlQVVQLWWHW 6RUWHUD HIWHU YLNW L IDOODQGH RUGQLQJ 'LVWULEXHUDW V\VWHP )|QVWHU V\VWHP 1nJRQDQQDQW\S DYWLOOlPSQLQJ 6RUWHULQJV DOJRULWP 1 .ODVVI|UDWWWHVWD VRUWHULQJVDOJRULWPHQ Java II - 3 6RUWHULQJVDOJRULWPHQ (QVRUWHULQJVDOJRULWP //-Fil:Sortera.java--------------------------------------class Sortera { public static void bubbel(Sorteringsbar[] element) { boolean klart = false; //Sant när allt är sorterat Java II - 4 (IWHUYLONDHJHQVNDSHUVNDREMHNWHQ VRUWHUDV" public final class Test { public static void main(String[] argument) { Arbetare[] anställd = new Arbetare[5]; while (!klart) { klart = true; anställd[0] anställd[1] anställd[2] anställd[3] anställd[4] for (int i=0; i<element.length - 1; i++) { Sorteringsbar temp = element[i]; = = = = = new new new new new Arbetare("Gustaf", 203000); Arbetare("Kajsa", 197000); Arbetare("Lotta", 356000); Arbetare("Fredrik", 245000); Arbetare("Lars", 321000); Sortera.bubbel(anställd); for (int i=0; i<anställd.length; i++) anställd[i].print(); if (temp.mindre(element[i+1])) { element[i] = element[i+1]; element[i+1] = temp; klart = false; } Student[] elev = new Student[4]; elev[0] elev[1] elev[2] elev[3] } } } } //-------------------------------------------------------- = = = = new new new new Student("Erik", "spelar jazz."); Student("Karl", ”åker skidor."); Student("Stina", "läser böcker."); Student("Karin", "reser mycket."); Sortera.bubbel(elev); for (int i=0; i<elev.length; i++) elev[i].print(); } } 2 6XSHUNODVVHQRFK JUlQVVQLWWHW .ODVVWLOOREMHNWVRP VNDVRUWHUDV Java II - 5 Java II - 6 6WXGHQW RFK $UEHWDUH lUYHU DY 3HUVRQ $UEHWDUH LPSOHPHQWHUDU JUlQVVQLWWHW RFK WDODU GlUPHG RP KXU REMHNW DY $UEHWDUH VND VRUWHUDV //-Fil:Person.java---------------------------------------public class Person { protected String name; //-Fil:Arbetare.java-------------------------------------public class Arbetare extends Person implements Sorteringsbar { private int inkomst; Person(String namn) { name = namn; } public void print() { System.out.println("Person: " + name); } } //-------------------------------------------------------- *UlQVVQLWW VRP JDUDQWHUDU HQ HJHQVNDS DWW VRUWHUD HIWHU Arbetare(String namn, int inkomst) { super(namn); //M†ste skrivas p† f”rsta raden! this.inkomst = inkomst; } public void print() { super.print(); System.out.println("Inkomst: " + inkomst + " kr\n"); } public boolean mindre(Sorteringsbar element) { Arbetare jobbare = (Arbetare)element; return inkomst < jobbare.inkomst; //-Fil: Sorteringsbar.java-------------------------------- interface Sorteringsbar { public boolean mindre(Sorteringsbar element); } } } //-------------------------------------------------------- //-------------------------------------------------------- 3 (QDQQDQNODVVWLOOREMHNW VRPVNDVRUWHUDV Java II - 7 6WXGHQW LPSOHPHQWHUDU JUlQVVQLWWHW RFK WDODU GlUPHG RP KXU REMHNW DY 6WXGHQW VND VRUWHUDV //-Fil:Student.java--------------------------------------public class Student extends Person implements Sorteringsbar { private String hobby; Student(String namn, String hobby) { super(namn); this.hobby = hobby; } public void print() { super.print(); System.out.println("Hobby: " + hobby + "\n"); } public boolean mindre(Sorteringsbar element) { Student elev = (Student)element; if (name.compareTo(elev.name) < 0) return true; else return false; } } //-------------------------------------------------------- Java II - 8 *UlQVVQLWWH[HPSHO Utskrift Person: Lotta Inkomst: 356000 kr Person: Lars Inkomst: 321000 kr Person: Fredrik Inkomst: 245000 kr Person: Gustaf Inkomst: 203000 kr Person: Kajsa Inkomst: 197000 kr Person: Stina Hobby: läser böcker Person: Karl Hobby: åker skidor Person: Karin Hobby: reser mycket Person: Erik Hobby: spelar jazz 4 Java II - 9 Java II - 10 5HGLJHULQJDYWDO ,PSRUWHUD SDNHWHW java.text.*; 6NDSD HQ LQVWDQV DY HQ UHGLJHUDUH 5HGLJHULQJDYWDO (WW H[HPSHO import java.text.*; NumberFormat r = NumberFormat.getInstance(); $QURSD PHWRGHQ IRUPDW I|U DWW RPYDQGOD HWW WDO µ[µ WLOO WH[W r.format(x); class Tid { private int timmar = 3; private int minuter = 24; private int sekunder = 7; public String toString() { NumberFormat nf = NumberFormat.getInstance(); 5HGLJHULQJVHJHQVNDSHU VlWWV PHG PHWRGHUQD r.setMaximumFractionDigits(n); //högst n decimaler r.setMaximumIntegerDigits(n); //högst n heltalssiffror r.setMinimumFractionDigits(n); //minst n decimaler r.setMinimumIntegerDigits(n); //minst n heltalssiffror r.setGroupingUsed(true); //siffrorna ska grupperas r.setGroupingUsed(true); //siffrorna ska inte grupperas nf.setMinimumIntegerDigits(2); String tid = nf.format(timmar) + ”:” + nf.format(minuter) + ”:” + nf.format(sekunder); return tid; } } Ger följande sträng 03:24:07 5 Java II - 11 Java II - 12 /lVQLQJRFKVNULYQLQJ 9LG OlVQLQJ RFK VNULYQLQJ DQYlQGV QnJRW VRP NDOODV 8WVNULIW 6\VWHPRXWKDUI|OMDQGHPHWRGHU VWU|PPDU 0HWRGHU VRP VNULYHU HWW REMHNW HOOHU YDULDEHO DY (Q VWU|P lU HWW IO|GH DY GDWD VRP VWU|PPDU LQ DQJLYHQ W\S WLOO XWVWU|PPHQ HOOHU XW IUnQ HWW SURJUDP print(Object), print(String), print(char[]), print(char), print(int), print(long), print(float), print(double), print(bolean) 0DQ VNLOMHU Sn LQVWU|PPDU RFK XWVWU|PPDU .ODVVHQ 6\VWHP L SDNHWHW java.lang LQQHKnOOHU 0HWRGHU VRP VNULYHU HWW REMHNW HOOHU YDULDEHO DY WUH ROLND VWU|PPDU GHILQHUDGH VRP DQJLYHQ W\S WLOO XWVWU|PPHQ RFK DYVOXWDU PHG HWW NODVVYDULDEOHU Q\UDGVWHFNHQ System.in System.out System.err println(Object), println(String), println(char[]), println(char), println(int), println(long), println(float), println(double), println(bolean) • System.in lU HWW InputStream. VWDWLVNW REMHNW DY NODVVHQ • System.out RFK System.err DY NODVVHQ PrintStream lU VWDWLVND REMHNW %nGH System.out NRSSODGH PRW System.err EUXNDU YDUD VNlUPHQ PHGDQ System.in EUXNDU RFK YDUD NRSSODG PRW WDQJHQWERUGHW ([HPSHO System.out.print(”Vad heter du: ”); System.out.flush(); 6WUlQJHQ VNULYV LQWH XW GLUHNW XWDQ PHOODQODJUDV L HQ XWGDWDEXIIHUW %XIIHUWHQ NDQ W|PPDV PHG KMlOS DY PHWRGHQ flush )XQNWLRQHQ println IOXVKDU EXIIHUWHQ DXWRPDWLVNW 6 Java II - 14 (WWH[HPSHOSn LQOlVQLQJDYHQKHOUDG Java II - 13 ,QOlVQLQJ 'DWD VRP VNULYV Sn WDQJHQWERUGHW KDPQDU L import java.io.*; VWU|PPHQ 6\VWHPLQ VRP lU HWW REMHNW DY InputStream L SDNHWHW java.io 'HW JnU LQWH DWW GLUHNW OlVD IUnQ 6\VWHPLQ XWDQ GHQ VWU|PPHQ PnVWH NRSSODV WLOO HQ DQQDQ VWU|P System.in (InputStream) (InputStreamReader) MinaInData (BufferedReader) public class Rad { public static void main(String[] argument) throws IOException { BufferedReader MinaInData = new BufferedReader(new InputStreamReader(System.in)); String namnet; System.out.print("Vad heter du: "); System.out.flush(); BufferedReader MinaInData = new BufferedReader( new InputStreamReader(System.in)); namnet = MinaInData.readLine(); System.out.println("Hej, " + namnet + "!"); , NODVVHQ %XIIHUHG5HDGHU ILQQV HQ PHWRGHQ } readLine } Exempel på exekvering ==> java Rad Vad heter du: Hans-Edy Mårtensson Hej, Hans-Edy Mårtensson! ==> 7 (WWH[HPSHOSn LQOlVQLQJDYWDO Java II - 15 ,QOlVQLQJDYWDO 7\YlUU Vn LQQHKnOOHU LQWH NODVVHQ %XIIHUHG5HDGHU LQJD PHWRGHU I|U DWW RPYDQGOD WLOO VWUlQJDU WLOO QXPHULVND GDWD 'HW ILQQV IOHUD ROLND WHNQLNHU I|U DWW RPYDQGOD HQ VWUlQJ WLOO HWW WDO (WW VlWW EHVNULYV KlU QHGDQ 2PYDQGOLQJHQ NDQ VNH PHG KMlOS DY GH SULPLWLYD import java.io.*; public class Tal { public static void main(String[] argument) throws IOException { BufferedReader MinaInData = new BufferedReader( new InputStreamReader(System.in)); GDWDW\SHUQDV RPVODJVNODVVHU ZUDSSHUV System.out.print("Skriv ett heltal: "); System.out.flush(); String textRad = MinaInData.readLine(); textRad = textRad.trim(); //Rensar bort blanktecken String textRad = minaInData.readLine(); int mittHeltal = Integer.parseInt(textRad); , GHW RYDQVWnHQGH H[HPSOHW DQYlQGV PHWRGHQ int helTalet = Integer.parseInt(textRad); System.out.println("Heltalet „r: " + helTalet); parseInt L RPVODJVNODVVHQ Integer I|U DWW RPYDQGOD VWUlQJHQ textRad WLOO PRWVYDUDQGH System.out.print("Skriv ett decimaltal: "); System.out.flush(); textRad = MinaInData.readLine(); textRad = textRad.trim(); KHOWDO Java II - 16 )|U DWW RPYDQGOD HWW VWUlQJ WLOO PRWVYDUDQGH GHFLPDOWDO PnVWH PDQ Jn WLOOYlJD Sn HWW QnJRW DQQRUOXQGD VlWW double decimalTalet = Double.valueOf(textRad).doubleValue(); String textRad = minaInData.readLine(); int mittDecimaltal = Double.valueOf(textRad).doubleValue(); System.out.println("Decimaltalet „r: " + decimalTalet); } } 8 -DYDLRNODVVHUVRP OlVHUE\WHVWU|PPDU Java II - 17 -DYDLRNODVVHUVRP Java II - 18 VNULYHUE\WHVWU|PPDU InputStream OutputStream SequenceInputStream FileInputStream PipedInputStream ByteArrayInputStream FileOutputStream PipedOutputStream FilterInputStream DataInputStream FilterOutputStream PushBackInputStream BufferedInputStream ByteArrayOutputStream DataOutputStream PrintStream BufferedOutputStream 9 -DYDLRNODVVHUVRP Java II - 19 OlVHUWHFNHQVWU|PPDU -DYDLRNODVVHUVRP VNULYHUWHFNHQVWU|PPDU Reader Writer PipedReader InputStreamReader OutputStreamWriter BufferedWriter CharArrayReader FileReader BufferedReader Java II - 20 FileWriter FilterReader PipedWriter StringReader LineNumberReader PushbackReader FilterWriter CharArrayWriter PrintWriter StringWriter 1 Java II - 21 7H[WILOHU )|U DWW OlVD HOOHU VNULYD HQ ILO VNDOO PDQ L -DYD NRSSOD (WWH[HPSHOSnDFFHVVJava II - 22 DYWH[WILOHUGHO import java.io.*; HQ VWU|P WLOO ILOHQ 2P PDQ VNDOO KDQWHUD ELQlUD GDWD Vn YlOMHU PDQ OlPSOLJD VXENODVVHU IUnQ VXSHUNODVVHQD HOOHU 2P PDQ VNDOO KDQWHUD WHFNHQ Vn YlOMHU PDQ OlPSOLJD VXENODVVHU IUnQ VXSHUNODVVHQD FileReader HOOHU FileWriter , SURJUDPPHW E|U PDQ LQWH OlVD GLUHNW IUnQ HQ VWU|P FileReader XWDQ BufferedReader DY NODVVHQ Reader HOOHU Writer 2P PDQ YLOO NRSSOD HQ ILO WLOO HQ WHFNHQVWU|P NDQ PDQ DQYlQGD NODVVHUQD InputStream OutputStream L VWlOOHW YLD 3n OLNQDGH VlWW DQYlQGHU PDQ NODVVHUQD BufferedWriter RFK FileWriter YLG VNULYQLQJ WLOO HQ WH[WILO )|U DWW In WLOOJnQJ WLOO PHWRGHUQD print RFK println Vn NDQ PDQ I|UVW OnWD VLQ WHFNHQVWU|P SDVVHUD PrintWriter public class Kopiera { public static void main(String[] argument) throws IOException, FileNotFoundException { //Vi skapar en indataström från tangentbordet BufferedReader inStrom = new BufferedReader( new InputStreamReader(System.in)); //Vi skapar en teckenström från den fil som ska //kopieras System.out.print("Infilens namn? : "); System.out.flush(); String filNamnet = inStrom.readLine(); BufferedReader inFil = new BufferedReader(new FileReader(filNamnet)); //Vi skapar en teckenström till den nya kopian System.out.print("Utfilens namn? : "); System.out.flush(); filNamnet = inStrom.readLine(); PrintWriter utFil = new PrintWriter( new BufferedWriter(new FileWriter(filNamnet))); //Programmet forsätter på nästa sida 1 (WWH[HPSHOSnDFFHVVJava II - 23 DYWH[WILOHUGHO //Och nu kopierar vi den ena filen till den andra int n = 0; while (true) { String rad = inFil.readLine(); if (rad == null) break; utFil.println(rad); n++; } utFil.close(); inFil.close(); System.out.println(n + " rader kopierade"); } } 1