Question
Example bomb question in lecture slides. import java.io.*; import java.util.*; // Displays a set of cities and simulates dropping a bomb on them. public class
Example bomb question in lecture slides.
import java.io.*;
import java.util.*;
// Displays a set of cities and simulates dropping a "bomb" on them.
public class Bomb {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("cities.txt"));
Point[] cities = readCities(input);
// drop the "bomb"
Scanner console = new Scanner(System.in);
Point bomb = new Point();
System.out.print("Blast site x? ");
bomb.x = console.nextInt();
System.out.print("Blast site y? ");
bomb.y = console.nextInt();
System.out.print("Blast radius? ");
int radius = console.nextInt();
boom( bomb , radius, cities);
}
public static Point[] readCities(Scanner input) {
int numCities = input.nextInt(); // first line = # of cities
Point[] cities = new Point[numCities];
for (int i = 0; i < cities.length; i++) {
cities[i] = new Point();
cities[i].x = input.nextInt(); // read city x/y from file
cities[i].y = input.nextInt();
}
return cities;
}
// Simulates dropping a bomb at the given location on the given cities.
public static void boom( Point bomb , int radius, Point[] cities ) {
for (int i = 0; i < cities.length; i++) {
int dx = cities[i].x - bomb.x;
int dy = cities[i].y - bomb.y;
double distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= radius) {
System.out.println ("(" + cities[i].x + ", " + cities[i].y + ")");
}
}
}
}
1) Create a bomb java program in n-dimensional space based on given program.
a) Use arrays inside Point class.
b) Create constructors.
c) Use private fields, create getters and setters.
2) Create a Bomb program with using polar coordinates.
a) Use private fields.
b) Create a constructor accepting Cartesian coordinates. The constructor should convert Cartesian coordinates into polar.
(conversion of cartesian coordinates to polar: https://www.mathsisfun.com/polar-cartesian-coordinates.html
c) Create getters and setters which accept and returns Cartesian coordinates.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started