Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

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

Java, Programming

  • Category:- Java
  • Reference No.:- M91612122
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Java

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

Assessment instructionsin this assessment you will design

Assessment Instructions In this assessment, you will design and code a simple Java application that defines a class, instantiate the class into a number of objects, and prints out the attributes of these objects in a spe ...

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

Can someone kindly help me to consider whether java

Can someone kindly help me to consider whether Java provides the facility of operator overloading? If it does, may you kindly describe how overloading operators can be accomplished? If not, may you kindly describe why yo ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As