CSE 11 HW 2, 3 and 4 Posted in public and on web Deadlines HW 2 turnin: Wed Jan 22 interview: Saturday January 25.class files in public directory for HW.

Slides:



Advertisements
Similar presentations
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Generics and the ArrayList Class
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Classes, Objects, and Methods
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 10. Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Computer Programming with JAVA.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
More Methods and Arrays Material from Chapters 5 to 7 that we didn’t cover in 1226.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CSE 1201 Object Oriented Programming ArrayList 1.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
More About Objects and Methods
User-Written Functions
Arrays in Classes and Methods
Building Java Programs
Building Java Programs
Building Java Programs Chapter 7
Building Java Programs
Building Java Programs
Building Java Programs
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Methods (II)
Building Java Programs
Building Java Programs
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
More About Objects and Methods Chapter 5 Programming with Methods
Presentation transcript:

CSE 11 HW 2, 3 and 4 Posted in public and on web Deadlines HW 2 turnin: Wed Jan 22 interview: Saturday January 25.class files in public directory for HW 2 & 3 Do not need to turnin the HW if you take an interview before the turnin deadline

Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any other purpose without the written permission of Walter Savitch.

Reading Cover approx. Chapters 1-5 so far. Next do Chapter 6, today, read all of it Chapter 10, sec Vectors Chapter 11 Recursion All needed for hw3-4

Inner Class Class defined within another class. Can be private and then local to the outer class. For now, we use it only to make a hw assignment into a single class for the turnin program. (Can make inner classes either public or private for this.)

You may need to make an inner class static public class Match { public static class Person { …. }//end Person …. }//end Match

Package A named collection of class. A library of classes. Can be stored in one directory and used in any directory.

Adding a class to a package package myLibrary; public class Sample {

Using a package import myLibrary; public class MyProgram {

Package Names and Directories Package name is a relative path name from a class path directory to the package directory. Uses dots rather than /, for example myPackages.CSE11.speciesStuff Class path is an environment variables of the operating system. How you set it depends on the operating system. Typically looks like: path; path; path; Be sure to include a dot for the current directory in your class path.

Class Path on sunpal setenv CLASSPATH.:/home/solaris/ieng9/cs11w/cs11wab/path1: /home/solaris/ieng9/cs11w/cs11wab/path2 Note dot at start Note the use of colons not semicolons

Class Path on sunpal setenv CLASSPATH ~/lib import iolib.savitchstuff.*; If SavitchIn.class is in ~/lib/iolib/savitchstuff Then it does not have to be in the same directory as your program or class.

Class Path on sunpal Must add the following to SavitchIn.java package iolib.savitchstuff; ~/lib should be on CLASSPATH SavitchIn.class must be in the directory ~/lib/iolib/savitchstuff Must add the following to your program import iolib.savitchstuff.*;

Arrays Give uniform names to a collection of variables all of the same type. Array type names: int[], double[], Species[] int[] a = new int[3]; declares 3 int variables named a[0], a[1], and a[2]

double[] temperature = new double[7]; int index; double sum, average; System.out.println("Enter 7 temps:"); sum = 0; for (index = 0; index < 7; index++) { temperature[index] = SavitchIn.readLineDouble( ); sum = sum + temperature[index]; } average = sum/7;

Java Arrays Indexes always start with 0 An array (as a whole) is an object Arrays check for index out of bounds Arrays know their size

Length Instance Variable String[] a = new String[10]; a.length has the value 10 a.length is read-only; You cannot change it.

Use of length for (index = 0; index < temperature.length; index++) { temperature[index] = SavitchIn.readLineDouble( ); sum = sum + temperature[index]; }

Array Instance Variables as Arguments Nothing new. If a is an array of doubles, the a[2] can be used just like any other variables of type double.

Entire Array as an Argument Array variables are reference variables, just like class objects. Array parameters are just like class parameters (Call-by-value for a reference.) Arrays know their size. One array type for all arrays with the same base type, e.g. double[] is the type for any array of doubles of any size.

Arrays Can Be Returned by a Method

public static double[] averageArray (int firstScore, int[] nextScore) { double[] temp = new double[nextScore.length]; int i; for (i = 0; i < temp.length; i++) temp[i] = average(firstScore, nextScore[i]); return temp; }

What is wrong? Species[] a = new Species[3]; a[0].readInput(); Error message “Null Pointer Exception” a[0] is a varaible of type Species, but is just a variable. It has no refernce to any object. Need a[0] = new Species();

Species[] a = new Species[3]; for (int i = 0; i < a.length; i++) a[i] = new Species(); a[0].readInput(); //OK a[1].readInput(); //OK a[2].readInput(); //OK

Multidimentional Arrays More than one index. Implemented as array of arrays Read on your own.

Recursion (Chapter 11) Recursive method == one that invokes itself Anything will compile. Need some care to get method to run OK. Read all of Chapter 11

writeVertical Example Static method ClassName.writeVertical(1234); Outputs

public static void writeVertical(int n) { if (n < 10) System.out.println(n); else //n is two or more digits long: { writeVertical(n/10); System.out.println(n%10); }

ClassName.writeVertcal(123); Equivalent to writeVertical(123/10); System.out.println(123%10); Equivalent to writeVertical(12); System.out.println(3); Equivalent to writeVertical(12/10); System.out.println(12%10); System.out.println(3);

writeVertical(12/10); System.out.println(12%10); System.out.println(3); Equivalent to writeVertical(1); System.out.println(2); System.out.println(3); Equivalent to System.out.println(1); System.out.println(2); System.out.println(3);

Stack Like a stack of paper Last-in/First-out Used to implement recursion

Successful Recursive Method Two cases Case with recursive calls Stopping case (Base case) with no recursive calls