Creating a Simple Calculator class in Java

Problem:

Write a class (SimpleCalculator) having two instances variables firstNumber and secondNumber (Doubles) with their corresponding setters and getters. The constructor should enable the user to set the two values when creating an instance of the class. In addition, your class should include four other methods: toString, add, sub, and divide. The toString method should return the two numbers as string as follows: "[First Number is: ...] [Second Number is: ...]" (without the quotes and replacing the three dots with the actual values). Each of the remaining three methods performs the operation on the two instance variables and returns the corresponding result. You should write a separate class (SimpleCalculatorTester) to test your class. In the test class, you should ask the user to enter the numbers and use set methods to set their values. Then use the getters to make sure that their values are correctly set. The tester should also include all methods in your class at least once (add, sub and divide).

Output:

Not needed.

Solution:

public class SimpleCalc
{
 private double firstNumber;
 private double secondNumber;

    public SimpleCalc(double x, double y) 
    {
     firstNumber = x;
     secondNumber = y;
    }
    
    public double getFirstNumber()
    {
     return firstNumber;
    }
    
    public double getSecondNumber()
    {
     return  secondNumber;
    }
    
    public  void setFirstNumber(double a)
    {
     firstNumber = a;
    }
    
    public  void setSecondNumber(double a)
    {
     secondNumber = a;
    }
    
    public double add()
    {
     double result = firstNumber + secondNumber;
     return result;
    }
    
    public double sub()
    {
     double result = firstNumber * secondNumber;
     return result;
    }
    
    public double divide()
    {
     double result = firstNumber/secondNumber;
     return result;
    }
    
    public String toString()
    {
     return ("[First Number is: "+ firstNumber + "] [Second Number is: "+ secondNumber +" ]");
    }
   


    public static void main(String args []) 
    {
     Scanner scan = new Scanner(System.in);
     System.out.println("Please enter two numbers: ");
     
     SimpleCalc calc= new SimpleCalc(scan.nextDouble(), scan.nextDouble());
     System.out.println(calc);
     
     System.out.println("The first number is: " + calc.getFirstNumber());
     System.out.println("The second number is: " + calc.getSecondNumber());
     System.out.println("The addition of the two numbers is: " + calc.add());
     System.out.println("The substraction of the first number by the second is: " + calc.sub());
     System.out.println("The division of the first number by the second number is: " + calc.divide());
    }
     
    
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact