Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
statistics with methods
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
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
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
SOFT Core
SOFT 160
statistics with methods
Commits
89cd40de
Commit
89cd40de
authored
3 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Added example to demonstrate type casting and method overloading
parent
b46f356e
Branches
main
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/edu/unl/cse/soft160/statistics/GeneralizedStatistics.java
+121
-0
121 additions, 0 deletions
...edu/unl/cse/soft160/statistics/GeneralizedStatistics.java
with
121 additions
and
0 deletions
src/main/java/edu/unl/cse/soft160/statistics/GeneralizedStatistics.java
0 → 100644
+
121
−
0
View file @
89cd40de
package
edu.unl.cse.soft160.statistics
;
import
java.util.Scanner
;
/**
* Computes simple statistics of three values
*/
public
class
GeneralizedStatistics
{
public
enum
StatisticsType
{
MEAN
,
MEDIAN
,
// to keep this example shorter, we're omitting maximum and minimum
}
private
static
String
getUserInput
(
String
prompt
,
Scanner
scanner
)
{
System
.
out
.
print
(
prompt
);
String
userInput
=
scanner
.
nextLine
();
return
userInput
;
}
private
static
Number
getValue
(
String
ordinal
,
Scanner
scanner
)
{
String
userInput
=
getUserInput
(
"Enter the "
+
ordinal
+
" value: "
,
scanner
);
if
(
userInput
.
contains
(
"."
))
{
return
Double
.
valueOf
(
userInput
);
}
else
{
return
Integer
.
valueOf
(
userInput
);
}
}
private
static
StatisticsType
getStaticsticsType
(
Scanner
scanner
)
{
String
userInput
=
getUserInput
(
"Please enter the statistic to calculate: "
,
scanner
);
userInput
=
userInput
.
toUpperCase
();
StatisticsType
statisticsType
=
StatisticsType
.
valueOf
(
userInput
);
return
statisticsType
;
}
private
static
double
computeMean
(
double
firstValue
,
double
secondValue
,
double
thirdValue
)
{
double
statistic
=
(
firstValue
+
secondValue
+
thirdValue
)
/
3
;
return
statistic
;
}
private
static
double
computeMedian
(
double
firstValue
,
double
secondValue
,
double
thirdValue
)
{
double
statistic
=
Double
.
NaN
;
if
(((
secondValue
<=
firstValue
)
&&
(
firstValue
<=
thirdValue
))
||
((
secondValue
>=
firstValue
)
&&
(
firstValue
>=
thirdValue
)))
{
statistic
=
secondValue
;
}
if
(((
firstValue
<=
secondValue
)
&&
(
secondValue
<=
thirdValue
))
||
((
firstValue
>=
secondValue
)
&&
(
secondValue
>=
thirdValue
)))
{
statistic
=
secondValue
;
}
if
(((
firstValue
<=
thirdValue
)
&&
(
thirdValue
<=
secondValue
))
||
((
firstValue
>=
thirdValue
)
&&
(
thirdValue
>=
secondValue
)))
{
statistic
=
secondValue
;
}
return
statistic
;
}
private
static
int
computeMean
(
int
firstValue
,
int
secondValue
,
int
thirdValue
)
{
int
statistic
=
(
firstValue
+
secondValue
+
thirdValue
)
/
3
;
return
statistic
;
}
private
static
int
computeMedian
(
int
firstValue
,
int
secondValue
,
int
thirdValue
)
{
int
statistic
=
Integer
.
MAX_VALUE
;
if
(((
secondValue
<=
firstValue
)
&&
(
firstValue
<=
thirdValue
))
||
((
secondValue
>=
firstValue
)
&&
(
firstValue
>=
thirdValue
)))
{
statistic
=
secondValue
;
}
if
(((
firstValue
<=
secondValue
)
&&
(
secondValue
<=
thirdValue
))
||
((
firstValue
>=
secondValue
)
&&
(
secondValue
>=
thirdValue
)))
{
statistic
=
secondValue
;
}
if
(((
firstValue
<=
thirdValue
)
&&
(
thirdValue
<=
secondValue
))
||
((
firstValue
>=
thirdValue
)
&&
(
thirdValue
>=
secondValue
)))
{
statistic
=
secondValue
;
}
return
statistic
;
}
private
static
Number
computeStatisticFromInput
(
StatisticsType
type
,
Scanner
scanner
)
{
Number
firstInputValue
=
getValue
(
"first"
,
scanner
);
Number
secondInputValue
=
getValue
(
"second"
,
scanner
);
Number
thirdInputValue
=
getValue
(
"third"
,
scanner
);
Number
statisticValue
=
null
;
if
(
firstInputValue
instanceof
Integer
&&
secondInputValue
instanceof
Integer
&&
thirdInputValue
instanceof
Integer
)
{
int
firstValue
=
(
int
)
firstInputValue
;
int
secondValue
=
(
int
)
secondInputValue
;
int
thirdValue
=
(
int
)
thirdInputValue
;
if
(
type
==
StatisticsType
.
MEAN
)
{
statisticValue
=
computeMean
(
firstValue
,
secondValue
,
thirdValue
);
}
else
if
(
type
==
StatisticsType
.
MEDIAN
)
{
statisticValue
=
computeMedian
(
firstValue
,
secondValue
,
thirdValue
);
}
else
{
System
.
err
.
println
(
"Cannot compute "
+
type
+
"."
);
statisticValue
=
Integer
.
MAX_VALUE
;
}
}
else
{
double
firstValue
=
(
double
)
firstInputValue
;
double
secondValue
=
(
double
)
secondInputValue
;
double
thirdValue
=
(
double
)
thirdInputValue
;
if
(
type
==
StatisticsType
.
MEAN
)
{
statisticValue
=
computeMean
(
firstValue
,
secondValue
,
thirdValue
);
}
else
if
(
type
==
StatisticsType
.
MEDIAN
)
{
statisticValue
=
computeMedian
(
firstValue
,
secondValue
,
thirdValue
);
}
else
{
System
.
err
.
println
(
"Cannot compute "
+
type
+
"."
);
statisticValue
=
Double
.
NaN
;
}
}
return
statisticValue
;
}
public
static
void
main
(
String
[]
args
)
{
Scanner
scanner
=
new
Scanner
(
System
.
in
);
StatisticsType
type
=
getStaticsticsType
(
scanner
);
Number
statistic
=
computeStatisticFromInput
(
type
,
scanner
);
scanner
.
close
();
System
.
out
.
println
(
"The "
+
type
.
toString
().
toLowerCase
()
+
" is "
+
statistic
);
}
}
\ No newline at end of file
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