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:
Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Chapter:
Object-oriented Design
Exercise:
Exercises
Question:7 | ISBN:9781118771334 | Edition: 6

Question

If we choose an increment of 128, how many calls to the nextValue method from the ArithmeticProgression class of Section 2.2.3 can we make before we cause a long-integer overflow?

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

// Modifying the class in 2.2.3 to solve this problem.
package Data_Structures.Chapter2;

public class Progression {

// instance variable
	protected long current;
	long nthValue = 0;

	/** Constructs a progression starting at zero. */
	public Progression() {
		this(0);
	}

	/** Constructs a progression with given start value. */
	public Progression(long start) {
		current = start;
	}

	/* Returns the next value of the progression. */
	public long nextValue() {
		long answer = current;
		advance(); // this protected call is responsible for advancing the current value
		return answer;
	}

	/* Advances the current value to the next value of the progression. */
	protected void advance() {
		current++;
	}

	/** Prints the next n values of the progression, separated by spaces. */
	public void printProgression(int n) {
		System.out.print(nextValue()); // print first value without leading space
		for (int j = 1; j < n; j++)
			System.out.print(" " + nextValue()); // print leading space before others
		System.out.println(); // end the line
	}

   
}


public class ArithmeticProgression extends Progression {
	protected long increment;

	/* Constructs progression 0,1,2,... */

	public ArithmeticProgression() {
		this(1, 0);
	} // start at 0 with increment of 1

	// Constructs progression 0,stepsize,2*stepsize,... 

	public ArithmeticProgression(long stepsize) {
		this(stepsize, 0);
	} // start at 0

	
	  // Constructs arithmetic progression with arbitrary start and increment.
	 

	public ArithmeticProgression(long stepsize, long start) {
		super(start);
		increment = stepsize;
	}

	/*
	 * Adds the arithmetic increment to the current value.
	 */

	protected void advance() {
		current += increment;

	}

	// We use this method to count the number of time we increment with before exceeding the specified value 

	public long longIntOverFlow() {
		long count = 0;

		
		// Here's the method to count to check how many times  you have to divide with 128
		// before exceeding max value
		
		while (nextValue() < Integer.MAX_VALUE) {
			// increment each time it enter loop
			count++;
		}
        
		
		// we return the count-1 because in the final increment it exceeds the max value
		return count-1;
	}

	public static void main(String args[]) {
		Progression prog;

		// test ArithmeticProgression

		System.out.print("number of time we increment 128 before exceeding the max integer is: ");
		// increment with 128 each time
		prog = new ArithmeticProgression(128);
		
		// print value returned by longIntOverFlow and hard check value
		System.out.println(((ArithmeticProgression) prog).longIntOverFlow() + " \nhard check 2147483647/128: "
		+ (Integer.MAX_VALUE)/128);

	}

}
Output:


number of time we increment 128 before exceeding the max integer is: 16777215 
hard check 2147483647/128: 16777215

 

0 0

Discussions

Post the discussion to improve the above solution.