Define a class called counter whose objects count things.

Assignment Help JAVA Programming
Reference no: EM13941249

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);
}}

Reference no: EM13941249

Questions Cloud

Firm pays out its excess cash as a cash dividend : A firm has a market value equal to its book value. Currently, the firm has excess cash of $7,700 and other assets of $27,300. Equity is worth $35,000. The firm has 700 shares of stock outstanding and net income of $3,500. What will the stock price pe..
Standard deviation of the portfolio return : Mr. Jones has a 2-stock portfolio with a total value of $560,000. $225,000 is invested in Stock A and the remainder is invested in Stock B. If standard deviation of Stock A is 16.80%, Stock B is 10.75%, and correlation between Stock A and Stock B is ..
Difference between paging and swapping : a) Explain the term ‘Process swapping'. Why is process swapping necessary in a typical modern operating system? Include an explanation of the difference between paging and swapping.
Spend all of excess cash on share repurchase program : A firm has a market value equal to its book value. Currently, the firm has excess cash of $2,000 and other assets of $4,800. Equity is worth $6,800. The firm has 850 shares of stock outstanding and net income of $1,050. The firm has decided to spend ..
Define a class called counter whose objects count things. : Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer
Environment in which human resource managers work : The environment in which human resource managers work has changed progressively over the last 2 to 3 decades and it is forecast to continue to change.
Development of management thoughts : Critically examine the central contribution of M Weber to development of management thoughts. Relevant issues to address would include: which management function or or topic is the thinker's contribution closely related?
The coupon rate is paid semi annually : BCD's $1,000 par value bonds currently sell for $798.40. The coupon rate is 10%, paid semi-annually. If the bonds have 5 years to maturity, what is the yield to maturity?
Compare an algorithm run in a parallel pipeline : Calculate the time unit and the number of processors needed for each type of systems in (i) using up to 20 processors and the size of data is n= 1000

Reviews

Write a Review

JAVA Programming Questions & Answers

  Create simple java program that determine cost of insurance

Need to create a simple Java program that determines cost of insurance by including any additional person based on age (thus questions regarding age and how many people apply to each category as far as age should be included)

  Modify the numbers guessing game program

Modify the numbers guessing game program. Suppose that the variable num and guess are as declared and the diff is an int variable.

  Build the gui layout of the game

Build the Build the GUI layout of the game in java.

  Explain the use of file input and output in a java program

Getting input from the keyboard and sending output to the screen has limitations. If the amount of input data is large, it is inefficient to type it each time you run a program.

  An elementary calculation yields the result

An elementary calculation yields the result that theoretically, the probability of turning up 7 when two dice are thrown is 1/6, or .166666. But what if more dice are thrown? If 3 dice are thrown, what is the probability that some 2 of three sum to 7..

  Complete the twins() method

Complete the twins() method, which takes an integer argument n and prints all the sets of twin primes that are less than n, one pair per line

  Write java code to read integers from an input file

write java code to read integers from an input file and write only the odd numbers to an output file. the two file names will be provided on the command line as the input file followed by the output file.

  Write a restful web service demo example using spring mvc

Write a restful web service demo example using spring mvc

  Design a java application that will read a file containing

Design a Java application that will read a file containing data

  Implement a shopping cart class with user interface

project will be to implement a shopping cart class with user interface (UI) that contains main() in Net Beans. The UI class will be used to perform user input/output and to invoke the appropriate methods of shopping cart class. When your program star..

  What is the output of the following program

How can you tell whether an exception is checked or unchecked? What is required of checked exceptions that is not required of those that are unchecked? What is the output of the following program

  Write a functions that takes an array of doubles

1. Write a function that is passed a single integer and then calculates the factorial of that number. A factorial is the product of the integer times all of the integers below it stopping at 1. So n!= n*(n-1)*(n-2).......3.2.1

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd