Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Adombang Munang Mbomndih
/
iax0583
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
6d07320a
authored
Oct 25, 2018
by
Adombang Munang Mbomndih
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Calculator.java
parent
02fe4820
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
152 additions
and
0 deletions
homework1/Calculator.java
homework1/Calculator.java
0 → 100644
View file @
6d07320a
/**
* File: Calculator.java
* Author: Adombang Munang Mbomndih
* Created: 22.10.2018
*
* Description: Calculator for a polynomial fraction, with variable inputs from users
*/
package
ee
.
taltech
.
homework1
;
import
java.util.Scanner
;
public
class
Calculator
{
private
Scanner
scanner
;
private
Double
startingValue
,
step
,
lowerLimit
,
X
,
Y
;
private
int
counter
=
0
;
/**
* Main entry point of application
*/
public
static
void
main
(
String
[]
args
)
{
Calculator
calculator
=
new
Calculator
();
calculator
.
scanner
=
new
Scanner
(
System
.
in
);
calculator
.
getStartingValue
();
calculator
.
getStep
();
calculator
.
getLowerLimit
();
System
.
out
.
println
(
"\nStarting value: "
+
calculator
.
startingValue
+
" | Step: "
+
calculator
.
step
+
" | Lower limit: "
+
calculator
.
lowerLimit
);
calculator
.
computeY
();
System
.
out
.
println
(
"\nDone!"
);
}
/**
* Function to initialize the start value with user's input
*/
private
void
getStartingValue
()
{
while
(
true
)
// Keep asking until user inputs correct value
{
try
{
System
.
out
.
println
(
"Enter starting value: "
);
startingValue
=
Double
.
parseDouble
(
scanner
.
nextLine
());
break
;
}
catch
(
NullPointerException
npe
)
{
System
.
out
.
println
(
"Starting value cannot be null!"
);
}
catch
(
NumberFormatException
nfe
)
{
System
.
out
.
println
(
"Starting value must be a real number!"
);
}
}
}
/**
* Function to initialize the step with user's input
*/
private
void
getStep
()
{
while
(
true
)
// Keep asking until user inputs correct value
{
try
{
while
(
true
)
{
System
.
out
.
println
(
"Enter step: "
);
step
=
Double
.
parseDouble
(
scanner
.
nextLine
());
if
(
step
>
0
)
break
;
System
.
out
.
println
(
"Step must be greater than zero!"
);
}
break
;
}
catch
(
NullPointerException
npe
)
{
System
.
out
.
println
(
"Step cannot be null!"
);
}
catch
(
NumberFormatException
nfe
)
{
System
.
out
.
println
(
"Step must be a real number!"
);
}
}
}
/**
* Function to initialize the lower limit of the function with user's input
*/
private
void
getLowerLimit
()
{
while
(
true
)
// Keep asking until user inputs correct value
{
try
{
System
.
out
.
println
(
"Enter lower limit: "
);
lowerLimit
=
Double
.
parseDouble
(
scanner
.
nextLine
());
break
;
}
catch
(
NullPointerException
npe
)
{
System
.
out
.
println
(
"Lower limit cannot be null!"
);
}
catch
(
NumberFormatException
nfe
)
{
System
.
out
.
println
(
"Lower limit must be a real number!"
);
}
}
}
/**
* Function to solve the equation based on the user's inputs. Prints out result on terminal.
*/
private
void
computeY
()
{
X
=
startingValue
;
System
.
out
.
println
(
"\nx:\ty=f(x): "
);
do
{
double
tempFraction1
=
(
2
*
Math
.
pow
(
X
,
2
))
+
(
3
*
X
)
+
4
;
double
tempFraction2
=
Math
.
pow
(
tempFraction1
,
3
)
-
7
;
if
(
tempFraction2
==
0
)
{
// cannot divide by zero
System
.
out
.
println
(
X
+
"\t'Not available!'"
);
}
else
if
(
tempFraction2
<
0
)
{
// square root of a negative number is complex
System
.
out
.
println
(
X
+
"\t'Complex number!'"
);
}
else
{
double
numerator
=
(
2
*
X
)
+
1
;
double
denomenator
=
Math
.
sqrt
(
tempFraction2
);
Y
=
numerator
/
denomenator
;
System
.
out
.
println
(
X
+
"\t"
+
Y
);
}
X
=
(++
counter
*
step
)
+
startingValue
;
}
while
(
lowerLimit
<=
Y
&&
counter
<
15
);
}
}
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