Question

Write a class, Calculator, that can add and multiply numbers. It also keeps a history of all previous results that is returne

Write a class, Calculator, that can add and multiply numbers. It also keeps a history of all previous results that is returned as a cell array. eg cal CalculatorO: cal.multiply(2,3); cal.add(4,4) cal.getResultHistory0 ans ([6], [8 cal.add(3,[3,4]): cal.multiply(4,[4,5]); cal.getResultHistory0 ans ([6], [8], [6,7], [16,201 cal.sumResultHistory: cal.getResultHistory ans ([6], [8], [6,7], [16,20], 63
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Calculator{ // Class Calculator
  
public static void main(String []args){
int[] ansArr; // Array to store results
int index = 0; // Index for ansArr
}
  
public static void getResultHistory(){ // Method to store print Result
System.out.println("{");
for(int i=0; i< ansArr.length; i++){
if((i-1) == null !! i==0){
System.out.println("[");
}
else if(i != 0){
System.out.println(",");
}
System.out.println("%d", ansArr[i]);
if((i+1) == null){
System.out.println("]");
}
}
System.out.println("}");
}
  
public static void multiply(int a, int[] arr){ // Method to calculate Multiplication
int[] multAns;
int val;
for(int i=0; i< arr.length; i++){
val = arr[i];
if(val != null){
ansArr[index++] = a * val;
]
}
ansArr[index++] = null;
}
  
public static void add(int a, int[] arr){ // Method to calculate Addition
int[] addAns;
int val;
for(int i=0; i< arr.length; i++){
val = arr[i];
if(val != null){
ansArr[index++] = a + val;
]
}
ansArr[index++] = null;
}
}

Add a comment
Know the answer?
Add Answer to:
Write a class, Calculator, that can add and multiply numbers. It also keeps a history of all previous results that is returned as a cell array. eg cal CalculatorO: cal.multiply(2,3); cal.add(4,4) cal...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT