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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 8: Arrays.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Computer Programming Lab 8.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Java Syntax Primitive data types Operators Control statements.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
JAVA Control Statement.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
5. Conditionals & Loops Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Application development with Java Lecture 6 Rina Zviel-Girshin.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Introduction to programming in java Lecture 21 Arrays – Part 1.
(Java Looping and Conditional Statements). Flow of control Sequential Executes instructions in order Method Calls Transfer control to the methods, then.
Web & Systems Developer, 30 credits
Chapter 6 More Conditionals and Loops
Repetition-Sentinel,Flag Loop/Do_While
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
SELECTION STATEMENTS (1)
Control Statement Examples
Chapter 6 More Conditionals and Loops
Introduction to Classes and Methods
Know for Quiz Everything through Last Week and Lab 7
python.reset() Also moving to a more reasonable room (CSE 403)
Arrays in Java.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Announcements Program 1 due noon Lab 1 due noon
How do you do the following?
More on iterations using
Control Statements:.
Presentation transcript:

Introduction to Programming G50PRO University of Nottingham Unit 8 : Methods 2 - Arrays Paul Tennent 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 current balance 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 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"); }

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 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

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 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 First Element at Index 0 Array Length is 8

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 First Element at Index 0 Array Length is 8 5

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; } First Element at Index 0 ar.length =

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