Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kurs_java_alx_20240321
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
kurs_java_alx_20240321
Commits
75501319
Commit
75501319
authored
Apr 10, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
jednostki miary
parent
4d17f85c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
88 additions
and
6 deletions
+88
-6
GeometriaProgram.java
src/p09_funkcje/GeometriaProgram.java
+28
-6
JednostkiMiary.java
src/p09_funkcje/JednostkiMiary.java
+60
-0
No files found.
src/p09_funkcje/GeometriaProgram.java
View file @
75501319
...
@@ -12,13 +12,12 @@ public class GeometriaProgram {
...
@@ -12,13 +12,12 @@ public class GeometriaProgram {
System
.
out
.
println
(
" O - koło"
);
System
.
out
.
println
(
" O - koło"
);
System
.
out
.
println
(
" P - prostokąt"
);
System
.
out
.
println
(
" P - prostokąt"
);
System
.
out
.
println
(
" T - trójkąt"
);
System
.
out
.
println
(
" T - trójkąt"
);
System
.
out
.
println
(
" KM - przelicz kilometry na mile"
);
System
.
out
.
println
(
" MK - przelicz mile na kilometry"
);
System
.
out
.
println
(
" CF - przelicz °C na °F"
);
System
.
out
.
println
(
" FC - przelicz °F na °C"
);
System
.
out
.
println
(
" Q - zakończ"
);
System
.
out
.
println
(
" Q - zakończ"
);
// ewentualnie: w klasie JednostkiMiary zdefiniuj funckje:
// mileNaKm , kmNaMile przeliczające odległość z kilometrów na mile (międzynarodowe) i owdrotnie
// farNaCel , celNaFar - stopnie Celsjusza i Fahrenheita
// https://pl.wikipedia.org/wiki/Skala_Fahrenheita
petla:
petla:
while
(
true
)
{
while
(
true
)
{
System
.
out
.
println
(
"\nWybierz figurę lub operację:"
);
System
.
out
.
println
(
"\nWybierz figurę lub operację:"
);
...
@@ -64,7 +63,30 @@ public class GeometriaProgram {
...
@@ -64,7 +63,30 @@ public class GeometriaProgram {
System
.
out
.
println
(
"Z tych liczb nie da się złożyć trójkąta"
);
System
.
out
.
println
(
"Z tych liczb nie da się złożyć trójkąta"
);
}
}
}
}
default
->
{
case
"KM"
->
{
System
.
out
.
print
(
"Podaj odległość w kilometrach: "
);
double
km
=
scanner
.
nextDouble
();
double
wynik
=
JednostkiMiary
.
km_na_mile
(
km
);
System
.
out
.
printf
(
"%.3f km = %.3f mil\n"
,
km
,
wynik
);
}
case
"MK"
->
{
System
.
out
.
print
(
"Podaj odległość w milach: "
);
double
mile
=
scanner
.
nextDouble
();
double
wynik
=
JednostkiMiary
.
mile_na_km
(
mile
);
System
.
out
.
printf
(
"%.3f mil = %.3f km\n"
,
mile
,
wynik
);
}
case
"FC"
->
{
System
.
out
.
print
(
"Podaj temperaturę w Fahrenheitach: "
);
double
f
=
scanner
.
nextDouble
();
double
wynik
=
JednostkiMiary
.
f_na_c
(
f
);
System
.
out
.
printf
(
"%.3f °F = %.3f °C\n"
,
f
,
wynik
);
}
case
"CF"
->
{
System
.
out
.
print
(
"Podaj temperaturę w Celsjuszach: "
);
double
c
=
scanner
.
nextDouble
();
double
wynik
=
JednostkiMiary
.
c_na_f
(
c
);
System
.
out
.
printf
(
"%.3f °C = %.3f °F\n"
,
c
,
wynik
);
}
default
->
{
System
.
out
.
println
(
"Nieznane polecenie "
+
wybor
);
System
.
out
.
println
(
"Nieznane polecenie "
+
wybor
);
}
}
}
}
...
...
src/p09_funkcje/JednostkiMiary.java
0 → 100644
View file @
75501319
package
p09_funkcje
;
public
class
JednostkiMiary
{
private
static
final
double
PRZELICZNIK_KM_MILE
=
1.609344
;
// https://pl.wikipedia.org/wiki/Mila_mi%C4%99dzynarodowa
public
static
double
mile_na_km
(
double
mile
)
{
return
mile
*
PRZELICZNIK_KM_MILE
;
}
public
static
double
km_na_mile
(
double
km
)
{
return
km
/
PRZELICZNIK_KM_MILE
;
}
// Stopnie Celsjusza na Fahrenheita i odwrotnie
// https://pl.wikipedia.org/wiki/Skala_Fahrenheita
public
static
double
c_na_f
(
double
c
)
{
// To jest źle, bo dzielenie 9/5 daje wynik 1
// return 32 + 9/5 * c;
// Poprawne zapisy:
// return c * 9 / 5 + 32;
// return 32.0 + 9.0 / 5.0 * c;
// return 32 + 9. / 5. * c;
return
32
+
1.8
*
c
;
}
public
static
double
f_na_c
(
double
f
)
{
// Niepoprawne, bo 5/9 daje wynik 0
// return 5/9 * (f - 32);
// Poprawne zapisy:
// return 5./9. * (f - 32.);
return
(
f
-
32
)
/
1.8
;
}
public
static
void
main
(
String
[]
args
)
{
// Tutaj w main wpiszemy tylko przykładowe wywołania funkcji,
// aby sprawdzić, czy one dobrze działają.
// To nie jest "praktyczny program" dla użytkownika.
System
.
out
.
println
(
"100 mil = "
+
mile_na_km
(
100
)
+
" km"
);
System
.
out
.
println
(
"300 mil = "
+
mile_na_km
(
300
)
+
" km"
);
System
.
out
.
println
(
"100 km = "
+
km_na_mile
(
100
)
+
" mil"
);
System
.
out
.
println
(
"160 km = "
+
km_na_mile
(
160
)
+
" mil"
);
System
.
out
.
printf
(
"%.1f km = %.1f mil\n"
,
160.0
,
km_na_mile
(
160
));
System
.
out
.
println
();
System
.
out
.
println
(
" 0 C = "
+
c_na_f
(
0
)
+
" F"
);
System
.
out
.
println
(
" 37 C = "
+
c_na_f
(
37
)
+
" F"
);
System
.
out
.
println
(
"100 C = "
+
c_na_f
(
100
)
+
" F"
);
System
.
out
.
println
();
System
.
out
.
println
(
" 0 F = "
+
f_na_c
(
0
)
+
" C"
);
System
.
out
.
println
(
" 50 F = "
+
f_na_c
(
50
)
+
" C"
);
System
.
out
.
println
(
"100 F = "
+
f_na_c
(
100
)
+
" C"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment