Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this exercise, we are going to take a look at an alternateCalculatorclass, but this one is broken. There are several scope issues in the

For this exercise, we are going to take a look at an alternateCalculatorclass, but this one is broken. There are several scope issues in the calculator class that are preventing it from running.

fix theCalculatorclass so that it runs and prints out the correct results. TheCalculatorTesteris completed and should function correctly once a fix theCalculatorclass.

public class CalculatorTester {   public static void main(String[] args) {     System.out.println("Starting at 5");     Calculator myTi = new Calculator(5);     System.out.println("Adding 10 ...");     System.out.print("Should print 15: ");     System.out.println(myTi.add(10));     System.out.println("Multiplying by 2 ...");     System.out.print("Should print 30: ");     System.out.println(myTi.multiple(2));     System.out.println("Changing value to 20 ...");     myTi.setValue(20);     System.out.print("Adding. Should print 50: ");     System.out.println(myTi.add());   } } 

public class Calculator {   private int total;   private int value;   public Calculator(int startingValue){     int total = startingValue;     value = 0;   }   public int add(int value){     int total = total + value;     return total;   }   /**   * Adds the instance variable value to the total   */   public int add(){     int total += value;     return total;   }   public int multiple(int value){     int total *= value;     return total;   }   public void setValue(int value){     value = value;   }   public int getValue(){     return value;   } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions