Quick Index
Problem Statement


For this we'll code a class called "Screen" that will compute the pixels per inch.

The calculation is a little involved. The calcs are described here:


However, this problem will be about finding component(s) to reuse to make things easy!

Algorithm


What we really have is two rectangles:

  • The size of the display (inches) - e.g. 17 inches by 13 inches
  • The resolution (pixels) - e.g. 1024 pixels by 768 pixels
Display
(inches)


Resolution
(pixels)


And the math is then just:
resolution / display
(using the diagonal for each)

Our Friend Rectangle


Here is part of the Rectangle method menu

Does "computeDiagonal" look helpful?
public int computeArea()
public int computePerimeter()
public double computeDiagonal()

Our Solution (Code)


We first start a class called "Screen". We then add two children (the rectangles).
public class Screen {
	private Rectangle display;
	private Rectangle resolution;

	//etc (more code follows)

}
By reusing Rectangle, we've reduced a long calc into one division statement! 👍🏽
	public double computePPI()  {
		//Pixels per inch (PPI) = pixels / inches
		double pixels, inches;
		pixels = this.resolution.computeDiagonal();
		inches = this.display.computeDiagonal();
		return pixels/inches;
	}

Download the source code here