Problem Decomposition: One Professor’s Approach to Coding
Fundamentals of Computer Science I
zombie[0] zombie[2] zombie[5] zombie[1] zombie[3] zombie[4]
Problem Decomposition: One Professors Approach to Coding Fewer - - PowerPoint PPT Presentation
Problem Decomposition: One Professors Approach to Coding Fewer Buuuuugs zombie[3] zombie[1] zombie[4] zombie[5] zombie[2] zombie[0] Fundamentals of Computer Science I Overview Problem Solving Understand the Problem Work
Fundamentals of Computer Science I
zombie[0] zombie[2] zombie[5] zombie[1] zombie[3] zombie[4]
2
3
4
5
6
7
8
9
10
11
12
13
34754 12029 EJ-475 38002 11193 CX-120 11899 28929 ? 39222 10028 ?
14
15
16
17
18
% java RadarContacts 1.0 2 radar4.txt Friendly aircraft: 2 Non-friendly aircraft: 2 % java RadarContacts 2.0 2 radar4.txt Friendly aircraft: 2 Non-friendly aircraft: 2 CX-120: ? (1.69)
19
% java RadarContacts 3.5 2 radar4.txt Friendly aircraft: 2 Non-friendly aircraft: 2 EJ-475: CX-120 (3.35) CX-120: ? (1.69) EJ-475 (3.35)
20
% java RadarContacts 3.5 1 radar4.txt Friendly aircraft: 2 Non-friendly aircraft: 2 EJ-475: CX-120 (3.35) CX-120: EJ-475 (3.35) % java RadarContacts 3.5 0 radar4.txt Friendly aircraft: 2 Non-friendly aircraft: 2 CX-120: ? (1.69)
21
22
– First, I have to output the count of the friendlies and the nonfriendlies – Then I have to output the “report” – for each friendly, I have to list any contacts that are “too close”, dependent on the mode that was input on the command line
23
– Here’s a start…
– OK, let’s keep going…
24
25
Get radius, mode from command line. Get number of contacts from file. For each contact, Get easting, northing, call sign. Count number of nonfriendlies. Calculate number of friendlies. Output these to the screen. For each friendly, Calculate the distance from it to each other contact. If distance < radius If mode is 0 or 2, and contact is nonfriendly, Write info to a nonfriendly String Else, if mode is 1 or 2 and contact is friendly, Write info to a friendly String. If friendly or nonfriendly isn’t empty, Print call sign followed by a colon. Print nonfriendly string. Print friendly string.
26
Get radius, mode from command line. Get number of contacts from file. For each contact, Get easting, northing, call sign. Count number of nonfriendlies. Calculate number of friendlies. Output these to the screen. For each friendly, Calculate the distance from it to each other contact. If distance < radius If mode is 0 or 2, and contact is nonfriendly, Write info to a nonfriendly String Else, if mode is 1 or 2 and contact is friendly, Write info to a friendly String. If friendly or nonfriendly isn’t empty, Print call sign followed by a colon. Print nonfriendly string. Print friendly string.
public class RadarContacts { public static void main(String [] args) { Scanner file; try { // Read the command line arguments double radius = Double.parseDouble(args[0]); int mode = Integer.parseInt(args[1]); file = new Scanner(new File(args[2])); // Read in the number of contacts and set up arrays // for the easting, northing and call signs int contacts = file.nextInt(); double [] east = new double[contacts]; double [] north = new double[contacts]; String [] callSign = new String[contacts]; // Set up variables for counting friendlies and nonfriendlies int friendlies = 0; int nonfriendlies = 0; String closeFriendlies; String closeNonfriendlies; // Read all the numbers in and count the numbers of each for(int i = 0; i < contacts; i++) { east[i] = file.nextInt() / 1000.0; north[i] = file.nextInt() / 1000.0; callSign[i] = file.nextLine(); if (callSign[i].equals(" ?")) { nonfriendlies++; } else { friendlies++; } } // Print out number of friendlies and nonfriendlies System.out.println("Friendly aircraft: " + friendlies); System.out.println("Non-friendly aircraft: " + nonfriendlies);
…
27