Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
k2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tie
ohj2
esimerkit
k2022
Commits
8c4545fd
Commit
8c4545fd
authored
3 years ago
by
Vesa Lappalainen
Browse files
Options
Downloads
Patches
Plain Diff
Lisaa-metodin toteutus
parent
6039271f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
luennot/live13/src/kerho/Jasenet.java
+77
-3
77 additions, 3 deletions
luennot/live13/src/kerho/Jasenet.java
luennot/live13/src/kerho/SailoException.java
+20
-0
20 additions, 0 deletions
luennot/live13/src/kerho/SailoException.java
with
97 additions
and
3 deletions
luennot/live13/src/kerho/Jasenet.java
+
77
−
3
View file @
8c4545fd
...
...
@@ -7,6 +7,75 @@ package kerho;
*/
public
class
Jasenet
{
private
static
final
int
MAX_JASENIA
=
5
;
private
int
lkm
=
0
;
private
Jasen
[]
alkiot
;
/**
* Luodaan alustava taulukko
*/
public
Jasenet
()
{
alkiot
=
new
Jasen
[
MAX_JASENIA
];
}
/**
* Lisää uuden jäsenen tietorakenteeseen. Ottaa jäsenen omistukseensa.
* @param jasen lisätäävän jäsenen viite. Huom tietorakenne muuttuu omistajaksi
* @throws SailoException jos tietorakenne on jo täynnä
* @example
* <pre name="test">
* #THROWS SailoException
* Jasenet jasenet = new Jasenet();
* Jasen aku1 = new Jasen(), aku2 = new Jasen();
* jasenet.getLkm() === 0;
* jasenet.lisaa(aku1); jasenet.getLkm() === 1;
* jasenet.lisaa(aku2); jasenet.getLkm() === 2;
* jasenet.lisaa(aku1); jasenet.getLkm() === 3;
* jasenet.anna(0) === aku1;
* jasenet.anna(1) === aku2;
* jasenet.anna(2) === aku1;
* jasenet.anna(1) == aku1 === false;
* jasenet.anna(1) == aku2 === true;
* jasenet.anna(3) === aku1; #THROWS IndexOutOfBoundsException
* jasenet.lisaa(aku1); jasenet.getLkm() === 4;
* jasenet.lisaa(aku1); jasenet.getLkm() === 5;
* jasenet.lisaa(aku1); #THROWS SailoException
* </pre>
*/
public
void
lisaa
(
Jasen
jasen
)
throws
SailoException
{
if
(
lkm
>=
alkiot
.
length
)
throw
new
SailoException
(
"Liikaa alkioita"
);
alkiot
[
lkm
]
=
jasen
;
lkm
++;
}
/**
* Palauttaa viitteen i:teen jäseneen.
* @param i monennenko jäsenen viite halutaan
* @return viite jäseneen, jonka indeksi on i
* @throws IndexOutOfBoundsException jos i ei ole sallitulla alueella
*/
public
Jasen
anna
(
int
i
)
throws
IndexOutOfBoundsException
{
if
(
i
<
0
||
this
.
lkm
<=
i
)
throw
new
IndexOutOfBoundsException
(
"Laiton indeksi: "
+
i
);
return
alkiot
[
i
];
}
/**
* Palauttaa kerhon jäsenten lukumäärän
* @return jäsenten lukumäärä
*/
public
int
getLkm
()
{
return
lkm
;
}
/**
* @param args ei käytössä
*/
...
...
@@ -20,16 +89,21 @@ public class Jasenet {
aku2
.
rekisteroi
();
aku2
.
vastaaAkuAnkka
();
jasenet
.
lisaa
(
aku
);
jasenet
.
lisaa
(
aku2
);
try
{
jasenet
.
lisaa
(
aku
);
jasenet
.
lisaa
(
aku2
);
}
catch
(
SailoException
e
)
{
System
.
err
.
println
(
e
.
getMessage
());
}
System
.
out
.
println
(
"============= Jäsenet testi ================="
);
for
(
int
i
=
0
;
i
<
jasenet
.
getLkm
();
i
++)
{
Jasen
jasen
=
jasenet
.
anna
(
i
);
System
.
out
.
println
(
"Jäsen indeksi: "
+
i
);
jasen
.
tulosta
(
System
.
out
);
}
}
...
...
This diff is collapsed.
Click to expand it.
luennot/live13/src/kerho/SailoException.java
0 → 100644
+
20
−
0
View file @
8c4545fd
package
kerho
;
/**
* Poikkeusluokka tietorakenteesta aiheutuville poikkeuksille.
* @author Vesa Lappalainen
* @version 1.0, 22.02.2003
*/
public
class
SailoException
extends
Exception
{
private
static
final
long
serialVersionUID
=
1L
;
/**
* Poikkeuksen muodostaja jolle tuodaan poikkeuksessa
* käytettävä viesti
* @param viesti Poikkeuksen viesti
*/
public
SailoException
(
String
viesti
)
{
super
(
viesti
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment