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:
Stuart Reges, Marty Stepp
Chapter:
Inheritance And Interfaces
Exercise:
Exercises
Question:16 | ISBN:9780136091813 | Edition: 2

Question

Declare an interface called Incrementable which represents items that store an integer that can be incremented in some way. The interface has a method called increment that increments the value and a method called getValue that returns the value. Once you have written the interface, write two classes called SequentialIncrementer and RandomIncrementer that implement the interface. The SequentialIncrementer begins its value at 0 and increases it by 1 each time it is incremented. The RandomIncrementer begins its value at a random integer and
changes it to a new random integer each time it is incremented.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Incrementable.java

public interface Incrementable
{
	public void increment();
	public int getValue();
}

SequentialIncrementer.java

public class SequentialIncrementer implements Incrementable
{
	private int value;
	
	public SequentialIncrementer()
	{
		value = 0;
	}
	
	public SequentialIncrementer(int v)
	{
		value = v;
	}
	
	@Override
	public void increment()
	{
		value += 1;
	}

	@Override
	public int getValue()
	{
		return value;
	}
}

RandomIncrementer.java

public class RandomIncrementer implements Incrementable
{
	private int value;
	
	public RandomIncrementer()
	{
		value = 0;
	}
	
	public RandomIncrementer(int v)
	{
		value = v;
	}
	
	@Override
	public void increment()
	{
		value += (int)(Math.random() * 10);
	}

	@Override
	public int getValue()
	{
		return value;
	}
}

Ch09Ex16

public class Ch09Ex16
{
	public static void main(String[] args)
	{
		Incrementable si = new SequentialIncrementer();		
		
		System.out.println("Initial value of SequentialIncrementer: " + si.getValue());
		si.increment();
		si.increment();
		System.out.println("After two increments, new value of SequentialIncrementer: " + si.getValue());
		
		Incrementable ri = new RandomIncrementer();
		
		System.out.println("\nInitial value of RandomIncrementer: " + ri.getValue());
		ri.increment();
		ri.increment();		
		System.out.println("After two increments, new value of RandomIncrementer: " + ri.getValue());
	}
}

Output:

Initial value of SequentialIncrementer: 0
After two increments, new value of SequentialIncrementer: 2

Initial value of RandomIncrementer: 0
After two increments, new value of RandomIncrementer: 7

 

0 0

Discussions

Post the discussion to improve the above solution.