
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
In city street grids, intersections are often defined by two integers, counting the position of horizontal and vertical streets (sometimes called streets and avenues). Imagine traveling from position (h1, v1) to (h2, v2). How many blocks do you traverse?

*Description*: The diagram shows a grid representing streets and avenues. Two points are marked on the grid: \((h_1, v_1)\) and \((h_2, v_2)\). Several possible routes between these points are drawn in different colors (blue, green, and red), illustrating that there are multiple paths to travel the same distance. However, irrespective of the route taken, the total distance in blocks remains the same.
2. **Calculating the Distance**
Even though there are many possible routes, the distance only depends on the differences \( h_2 - h_1 \) and \( v_2 - v_1 \). However, you need to take the absolute value because the differences might be negative.
3. **Programming Challenge**
Complete the following program that prints the number of blocks traveled, given the origin and destination of the trip (which will change as your code is tested).
```java
Distance.java
public class Distance
{
public static void main(String[] args)
{
// These values will be changed during testing
int h1 = 3;
int v1 = 4;
int h2 = 4;
int v2 = 4;
// Calculate the distance
int distance = Math.abs(h2 - h1) + Math.abs(v2 - v1);
System.out.print("Distance: ");
System.out.println(distance);
}
}
```
This code snippet calculates the distance between two points on a grid by summing the absolute differences of their respective coordinates. The printed distance reflects the total number of blocks traveled.](https://content.bartleby.com/qna-images/question/d772767f-fd91-4ca5-b737-b5766e3fef0a/2fe938ba-cdd1-4259-88fc-2c9ec945825b/tyhzpt1_thumbnail.png)
Transcribed Image Text:### Understanding Distance in a City Street Grid
1. **City Street Grid Distances**
In city street grids, intersections are often defined by two integers, counting the position of horizontal and vertical streets (sometimes called streets and avenues). Imagine traveling from position \((h_1, v_1)\) to \((h_2, v_2)\). How many blocks do you traverse?

*Description*: The diagram shows a grid representing streets and avenues. Two points are marked on the grid: \((h_1, v_1)\) and \((h_2, v_2)\). Several possible routes between these points are drawn in different colors (blue, green, and red), illustrating that there are multiple paths to travel the same distance. However, irrespective of the route taken, the total distance in blocks remains the same.
2. **Calculating the Distance**
Even though there are many possible routes, the distance only depends on the differences \( h_2 - h_1 \) and \( v_2 - v_1 \). However, you need to take the absolute value because the differences might be negative.
3. **Programming Challenge**
Complete the following program that prints the number of blocks traveled, given the origin and destination of the trip (which will change as your code is tested).
```java
Distance.java
public class Distance
{
public static void main(String[] args)
{
// These values will be changed during testing
int h1 = 3;
int v1 = 4;
int h2 = 4;
int v2 = 4;
// Calculate the distance
int distance = Math.abs(h2 - h1) + Math.abs(v2 - v1);
System.out.print("Distance: ");
System.out.println(distance);
}
}
```
This code snippet calculates the distance between two points on a grid by summing the absolute differences of their respective coordinates. The printed distance reflects the total number of blocks traveled.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 2 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- There is more than one way to calculate the value of T. One way that this can be done is by generating random numbers. This works by recognizing that if you take a unit square that you can draw a quarter circle of unit radius inside the square. The area of the quarter circle is exactly π/4 and the area of the square is 1. So if you randomly generate a pair of uniform numbers between 0 and 1 they will be distributed uniformly across the square. If you count the total number of points generated and the number of points (x, y) where x² + y² = ² < 1 then the ratio of those two numbers will tend towards the ratios of the areas of the square and the circle as the number of points generated increases. The ratio of the areas is just π/4 so if you take that ratio and multiply it by 4 you get an estimate for . a) b Write code to estimate π using this method. You can generate random numbers in the range 0arrow_forwardWrite a code that can be used to plot a 3D graph where x & y are between -5 to 5 and z is defined by the following equation. “z=4x4y - 3x3y2 + x”arrow_forwardWrite a Java Program to print a 2D matrix in the Spiral format. You can assume the 2D matrix is your own.arrow_forward
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education