Purchase Solution

Counting Programs in Java

Not what you're looking for?

Ask Custom 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 will be no input method or other mutator methods. The only method that can set the counter is the one that sets it to zero. Also, include a toString method and an equals method. Write a program (or programs) to test all the methods in your class definition.

Hint:

The CounterDemo class assumes that the Counter class will have methods named resetToZero(), increment(), decrement(), and getValue(). Make sure that you implement these methods.

If the output looks like:

Initial value is 0
After two increments, value is Counter@cf8583
After one decrement, value is Counter@cf8583
Result of calling counter.output() :
Counter value is 1
Counter@cf8583 equals Counter@4693c7? false
Counter@cf8583 equals Counter@4693c7? true
After resetting to zero, value is Counter@4693c7

then you have not properly defined a method named toString() that returns a string representation of the Counter's value. The strange output (e.g., "Counter@4693c7") nis produced by the default implementation of toString() in nclass java.lang.Object. n

The getValue(), equals(...), and toString() methods must be declared to return a value, and must include a return statement in the body of the method. The other methods should be declared with a return type of void. In this project, these methods should not include a return statement.

/**
Demo program that exercises the methods of the Counter class
*/
public class CounterDemo {

public static void main(String[] args) {
// Make a new counter
Counter counter = new Counter();
System.out.println("Initial value is " + counter.getValue());

// Test the increment and toString() methods.
counter.increment();
counter.increment();
System.out.println(
"After two increments, value is " + counter.toString());

// Test the decrement method
counter.decrement();
System.out.println("After one decrement, value is " + counter);

// Test the output() method
System.out.println("Result of calling counter.output() :");
counter.output();

// Make a second counter to test equals.
Counter counter2 = new Counter();
System.out.println(counter + " equals " + counter2 + "? " +
counter.equals(counter2));

// Increment counter2 so that they are equal
counter2.increment();
System.out.println(counter + " equals " + counter2 + "? " +
counter.equals(counter2));

// Reset counter2 to zero
counter2.resetToZero();
System.out.println("After resetting to zero, value is " + counter2);
}

}

/**
Class that counts things.
*/
class Counter {
/**
Current value of the counter
*/
private int value = 0;

// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------

// --------------------------------
// --------- END USER CODE --------
// --------------------------------

/**
Prints the current value to System.out
*/
public void output() {
System.out.println("Counter value is " + value);
}

}

Purchase this Solution

Solution Summary

This solution provides a counting program written in Java.

Purchase this Solution


Free BrainMass Quizzes
Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

C++ Operators

This quiz tests a student's knowledge about C++ operators.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.