1 Lecture 23/2/11 Working with an array Array of user-defined objects (if time permits) 2 MARCH, next Wednesday: Reading week - No lecture 6-7.30, but.

Slides:



Advertisements
Similar presentations
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Advertisements

Introduction to Programming
1 Lecture 16/3/11: Contents Example of an array of user-defined objects: Car and Carr Constructor Providing functionalities for a user- defined object.
Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
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.
1 One-Dimensional Arrays  What are and Why 1-D arrays?  1-D Array Declaration  Accessing elements of a 1-D Array  Initializer List  Passing Array.
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Introduction to Objects A way to create our own types.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
SOFTWARE AND PROGRAMMING 1 Revision Lecture 11/5/2011: Review of concepts involved in lectures and exam Review of questions in previous exam papers (they.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
SOFTWARE AND PROGRAMMING 1 Lecture 7/5/8: Review of concepts involved in lectures and exam Lecture 14/5/8: Review of questions in previous exam papers.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
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.
Methods We write methods in our programs for many reasons:
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: (from ) Ms Mihaela Cocea Room London Knowledge Lab Emerald.
Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
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.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
1 Lecture 9/3/11: Contents Array of user-defined objects 2D array.
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];
SOFTWARE AND PROGRAMMING 1 Lecture 4: Ticketing machine details: Constructor, method, static/instance variables Instructor: Prof. Boris Mirkin
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
What’s cheating and what is not?
Building Java Programs
Arrays An Array is an ordered collection of variables
CSC 113 Tutorial QUIZ I.
python.reset() Also moving to a more reasonable room (CSE 403)
class PrintOnetoTen { public static void main(String args[]) {
Factoring if/else code
Arrays in Java.
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

1 Lecture 23/2/11 Working with an array Array of user-defined objects (if time permits) 2 MARCH, next Wednesday: Reading week - No lecture , but – room B12 Cruciform is available from : Martin O’Shea will consult over any questions on SP1

2 Advert In-class open-book Test 9/3/11 subjects –Strings –Methods –Arrays

3 Test 1 results 39 or less5 40 through or more9 Absent 17 Total52 Those with marks 50 or less should put more effort: (a)Look through previous lecture slides before lectures (b)Try ask questions and do not stop if anything remains unclear; (c)Visit all lab sessions; (d) Try do the same tasks by yourself in DCSIS labs;

4 Array Array is an indexed list of elements of the same type; the index is supplied by default (!) A string array nam[ ] : contains both entries and index. String[] nam ={“John”,“Paul”,“George”,“Ringo”}; Index: Length (the number of entries) is 4 An integer array age[ ]: int age[ ]= {23, 32, 19, 30, 25, 25, 23, 30}; Index: Length is 8

5 Work with arrays(1) Data of 5 students: double height[ ]={1.56, 1.72, 1.80, 1.85, 1.90}; //in m double weight[ ]={65.3,80.0,78.1,76.5,112.8}; // in kg Problem: compute the body mass index for all the students, bmi=weight/height 2 (in the US, those with bmi between 20 and 25 are considered of normal weight)

6 Work with arrays(2) Loop for is natural with arrays: the index used as the counter double bmi[ ]=new double[5]; for (int I = 0; I < 5; I + +) bmi[I]=weight[I] / (height[I]  height[I]); If length of student arrays is not known or is variable, put array’s length whatever it is: double bmi[ ]=new double[height.length]; for (int I = 0; I < height.length; I + +) bmi[I]=weight[I] / (height[I]  height[I]);

7 Work with arrays(3) The same result with a method for the bmi: double[ ] bmiind(double h[ ], double w[ ]){ double in[ ]; for (int ii = 0; ii < h.length; ii = ii+1) in[ii]=h[ii]/(w[ii]  w[ii]); return in; } Method bmiind is just a frame box; to make it work, one needs to put within a class this: double[ ] bmi=bmiind(weight, height);

8 Work with arrays(4) Still, no result on the screen!!! Printing arrays is not easy in Java: no innate methods for that! double[ ] bmip(double h[ ], double w[ ]){ double in[ ]; for (int a = 0; a < h.length; a++){ in[a]=h[a]/(w[a]  w[a]); System.out.println(a + “ ’s bmi is “+in[a]);} return in; } double[ ] b=bmip(weight, height); //Outputs and Prints

9 Finding a maximum in an array double x[ ]; //assume taken from somewhere int place=-1; //index of the max entry double maxim=-1000; for (int i = 0; i < x.length; i = i+1) { if (x[i] > maxim) {maxim=x[i]; place=i;} } Question: Make it into a method.

10 Finding maximum : a method int place= -1; //for keeping the index of max entry double findMax(double x[ ]){ //method’s wrap-up double maxim= -1000; for (int i = 0; i < x.length; i = i+1) { if (x[i] > maxim) {maxim=x[i]; place=i;} } } // Note: a trick with “place”

11 Finding the average in an array double x[ ]={1.1, 1.2, 1.6, 2.0,1.1}; double average=0; //to accumulate the sum for (int i = 0; i < x.length; i++) average=average+x[i]; //after this, average=7.0; average=average/x.length; //average=7.0/5=1.4; Question: Make it into a method.

12 Finding the average: method public static void main(String[ ] args){ double x[ ]={1.1, 1.2, 1.6, 2.0,1.1}; double average=MetAv(x);} //average=1.4 public static double MetAv(double[ ] a){ double av=0; for (int i = 0; i < a.length; i++) av=av+a[i]; av=av/a.length; return av; }

13 Constrained average: method public static void main(String[ ] args){ double x[ ]={1.1, 1.2, 0, 1.6, 2.0, 0}; //av. of no 0s double average=ConAv(x);} public static double ConAv(double[ ] a){ double av=0; int counter=0; for (int i = 0; i < a.length; i++) { if(a[i]!=0){ counter++; av=av+a[i];}} return av/counter; }

14 User defined type reading Follows such classes as: - Oblong and OblongTester, sections 6.3 and 7.2, Charatan and Kans, “Java in two semesters” - Counter and CounterTest, sections , Pohl and McDowell, “Java by dissection” - Employee and TwoEmployee, p , Ch.3, Farrell, “Java programming”

15 Array of user-defined type(1) The setting: We have a number of applicants for whom we have separate, but matching, lists of names and id’s organised as arrays. We would like to develop a new type for an Applicant to hold all individual data of the applicant, in this case just id and name, but it can have as many attributes as it takes. With this new type, we would like to organise a list of applicants as an array of this type

16 Array of user-defined type(2) To develop an array of applicants, we need –A named class, say Appl, with variables declared to hold all individual applicant data (A); –A constructor in this class that would take values from the arrays holding id and name information (B); –Representation of the id and name arrays (C); –Generation of an instance of array of new type, Appl (D); –Feeding the Id’s and names into the Appl array (E); We can show that this does work by printing out data of all the entries in the Appl array.

17 Array of user-defined type(3) class Appl{ public int ids; public String nms; public Appl(int iden, String nnn){ ids=iden; nms=nnn;} static int[] iden(){ int ii[]={12, 15, 22, 18, 11}; return ii;} static String[] namen(){ String jj[]={"Aa", "Bb", "Cc", "Dd", "Ee"}; return jj;}

18 Array of user-defined type(4) public static void main(String[] args){ int id[]=iden(); String name[]=namen(); Appl[] many=new Appl[id.length]; for(int i=0;i<id.length;i++) many[i]=new Appl(id[i],name[i]); //many – array of Appl type objects. Check: for(int i=0;i<name.length;i++){ System.out.println(i +"th applicant data:"); System.out.println("Id: "+many[i].ids); System.out.println("Name: "+many[i].nms); System.out.println(); } }

19 Array of user-defined type(5) Question: Identify which parts of class Appl correspond to tasks A, B, C, D and E on slide 16