150 likes | 164 Views
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. Methods.
E N D
Introduction to ProgrammingG50PROUniversity of NottinghamUnit 8 : Methods 2 - Arrays Paul Tennent http://paultennent.wordpress.com/G50PRO.html Paul.tennent@nottingham.ac.uk Room C7
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
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
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
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
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 currentbalance if needed class VendingMachine { static final int CHOC = 37; static final int DRINK = 69; static int credit = 0; }
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 or Drink 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"); }
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
Menu choice 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 • 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
main Method public static void main ( String[] args ) { while(true) { runMenu(); } //end of while loop } //end of main
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
Array Declaration • An array's type is written as type[ ], where type is the data type of the contained elements int[] ar; // declares an array of integers ar = new int[8]; // allocates memory for 8 integers Array Length is 8 First Elementat Index 0 0 1 2 3 4 5 6 7
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 Array Length is 8 First Elementat Index 0 5 0 1 2 3 4 5 6 7
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; } ar.length = 8 First Elementat Index 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7
Summary • Methods Vending Machine Example • Arrays • Array Declaration • Accessing Array Elements • Initializing Array elements