Endangered Animals

/* This program requires the user to input how many of each species of animal is left in the wild. This program will then name the animal that is most endangered at the end. Date--01/11/16 Author---- Tsotne Gvadzabia--- */ import java.util.Scanner; class endangered { public static void main (String[]p) { int [] numarray = new int [5]; //Array for storing animal data String [] qarray = new String [5]; //Array for the species of animals //Initialising Array with animal names qarray[0]="Komodo Dragon"; qarray[1]="Manatee"; qarray[2]="Kakpo"; qarray[3]="Florida Panther"; qarray[4]="White Rhino"; String large = "Komodo Dragon"; //Initialising variable int smallest = 0; //Initialising variable //method list here startmsg(); numarray=loop(numarray,qarray); smallest=calculator(numarray,smallest); large=calc2(numarray,qarray,smallest,large); result(numarray,qarray,smallest,large); System.exit(0); } public static void startmsg() //Start msg { System.out.println("Welcome, for calculations to be accurate please enter the information correctly"); } public static int[] loop(int [] numarray,String [] qarray) //Method for printing out questions and collecting data { String questions = "How many are left in the wild?"; Scanner scanner = new Scanner(System.in); for (int i = 0;i<=4; i++) { System.out.println(qarray[i]); System.out.println(questions); numarray[i] = scanner.nextInt(); } return numarray; } public static int calculator(int [] numarray,int smallest) //Method for calculating the most endangered animal- Numbers { smallest = numarray[0]; for (int i = 0;i<numarray.length;i++) { if (numarray[i]<smallest) { smallest = numarray[i]; } } return smallest; } public static String calc2(int[]numarray,String[]qarray,int smallest,String large) //Method for searching the most endangered animal- String { int result=0; for (int i = 0;i<numarray.length;i++) { if (smallest==numarray[i]) { result = i; //Store position found break; //break the loop } } large = qarray[result]; return large; } public static void result(int [] numarray,String[] qarray,int smallest ,String large) //Method for displaying the results of the findings. { for (int i = 0; i<=4; i++) { System.out.println(qarray[i] +": " + numarray[i]); } System.out.println("The most endangered animal is the "+ large); System.out.println("There are only "+smallest+" left in the wild"); } } //END of Program
The program gives you a list of species to which the user inputs the number of how many remain in the wild

The program then looks at the data and gives you the result, showing the most endangered animal.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.