Presentation is loading. Please wait.

Presentation is loading. Please wait.

14 BirthMonth1February 16 14 BirthMonth CE00858-1: Fundamental Programming Techniques.

Similar presentations


Presentation on theme: "14 BirthMonth1February 16 14 BirthMonth CE00858-1: Fundamental Programming Techniques."— Presentation transcript:

1 14 BirthMonth1February 16 14 BirthMonth CE00858-1: Fundamental Programming Techniques

2 14 BirthMonth2February 16 Objectives In this session, we will: see how arrays can be passed to methods analyse a problem that uses arrays implement the solution using concepts introduced so far

3 14 BirthMonth3February 16 Array parameters method parameters can be arrays: //create array in main method, then pass it to other methods for input and output import java.util.*; public class ArraysInMethods { public static void main (String [] args) { //create a Scanner that can be used in the main method Scanner kybd = new Scanner(System.in); System.out.print("How many elements in the array? "); int n = kybd.nextInt(); int [] arr = new int[n]; //create array of n elements inputArray(arr); //call inputArray to read data into array outputArray(arr); //call outputArray to output contents of array } ArraysInMethods.java

4 14 BirthMonth4February 16 public static void inputArray(int[] a) { //create a Scanner object that can be used in this method Scanner kybd = new Scanner(System.in); System.out.println("Enter " + a.length + " numbers to store in array"); for (int i = 0; i < a.length; i++) { a[i] = kybd.nextInt(); } public static void outputArray(int[] a) { System.out.println("Contents of the array"); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); }

5 14 BirthMonth5February 16 References to arrays pointer to array is passed as parameter, not actual contents any changes to arrays will remain after method has finished arr 0 arr passed to inputArray method as parameter a data input into a 3 arr at end of method arr contains input data a 4 1 3 7 4 9 6 2 3 3 4 1 3 7 4 9 6 2 3 0 0 0 0 0 0 0 0 0 new array called arr with 10 elements created in main method

6 14 BirthMonth BirthMonth specification record frequency of month of birth until -1 input once all the data has been entered, a table of months and number of births in that month is output also output most frequent month data needs to be validated February 166

7 14 BirthMonth Analysis - breakdown problem can be broken down into a number of steps: input and validate data output table of births in each month find and output most frequent month for this example, each step will be implemented using a separate method also need to consider how to represent the data February 167

8 14 BirthMonth Representing the data need to have a count for each month integer array containing 12 locations elements initialised to 0 individual element incremented when user chooses month user will enter data in range 1 – 12 months are numbered in range 0 - 11 February 168 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 monthTally

9 14 BirthMonth Month names and numbers need to convert month number to name method name: toString parameters:integer number representing month return:String name of month February 169 public static String toString(int num) { String [] monthNames = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; return monthNames[num]; } Jan 0 Feb 1 Mar 2 Apr 3 May 4 Jun 5 Jul 6 Aug 7 Sep 8 Oct 9 Nov 10 Dec 11 monthName

10 14 BirthMonth //program to analyse months of birth import java.util.*; public class BirthMonth { public static void main (String args[]) { //input and validate months //output table of results //find and output most frequent month } February 1610

11 14 BirthMonth Testing need to test: invalid month processed correctly boundary cases: 0, 1, 12, 13 month converted to string correctly most frequent month determined correctly most frequent month – January most frequent month – December two months with same frequency February 1611

12 14 BirthMonth12February 16 Summary In this session we have: analysed a problem to determine what is required implemented a solution that: uses arrays to store data passed arrays as parameters In the next session we will: introduce sorting data


Download ppt "14 BirthMonth1February 16 14 BirthMonth CE00858-1: Fundamental Programming Techniques."

Similar presentations


Ads by Google