Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The single instance variable (counter) of a counter object holds non-negative in

ID: 3634815 • Letter: T

Question


The single instance variable (counter) of a counter object holds non-negative integer. The method of the counter class allows a client to add 1 to counter, set the value of counter to zero, and retrieve the current value of counter. The default constructer sets counter to zero, and the one-argument constructer initializes counter to non- negative integer.
Implement a counter class and test your class by writing a main(…) method that
. Instantiates a counter object
. Interactively reads a sequence of integers until a zero is entered.
. and uses the counter object to determine how many non-zero integers compromise the sequence

Explanation / Answer

Hope this helps, please rate! import java.util.Scanner; public class Counter { int counter; public Counter(){ counter = 0; } public Counter(int count){ if(count >= 0){ counter = count; } } public void add(){ counter++; } public void setZero(){ counter = 0; } public int getCount(){ return counter; } public static void main(String[] args) { Counter test = new Counter(); Scanner sc = new Scanner(System.in); System.out.println("Enter Numbers (0 to exit)"); int x = sc.nextInt(); while(x != 0){ test.add(); x = sc.nextInt(); } System.out.println("Non-zero numbers entered: " + test.getCount()); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote