Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Ø
Øving 4
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Programmering 1 Øvinger
Øving 4
Commits
9a06f76e
Commit
9a06f76e
authored
8 months ago
by
Benjamin Kvello-Hansen
Browse files
Options
Downloads
Patches
Plain Diff
Update 2 files
- /oppgave1.java - /oppgvae2.java
parent
9a65fef8
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
oppgave1.java
+74
-0
74 additions, 0 deletions
oppgave1.java
oppgvae2.java
+54
-0
54 additions, 0 deletions
oppgvae2.java
with
128 additions
and
0 deletions
oppgave1.java
+
74
−
0
View file @
9a06f76e
import
java.util.Scanner
;
class
Valuta
{
private
double
kurs
;
private
String
navn
;
public
Valuta
(
double
kurs
,
String
navn
)
{
this
.
kurs
=
kurs
;
this
.
navn
=
navn
;
}
public
double
tilNok
(
double
antall
)
{
return
antall
*
kurs
;
}
public
double
fraNok
(
double
antall
)
{
return
antall
/
kurs
;
}
public
String
getNavn
()
{
return
navn
;
}
public
static
void
main
(
String
[]
args
)
{
Valuta
dollar
=
new
Valuta
(
10.52
,
"Dollar"
);
Valuta
euro
=
new
Valuta
(
11.69
,
"Euro"
);
Valuta
svenskeKroner
=
new
Valuta
(
1.04
,
"Svenske kroner"
);
Scanner
inp
=
new
Scanner
(
System
.
in
);
int
svar
=
0
;
while
(
svar
!=
4
)
{
System
.
out
.
println
(
"Velg valuta:"
);
System
.
out
.
println
(
"1. "
+
dollar
.
getNavn
());
System
.
out
.
println
(
"2. "
+
euro
.
getNavn
());
System
.
out
.
println
(
"3. "
+
svenskeKroner
.
getNavn
());
System
.
out
.
println
(
"4. avslutt"
);
svar
=
inp
.
nextInt
();
if
(
svar
==
4
)
{
System
.
out
.
println
(
"Avslutter..."
);
break
;
}
System
.
out
.
println
(
"Vil du regne fra eller til NOK?"
);
System
.
out
.
println
(
"1. til NOK"
);
System
.
out
.
println
(
"2. fra NOK"
);
int
fraTil
=
inp
.
nextInt
();
System
.
out
.
println
(
"Hvor mye vil du regne om?"
);
double
antall
=
inp
.
nextDouble
();
double
resultat
=
0
;
switch
(
svar
)
{
case
1
:
resultat
=
(
fraTil
==
1
)
?
dollar
.
tilNok
(
antall
)
:
dollar
.
fraNok
(
antall
);
System
.
out
.
printf
(
"%s %.2f %s er %.2f NOK\n"
,
fraTil
==
1
?
"Dollar"
:
"NOK"
,
antall
,
fraTil
==
1
?
""
:
dollar
.
getNavn
(),
resultat
);
break
;
case
2
:
resultat
=
(
fraTil
==
1
)
?
euro
.
tilNok
(
antall
)
:
euro
.
fraNok
(
antall
);
System
.
out
.
printf
(
"%s %.2f %s er %.2f NOK\n"
,
fraTil
==
1
?
"Euro"
:
"NOK"
,
antall
,
fraTil
==
1
?
""
:
euro
.
getNavn
(),
resultat
);
break
;
case
3
:
resultat
=
(
fraTil
==
1
)
?
svenskeKroner
.
tilNok
(
antall
)
:
svenskeKroner
.
fraNok
(
antall
);
System
.
out
.
printf
(
"%s %.2f %s er %.2f NOK\n"
,
fraTil
==
1
?
"Svenske kroner"
:
"NOK"
,
antall
,
fraTil
==
1
?
""
:
svenskeKroner
.
getNavn
(),
resultat
);
break
;
default
:
System
.
out
.
println
(
"Ugyldig valg. Vennligst prøv igjen."
);
}
}
inp
.
close
();
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
oppgvae2.java
0 → 100644
+
54
−
0
View file @
9a06f76e
import
java.util.*
;
class
Spiller
{
private
int
sum
;
public
Spiller
(
int
sum
)
{
this
.
sum
=
sum
;
}
public
int
getSumPoeng
(
int
terningKast
)
{
if
(
terningKast
==
1
)
{
return
0
;
}
else
{
sum
=
sum
+
terningKast
;
return
sum
;
}
}
public
int
kastTerningen
()
{
Random
random
=
new
Random
();
int
terningKast
=
random
.
nextInt
(
6
)
+
1
;
return
getSumPoeng
(
terningKast
);
}
public
boolean
erFerdig
()
{
return
sum
>=
99
;
}
public
static
void
main
(
String
[]
args
)
{
Spiller
A
=
new
Spiller
(
0
);
Spiller
B
=
new
Spiller
(
0
);
int
rundeNr
=
1
;
System
.
out
.
println
(
"Spiller A starter"
);
while
(
A
.
sum
<
100
&&
B
.
sum
<
100
)
{
A
.
kastTerningen
();
System
.
out
.
println
(
"Runde: "
+
rundeNr
+
" Sum spiller A: "
+
A
.
sum
);
if
(
A
.
sum
<
100
)
{
B
.
kastTerningen
();
System
.
out
.
println
(
"Runde: "
+
rundeNr
+
" Sum spiller B: "
+
B
.
sum
);
}
rundeNr
++;
}
if
(
A
.
erFerdig
()){
System
.
out
.
println
(
"Spiller A vant med "
+
A
.
sum
+
" Poeng!"
);
}
else
{
System
.
out
.
println
(
"Spiller B vant med "
+
B
.
sum
+
" Poeng!"
);
}
}
}
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