Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language: Python Program used: PyCharm Interpreter: 3.6.1 at ~/anaconda/bin/pyth

ID: 3600669 • Letter: L

Question

Language: Python

Program used: PyCharm

Interpreter: 3.6.1 at ~/anaconda/bin/python

-----

-----

Starter code: https://pastebin.com/NgrECZQ7

candidates_2016: https://www.dropbox.com/s/oc8ws0qfz01664n/sk_election_candidates_2016.csv?dl=1

votecounts_2016: https://www.dropbox.com/s/ucgaf263q2x3qrm/sk_election_votecounts_2016.csv?dl=1

-----

YOU JUST NEED QUESTION #9 FROM THE STARTER CODE PROVIDED ABOVE.

-----

Flowchart: https://www.dropbox.com/s/kb3fo1nkgozxwkr/infoflow.pdf?dl=1

-----

Question 9 (8 points): Purpose: To practice building applications out of functions. Degree of Difficulty: Moderate. You'll have to move the data around from function to function. All right. Now let's put all the pieces together, and show that we've got everything working Task: Write a few lines of Python after all your function definitions from above that will Open the provided vote count file (sk_election_votecounts_2016.csv) with read party_names . Open both provided files (sk_election candidates_2016.csv) and (sk_election_votecounts_2016.csv) with read election files (). Remember that this function is supposed to return a tuple! . Construct the district database with index all_districts Find the winner in every district with find all1 winners Write the election results to a file with write-election-results The final task is to determine which party won the election and to print to the console a report summarizing the election results. Your program should generate and print a report that has the names of all the parties the number of districts won by each party, the winning party and the number of districts the winning party claimed. You can decide to print this information however you wish, but keep in mind that lists would be especially well suited to store all the party names and dictionaries would be especially well suited to store the number of districts each party won. If you choose to enter the districts won by each party into a dictionary, you can use your find winner function from question 4 to display the winning party. Your summary report may Look like this: Election summary Participating parties: ['GP 'LIB' 'NDP', 'PC', 'SK', 'WIP', 'IND'] Districts won by each party: ['NDP': 10, 'SK' 51) SK won the election with 51 districts After running your program, check the election results file to make sure there is a winning party and can- didate declared for every district. Make sure the winning number of votes is reasonable At this point, you should have a working program that reads the data from the given files, and creates a new file containing the election results. You may still have some testing code in your program, which you should comment out (don't delete it until you're ready to hand in your work - you never know if it might be useful) As a final note, your application could very easily be expanded to analyze more aspects of elections like voter turnout or voter swing. Consider the types of changes you would need to make to the internal data structures to accommodate these features

Explanation / Answer

import java.util.Scanner;

public class ProgrammingAssignment_2 {

   public static void main(String [] args){   

       //Scanner to take all users input

       Scanner input = new Scanner(System.in);   

       //Declare variables

       int shippingLocation;

       String another;

       double totalShipping = 0; //variable for cost of all locations

       int counter = 0;  

       do{   

       //Displays the available shipping locations and prompts user for location

           System.out.println("Shipping Locations: " + "1, If Local(US) " + "2, If Canada " +

                   "3, If South America " + "4, If Europe ");      

           System.out.print("Select a shipping location: ");

           shippingLocation = input.nextInt();   

           //error check to ensure user enters a location between 1-4

           while (shippingLocation <=0 || shippingLocation >=5){

               System.out.print("Enter a location between 1 and 4: ");

               shippingLocation = input.nextInt();

           }      

      

       //creates an array for the number of packages the users can ship.

       //User can only input weights between 4 ranges  

       int[] packageArray = new int[4];

      

       //Prompts the user for the quantity of packages within the given ranges,

       //the input is entered to package array index 0 through 3.

       System.out.println("Enter the number of packages shipped or 0 if no packages.");

for(int i=0; i < packageArray.length; i++)

{    

if (i == 0)

{

System.out.print("Packages of weight range 0 < w <= 1: ");

}

if (i == 1)

{

System.out.print("Packages of weight range 1 < w <= 3: ");

}

if (i == 2)

{

System.out.print("Packages of weight range 3 < w <= 10: ");

}

if (i == 3)

{

System.out.print("Packages of weight range 10 < w <= 20: ");

}   

packageArray[i] =input.nextInt();

}   

//Outputs to console the cost of shipping and method calls for all shipping locations

       //shipping Locally(US)

       if(shippingLocation == 1){

           double cost = localShipment(packageArray);   

           totalShipping = totalShipping + cost;

          

           System.out.println(" Cost of shipping to Local(US): $" +cost);

       }

       //Shipping to Canada

       else if(shippingLocation == 2){

           double cost = canadaShipment(packageArray);   

           totalShipping = totalShipping + cost;

           System.out.println(" Cost of shipping to Canada: $" + cost);

       }

       //Shipping to South America

       else if(shippingLocation == 3){

           double cost = southAmericaShipment(packageArray);

          

           totalShipping = totalShipping + cost;

           System.out.println(" Cost of shipping: $" + cost);

       }

       //Shipping to Europe

       else{              

           double cost = europeShipment(packageArray);   

           totalShipping = totalShipping + cost;

           System.out.println(" Cost of shipping: $" + cost);          

       }   

       //prompt user whether they want to ship packages to a different location

       input = new Scanner(System.in);      

       System.out.print("Do you wish to ship to a different location? (y/n)");

       another =input.nextLine();   

       counter++;

      

   //"do-while" location entered is less than 4 and if the user enters y for yes.

   //This loop terminated when user enters n for no.

   }while(counter < 4 && another.equalsIgnoreCase("yes") || another.equalsIgnoreCase("y"));

   //Displays the cost of shipping to all locations entered by user.

   System.out.println(" Total shipping cost to selected locations: $" + totalShipping);   

}   

//Local shipment method called from main.

//This return method calculates the cost of shipping by taking user input from package array

//and multiplying by the price of then weight. This return method returns the cost of shipping.

   public static double localShipment(int[] packageArray){

       double total = 0;

       for (int i =0; i < packageArray.length; i++) {

      double current =0;

      if (i == 0)

      {

      current = 3.5 * packageArray[i];

      }else if (i == 1)

      {

      current = 5.5 * packageArray[i];

      }

      else if (i == 2)

      {

      current = 8.5 * packageArray[i];

      }

      else if (i == 3)

      {

      current = 10.5 * packageArray[i];

      }

      total = total + current;   

      }

       return total;

   }   

//Canada shipment method called from main.

//This return method calculates the cost of shipping by taking user input from package array

//and multiplying by the price of then weight. This return method returns the cost of shipping.

   public static double canadaShipment(int[] packageArray){

       double total = 0;

      for (int i =0; i < packageArray.length; i++) {

      double current =0;

      if (i == 0)

      {

      current = 4.5 * packageArray[i];

      }else if (i == 1)

      {

      current = 6.5 * packageArray[i];

      }

      else if (i == 2)

      {

      current = 10.5 * packageArray[i];

      }

      else if (i == 3)

      {

      current = 12.5 * packageArray[i];

      }

      total = total + current;

      }

       return total;

   }

//SouthAnerica shipment method called from main.

//This return method calculates the cost of shipping by taking user input from package array

//and multiplying by the price of then weight. This return method returns the cost of shipping.      

   public static double southAmericaShipment(int[] packageArray){

       double total = 0;

       for (int i =0; i < packageArray.length; i++) {

double current =0;

  

if (i == 0)

{

current = 5.5 * packageArray[i];

}else if (i == 1)

{

current = 7.5 * packageArray[i];

}

else if (i == 2)

{

current = 9.5 * packageArray[i];

}

else if (i == 3)

{

current = 11.5 * packageArray[i];

}

total = total + current;

       }

       return total;

   }   

//Local shipment method called from main.

//This return method calculates the cost of shipping by taking user input from package array

//and multiplying by the price of then weight. This return method returns the cost of shipping.

   public static double europeShipment(int[] packageArray){

       double total = 0;

       for (int i =0; i < packageArray.length; i++) {

double current =0;

//

if (i == 0)

{

current = 7.5 * packageArray[i];

}else if (i == 1)

{

current = 9.5 * packageArray[i];

}

else if (i == 2)

{

current = 11.0 * packageArray[i];

}

else if (i == 3)

{

current = 15.0 * packageArray[i];

}

total = total + current;

}

       return total;

   }

}