Advertisement
apez1

Term 1: Lesson 20 Coding Activity (with array)

Oct 1st, 2018
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. /*
  2.  * find the largest number in an array arlat and arlon
  3.  * remove a number from the array if out of range
  4.  */
  5.  
  6. package project;
  7.  
  8. import java.util.*;
  9. import java.util.Scanner;
  10. import java.util.Collections;
  11.  
  12. public class Yourmom {
  13.  
  14.     public static void main(String[] args) {
  15.        
  16.         double lat;
  17.         double lon;
  18.         int reply = 1;
  19.        
  20.         ArrayList <Double> arlat = new ArrayList<Double>();
  21.         //double arlat[] = new double[7];
  22.         //double arlon[] = new double[7];
  23.         ArrayList <Double> arlon = new ArrayList<Double>();
  24.  
  25.         Scanner scanner = new Scanner(System.in);
  26.            
  27.         while(reply != 0) {
  28.            
  29.         int c = 0;
  30.         c++;   
  31.         System.out.println("Please enter the latitude:");
  32.         lat = scanner.nextDouble();
  33.         //arlat[c]=lat;
  34.         arlat.add(lat);
  35.        
  36.         System.out.println("Please enter the longitude:");
  37.         lon = scanner.nextDouble();
  38.         //arlon[c]=lon;
  39.         arlon.add(lon);
  40.        
  41.         int flag = 0;
  42.        
  43.         if(lat < -90 || lat > 90 ) {
  44.             flag = 1;      
  45.         }
  46.         if(lon < -180 || lon > 180 ) {
  47.             flag = 1;      
  48.         }
  49.    
  50.         if(flag == 1) {
  51.             System.out.println("Incorrect Latitude or Longitude");
  52.             reply = 1;
  53.             //remove numbers from array
  54.             arlon.remove(lon);
  55.             arlat.remove(lat);
  56.  
  57.         }
  58.            
  59.         if(flag != 1) {
  60.             System.out.println("Would you like to enter another location?");
  61.             reply = scanner.nextInt();                 
  62.             }  
  63.         }
  64.            
  65.         double largestlat = Collections.max(arlat);
  66.         double smallestlat = Collections.min(arlat);
  67.  
  68.         double largestlon = Collections.max(arlon);
  69.         double smallestlon = Collections.min(arlon);
  70.                        
  71.         if(reply ==0) {        
  72.             System.out.println("Farthest North: " + largestlat);
  73.             System.out.println("Farthest South: " + smallestlat);       //*
  74.            
  75.             System.out.println("Farthest East: " + largestlon) ;
  76.             System.out.println("Farthest West: " + smallestlon);        //*
  77.                        
  78.         }  
  79.  
  80.     }  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement