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YQHW0cY UHOLN.ODVVHQDYQHWWLOGHQHQHNODVVHQSnILOHQ
VRPHUSXEOLFRJVRPUHVWHQDYV\VWHPHWKDUGLUHNWHDGJDQJWLO
❙ )LOXWYLGHOVHQ MDYD
GYVHQILOPHGNODVVHQ¶7HVW¶KHWHU7HVWMDYD
.RPSLOHULQJRJNM¡ULQJ
❚ 1nUGXODJHUHWSURJUDP
1%SURJUDPILOHQKHWHUGHWVDPPH
VRPNODVVHQ
©Arne Maus, aug. 1999
10
❚ ,QQHKROGHUHQPHWRGHSURVHG\UH
public static void main(..)LQQHLHQ
EUXNHUGHILQHUWNODVVH,QWHWNUDYWLOQDYQHWSnGHQ
NODVVHQ
❚ PDLQNDOOHVYHGDYNM¡UHV\VWHPHWYHGVWDUW
❚ .M¡UHPHGYDQOLJXWVNULIWWLOOLQMHRULHQWHUWVNMHUPHOOHUWLO
*8,EDVHUWVNMHUP$:7 YLQGXHURJNQDSSHU
❚ .DQOHVHVNULYHILOHUGDWDEDVHURO
❚ -DYDHUVRPDQGUHSURJUDPPHULQJVVSUnN
/DJHYDQOLJHSURJUDPPHU
DSSOLFDWLRQ
E:\java-unix>java Test
Arne: 3 * 4 er:12
E:\java-unix>javac Test.java
Kjøring:
}
protected int xx (int i)
{ return i*3;}
public class Test
{
public static void main (String [ ] args )
{
test am = new Test( );
System.out.println("Arne: 3 * 4 er:" + am.xx(4) );
}
//Fil: Test.java
import java.io.*;
Kildefil: Test.java (ant. byte: 247)
©Arne M aus, sept1999
❚ 'HWWHJMHOGHULNNHSULQWOQ«VRPµIOXVKHU¶VHOY
System.out.print(”Gi neste tall:”);
System.out.flush();
❚ +YLVGXEUXNHUIXQNVMRQHQSULQWVnPnGXRJVn
µIOXVKH¶XWVNULIWVHOYL:LQHNV
26
1%0nJM¡UHVL-DYDSn:LQHOOHU
:LQ
`0` ´`−()Ljava/lang/String;´`˜()
V´`¯(I)I´` (I)Ljava/lang/StringBuffer;
´` (Ljava/lang/String;)V´`
([Ljava/lang/String;)V´`˙
<init>´`flArne: 3 * 4 er:´`¯
Code´`
ConstantValue´`
Exceptions´`flLineNumberTable
´` Ljava/io/PrintStream;´`
De mest interessante delene av Test.class (=Bytekode, totalt 693 byte)
©Arne Maus, aug. 1999
❙ /DJHWSURJUDPVRPVNULYHUXWGHWUHI¡UVWHDUJXPHQWHQHIUD
NRPPDQGROLQMHQ
❚ +MHPPHRSSJDYH
❙ 7DXWJDQJVSXQNWLGHWI¡UVWHSURJUDPPHWRJODJHWHQNOHUH
SURJUDP7HVWVRPVNULYHUXWGLWWQDYQYHUGLHQDYHW
GHNODUHUWKHOWDOO¶M¶
❚ 2SSJDYHQnPLQ
14
2SSJDYH
©Arne Maus, aug. 1999
15
NRPSLOHUHUNLOGHNRGH!%\WHNRGHGYVMDYDILOWLO
HQFODVVILOIRUKYHUNODVVHSnMDYDILOHQ
!MDYD
XWI¡UH%\WHNRGHLQWHUSUHW
!MUH
HQNOHUHXWJDYHDYµMDYD¶
!DSSOHWYLHZHUEUXNHVIRUnNM¡UHWHVWH$SSOHWV
!MGE
GHEXJJHU
!MDYDS
GLVVDVVHPEOHUHUW\GHUHQFODVVILO
!MDYDGRFODJHUKWPOGRNXPHQWDVMRQIUDHQMDYDILOHOOHUKHOVW
DYHQSDNNH
!MDU
ODJHUHQMDUILODYIOHUHFODVVILOHUGYVDYHQSDNNH
HQGDIOHUH
!MZV
JUDILVNXWYLNOLQJVYHUW¡\DOD9LVXDO&
!MDYDF
MGNPDQJH¶YHUNW¡\·LHQNDVVH
YLEUXNHUEDUHQRHQ
©Arne Maus, aug. 1999
16
❙ VLNULQJPRWYLUXV
❙ .M¡UHULHQHJHQEHVN\WWHW¶VLNNHUKHWV¶WRONHUVDQGNDVVHLQHWWOHVHU
❙ 'HWWHNDOOHV¶6DQGNDVVHQ¶HUHQGUHWXWYLGHWLYHU
❚ +HQVLNW/DJHOLWHSURJUDPVRPNDQVWDUWHVIUDHQKWPOVLGH
❚ (QDYNODVVHQHHUVXENODVVHDYNODVVHQ$SSOHWVRPKHQWHV
IUDHWELEOLRWHN¶DSSOHW¶
❚ /DVWHVVnRSSDYHQ:::VLGHNRGHWL¶KWPO¶RJNM¡UHUGHU
❚ (QDSSOHWNDQEODLNNHJM¡UH,2PRWILOHUSJD
/DJHHQ:HEVQXWWDSSOHW
<applet
code=Applet1
width=500
height=200>
</applet>
Tilhørende lagt inn på en ’html-side’(fil: xyz.html):
public class Applet1 extends Applet {
public void paint(Graphics g) {
g.drawString("Arnes første applett", 10,
10);
}
}
import java.awt.*;
import java.applet.*;
//App1et1.java
Applet -eksempel
©Arne Maus, aug. 1999
%¡QQHUOLWHLNNHGHNNHWDYLQ
❙ 6WDQGDUGLVHUWJUHQVHVQLWWJM¡UHGHWPXOLJOHWWHUHIRUDQGUH
NRPSRQHQWHUEHDQVnVQDNNHPHGEUXNHGHQQH¶E¡QQD¶
❚ HQKYHUNODVVHNDQY UHHQ¶EHDQ¶PHQ
18
❙ IRUKYHUHJHQVNDS;;;IHNVGDWDDYW\SHQLQWIORDW6WULQJ
HQVHWRJHQJHWPHWRGHJHW;;;
❙ JHQHUHOOHDGGRJUHPRYHPHWRGHUIRUXOLNEUXNHU,2PXVRO
❙ HJHQGHILQHUWHSXEOLFPHWRGHUSUREOHPVSHVLILNNH
❙ JLULQIRRPVLWWHJHWJUHQVHVQLWWG\QDPLVN
❚ /DJHNRPSRQHQWHUPHGYHOGHILQHUWHJUHQVHVQLWWVRPSDVVHU
LQQVRPGHODYHWDQQHWVW¡UUHV\VWHP
❚ (QMDYDE¡QQHKDU
/DJHNRPSRQHQWHUEHDQV
©Arne Maus, aug. 1999
0DQJHPnWHUnEUXNHQHWWHWSnL-DYD
❚ $SSOHWYLD+70/VLGH
❚ &*,
❚ 6RFNHWV
❚ 50,5HPRWH0HWKRG,QYRFDWLRQ
❚ 6HUYOHW-'%&
❚ '&20
❚ &25%$
19
-DYDRJQHWWHW
+YLONHWVSUnNGHXOLNHODJHQHHUVNUHYHWL
KYLONHRS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OHULQJPHQ,..(M N
M QHZ,QWHJHU,QWHJHUNLQW9DOXH2.NRPSLOHULQJKXVNSDUHQWHVHQ
0(1JLUNM¡UHIHLOKYLVN,..(YDUHQ,QWHJHU
2EMHFWN
❚ 9LNDQVHOYVDJWSHNHSnREMHNWHUYLHUGHNODUHUWVRPSHNHUHWLO
❚ 9LNDQSU¡YHnSHNHSnREMHNWHUVRPHUVXENODVVHDYYnUW\SHPHQGHW
NDQJLNM¡UHWLGVIHLO
❚ ¶QXOO¶HUVRP6LPXOD¶QRQH¶
❚ 3HNHUYLSnVXSHUW\SHREMHNWHUPnYLJM¡UHW\SHNRQYHUWHULQJ
,QWHJHUM
❙ LNNHORNDOW¶LQOLQH¶VRP&NDQ
❚ $OOHREMHNWHUOLJJHUSn¶KHDS¶HQGYV
$OOWLGDGJDQJYLD·KDQGOHV·KnQGWDN
WLOREMHNWHU
©Arne Maus, aug. 1999
public static void main
( String[] args)
{
int i = 0;
biltest b = new biltest( );
b.make_queue( );
}
public class biltest{
Bil first = new Bil( );
Bil sist = first;
Bil refA;
class Bil{
Bil neste ;
int reg_nr = 0;
}
// fil: biltest.java
import java.io.*;
24
Kjøring:
E:\Sun-NT-Java\PC-Java>java biltest
Start test
Slutt test, antall biler er : 1001
} //
}
for (i= 1; i <= 1000; i++){
refA = new Bil( );
sist.neste = refA;
sist = sist.neste;
}
System.out.println
("Slutt test, antall biler er : ” + i);
void make_queue ( )
{ int i;
System.out.println("Start test");
9LPnODJHDOOHREMHNWHUPHGQHZ
©Arne Maus, aug. 1999
3URYLGHVVLPSOHLQSXWRXWSXWIURPWKHNH\ERDUGDQGILOHV
FODVV7H[WLµ-DYD*HQWO\¶ERNDHUIRUHWDQQHWIRUPnO
❙ LNNHVnP\HEUXNW
6WULQJ%XIIHUVE QHZ6WULQJ%XIIHU+HLKnQnHUGHWVQDUWS¡VNH
VEVHW&KDU$WVEOHQJWK
n
❚ NODVVHQ6WULQJ%XIIHUWHNVWHUVRPNDQHQGUHV
❘ EUXNHVP\H
❙ 6WULQJV6 ³+HLKnQnHUGHWMXOLJMHQ´
V 6³GHWHUYHOQRHRYHUGUHYHW´
❚ NODVVHQ6WULQJWHNVWNRQVWDQWHUNDQLNNHHQGUHV
❙ FKDUEXI>@ QHZFKDU>@
❚ &KDUDFWHUDUUD\
25
7HNVWHUL-DYD
©Arne Maus, aug. 1999
public static void main
( String[ ] args){
new astest( ).test( );}
// array av string-pekere
String [ ] sa;
public class astest{
int [ ] a = new int [100];
Bil [ ] b = new Bil [14];
String s1 =”Hei, hei”;
class Bil{
Bil neste ;
int reg_nr = 0;
}
// fil: astest.java
import java.io.*;
}
} //
26
if ( sa[0] != null) System.out.
println(“sa[0].length( ) = “ + sa[0].length( ) );
else System.out.println(“Null-peker”);
System.out.println("a.length = “ + a.length );
System.out.println("b.length = “ + b.length );
System.out.println(“s1=” + s1+ ",
s1.length ( ) = “ + s1.length( ));
sa = new String [17];
System.out.println("sa.length = “ + sa.length );
System.out.println(“sa[0]=” + sa[0] );
void test( ) {
for (int i = 0; i < b.length ; i++)
b[i] = new Bil();
$UUD\HURJ6WULQJHUHJQHREMHNWHU
$UUD\OHQJWKRJ6WULQJOHQJWK
©Arne Maus, aug. 1999
E:\Sun-NT-Java\PC-Java>
E:\Sun-NT-Java\PC-Java>java astest
a.length = 100
b.length = 14
s1=Hei, hei, s1.length() = 8
sa.length = 17
sa[0]=null
Null-peker
27
.M¡ULQJ
©Arne Maus, aug. 1999
28
❙ 7DUWLGFDWLOOHJJSnNM¡UHWLGDRJNDQJL¶U\NNHWH¶RSSI¡UVHO
YHGLQWHUDNWLYEUXN
❚ /LWHQXOHPSH
❙ (WNM¡UHQGHSURJUDP¶YRNVHULNNH¶SJDXDNWXHOOH¶JDPOH¶
REMHNWHU
❚ $OOHREMHNWHUVRPLNNHKDUHWKnQGWDNKDQGOHSHNHUVRP
SHNHUSnVHJWDVI¡UHOOHUVLGHQDYHW
V¡SSHOW¡PPLQJVSURJUDPVRPL6LPXODPHQLNNHL&
❚ 3ODVVHQJLVWLOEDNHWLOV\VWHPHW
❚ 6WRUIRUGHO
'¡GHREMHNWHUVDPOHVLQQJDUEDJH
FROOHFWRU
©Arne Maus, aug. 1999
SXEOLFVWDWLFYRLGPDLQ6WULQJ>@DUJV^`
❚ 6WDWLVNHYDULDEOHHUIHOOHVIRUDOOHREMHNWHUDYHQNODVVH
❚ 'HWHUEDUHHWWVWNDYKYHUDYGLVVH
❚ (W¶NODVVHREMHNW¶VRPLQQHKROGHUGLVVHVWDWLVNH
RSSUHWWHVI¡USURJUDPPHWVWDUWHV
❚ 'HUIRUHNVLVWHUHU¶PDLQ¶YHGVWDUWDYSURJUDPPHW
29
VWDWLF NODVVHYDULDEOH
©Arne Maus, aug. 1999
❚ *HQHUHUHUHQVWDQGDUGJRGGRNXPHQWVMRQDY
SURJUDPPHW
❚ .RPPHQWDUHULSURJUDPPHWNRGHWSnHQDQJLWWPnWH
JLUHQGDEHGUHGRNPHG¶MDYDGRF¶
!MDYDGRF[[[MDYD
30
-DYDGRF
1 arnem
1 arnem
1 arnem
1 arnem
©Arne Maus, aug. 1999
-rw-------rw-------rw-------rw-------
arnem
arnem
arnem
arnem
31
2019 Dec 17 13:30 AllNames.html
1438 Dec 17 13:30 astest.html
463 Dec 17 13:30 packages.html
464 Dec 17 13:30 tree.html
!MDYDGRFDVWHVWMDYD
NM¡UHMDYDGRFSnELOWHVWHNVHPSHOHW
©Arne Maus, aug. 1999
33
LIS QXOOSM WHVWSM XWI¡UHVEDUHKYLVS QXOO
LIL __N!
WHVWN!XWI¡UHVEDUHKYLVL 2SHUDWRUHUKDUYDQOLJSUHVHGHQVEUXNSDUHQWHVHU
VKLIWRJELWRSHUDWRUHU
+nQGWDNRJDOLDVSUREOHPHW
6WULQJRJ
,ERROVNHXWWU\NNKDUYLRJVnEHWLQJHW¶DQG¶RJEHWLQJHW
¶RU¶
❚ NRPPHQWDUHURJ
❚
❚
❚
❚
❚
9DQOLJHVHWQLQJHU,
©Arne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UHI¡UHOOHUVLGHQDQWDOO
RJHOOHUW\SHU'HWDNWXHOOHNDOOHWYHGQHZDYJM¡UGDYHG
DQWDOOW\SHSDUDPHWUHVRPHUEUXNWKYLONHQVRPNDOOHV
❚ +DUYLLNNHVNUHYHWQRHQInUYLHQ¶GHIDXOW¶NRQVWUXNWRUPHGQXOO
SDUDPHWUHVRPLNNHJM¡UQRHDQQHWHQQHYWNDOOHVXSHUNODVVHQV
¶GHIDXOW¶NRQVWUXNWRURVY
❚ (UNODVVHQHQVXENODVVHNDOOHVVXSHUNODVVHQVNRQVWUXNWRUI¡UVW
PnJM¡UHVHNVSOLVLWWIUD¶I¡UVWHOLQMH¶LNRQVWUXNWRUHQLVXENODVVHQ
KYLVPDQKDUHJQHNRQVWUXNWRUHU
super(...param..);
.RQVWUXNWRU
}
}
Student (String u, int i)
{ navn = u + i; antall++;}
Student (int i)
{ navn="in105-student:”+i;antall++;}
class Student {
static int antall = 0;
String navn;
void lagkurs(String s)
{
for(int i = 0 ; Student.antall < 749 ; i = i +2)
{
in105[i] = new Student (i);
in105[i+1] = new Student (s,i+1);
}
System.out.println("Student nr 99:" + in105[99].navn);
System.out.println("Student nr 100:" + in105[100].navn);
}
class StudentGenerator {
Student [ ] in105 = new Student [750];
}
public static void main ( String[] args)
{ new StudentGenerator().lagkurs(args[0]); }
public class A {
// file:A.java
import java.io.*;
E:\in105-Java\PC-Java>java A
java.lang.ArrayIndexOutOfBoundsException: 0
at A.main(A.java:7)
E:\in105-Java\PC-Java>java A Ola
Student nr 99:Ola99
Student nr 100:in105-student:100
E:\in105-Java\PC-Java>javac A.java
Kompilering og kjøring
©Arne Maus, aug. 1999
1%'HWWHHULNNHGHWVDPPHVRP¶YLUWXHOOH¶PHWRGHU
❚ 6RPYHGNRQVWUXNWRUHUNDQVDPPHPHWRGHUPHG
VDPPHQDYQGHNODUHUHVYHGVLGHQDYKYHUDQGUH
❚ 'HPnKDXOLNHSDUDPHWHUVHWWDQWDOOHWRJHOOHUW\SHQH
PnI¡UHOOHUVLGHQY UHXOLNHVRPNRQVWUXNWRUHQH
❚ $NWXHOWNDOOPHGVLQHSDUDPHWUHNDQGDVDPPHQOLJQHV
PHGGHXOLNHSDUDPHWHUOLVWHQHRJULNWLJPHWRGHYHOJHV
YHGNRPSLOHULQJ
40
0HWRGHRYHUODVWLQJ
©Arne Maus, aug. 1999
❚ ¶WKLV¶HUHWUHVHUYHUWRUGVRPDOOWLGJLUHWKnQGWDNWLOGHW
REMHNWHWYLHULQQHL
41
WKLV
©Arne Maus, aug. 1999
FODVV$^LQWDD^UHWXUQ``
FODVV%H[WHQGV$^LQWDD^UHWXUQVXSHUDD``
42
7RW\SHUEUXN
.DOODYVXSHUNODVVHQVNRQVWUXNWRUPHGSDUHQWHVRJ
SDUDPHWUH
1%PnY UHI¡UVWHVHWQLQJLHJHQNRQVWUXNWRU
*HQHUHOOSHNHUWLOVXSHUNODVVHQEUXNHVIHNVIRUnNDOOH
GHQ¶JDPOHPHWRGHQ¶QnUGHQQHUHGHILQHUHVYLUWXHOWLHQ
VXENODVVH
VXSHU
©Arne Maus, aug. 1999
❚ 0HWRGHVRPNDOOHVDXWRPDWLVNQnUGHWWHREMHNWHW
V¡SSHOW¡PPHV
❚ /LWHQ\WWLJ
43
ILQDOL]HEDUHYHGV¡SSHOW¡PPLQJ
class Tell
{ static int tell;
protected void finalize(){tell++;}
int [] A = new int [100];
}
}
Kjøring:
E:\java-unix>java garb
Genererte obj= 0Ant garb:0
Genererte obj= 10Ant garb:0
Genererte obj= 10Ant garb:0
Genererte obj= 110Ant garb:0
Genererte obj= 110Ant garb:0
Genererte obj= 1110Ant garb:0
Genererte obj= 1110Ant garb:0
Genererte obj= 11110Ant garb:10288
Genererte obj= 11110Ant garb:10288
Genererte obj= 111110Ant garb:109988
void objgen (int i)
{ System.out.println("Genererte obj= “+ gen + “Ant garb:” + Tell.tell);
for (int j = i ; j > 0 ; j--) t = new Tell();
gen += i;
System.out.println("Genererte obj= “+ gen + “Ant garb:” + Tell.tell);
}
public static void main (String [] args )
{ garb g = new garb();
g.objgen(10);
g.objgen(100);
g.objgen(1000);
g.objgen(10000);
g.objgen(100000);
}
public class garb
{ Tell t ;
int gen = 0;
//Fil: garb.java
import java.io.*;
©Arne Maus, aug. 1999
45
❚ /DJHQNODVVH7PHGHQGHIDXOWNRQVWUXNW¡UGYVXWHQ
SDUDPHWUHVRPVNULYHUXWHQPHOGLQJ
/DJHWREMHNWDYGHQQHNODVVHQ
❚ /HJJSnHQQ\NRQVWUXNW¡UL7PHGHQLQWHJHUSDUDPHWHU
❚ /HJJSnHQVXENODVVH7DYWHOOPHGNRQVWUXNW¡UHURJ
ODJHWREMHNWDYNODVVHQ7
2SSJDYHQn
class T2 extends T {
T2 ( )
{ super( );}
T2 (int j) { super(j);}
}
}
T()
{ i = 17;}
T (int j) { i = j;}
public class T {
int i ;
public static void main ( String[] args)
{ T2 t =new T2( ), tt = new T2(5); }
// file:T.java
import java.io.*;
❙ %DUHHQNODVVHVRPHUSXEOLFGHWHUGHQQHVRPDQGUH
SURJUDPPHURJILOHUNDQUHIHUHUH
❙ HYWDQGUHLQGUHNODVVHU
8WVWUDNWEUXNDY¶SDFNDJH¶L-DYD
(QSDNNHEHVWnUDYHQUHNNHILOHUSnHWILORPUnGH
)LORPUnGHWVQDYQHUSDNNHQVQDYQ
+YHUILODYW\SHQFODVVLQQHKROGHU
©Arne Maus, aug. 1999
❚ KHQWHVLQQPHG¶LPSRUW¶
❙ VRPHUI¡UVWHVHWQLQJLILODHYW(WWHUNRPPHQWDUHU
❚ NDQODJHVPHGVHWQLQJHQ¶SDFNDJH¶
❚
❚
❚
❚
47
-DYDRJSDNNHU ELEOLRWHNHU
VRP*8,GDWDEDVH
[HUILORPUnGHHYW[[[Q
\HUILORPUnGHRJSDNNHQDYQHW
]HUQDYQHWSnNODVVHQPDQYLOLPSRUWHUH
EHW\UDWPDQLPSRUWHUHUDOOHNODVVHQHLSDNNHQ\
[\RYHUVHWWHVWLO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UXNHVLLQPHQJGHPHGV¡NHQ¡NNHO
❚ +DVKWDEOH
❙ 9HFWRUSODWHVDPOLQJ QHZ9HFWRU
❙ DGG(OHPHQWHOHPHQW$WLVL]HRSHUDVMRQHU
❚ $UUD\UDVNWRJIDVWOHQJGH
❚ 9HNWRUIOHNVLEHOPHQODQJVRPPHUHWDULPRWDOWPDQSXWWHULGHQ
¶LQJHQ¶¡YUHJUHQVHSnOHQJGHQ
56
$UUD\HURJ·PHQJGHU·
©Arne Maus, aug. 1999
57
To retrieve a number, use the following code:
Integer n = (Integer)numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}
This example creates a hashtable of numbers. It uses the names of
the numbers as keys:
Hashtable numbers = new Hashtable();
numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));
FODVV+DVKWDEOH
©Arne Maus, aug. 1999
59
❙ VnYDQOLJDWPDQLNNHEHK¡YHU¶IDQJH¶GHPSURJUDPPHWWHUPLQHUHU
KYLVYLLNNHIDQJHUGHP
❚ 5XQ7LPH([FHSWLRQGLYLVMRQPHGQXOOQXOOSHNHU
❙ XWI¡UNRGHVRPNDQJLIHLOPHOGLQJ
❚ WU\
❙ )DQJRSSIHLOPHOGLQJ
❚ FDWFK
❙ GHNODUDVMRQDYKYLONHIHLOPHOGLQJHUHQPHWRGHNDQNDVWH
❚ WKURZV
❙ YHUEIRUnJLIHLOPHOGLQJPHWRGHQPDQHULQQHLDYVOXWWHV
❚ WKURZ
❙ HWREMHNWPHGIHLOPHOGLQJHQ
3URJUDPPHWVNDOVHOYEOLNDOWYHGIHLOVLWXDVMRQHU
❚ ([FHSWLRQ
)HLOKnQGWHULQJ
©Arne Maus, aug. 1999
// kode som kan gi feil
// f.eks kalles en metode som sier at
// den kaster visse feilmeldinger
}
catch (Exception e){
// kode som behandler feilsituasjonen, f.eks:
e.printStackTrace( );
}
try {
❚ .DQHQWHQIDQJHKYHUW\SHDYIHLOPHOGLQJWHVWHUGDSn
W\SHQLFDWFKRJKDUIOHUHFDWFKVHWQLQJHUHOOHUDOOH
XDQVHWWW\SH([FHSWLRQVHRJVnHNVHPSHOXQGHU,2
❙ D)DQJRSSIHLOREMHNWHWLNRGHQVHOYWU\
❙ ENDVWIHLOREMHNWHWYLGHUHWKURZVVRP7H[WL-DYD*HQWO\
❚ 7RVWUDWHJLHU
60
*HQHUHOW
©Arne Maus, aug. 1999
❙ IRUVHJVHOY5DQGRP$FFHVV)LOH
❘ /LQMHYLVOHVLQJRJVNULYLQJ
❘ %XIUHW,2RVY
61
❙ 'LVVHNRPELQHUHVOHJJHUUXQGWEDVLVNODVVHQHIRUnJLNMHQWH,2
PRGHOOHUVRP
❘ 5HDGHU:ULWHU
❘ )LOH5HDGHU)LOH:ULWHU
❘ 6WULQJ5HDGHU6WULQJ:ULWHU
❘ &KDU$UUD\5HDGHU&KDU$UUD\:ULWHU
❘ 3LSHG5HDGHU3LSHG:ULWHU
0RGLILVHUHQGHNODVVHUVRP/LQH1XPEHU5HDGHU%XIIHUHG5HDGHUIRUn
JLWLOOHJJVIXQNVMRQDOLWHW
❙ $OWEHWUDNWHVVRPVWU¡PPHU.ODVVHU
❚ -DYDJDPPHOWYDQVNHOLJ
❚ -DYD,26WUHDPVILOHUKXNRPPHOVHQSLSHVEHGUH
,2ILOHU
©Arne Maus, aug. 1999
System.out.println(s+”, tolket som tall” + i);
System.out.print(”Skriv et tall:”)
s = stdin.readLine();
i = Integer.parseInt(s);
int i;
String s = new String();
62
BufferedReader stdin =
new BufferedReader(new FileReader(System.in));
VWDQGDUGLQSXW·YDQVNHOLJ·
©Arne Maus, aug. 1999
63
while ((s = in.readLine()) != null)
s2 += s + ”\n”;
in.close();
} catch (FileNotFoundException e) {
System.out.println(”File not found: ” + args[0])
} catch(IOException e) {
System.out.println(”IO Exception”);
}}
try{
BufferedReader in = new BufferedReader(
new FileReader(args[0]));
String s, s2 = new String();
..public static void main(String[] args) {
import java.io.*;
(NVOHVHLQQILOOLQMHYLVEXIIUHW
©Arne Maus, aug. 1999
}
64
/*public static void prompt (String s)
* public static int
readInt (BufferedReader in)
* public static double readDouble (BufferedReader in)
* public static String readString (BufferedReader in)
* public static char readChar (BufferedReader in)
* public static String writeInt (int number, int align)
* public static String writeDouble (double number, int align, int frac)
* public static BufferedReader open (InputStream in)
* public static BufferedReader open (String filename)
* public static PrintWriter
create (String filename) */
public class Text {
FODVV7H[WL-DYD*HQWO\
HQNHOILOVNMHUPWDVWDWXULQQXW
Les fra
tastatur:
}
// Read and check the rest of the numbers
int number;
for (int i = 2; i <= n; i++) {
Text.prompt(i+">");
number = Text.readInt(in);
if (number > highest) highest = number;
}
System.out.println("That’s enough, thanks");
System.out.println("The highest number was "+ highest);
}
System.out.println("Type them in");
Text.prompt("1>");
int highest = Text.readInt(in);
Text.prompt("How many numbers (1 or more)?");
int n = Text.readInt(in);
BufferedReader in = Text.open(System.in);
class HighestValue {
public static void main(String[] args) throws IOException {
import java.io.*;
import javagently.*;
Les fra fil
og tastatur:
}
}
for (int i = 1; i <= count; i++) {
number = Text.readDouble(fin);
total += number;
}
System.out.println("That’s enough, thanks.");
System.out.println("The total is "+total);
Text.prompt ("How many numbers?");
count = Text.readInt(in);
System.out.println("****** Summing numbers from a file ******");
BufferedReader in = Text.open(System.in);
BufferedReader fin = Text.open("numbers.dat");
int count;
double total = 0;
double number;
public static void main (String [] args) throws IOException {
class Summation2 {
/* Reads in numbers from a file and display their sum.
* Illustrates the declaration of input from both
* the keyboard and a file.
*/
import java.io.*;
import javagently.*;
©Arne Maus, aug. 1999
❚ *RGWELEOLRWHNPHGNODVVHUIRUYLQGXHUGLDORJHU
WU\NNQDSSHURJDOWDQQHWLHW*8,
❚ 'HWVRPVNMHULSURJUDPPHWHUDWGHWOLJJHU¶VWLOOH¶HWWHU
LQLWLHULQJRJYHQWHUSnEUXNHUHQVKDQGOLQJ
PXVHEHYHJHOVHNOLNNWDVWHWU\NNRVY
❚ +YHUVOLNKDQGOLQJJHQHUHUHQKHQGHOVHHYHQWVRP
SDNNHVLQQVRPHQPHOGLQJREMHNWIUDRSV\VWHPHW
❚ 'LVVHPHOGLQJHQRYHUVHQGHVSURJUDPPHWLJHQHUHUW
UHNNHI¡OJH
67
$:7YLQGXHU
RJNQDSSHU
Ingebjørg
Theiss
1999
©Arne Maus,
aug.september
1999
68
❙ FODVV0RWWDJHU.ODVVHLPSOHPHQWV;;;/LVWHQHU^`
❙ 'HQPHWRGHQVRPIUDJUHQVHVQLWWHWVNDOKDHQPHOGLQJNDOOHVLDOOH
DERQQHUHQGHNODVVHU
❚ .ODVVHQHPnRJVnLPSOHPHQWHUH;;;/LVWHQHUJUHQVHVQLWWHW
RJGHPHWRGHUGHWWHJUHQVHVQLWWHWGHNODUHUHU
❙ 3nDYPHOGLQJVNMHUYHGnNDOOHREMHNWHWVDGGUHPRYH;;;/LVWHQHU
PHWRGHPHGGHWDERQQHUHQGHREMHNWHWVRPSDUDPHWHU
❙ 'HW\SHUDYPHOGLQJHUVRPLQJHQNODVVHDERQQHUHUSnNDVWHV
❙ +YLVIOHUHNODVVHUYLOKDHQPHOGLQJVW\SHInUGHKYHUVLQH
❚ .ODVVHUVRPYLODERQQHUHSnKHQGHOVHUPnEOLSnPHOGWWLO
REMHNWHWVRPJHQHUHUKHQGHOVHQH
+HQGHOVHV(YHQWSURJUDPPHULQJ
©Arne Maus, aug. 1999
❙ 'HW\SHUDYPHOGLQJHUVRPLQJHQNODVVHYLOKDNDVWHV
❙ +YLVIOHUHNODVVHUYLOKDHQPHOGLQJVW\SHInUGHKYHUVLQH
❙ 'HQPHWRGHQVRPSHUGHIVNDOKDVOLNPHOGLQJNDOOHVLDOOHGH
NODVVHQHVRPKDUVDJWIUD
69
❘ HNVimplements ActionListenerRJHQDYPHWRGHQHNODVVHQGD
LPSOHPHQWHUHUGHQPHWRGHQHVRPSHUGHIPRWWDUPHOGLQJHQ
actionPerformed(ActionEvent e) {..’her behandles en
slik hendelse e.}
❙ .ODVVHQHVRPYLOPRWWDVOLNHPHOGLQJHUVLHUKYLONHW\SHUPHOGLQJHU
GHYLOKD
❚ (YHQWPRGHOOHQHUPHJHWHQNHO
(YHQWSURJUDPPHULQJ
public void init() {
b1.addActionListener(new B1());
b2.addActionListener(new B2());
add(b1);
add(b2);
add(t);
}
class B1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
t.setText("Button 1");
}
}
public class Button2NewB extends Applet {
Button
b1 = new Button("Button 1"),
b2 = new Button("Button 2");
TextField t = new TextField(20);
//: Button2NewB.java – s.516 Bruce Eckel
// An application and an applet
import java.awt.*;
import java.awt.event.*; // Must add this
import java.applet.*;
class B2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
t.setText("Button 2");
}
}
// To close the application:
static class WL extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
// A main() for the application:
public static void main(String[] args) {
Button2NewB applet = new Button2NewB();
Frame aFrame = new Frame("Button2NewB");
aFrame.addWindowListener(new WL());
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300,200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
///:~
©Arne Maus, aug. 1999
72
❙ 1nUWUnGHUWUHQJHUnOHVHVNULYHIHOOHVGDWDNDQGHWVNMHRUGQHW
LNNHVDPWLGLJPHQN¡HVRSS
❙ V\QFKURQL]HGPHWRGHU
❚ 7UnGHUWKUHDGVJLUVHSDUDWHSURJUDPHNVHNYHULQJHU
LQQHLHWWSURJUDPPHQIHOOHVDGUHVVHRPUnGH
❚ +YHUWUnGVSUHUVHJHYWSnKYHUVLQ&38KYLVIOHU&38
❚ 6WRSSHUGHQHQHIRUVHWWHUGHQDQGUHSUREOHPIULWW
❚ 9LONnUOLJKDVWLJKHWIUDPGULIWLIRUKROGKYHUDQGUH
❚ 7UnGHUNDQV\QNURQLVHUHV
7UnGHURJSDUDOOHOOLWHW
public class SimpleThread extends Thread{
private int countDown = 5;
private int threadNumber;
private static int threadCount = 0;
public SimpleThread() {
threadNumber = ++threadCount;
System.out.println("Making " + threadNumber);
}
public void run() {
while(true) {
try {System.out.println("Thread " +
threadNumber + "(" + countDown + ")");
sleep(50); // sov 50 millisek.
if(--countDown == 0) return;
} catch (InterruptedException e){
System.out.println(“Exception” + e);
}
}
}
public static void main(String[] args) {
for(int i = 0; i < 5; i++)
new SimpleThread().start();
System.out.println("All Threads Started");
}
} ///:~
//: SimpleThread.java. litt endret Bruce Eckel, 1998
Kjøring:
Making 1
Making 2
Making 3
Making 4
Making 5
All Threads Started
Thread 1(5)
Thread 2(5)
Thread 3(5)
Thread 4(5)
Thread 5(5)
Thread 1(4)
Thread 2(4)
Thread 3(4)
Thread 4(4)
Thread 5(4)
Thread 1(3)
Thread 2(3)
Thread 3(3)
Thread 5(3)
Thread 4(3)
Thread 2(2)
Thread 1(2)
Thread 3(2)
Thread 5(2)
Thread 4(2)
Thread 2(1)
Thread 3(1)
Thread 5(1)
Thread 1(1)
Thread 4(1)
Download