1 Lecture 9/3/11: Contents Array of user-defined objects 2D array.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

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.
Phil Campbell London South Bank University Java 1 First Steps.
CSCI 160 Midterm Review Rasanjalee DM.
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.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
Important Notes Pay attention to file names!!! You must name them what we tell you to. mkdir – make a folder (make the ones we tell you to) rm – remove.
2  An instruction or group of instructions need to be repeated several times.  2 types of iteration: 1.You do not know how many times you will need.
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 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
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.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
1.00/ Lecture 8 Arrays and Vectors. Arrays-1 Arrays are a simple data structure Arrays store a set of values of the same type – Built-in types.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
Java SE 8 for Programmers, Third Edition
Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
F27SA1 Software Development 1 7. Java Programming 6 Greg Michaelson.
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.
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: (from ) Ms Mihaela Cocea Room London Knowledge Lab Emerald.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
2D Arrays A multi dimensional array is one that can hold all the values above. You set them up like this: int[ ][ ] gradeTable = new int[7][5]; With the.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
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.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
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
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Chapter 9 Introduction to Arrays Fundamentals of Java.
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
Lecture 4b Repeating With Loops
SOFTWARE AND PROGRAMMING 1
Accessing Files in Java
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Computing Adjusted Quiz Total Score
CSC 113 Tutorial QUIZ I.
Assignment 7 User Defined Classes Part 2
python.reset() Also moving to a more reasonable room (CSE 403)
Sampath Kumar S Assistant Professor, SECE
class PrintOnetoTen { public static void main(String args[]) {
Method of Classes Chapter 7, page 155 Lecture /4/6.
Arrays review.
Thanachat Thanomkulabut
Presentation transcript:

1 Lecture 9/3/11: Contents Array of user-defined objects 2D array

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

3 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

4 Array of user-defined type(2) To develop an array of applicants, we need –The ID and name arrays (A); –A named class, say Appl, with variables declared to hold all individual applicant data (B); –A constructor in this class that would take values from the arrays holding ID and name information (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

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

6 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(); } }

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

8 2D arrays The last subject to study in SP1

9 2D arrays: example Example - week sales at each of four shops: Sunday Days | | | 1| | 2| | 3|

10 2D arrays: actions Declaration and initialisation (with zeros): int[ ][ ] sales = new int[4][7]; Filling in: sales = { {22, 49, 4, 93, 0, 12, 32}, ………………………, {54, 0, 76, 31, 4, 3, 99} }

11 2D arrays: accessing Reaching a component: sales[2][5] = 23 or int aa = 2; int bb = 5; sales[aa][bb]=23 sales[bb][aa] = ? (answer: error) because sales[5][2] does not exist

12 2D arrays: processing Summary sales: int sum =0; for (int shop = 0; shop < 4; shop ++) for(int day = 0; day < 7; day ++) sum = sum + sales[shop][day]; As a method: public int sum( int[][] a) { int total = 0; for (int row = 0; row < a.length; row ++) for( int col = 0; col < a[0].length; col ++) total = total + a [row][col]; return total; }

13 2D arrays: different row lengths Different row lengths: int[ ] Twodarray={{1, 1, 1}, {1, 3}, {4,5,4,5}} Modifying the summation method: public int sum( int[][] a) { int total = 0; for (int row = 0; row < a.length; row ++) for( int col = 0; col < a[row].length; col ++) total = total + a[row][col]; return total; } int summa=sum(Twodarray); //application of the method

14 Two-dimensional arrays Summary sales for Wednesday (4 th day): int sum =0; for (int row=0; row< 4; row ++) sum = sum + sales[row][3]; Further problems: methods: –summary sales on Wednesday (next page) –summary sales by week-day (after next page) –summary sales by shop

15 Summary sales on any day Method: summary sales in sales[][] on any day int sss(sales[][], int day){ int sum =0; for (int r=0; r<sales.length; r++) sum = sum + sales[r][day]; return sum;}

16 Summary sales on all days: method int[] sss(sales[][], int day){ //note: output is an array! int[] sum =new int[sales[0].length]; for (int c=0; c<sales[0].length; c++) for (int r=0; r<sales.length; r++) sum[c] = sum[c] + sales[r][c]; return sum;}