Question

In: Computer Science

Please explain clearly which line means what and how can I get the answer. (correct answer...

Please explain clearly which line means what and how can I get the answer.
(correct answer 2 2)

public class Checker 
{
    public static int count = 0;
    public int number = 0;

    public Checker(){
        count++;
        number = count;
    }

    public int getCount() { return count; }
    public int getNumber() { return number; }
}

What is output from the code fragment below?

Checker one = new Checker();
Checker two = new Checker();
System.out.println(one.getCount() + " " + two.getCount());

Solutions

Expert Solution

code:

class Checker
{
public static int count = 0; //static variable (common to all instatnces(or objects) of class)
public int number = 0;

public Checker(){ //constructor (executed every time object is ceated)
count++; //count increament (here value of count will increase by 1 everytime the object is craeted)
number = count; //sets number variable equal to count
}

public int getCount() { return count; } //returns the value of static variable count
public int getNumber() { return number; } //returns value of variable number
  
}
public class Main{
public static void main(String a[])
{
Checker one = new Checker(); //constructor executed(setting count=1 and number=count i.e. number=1)
Checker two = new Checker(); //again constructor executed (setting count=2 now and likewise number=count=2)
System.out.println(one.getCount() + " " + two.getCount());
//getCount here will now return value 2 since count variable is static (i.e. it is common to both objects created here)
//if getNumber used, one.getNumber=1 and two.getNumber=2 because separate copy if each variable is maintained for number variable
  
}
}


Related Solutions

Please see answer below. Can someone please explain how to get the answer with excel? Company...
Please see answer below. Can someone please explain how to get the answer with excel? Company C is seeking for help to decide this option to choose to upgrade their current bottleneck equipment. There are two vendors and one rental option. The cost details are shown in the table below. Option Vender R Vender T Rental Initial Cost 75000 125000 0 Annual Operation Cost 28000 12000 52000 Salvage Value 0 30000 0 Estimated Life in Year 2 3 Maximum 3...
The correct answers are already bolded, but please show your work/explain how you get the answer....
The correct answers are already bolded, but please show your work/explain how you get the answer. 1. Rick can cut 6 pieces of wood or catch 6 fish per day. Timmy can cut 9 pieces of wood or catch 18 fish per day. Which one of the following points is not on the Timmy’s production possibility frontier fish is on the horizontal axis and pieces of wood is on the vertical axis? a. (0, 9) b. (18, 0) c. (8,...
Please select the correct answer: 1. The patient ask nurse : how can I prevent the...
Please select the correct answer: 1. The patient ask nurse : how can I prevent the reoccurrence of urinary tract infection?. The nurse uses evidenced-based practice and advised patient about which of the following? A – Take anaphylactic antibiotics B – Urinate regularly every 3 hours C – Consume cranberry juice D – Obtain a urinalysis monthly 2. A patient with acute kidney injury is undergoing dialysis for the first time. The charge nurse monitor the patient closely for dialysis...
Can I get a breakdown line by line of the time complexity, I know it is...
Can I get a breakdown line by line of the time complexity, I know it is O(NlogN) but I am trying to better understand time complexity as a whole. Input :- N , Array[N]                                function findSmallestPair()                                sort the Array                                int minimumDiff := INFINITY , A := -1 , B := -1;                                  for(int i : 1 to N-1)                                begin for                currentDiff = Array[i]-Array[i-1]                if(currentDiff < minimumDiff)                begin if   ...
Can you please explain how to get the primers?
Can you please explain how to get the primers?
Please answer in less than an hour :) Can you explain and show how we get...
Please answer in less than an hour :) Can you explain and show how we get the Specific Cutting pressure in GPa if vertical force is 0.882 kN, feed rate is .0508 mm/rev, and depth of the cut is 1.8161 mm.  
Please, explain your answer clearly (step-by-step), so I can trace the output without using the Visual...
Please, explain your answer clearly (step-by-step), so I can trace the output without using the Visual Studio Program. Please, clearly describe how the mechanism of the loop works. The answers in bold. What is printed by the following code snippet? for (int i = 0; i < 5; i++) {    for (int j = 0; j <= i; j++)    {       cout << i << " ";    } } 0 1 2 3 4 5 0 0...
Which of the following statements is NOT correct? (Please explain why answer E is correct) a)corporate...
Which of the following statements is NOT correct? (Please explain why answer E is correct) a)corporate governance is the set of rules that control a company's behavior towards its directors, managers, employees, shareholders, creditors, customers, competitors, and community b) agency problem is that managers may act in their own interests and not on behalf of stockholders. c) Corporate governance is the set of rules that control a company's behavior towards its directors, managers, employees, shareholders, creditors, customers, competitors, and community....
Please show how you get to the answer and explain it! Please thoroughly show how to...
Please show how you get to the answer and explain it! Please thoroughly show how to do this! I'm so confused. Given the following information for Gator Company who uses the LIFO method: ​ ​ ​ ​ Net ​ NRV Minus ​ ​ ​ Realizable Replacement Normal Item Quantity Cost Value Cost Profit 1 1 $17.70 $24.60 $18.00 $17.10 2 1 10.80 8.28 9.30 5.58 3 1 72.00 64.80 67.20 57.60 4 1 4.80 3.12 2.88 2.64 5 1 12.00...
Please, I need a correct answer and clear explanation. Thanks, Explain how a prepaid expense differs...
Please, I need a correct answer and clear explanation. Thanks, Explain how a prepaid expense differs from an accrued expense.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT