Question

In: Computer Science

Download the data type class named as MathOperation:(given below) Create the project of part 2 then...

Download the data type class named as MathOperation:(given below)
Create the project of part 2 then add class as a data type class and class MathCalculator_yourLastName as a driver class with main() of part 2
a. Draw UML of class MathOperation (See the topic about UML at TIP FOR LAB on eCampus)
b. Create the pseudo-code or draw flowchart of the main of a driver class based on the
requirement listed below (see the topic about pseudo-code or flowchart at TIP FOR LAB on
eCampus)
c. Copy the content of the class MathOperation downloaded from eCampus to the data type class
you have added to the part 2
Requirement: Provide the application that first displays the following line and menu: File name: MathCalculator_Smith
MENU – CALCULATOR ON TWO NUMBERS
1. Add two integers
2. Subtract two integers
3. Multiply two integers
4. Divide two integers
5. Add two decimal numbers
6. Subtract two decimal numbers
7. Multiply two decimal numbers
8. Divide two decimal numbers
0. Exit
Enter a number from 0 to 8 to continue:
Based on the number that users enter to continue ask for input:
-If users enter 1 to 4: ask for two integer numbers -if users enter 5 to 8: ask for two decimal numbers
Then pass two numbers read from input to the object of class MathOperation
Use the object to display the result. The result in one of the following.
After getting the result, the program should re-display the menu to allow users to continue using the
application to select other tasks.

(mathoperation.java)
//MathOperation.java

import java.util.Scanner;

public class MathOperation
{
//A
int intNum1;
int intNum2;
float fNum1;
float fNum2;
int op;
//B
public MathOperation()
{
intNum1 = 0;
intNum2 = 0;
fNum1 = 0;
fNum2 = 0;
op = 0;
}
//C
public MathOperation(int n1, int n2, int n)
{
intNum1 = n1;
intNum2 = n2;
fNum1 = 0;
fNum2 = 0;
op = n;
}
//D
public MathOperation(float d1, float d2, int n)
{
fNum1 = d1;
fNum2 = d2;
intNum1 = 0;
intNum2 = 0;
op = n;
}
//E
public String toString()
{
String str = "CALCULATOR OF LIEM LE";
String str2 = "", str3 = "";
int iRet= 0;
float fRet = 0.0f;
switch (op)
{
case 1: // add 2 int
str2 = "ADD TWWO INTEGERS";
iRet = intNum1 + intNum2;
str3 = intNum1 + " + " + intNum2 + " = " + iRet;
break;
case 2: // minus 2 int
str2 = "MINUS TWWO INTEGERS";
iRet = intNum1 - intNum2;
str3 = intNum1 + " - " + intNum2 + " = " + iRet;
break;
case 3: // multiply 2 int
str2 = "MULTIPLY TWWO INTEGERS";
iRet = intNum1 * intNum2;
str3 = intNum1 + " * " + intNum2 + " = " + iRet;
break;
case 4: // divide 2 int
str2 = "DIVIDE TWWO INTEGERS";
iRet = intNum1 /intNum2;
str3 = intNum1 + " / " + intNum2 + " = " + iRet;
break;
case 5: // add 2 float
str2 = "ADD TWWO DECIMAL NUMBERS";
fRet = fNum1 + fNum2;
str3 = fNum1 + " + " + fNum2 + " = " + fRet;
break;
case 6: // minus 2 floaf
str2 = "MINUS TWWO DECIMAL NUMBERS";
fRet = fNum1 - fNum2;
str3 = fNum1 + " - " + fNum2 + " = " + fRet;
break;
case 7: // multiply float
str2 = "MULTIPLY TWWO DECIMAL NUMBERS";
fRet = fNum1 * fNum2;
str3 = fNum1 + " * " + fNum2 + " = " + fRet;
break;
case 8: // divide float
str2 = "DIVIDE TWWO DECIMAL NUMBERS";
fRet = fNum1 / fNum2;
str3 = fNum1 + " / " + fNum2 + " = " + fRet;
break;
}
return str + "\n" + str2 + "\n" + str3 + "\n";
}
}

Solutions

Expert Solution

//Java code

