Question

In: Computer Science

What is displayed to the screen when the following code executes? Counter c1 = new Counter();...

 What is displayed to the screen when the following code executes?

Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
Counter[] A = {c1, c2};
JAVA

A[0].clickButton();
Counter[] B = new Counter[2];
for(int i = 0; i < A.length; i++)
    B[i] = A[i];
B[0] = c3;
System.out.println("A[0] is " + A[0].getCount());
System.out.println("B[0] is " + B[0].getCount());

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

It depends on the code for the count class.

If it is as below.

package staticclasses;


class Counter{
   private int count;
  
   Counter(){
       count=0;
   }
   public void clickButton() {
       count++;
   }
   public int getCount() {
       return count;
   }

}
public class Main {
   public static void main(String[] args) {

       Counter c1 = new Counter();
       Counter c2 = new Counter();
       Counter c3 = new Counter();
       Counter[] A = {c1, c2};
       A[0].clickButton();
       Counter[] B = new Counter[2];
       for(int i = 0; i < A.length; i++)
           B[i] = A[i];
       B[0] = c3;
       System.out.println("A[0] is " + A[0].getCount());
       System.out.println("B[0] is " + B[0].getCount());
   }
}

output will be


Related Solutions

What are the values in arrays a, b, and c after the following code executes (list...
What are the values in arrays a, b, and c after the following code executes (list all of the elements of the arrays)? double[] a = new double[4]; double[] b = {6,4,2}; a[a.length-1] = b[b.length-1]; double[] c = b; c[0] = -1; b[1] = c[2]; c = a; c[0] = -2; a[1] = c[3];
What will the following code segments print on the screen? int num = 3; switch (num){...
What will the following code segments print on the screen? int num = 3; switch (num){             case 1:                         System.out.println(“Spring”); case 2:                         System.out.println(“Summer”); case 3:                         System.out.println(“Autumn”); case 4:                         System.out.println(“Winter”); }
How can I use the R language to code a new group to screen some variables?...
How can I use the R language to code a new group to screen some variables? for example, there is a data set include 5 candy's brand: a b c d e I just want to create a new group include a, b, c variable. How can I code it?
​​​​​​​in c code Add an intro screen Ask the user for their name, what type of...
​​​​​​​in c code Add an intro screen Ask the user for their name, what type of cookie they would like to order and how many. The types are sugar, chocolate chip, and peanut butter. Assign a cost to each cookie. Show total for cookie purchase and ask if the would like to place another order. Include at least one function and one loop. Add one extra feature. For example, a sample execution of your code would be as follows: Cookie...
After the code executes (in C++), what will the linked list 8->4->3->7->5 look like? (8 is...
After the code executes (in C++), what will the linked list 8->4->3->7->5 look like? (8 is the head) 1a) Node* newNode = new Node(26); newNode->next = head->next; head = newNode; 1b) Node* curNode = head; for(int i=0; i<2; i++) curNode = curNode->next; Node* temp = curNode->next; curNode->next = curNode->next ->next; delete temp; temp = nullptr;
SOLVE FOLLOWING a.   Desgin and VERILOG code of a 3 bit up down counter USING T...
SOLVE FOLLOWING a.   Desgin and VERILOG code of a 3 bit up down counter USING T FLIP FLOP..... b. using behavioural module.Write a verilog discription of an N-BIT up down binary counter. Record the simulation output waveform in observation.....
Using the following VHDL code for an 8 bit adder, make the sum be displayed on...
Using the following VHDL code for an 8 bit adder, make the sum be displayed on the seven segment display of an Elbert V2 Spartan 3A FPGA Board. VHDL: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity state_bit_adder is Port ( clk : in STD_LOGIC; reset : in STD_LOGIC;            D : in STD_LOGIC;            Enable : out STD_LOGIC_vector (2 downto 0);            input: in std_logic_vector(7 downto 0);            SUM: out...
Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file....
Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file. Postfix Expression                Result 4 5 7 2 + - * = -16 3 4 + 2  * 7 / = 2 5 7 + 6 2 -  * = 48 4 2 3 5 1 - + * + = 18    List as Stack """ File: pyStackPostfix.py Author: JD """ # 5 7 + 6 2 -  * = 48 print("Postfix Calculator\n") stack = []              # Empty stack...
Thinking in Assembly language What values will be written to the array when the following code...
Thinking in Assembly language What values will be written to the array when the following code executes? .data array DWORD 4 DUP(0) .code main PROC mov eax,10 mov esi,0 call proc_1 add esi,4 add eax,10 mov array[esi],eax INVOKE ExitProcess,0 main ENDP proc_1 PROC call proc_2 add esi,4 add eax,10 mov array[esi],eax ret proc_1 ENDP proc_2 PROC call proc_3 add esi,4 add eax,10 mov array[esi],eax ret proc_2 ENDP proc_3 PROC mov array[esi],eax ret proc_3 ENDP
Design a Count-up Counter in Aiken code with following flip flops: a) D-FF (Active edge is...
Design a Count-up Counter in Aiken code with following flip flops: a) D-FF (Active edge is high to low) b) SR-FF (Active edge is high to low) c) Use of output of circuit in part (b) and minimum number of logic gates for getting the Countdown counter in Aiken code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT