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]
3 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:
vekshin13 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
Which of the following is NOT an argument used to determine how a shot is fired from an A. object the shot itself being instanti
Firdavs [7]
C. The color of the shot...
7 0
4 years ago
Create a variable total of type Double set to 0. Then loop through the dictionary, and add the value of each integer and double
svp [43]

Answer:

total = 0.0

for x in dictionary.values():

   if x == str(x):

       total += 1

   elif x is bool:

       total += 2 if x is True else total -3

   else:

       total += x

print( total )

Explanation:

The python source code defines a variable 'total' and iterates through a dictionary and adds its values to the total variable. Finally, the total value is printed.

6 0
3 years ago
Which statement is true of the digital sound produced by a computer using a<br> lower sample rate?
konstantin123 [22]

Answer:

The digital sound is less likely to match the sound it is copying.

Explanation:

8 0
2 years ago
To move one slice of a pie chart away from the pie creates a(n) ____. perfect pie chart spontaneous pie chart exploded pie chart
seraphim [82]
To move one slice of a pie chart away from the pie creates a(n) ____. exploded pie chart
5 0
4 years ago
Read 2 more answers
Should I Ban Pog on my Discord Server
IRINA_888 [86]

Answer:

Do as you wish :)

6 0
3 years ago
Read 2 more answers
Other questions:
  • What is collaboration software?
    7·2 answers
  • Who is a miner?
    9·1 answer
  • Digital cameras are less susceptible to UV light true or false
    13·2 answers
  • Adding all the three primary colors-red, green, and blue-at maximum intensity produces the color _____, while adding any two of
    9·1 answer
  • Create a class that acts as a doubly linked list node. it should contain all the required data fields, at least 1 constructor, a
    7·1 answer
  • Love me love me say that u love me fool me fool me go on and fool me : ) answer the question thx
    6·2 answers
  • The relational algebra expressions can be rearranged and achieve the same result but with a better processing time. A selection
    8·1 answer
  • Handhed computer is otherwise called as<br> 1.laptop<br> 2.Notebook<br> 3.Palmtop
    7·1 answer
  • What kind of material is used for DRAM (dynamic random-access memory)?
    13·1 answer
  • A help desk technician determines that a user's issue is caused by a corrupt file on their computer. What is the fastest way to
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!