Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming G50PRO University of Nottingham Unit 8 : Methods 2 - Arrays Paul Tennent

Similar presentations


Presentation on theme: "Introduction to Programming G50PRO University of Nottingham Unit 8 : Methods 2 - Arrays Paul Tennent"— Presentation transcript:

1 Introduction to Programming G50PRO University of Nottingham Unit 8 : Methods 2 - Arrays Paul Tennent http://paultennent.wordpress.com/G50PRO.html Paul.tennent@nottingham.ac.uk Room C7

2 Methods A method is a group of instructions that is given a name and can be called up at any point in a program simply by using a method call Allow the developer to break the problem into smaller sub problems Reusability of the parts, is highly appreciated A method can be called any number of times

3 Vending Machine Exercise Write a method called enterCash which will prompt and read an amount of money entered into the machine by the user public static int enterCash() { System.out.print("\nMoney Entered: ") ; Scanner scan = new Scanner(System.in); int amount = scan.nextInt(); if(amount < 0) { System.out.println("Illegal money value entered"); amount = 0 } return amount; } // end of enterCash method

4 Vending Machine Exercise Write A method called checkMoney which checks to see if the user has entered enough money to pay for an item, it should return the answer as a boolean. public static boolean checkMoney(int cash, int cost) { boolean result = false; if( cash >= cost ) result = true; return result; } // end of checkMoney method

5 Vending Machine Exercise A method called printMenu which returns nothing public static void printMenu() { System.out.println("Vending Machine \n "); System.out.println("a) Enter Money"); System.out.println("b) Vend Chocolate"); System.out.println("c) Vend Drink"); System.out.println("d) Return Change"); System.out.print("\nEnter Choice: "); } // end of printMenu method

6 Vending Machine Exercise A chocolate bar costs 37 pence and a drink 69 pence Users should be able to enter money at any point, topping up the current balance if needed class VendingMachine { static final int CHOC = 37; static final int DRINK = 69; static int credit = 0; }

7 Vending Machine Exercise when the user selects to vend an item it should first check and If there is enough money then you should print: Chocolate Vended orDrink Vended subtract the cost from their current credit. If (checkMoney ( credit, CHOC ) ) { System.out.println("Chocolate Vended"); credit -= CHOC; } else { System.out.println("Insufficient Funds, enter more money"); }

8 Vend Method static void vend (int type){ int cost = DRINK; String msg = "Drink"; if (type == 1){ cost = CHOC; msg = "Chocolate "; } if(checkMoney(credit, cost)) { credit -= cost; System.out.println( msg + " Vended"); System.out.println( "Current Balance is: " + credit + " pence"); } else { System.out.println("Insufficient Funds, enter more money"); } }// end of vend method

9 Menu choice The vending machine is to offer the user a menu allowing them to enter money, request a chocolate bar or a drink, or return any remaining change. They should be able to enter money at any point, topping up the current balance if needed static void runMenu(){ printMenu(); char choice = UserInput.readChar(); switch(choice) { case 'a': case 'A': credit += enterCash(); break; case 'b': case 'B': vend (1);//vend chocolate break; case 'c': case 'C': vend(2); //vend drink break; case 'd': case 'D': System.out.println("Money returned is " + credit + " pence"); System.exit(0); default: System.out.println("Illegal Menu Option!"); } // end of switch } //end of runMenu method

10 main Method public static void main ( String[] args ) { while(true) { runMenu(); } //end of while loop } //end of main

11 Arrays An array is a container object that holds a fixed number of values of a single type Each item in an array is called an element, and each element is accessed by its numerical index Index numbering begins with 0

12 Array Declaration int[] ar; // declares an array of integers ar = new int[8]; // allocates memory for 8 integers An array's type is written as type[ ], where type is the data type of the contained elements 0 1 2 3 4 5 6 7 First Element at Index 0 Array Length is 8

13 Accessing Array Elements ar[0] = 5; // sets the first element of ar to 5 int x = ar[0] ; // assigns the value of the first element of ar to x 0 1 2 3 4 5 6 7 First Element at Index 0 Array Length is 8 5

14 Initializing Array elements Using loops is very useful with arrays int[] ar; // declares an array of integers ar = new int[8]; // allocates memory for 8 integers for (int i = 0; i < ar.length; i++){ ar[i] = 0; } 0 1 2 3 4 5 6 7 First Element at Index 0 ar.length = 8 0 0 0 0

15 Summary Methods Vending Machine Example Arrays Array Declaration Accessing Array Elements Initializing Array elements


Download ppt "Introduction to Programming G50PRO University of Nottingham Unit 8 : Methods 2 - Arrays Paul Tennent"

Similar presentations


Ads by Google