public class MathOperation
{
    //A
    int intNum1;
    int intNum2;
    float fNum1;
    float fNum2;
    int op;
    //B
    public MathOperation()
    {
        intNum1 = 0;
        intNum2 = 0;
        fNum1 = 0;
        fNum2 = 0;
        op = 0;
    }
    //C
    public MathOperation(int n1, int n2, int n)
    {
        intNum1 = n1;
        intNum2 = n2;
        fNum1 = 0;
        fNum2 = 0;
        op = n;
    }
    //D
    public MathOperation(float d1, float d2, int n)
    {
        fNum1 = d1;
        fNum2 = d2;
        intNum1 = 0;
        intNum2 = 0;
        op = n;
    }
    //E
    public String toString()
    {
        String str = "CALCULATOR OF LIEM LE";
        String str2 = "", str3 = "";
        int iRet= 0;
        float fRet = 0.0f;
        switch (op)
        {
            case 1: // add 2 int
                str2 = "ADD TWO INTEGERS";
                iRet = intNum1 + intNum2;
                str3 = intNum1 + " + " + intNum2 + " = " + iRet;
                break;
            case 2: // minus 2 int
                str2 = "MINUS TWO INTEGERS";
                iRet = intNum1 - intNum2;
                str3 = intNum1 + " - " + intNum2 + " = " + iRet;
                break;
            case 3: // multiply 2 int
                str2 = "MULTIPLY TWO INTEGERS";
                iRet = intNum1 * intNum2;
                str3 = intNum1 + " * " + intNum2 + " = " + iRet;
                break;
            case 4: // divide 2 int
                str2 = "DIVIDE TWO INTEGERS";
                iRet = intNum1 /intNum2;
                str3 = intNum1 + " / " + intNum2 + " = " + iRet;
                break;
            case 5: // add 2 float
                str2 = "ADD TWO DECIMAL NUMBERS";
                fRet = fNum1 + fNum2;
                str3 = fNum1 + " + " + fNum2 + " = " + fRet;
                break;
            case 6: // minus 2 floaf
                str2 = "MINUS TWO DECIMAL NUMBERS";
                fRet = fNum1 - fNum2;
                str3 = fNum1 + " - " + fNum2 + " = " + fRet;
                break;
            case 7: // multiply float
                str2 = "MULTIPLY TWO DECIMAL NUMBERS";
                fRet = fNum1 * fNum2;
                str3 = fNum1 + " * " + fNum2 + " = " + fRet;
                break;
            case 8: // divide float
                str2 = "DIVIDE TWO DECIMAL NUMBERS";
                fRet = fNum1 / fNum2;
                str3 = fNum1 + " / " + fNum2 + " = " + fRet;
                break;
        }
        return str + "\n" + str2 + "\n" + str3 + "\n";
    }
}

//================================================

import java.util.Scanner;

public class Main {
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args)
    {
        int choice=-1;


        while (true)
        {
            displayMenu();
            choice = input.nextInt();
            switch (choice)
            {
                case 1:
                   calculate(choice);
                    break;
                case 2:
                    calculate(choice);
                    break;
                case 3:
                    calculate(choice);
                    break;
                case 4:
                    calculate(choice);
                    break;
                case 5:
                    calculate(choice);
                    break;
                case 6:
                    calculate(choice);
                    break;
                case 7:
                    calculate(choice);
                    break;
                case 8:
                    calculate(choice);
                    break;
                case 0:
                    break;
                    default:
                        System.err.println("Wrong Choice... try again..");
                        break;
            }
            if(choice==0)
                break;
        }
    }

    /**
     * Calculate the result and display
     * @param choice
     */
    public static void calculate(int choice)
    {
        int num1,num2;
        float numf1,numf2;
        //Object of MAthOperation
        MathOperation mathOperation= null;
        //ask for int for choice 1-4
        if(choice>=1 && choice<5)
        {
            System.out.println("Enter first number: ");
            num1 = input.nextInt();
            System.out.println("Enter second number: ");
            num2 = input.nextInt();
            mathOperation = new MathOperation(num1,num2,choice);
            System.out.println(mathOperation);
        }
        //ask for float numbers from 5-8
        else
        {
            numf1 = input.nextFloat();
            System.out.println("Enter second number: ");
            numf2 = input.nextFloat();
            mathOperation = new MathOperation(numf1,numf2,choice);
            System.out.println(mathOperation);
        }
    }
    //Finction to display menu
    public static void displayMenu()
    {
        System.out.println("1. Add two integers\n" +
                "2. Subtract two integers\n" +
                "3. Multiply two integers\n" +
                "4. Divide two integers\n" +
                "5. Add two decimal numbers\n" +
                "6. Subtract two decimal numbers\n" +
                "7. Multiply two decimal numbers\n" +
                "8. Divide two decimal numbers\n" +
                "0. Exit\nEnter choice: ");
    }
}

//UML Class Diagram

+ sign represents public

- sign represents private

underline method represents static

//Pseudocode

(1) Start the Main class and write main() method

(2) Create a static method displayMenu() which returns void and takes 0 arguments and print the menu

(3) Create a static method calculate(), which returns void and takes one argument of int type.

(4) in calculate(int choice),

(1) create an object of MathOperation() class and initialize it to null and write if-else statement

(2) if( choice >=1 && choice<5) then prompt for int numbers and call the constructor and pass two int numbers and choice

(3) else prompt for float numbers and call the constructor and pass two float numbers and choice

(5) create a infinite while loop in main()

while(true):

call the displayMenu()

and get the choice from user

swich(choice):

//create cases according to the menu followed by break statement

if(choice==0)

break the loop and exit program.

(6) End of main()

//output

// If you need any help regarding this solution .......... please leave a comment ..... thanks


Related Solutions

Java Create a Project named Chap4b 1. Create a Student class with instance data as follows:...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows: student id, test1, test2, and test3. 2. Create one constructor with parameter values for all instance data fields. 3. Create getters and setters for all instance data fields. 4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program. 5. Create a displayInfo method that receives the average from the driver program and displays...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Part I – Understand a Given Class (die class) (40 pts) • Download the die class...
Part I – Understand a Given Class (die class) (40 pts) • Download the die class (attached as usingDieClass.cpp), save it as LabUseDieClassFirstName1_FirstName2.cpp • Add proper opening comments at the beginning of the program. The comments must include the description of all three parts of this lab. You may want to modify the comments after all parts are done to be sure that it is done properly. • Run the program and answer the following questions: o How many objects...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include...
Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include member variables for name (string), price (double), shares (double). Write a default constructor. Write a constructor that takes values for all member variables as parameters. Write a copy constructor. Implement Get/Set methods for all member variables. Implement the CalculateValue function. This function should multiply the prices by the shares and return that value. Use the following function header: double CalculateValue(). Add a member overload...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT