1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
timama [110]
2 years ago
15

For this exercise, we are going to take a look at an alternate Calculator class, but this one is broken. There are several scope

issues in the calculator class that are preventing it from running. Your task is to fix the Calculator class so that it runs and prints out the correct results. The CalculatorTester is completed and should function correctly once you fix the Calculator class.
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;
}
}
Computers and Technology
1 answer:
vekshin12 years ago
7 0

Answer:

public class Calculator {

   private int total;

   private int value;

   

   public Calculator(int startingValue){

       // no need to create a new total variable here, we need to set to the our instance total variable

       total = startingValue;

       value = 0;

   }

   public int add(int value){

       //same here, no need to create a new total variable. We need to add the value to the instance total variable

       total = total + value;

       return total;

   }

   /**

   * Adds the instance variable value to the total

   */

   public int add(){

       // no need to create a new total variable. We need to add the value to the instance total variable

       total += value;

       return total;

   }

   public int multiple(int value){

       // no need to create a new total variable. We need to multiply the instance total variable by value.

       total *= value;

       return total;

   }

   //We need to specify which value refers to which variable. Otherwise, there will be confusion. Since you declare the parameter as value, you need to put this keyword before the instance variable so that it will be distinguishable by the compiler.

   public void setValue(int value){

       this.value = value;

   }

   public int getValue(){

       return value;

   }

}

Explanation:

I fixed the errors. You may see them as comments in the code

You might be interested in
Wireframing and storyboarding are helpful in which step of web development?
Thepotemich [5.8K]
Since wireframing and storyboarding are both part of the developing phase, I would think that the answer is Planning
4 0
3 years ago
Read 2 more answers
How do you destroy data on hard drive?
Nataly_w [17]
You have to destroy the hard drive as it is like a memory for the computer so it can never be deleted
7 0
2 years ago
Read 2 more answers
To share a server folder, access the ____ tab in the folder properties dialog box and click the share button (or from the window
anygoal [31]
Oh lord I have no idea :/ I suck at this yeah idk
5 0
3 years ago
People often want to save money for a future purchase. You are writing a program to determine how much to save each week given t
GenaCL600 [577]
.................................
7 0
3 years ago
Read 2 more answers
Can someone help me plz
8090 [49]
B = x is equal to 5
and
C = “ have special meaning and should not be used when naming variables “.
3 0
2 years ago
Other questions:
  • In today's society, unethical actions are: A) Easier than ever to get away with, because the general public and insurers are les
    13·2 answers
  • Which social network site has 1.5 billion actives users per month?
    15·1 answer
  • Commands are organized into tabs on the
    8·2 answers
  • assume that you want to sort an array have 100000 elements which algorithm (insert sort algorithm or quick sort algorithm) is th
    12·1 answer
  • "Create a Matlab Scrapt that will take the a Matrix A and Matrix B (of AX=B), then calculate the Determinant of Matrix A, Invers
    5·1 answer
  • Susan has always wanted to be a veterinarian. When doing her research, she answers all self-assessments geared toward that caree
    13·1 answer
  • What happens when a flash memory card is installed in a slot and it is recognized by windows?
    6·1 answer
  • Selection Sort List the resulting array after each iteration of the outer loop of the selection sort algorithm. Indicate the num
    10·1 answer
  • How many types of operating systems do we have as from 2010 till date​
    13·1 answer
  • Why would you browse by entering a URL rather than use a link in a Web page
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!