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;
}
}

Answers

Answer 1

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


Related Questions

Please program this in Python.


Create a list of your favorite 4 movies. Print out the 0th element in the list. Now set the 0th element to be “Star Wars” and try printing it out again.

Answers

lst = ["Star Wars", "Movie 2", "Movie 3", "Movie 4"]

print(lst[0])

I wrote my code in python 3.8. Best of luck

Another technique that makes use of the colon slicing technique to directly refer to element places is list slicing. Use a blank value before the first colon to access the first element, then use len() with -1 as the input to access the last element.

What is the 0th element in the list?

The element of the current ArrayList object at the provided index is returned by the get() function of the ArrayList class, which accepts an integer indicating the index value.

As a result, if you supply 0 or list to this method, you can obtain the first element of the current ArrayList.

Use a for loop with range (0, N) to get the first N members of a list. Then, make a new empty list and append the elements of the source list to the new list within the for loop. Range(0, N) iterates in steps of 1 from 0 to N-1. N is not comprised.

Therefore,lst = ["Star Wars", "Movie 2", "Movie 3", "Movie 4"] print(lst[0]).

Learn more about element here:

https://brainly.com/question/19312961

#SPJ2

Identify the symbol. please help!!

Answers

Explanation:

counter bore

Answer:

Diameter

Explanation:

On technical literature or drawings, it could be a symbol to mean diameter.

There's a chupacabra that looks like a mixed alligator , gecko it looks' like that but if you try harming it will it eat you "look up chupacabra only don't try looking up if it eats meat because that thing is a legendary animal and barely haves answers and has been cured alot and lives in the folklore mostly like the woods" it looks scary and cool the same time but some people said it has been instinct but it isn't it is spanish "which is puerto rican" lives in the united states, and mexico.. ( does it eat meat ) hasn't been seen around but you'll see a picture of it..

Answers

Answer:

wait. is this even a question or is it just informing us?

Explanation:

either way it is cool

Other Questions
Find the value of "c". 4c + 18 = 74 * The shadow of a building is 72 feet long. At the same time, John, who is 5 feet tall, casts a shadow that's 6 feet long. How tall is the building? FILL IN THE BLANKS:MY ______ ALL BAD, MY _______ ALL REAL, RIDE HIS _______ IN SOME BIG TALL _______. help me please the first answer gets the brainiest what party looks for a larger military and is stricter on illegalimmigration what peoples did the kievan rus originally come from When factored completely, x3+ 3x2 - 4x-12 equals? what is the smallest atomic radius Please help me ASAP Ill mark Brainly Which conjugated form of the verb SER means "We are?" This is for AP World History and the map is in the picture attached please do your best and will give brainliest and lots of pointsAnalyze the attached map: Based on your learning so far of the time period 1450 - 1750, explain three (3) take-aways from this map (ie: what is happening here? what has changed in history since 1450?) HELPPP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CCC Companys most recent income statement shows (in thousands of dollars) sales $2,000, interest payments $100, and net income $140. Its most recent balance sheet shows (also in thousands of dollars) total debt financing $800. If the total asset turnover ratio computed from the companys most recent financial statements was 1.5, what would we compute return on assets (ROA) to be? (Hint: you should use the DuPont method of analysis.) A. 4.7% B. 10.5% C. 26.7% D. 18.7% E. 3.0% An excess of nutrients in water can be the result of In the United States criminal court system, defendants are? Graph: y=3/4x+5 Please let me know guys Find the length of each segment write your answer in simplest radical form. I NEED HELP ASAP Can anyone help? Ill give brainliest!! :)Describe the types of matter and energy that are transferred or converted during each of these processes (photosynthesis and cellular respiration). Make sure to specify the process involved with each transformation or conversion. Mr. Tang gives a math test worth 90 points that contain 25 questions. Multiple-choice questions are worth 3 points and word problems are worth 4 points. How many of each type of question are there? 100 POINTS! SOLVE FOR X. PIC ATTACHED. Random answers will be reported.