Question

In: Computer Science

----------------------------------------------------------------------------------------------- Create a class called MathOperations that a teacher might use to represent the basic type...

-----------------------------------------------------------------------------------------------

Create a class called MathOperations that a teacher might use to represent the basic type of mathematical operations that may be performed. The class should include the following:

  1. Three double private variables as instance variables, number1, number2, and result.

  2. Your class should have a default constructor that initializes the three instance variables to zero.

  3. Your class should also have a constructor that initializes the two instance variables (number1 and number2) to the value entered by the user from keyboard.

  4. Also provide an accessor (getter) and mutator (setter) method for each instance variable except the result, that only has an accessor method.

  5. In addition, provide 4 methods called addNumbers(), subNumbers(), divNumbers() and mulNumbers() that calculate the relevant addition, subtraction... operations on the two numbers entered by the user and stores it in the result variable.

  6. Create a main method and within this method create 2 objects (obj1, obj2) of the MathsOperations class that sets/initializes the two instance variables (number1 and number2) by values entered by the user from the keyboard (Hint: Use Scanner class object).

7. Call/invoke each method on these 2 objects and print the result of each operation performed (using the result getter method).

8. Note: Please make sure to check when performing a division operation that the denominator is not zero. If the denominator is zero then simply set the result to zero. (Hint: Use condition statements)

Solutions

Expert Solution


import java.util.Scanner;

class MathOperations {
        private double num1;
        private double num2;
        private double result;
        public MathOperations() {
                num1=0;
                num2=0;
                result=0;
        }
        public MathOperations(double aNum1, double aNum2) {
                super();
                num1 = aNum1;
                num2 = aNum2;
        }
        public double getNum1() {
                return num1;
        }
        public double getNum2() {
                return num2;
        }
        public double getResult() {
                return result;
        }
        public void setNum1(double aNum1) {
                num1 = aNum1;
        }
        public void setNum2(double aNum2) {
                num2 = aNum2;
        }
        public void addNumbers() {
                result = num1+num2;
        }
        public void subNumbers() {
                result = num1-num2;
        }
        public void mulNumbers() {
                result = num1*num2;
        }
        public void divNumbers() {
                if(num2!=0)
                result = num1/num2;
                else
                result=0;
                
        }
}

public class TestMathOperations {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter 2 numbers: ");
                double d1=sc.nextDouble();
                double d2=sc.nextDouble();
                MathOperations m1 = new MathOperations(d1,d2);
                m1.addNumbers();
                System.out.println(m1.getResult());
                m1.subNumbers();
                System.out.println(m1.getResult());
                m1.mulNumbers();
                System.out.println(m1.getResult());
                m1.divNumbers();
                System.out.println(m1.getResult());
                
        }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
with PHP Create a class called Invoice that a hardware store might use to represent an...
with PHP Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables — a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables-a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for...
With PHP Create a class called Account that a bank might use to represent customers' bank...
With PHP Create a class called Account that a bank might use to represent customers' bank accounts. Your class should include one data member of type int to represent the account balance. Your class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0. If not, the balance should be set to 0 and the...
Windows Forms application using Visual Basic: Create a class called Character that a role-playing game might...
Windows Forms application using Visual Basic: Create a class called Character that a role-playing game might use to represent a character within the game. A character should include six stats as instance variables – strength, dexterity, constitution, intelligence, wisdom and charisma (all types are int). Your class should have a constructor that initializes these six instance variables. Provide Properties with an appropriate set and get block for each instance variable. In addition, provide a method named getStatsTotal that calculates the...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called DataBaseManager as in Lecture 5 and create a database table in SQLite, called StudentInfo. The fields for the StudentInfo table include StudentID, FirstName, LastName, YearOfBirth and Gender. Include functions for adding a row to the table and for retrieving all rows, similar to that shown in lecture 5. No user interface is required for this question, t -Continuing from , follow the example in...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
. Create a class called Book to represent a book. A Book should include four pieces...
. Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. Inaddition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT