Question

Need a Java program, great if anyone is able to help You have been asked to...

Need a Java program, great if anyone is able to help

You have been asked to write an analysis of the deer population by town in New Jersey. The accompanying file contains data about each town. The format of the file is the name of the town, square miles and the number of deer counted. You will need to produce the report as follow:

Township               Square    Deer                   Deer Per           Deer/Tick        Tick Threat
Name                      Miles      Population         Square Mile     Coefficient      Indicator

Green Town         158.98       9865                    62.05                  0.39               GREEN

The Deer/Tick coefficient is calculated by dividing the number (deer per square mile)/Square miles.

The higher the coefficient the denser the deer population and the greater the threat of tick infestation to humans. The tick threat is indicated by a color to indicate the danger to human for diseases such as Lyme disease as follows; red if the coefficient is .35 or below, green if coefficient is above .35 and below .50, yellow of the coefficient is above .50 and below .65, red if the coefficient is about .65 and below 0.75 and white if the coefficient is about 0.75.

You will create program to offer the user the ability to produce the report as show about in alphabetical order by township name or in size order by township square mile. You program must also allow the user to type in the name of the township or use a menu to select a town to look up the above data for the selected township.

Your program must use a class to hold and PROCESS the town data. Your program must also use an array of objects to process the data. You will be graded on your use of Methods, on your reading of the data from the file, your use of proper formatting techniques.

Here is the data file info

City of Red
37.81 1137
Boro of Orange
1.77 1071
Yellow City
83.13 1034
Green Town   
4.79 2819
Blueville
45.71 1514
Indigo Village
4.15 442
Violeton   
9.27 2225
Redburg
7.46 977
Orange Park
1.36 533
Yellow Falls   
18.75 4556
Green Haven
6.12 242
Blue City
44.69 1979
Indigo Township
3.56 365
Violet Point   
35.27 4161

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. Please do rate the answer if it helped. Thank you.
If you are using eclipse as your IDE, make sure you don't put the input file inside the src folder. Just put is directly under the project folder, otherwise you will get file not found error.


towns.txt
----
City of Red
37.81 1137
Boro of Orange
1.77 1071
Yellow City
83.13 1034
Green Town   
4.79 2819
Blueville
45.71 1514
Indigo Village
4.15 442
Violeton   
9.27 2225
Redburg
7.46 977
Orange Park
1.36 533
Yellow Falls   
18.75 4556
Green Haven
6.12 242
Blue City
44.69 1979
Indigo Township
3.56 365
Violet Point   
35.27 4161

TownData.java
-----

public class TownData {
   private String townshipName;
   private double squareMiles;
   private int deerPopulation;
  
   public TownData(String name) {
       townshipName = name;
   }
   public String getTownshipName() {
       return townshipName;
   }
   public void setTownshipName(String townshipName) {
       this.townshipName = townshipName;
   }
   public double getSquareMiles() {
       return squareMiles;
   }
   public void setSquareMiles(double squareMiles) {
       this.squareMiles = squareMiles;
   }
   public int getDeerPopulation() {
       return deerPopulation;
   }
   public void setDeerPopulation(int deerPopulation) {
       this.deerPopulation = deerPopulation;
   }
  
  
   public double getDeerPerSqMile() {
       return deerPopulation/squareMiles;
   }
  
   public double getTickCoefficient() {
       return getDeerPerSqMile()/squareMiles;
   }
  
  
   public String getThreatIndicator() {
       double tc = getTickCoefficient();
       if(tc <= 0.35)
           return "RED";
       else if(tc <= 0.50)
           return "GREEN";
       else if(tc <= 0.65)
           return "YELLOW";
       else if(tc <= 0.75)
           return "RED";
       else
           return "WHITE";
   }
  
   public String toString() {
       return "Township Name: " + townshipName + "\n"
               + String.format("Square Miles: %.2f" , squareMiles) + "\n"
               + "Deer Population: " + deerPopulation + "\n"
               + String.format("Deer per Square Mile: %.2f" , getDeerPerSqMile()) + "\n"
               + String.format("Deer/Tick Coefficient: %.2f" , getTickCoefficient()) + "\n"
               + "Tick Threat Indicator: " + getThreatIndicator() +"\n";
   }
}

public class TownData {
   private String townshipName;
   private double squareMiles;
   private int deerPopulation;
  
   public TownData(String name) {
       townshipName = name;
   }
   public String getTownshipName() {
       return townshipName;
   }
   public void setTownshipName(String townshipName) {
       this.townshipName = townshipName;
   }
   public double getSquareMiles() {
       return squareMiles;
   }
   public void setSquareMiles(double squareMiles) {
       this.squareMiles = squareMiles;
   }
   public int getDeerPopulation() {
       return deerPopulation;
   }
   public void setDeerPopulation(int deerPopulation) {
       this.deerPopulation = deerPopulation;
   }
  
  
   public double getDeerPerSqMile() {
       return deerPopulation/squareMiles;
   }
  
   public double getTickCoefficient() {
       return getDeerPerSqMile()/squareMiles;
   }
  
  
   public String getThreatIndicator() {
       double tc = getTickCoefficient();
       if(tc <= 0.35)
           return "RED";
       else if(tc <= 0.50)
           return "GREEN";
       else if(tc <= 0.65)
           return "YELLOW";
       else if(tc <= 0.75)
           return "RED";
       else
           return "WHITE";
   }
  
   public String toString() {
       return "Township Name: " + townshipName + "\n"
               + String.format("Square Miles: %.2f" , squareMiles) + "\n"
               + "Deer Population: " + deerPopulation + "\n"
               + String.format("Deer per Square Mile: %.2f" , getDeerPerSqMile()) + "\n"
               + String.format("Deer/Tick Coefficient: %.2f" , getTickCoefficient()) + "\n"
               + "Tick Threat Indicator: " + getThreatIndicator() +"\n";
   }
}

ProcessTowns.java
-------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ProcessTowns {
   private static Scanner input = new Scanner(System.in);

   private static int readFile(String filename, TownData[] towns) {
       int n = 0;
       try {
           Scanner inFile = new Scanner(new File(filename));
           while(inFile.hasNextLine()) {
               towns[n] = new TownData(inFile.nextLine().strip());
               towns[n].setSquareMiles(inFile.nextDouble());
               towns[n].setDeerPopulation(inFile.nextInt());
               if(inFile.hasNextLine())
                   inFile.nextLine();//get rid of newline
               n++;
           }
           inFile.close();
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
           System.exit(1);
       }
       return n;

      
   }
   public static void main(String[] args) {
       String filename;
       TownData[] towns = new TownData[100];
       int n, choice = 0;
      
       System.out.print("Enter input filename: ");
       filename = input.nextLine();
      
      
       n = readFile(filename, towns);
       while(choice != 4) {
           System.out.println("1. Print towns sorted by name");
           System.out.println("2. Print towns sorted by square miles");
           System.out.println("3. Lookup town");
           System.out.println("4. Exit");
           System.out.println("Enter your choice: ");
           choice = input.nextInt();
           input.nextLine();//get rid of newline
           switch(choice) {
           case 1:
               sortByNameAsc(towns, n);
               print(towns, n);
               break;
           case 2:
               sortByMilesAsc(towns, n);
               print(towns, n);
               break;
           case 3:
               lookup(towns,n);
               break;
           case 4:
               break;
           default:
               System.out.println("Invalid Choice!");
           }
           System.out.println();
       }
  
   }
  
   private static void sortByNameAsc(TownData[] towns, int n) {
       int i, j, minIdx;
       TownData temp;
      
       for(i = 0; i < n; i++){
           minIdx = i;
           for(j = i+1; j < n; j++){
               if(towns[j].getTownshipName().compareTo(towns[minIdx].getTownshipName()) < 0)
                   minIdx = j;
           }
           temp = towns[i];
           towns[i] = towns[minIdx];
           towns[minIdx] = temp;
       }
   }
  
   private static void sortByMilesAsc(TownData[] towns, int n) {
       int i, j, minIdx;
       TownData temp;
      
       for(i = 0; i < n; i++){
           minIdx = i;
           for(j = i+1; j < n; j++){
               if(towns[j].getSquareMiles() < towns[minIdx].getSquareMiles())
                   minIdx = j;
           }
           temp = towns[i];
           towns[i] = towns[minIdx];
           towns[minIdx] = temp;
       }
   }
  
   private static void print(TownData[] towns, int n ) {
       System.out.printf("%15s %15s %15s %15s %15s %15s\n", "Township", "Square", "Deer" , "Deer Per", "Deer/Tick", "Tick Threat");
       System.out.printf("%15s %15s %15s %15s %15s %15s\n", "Name", "Miles", "Population" , "Square Mile", "Coefficient", "Indicator");
       for(int i = 0; i < n; i++) {
           System.out.printf("%15s %15.2f %15d %15.2f %15.2f %15s\n", towns[i].getTownshipName(), towns[i].getSquareMiles(), towns[i].getDeerPopulation() ,
                   towns[i].getDeerPerSqMile(), towns[i].getTickCoefficient(), towns[i].getThreatIndicator());

       }
       System.out.println();
   }
  
   private static int search(TownData[] towns, int n, String name) {
       for(int i = 0; i < n; i++) {
           if(towns[i].getTownshipName().equalsIgnoreCase(name))
               return i;
       }
       return -1;
   }
  
   private static void lookup(TownData[] towns, int n ) {
       String name;
       System.out.println("Enter name of town to lookup: ");
       name = input.nextLine();
       int idx = search(towns, n, name);
       if(idx == -1)
           System.out.println("No such town!");
       else
           System.out.println(towns[idx]);
      
      
   }
}


output
----
Enter input filename: towns.txt
1. Print towns sorted by name
2. Print towns sorted by square miles
3. Lookup town
4. Exit
Enter your choice:
1
Township Square Deer Deer Per Deer/Tick Tick Threat
Name Miles Population Square Mile Coefficient Indicator
Blue City 44.69 1979 44.28 0.99 WHITE
Blueville 45.71 1514 33.12 0.72 RED
Boro of Orange 1.77 1071 605.08 341.86 WHITE
City of Red 37.81 1137 30.07 0.80 WHITE
Green Haven 6.12 242 39.54 6.46 WHITE
Green Town 4.79 2819 588.52 122.86 WHITE
Indigo Township 3.56 365 102.53 28.80 WHITE
Indigo Village 4.15 442 106.51 25.66 WHITE
Orange Park 1.36 533 391.91 288.17 WHITE
Redburg 7.46 977 130.97 17.56 WHITE
Violet Point 35.27 4161 117.98 3.34 WHITE
Violeton 9.27 2225 240.02 25.89 WHITE
Yellow City 83.13 1034 12.44 0.15 RED
Yellow Falls 18.75 4556 242.99 12.96 WHITE


1. Print towns sorted by name
2. Print towns sorted by square miles
3. Lookup town
4. Exit
Enter your choice:
2
Township Square Deer Deer Per Deer/Tick Tick Threat
Name Miles Population Square Mile Coefficient Indicator
Orange Park 1.36 533 391.91 288.17 WHITE
Boro of Orange 1.77 1071 605.08 341.86 WHITE
Indigo Township 3.56 365 102.53 28.80 WHITE
Indigo Village 4.15 442 106.51 25.66 WHITE
Green Town 4.79 2819 588.52 122.86 WHITE
Green Haven 6.12 242 39.54 6.46 WHITE
Redburg 7.46 977 130.97 17.56 WHITE
Violeton 9.27 2225 240.02 25.89 WHITE
Yellow Falls 18.75 4556 242.99 12.96 WHITE
Violet Point 35.27 4161 117.98 3.34 WHITE
City of Red 37.81 1137 30.07 0.80 WHITE
Blue City 44.69 1979 44.28 0.99 WHITE
Blueville 45.71 1514 33.12 0.72 RED
Yellow City 83.13 1034 12.44 0.15 RED


1. Print towns sorted by name
2. Print towns sorted by square miles
3. Lookup town
4. Exit
Enter your choice:
3
Enter name of town to lookup:
abc
No such town!

1. Print towns sorted by name
2. Print towns sorted by square miles
3. Lookup town
4. Exit
Enter your choice:
3
Enter name of town to lookup:
Redburg
Township Name: Redburg
Square Miles: 7.46
Deer Population: 977
Deer per Square Mile: 130.97
Deer/Tick Coefficient: 17.56
Tick Threat Indicator: WHITE


1. Print towns sorted by name
2. Print towns sorted by square miles
3. Lookup town
4. Exit
Enter your choice:
4

Add a comment
Know the answer?
Add Answer to:
Need a Java program, great if anyone is able to help You have been asked to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • Need help with java In this program you will use a Stack to implement backtracking to solve Sudok...

    need help with java In this program you will use a Stack to implement backtracking to solve Sudoku puzzles. Part I. Implement the stack class.  Created a generic, singly-linked implementation of a Stack with a topPtr as the only instance variable. Implement the following methods only: public MyStack() //the constructor should simply set the topPtr to null public void push(E e) public E pop()  Test your class thoroughly before using it within the soduku program Part II. Create...

  • THIS IS FINAL COURSE ASSIGNMENT, PLEASE FOLLOW ALL REQUIREMENTS. Hello, please help write program in JAVA...

    THIS IS FINAL COURSE ASSIGNMENT, PLEASE FOLLOW ALL REQUIREMENTS. Hello, please help write program in JAVA that reads students’ names followed by their test scores from "data.txt" file. The program should output "out.txt" file where each student’s name is followed by the test scores and the relevant grade, also find and print the highest test score and the name of the students having the highest test score. Student data should be stored in an instance of class variable of type...

  • Help with java . For this project, you will design and implement a program that analyzes...

    Help with java . For this project, you will design and implement a program that analyzes baby name popularities in data provided by the Social Security Administration. Every 10 years, the data gives the 1,000 most popular boy and girl names for kids born in the United States. The data can be boiled down to a single text file as shown below. On each line we have the name, followed by the rank of that name in 1900, 1910, 1920,...

  • Python help. Any help is appreciated, thank you! Write a program that asks the user for...

    Python help. Any help is appreciated, thank you! Write a program that asks the user for a CSV of collision data (see note below about obtaining reported collisions from NYC OpenData). Your program should then list the top three contributing factors for the primary vehicle of collisions "CONTRIBUTING FACTOR VEHICLE 1"in the file. A sample run: Enter CSV file name: collisionsNewYears2016.csv Top three contributing factors for collisions: Driver Inattention/Distraction 136 Unspecified 119 Following To0 Closely 37 Name: CONTRIBUTING FACTOR VEHICLE...

  • Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements...

    Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements description: Assume you work part-time at a sandwich store. As the only employee who knows java programming, you help to write a sandwich ordering application for the store. The following is a brief requirement description with some sample output. 1. Selecting bread When the program starts, it first shows a list/menu of sandwich breads and their prices, then asks a user to select a...

  • Description: You have been asked to help the College of IST Running Club by designing a...

    Description: You have been asked to help the College of IST Running Club by designing a program to help them keep track of runners in the club, the races that will occur, and each runner's performance in each race! Requirements: Please use Java to implement the program Your program must: Store information about each Runner. Store information about each Race. Store information about the performance of each runner in each race that they compete in. Note that each race may...

  • please help! (#5 11.1) i dont know what you mean. what other information do you need...

    please help! (#5 11.1) i dont know what you mean. what other information do you need fill in the blanks for the question Conduct the hypothesis test and provide the test statistic and the critical value, and state the conclusion A company claims that its packages of 100 candies are distributed with the following color percentages: 15% red, 18% orange, 15% yellow, 14% brown, 24% blue, and 14% green. Use the given sample data to test the claim that the...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • I need help with questions 1 through 2, thank you! The Esatina salamanders (Ensatina eschscholtzii), shown...

    I need help with questions 1 through 2, thank you! The Esatina salamanders (Ensatina eschscholtzii), shown above, live along the West Coast of North America from Vancouver to Baja California. We will be focusing two populations: 1. individuals that live in Northern California and represent a more ancestral population 2. individuals that live in Southern California. Researchers have found that these all individuals of this salamander species have two color phenotype, either red or blotchy. Genetic analysis has shown that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT