Add Calculator.java

parent 02fe4820
Showing with 152 additions and 0 deletions
/**
* 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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment