SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Defining Classes I
Exercise:
Programming Projects
Question:5 | ISBN:9780132830317 | Edition: 5

Question

Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Be sure that no method allows the value of the counter to become negative. Include an accessor method that returns the current count value and a method that outputs the count to the screen. There should be no input method or other mutator methods. The only method that can set the counter is the one that sets it to 0. Also, include a toString method and an equals method. Write a program (or programs) to test all the methods in your class definition.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Explanation:
The following program contains the Counter class and the CounterTest class. 
The Counter class contains, 
•    The setCounter() method to set the counter to 0.
•    The incCounter() method to increment the counter.
•    The dscCounter() method to decrement the counter.
•    The getCount() method to get the counter value.
•    The displayCount() method to display the counter value
•    The toString() method to returns the Counter value of the class.

The CounterTest class is used to test the Counter class and to use all the method in the class named Counter.
 

Program:


/************ File name: Counter.java  ***********/
//  Create a class called Counter
public class Counter
{
	// Declare a integer variable 
	private int count;

	// Create a method named setCounter().
	// This method helps to set the counter to 0.
	public void setCounter()
	{
		count = 0;
	}
  
	// Create a method named incCounter().
	// This method helps to increase the count by 1.
	public void incCounter()
	{
		count += 1;
	}
  
	// Create a method named dscCounter().
	// This method helps to decrease the count by 1.	
	public void dscCounter()
	{
		// Create an if-statement to check the counter
		// is a positive integer or not.
		// To avoid negative values.
		if (count > 0)
		{
			count -= 1;
		}		
	}
	
	// Accessor Method
	// Create an accessor method to get the current count value.
	public int getCount()
	{
		return count;
	}
	
	// Create a method named displayCount().
	// This method helps to outputs the count to the screen. 
	public void displayCount()
	{
		System.out.println("Current Count Value: " + count);
	}

	// Create a toString() method.	
	public String toString()
	{
		return "Counter [count=" + count + "]";
	}
	
	// Create a equals() method.
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Counter other = (Counter) obj;
		if (count != other.count)
			return false;
		return true;
	}


 }

 

/************ File name: CounterTest.java ************/
// Create a class named CounterTest
public class CounterTest
{
	// Create a main() method to run the program.
	public static void main(String[] args)
	{
		// Create an object named cObj for the Counter class.
		Counter cObj = new Counter();
		
		// Set Counter
		cObj.setCounter();		
		// Display Counter
		cObj.displayCount();
		
		// Increment the counter
		cObj.incCounter();		
		// Display Counter
		cObj.displayCount();
		
		cObj.incCounter();
		cObj.displayCount();
		
		// Create another counter object
		Counter cObj2 = new Counter();
		cObj2.setCounter();
		cObj2.incCounter();
		
		
		// Display counter values of two objects
		System.out.println("cObj Count Value: "+cObj.getCount());
		System.out.println("cObj2 Count Value: "+cObj2.getCount());
		
		// Check first counter object is eaual to the second counter object or not.
		System.out.println("cObj is equal to cObj2: "+cObj.equals(cObj2));
		
		// Decrement the counter.
		cObj.dscCounter();
		
		// Display the counter using the toString().
		System.out.println("cObj: "+cObj.toString());
		System.out.println("cObj2: "+cObj2.toString());
		
		System.out.println("cObj is equal to cObj2: "+cObj.equals(cObj2));
		
	}
	
}

 

Output:

Current Count Value: 0
Current Count Value: 1
Current Count Value: 2
cObj Count Value: 2
cObj2 Count Value: 1
cObj is equal to cObj2: false
cObj: Counter [count=1]
cObj2: Counter [count=1]
cObj is equal to cObj2: true

 

0 0

Discussions

Post the discussion to improve the above solution.