Week101 APCS-A: Java November 8, 2005. week102 Java Packages All Java classes are grouped into libraries (or packages)  String is part of the java.lang.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Programming with Collections Collections in Java Using Arrays Week 9.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Week71 APCS-AB: Java Control Structures October 17, 2005.
Writing Classes (Chapter 4)
Java Classes Using Java Classes Introduction to UML.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Week91 APCS-AB: Java Problem Solving November 1, 2005.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
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.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CSE 1201 Object Oriented Programming Using Classes and Objects.
Week101 APCS-AB: Java Miscellaneous Topics: Snippets we missed in Chapters 1-6 of book November 11, 2005.
CSE 1201 Object Oriented Programming ArrayList 1.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
BIT115: Introduction to Programming
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
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.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
Week91 APCS-A: Java Problem Solving November 2, 2005.
Chapter 9 Introduction to Arrays Fundamentals of Java.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Chapter VII: Arrays.
Formatting Output & Enumerated Types & Wrapper Classes
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 3: Using Methods, Classes, and Objects
MSIS 655 Advanced Business Applications Programming
Defining methods and more arrays
Object Oriented Programming in java
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Packages & Random and Math Classes
Object Oriented Programming in java
Outline Creating Objects The String Class The Random and Math Classes
Presentation transcript:

week101 APCS-A: Java November 8, 2005

week102 Java Packages All Java classes are grouped into libraries (or packages)  String is part of the java.lang package, which is pre- loaded when you are programming in Java  We’ve already seen one other library, the java.util library, where Scanner is Some of the other standard Java Libraries:  java.appletjava.util  java.awtjava.math  java.iojava.net  java.lang javax.swing

week103 Using Packages Everything in java.lang is available for use  So it’s as if somebody already did: import java.lang.*; To use other packages, we need to import either the specific class or the entire package (just like we did for Scanner class)  To import a class we use the whole package name: import java.util.Scanner; import java.io.File;  To import an entire library we use the asterisk: import java.util.*; import java.io.*;

week104 Math Class Defined in the java.lang package All methods in the Math class are static methods (aka class methods)  This means that you do not need an object of the class to call the methods - you invoke them directly through the name of the class Some useful methods:  static double sqrt (double power)  static double pow (double num, double power)  static double random ()  static double cos (double num)  static double sin (double num)  static double tan (double num)

week105 static Static Methods - invoked by class name, no object needed  Can be used when no object state is needed to do an action Static Variables (also called Class Variables) are shared among all instances of a class  There is one copy of the variable for all objects in the class  This is different from an instance variable in which each instance (object) has its own version of the variable

week106 enum Enumerated type, lists all the possible values of a variable of the type  The values given are identifiers Examples: enum Season {winter, spring, summer, fall} enum Grade {A, B, C, D, F} enum STA-grade {Aplus, A, Bplus, B, Cplus, C, Dplus, D, F} Using an enum type: STA-grade myGrade = STA-grade.Aplus; Season time = Season.fall;

week107 More about Classes & Design Need to be consistent with our models and design of classes. One way to do this is to use UML (Unified Modeling Language) diagrams to visualize programs Use box to represent class  Top box - Class name  Middle box - instance variables  Bottom box - methods Song length : int artist : Artist album : Album play() : void toString(): String

week108 Encapsulation The instance data of an object should only be modified by that object  Keep the data private Make other objects use getter (accessor) and setter (mutator) methods to access and change data This guarding of data is called encapsulation Violate encapsulation Provide services to clients Support other methods in class Enforce encapsulation public private variables methods Figure 4.5

week109 Wednesday Finishing up lecture from yesterday…

week1010 Class Relationships Dependency (one class “uses” another)  But how does a class gain access to another object? One class can instantiate (create) the other object One class can gain access to another by getting that object as a parameter to a method  The more classes depend on one another, the more changes in one class can impact another (which can be troublesome) Classes can depend upon objects of the same class (ie one object of a class interacts with another object of the same class)

week1011 Class Relationships Composition (aggregation) - one object is made up of other objects  Can be described as a “has-a” relationship  A special type of dependency

week1012 Homework Write a class that will represent a student at STA or NCS. This is a very simplistic model, but this student only knows what form he or she is in, and can only do math  The student should be assigned to a form when constructed  Yesterday we talked about enums. They can be useful in many situations, but here we will use it to define the range of legal values for form assignment  The student class should have a method called “doMath” that will do the type of math appropriate for their grade level (defined below) The function should use input and output as we have previously done (input using Scanner) The method should make appropriate calls to static methods of the Math class

week1013 More Homework The following is the mapping of forms to the math they can do  C Form - parrot back the number you are given  B Form - give the IEEE remainder (std 754) of 2 nums  A Form - rounding a number  I Form - providing the absolute value of a number  II Form - exponentiation  III Form - Square roots  IV Form - Sin functions  V Form - provide the larger of two numbers  VI Form - Quadratic Formula

week1014 APCS-A: Java Arrays (related information in Chapter 7 of Lewis & Loftus) November 11, 2005

week1015 Grouping objects Fixed-sized collections  When you know in advance how many items will be stored in a collection (and that stays fixed for the life of the collection)  A fixed-sized collection is an array It’s kind of like a tray of cups – each cup can hold an object or a primitive data type Note: the array itself is an object Flexible-sized collections  When you don’t know in advance how many items you will need to store  Will go into more details about these in a few weeks

week1016 Arrays An array is a group of variables (called elements or components) containing values that all have the same data type To refer to a particular element in an array, use the array’s name and the position number of the element in the array int array called c c[0] c[3] c[4] c[5] c[6] c[7] c[8] c[1] c[2]

week1017 Anatomy of an array Array names are the same as any other variable name An array with 9 elements (or variables) First element has index zero (the zeroth element) ith element of array c is c[i -1] Index must be a positive integer (or an integer expression that can be promoted to an int) int array called c c[0] c[3] c[4] c[5] c[6] c[7] c[8] c[1] c[2]

week1018 Advantages to using arrays It is easy to access individual items in an array (and it’s efficient) Arrays are able to store objects or primitive type values (int, double, float, etc)  (We’ll see later that the flexible-sized collections can only store objects)

week1019 Declaring and creating arrays Array objects occupy space in memory. All objects in Java must be created with a keyword new When you declare an array you must tell Java the type of the array elements and the number of the elements that will appear in the array Example declaration for an integer array called hourArray: int hourArray[] = new int[24]; // declares and creates an array to hold 24 int elements

week1020 An array hourArray :int[ ]

week1021 How do we fill an array? All values are initially set to zero/null when we initialize an array One way to fill the array is to use an array initializer, a comma-separated list of expression enclosed in braces: when you declare it int n[] = {10, 23, 34, 235, 234};  The array length is automatically determined by the number of elements in the initializer list

week1022 Class Exercise Write code to declare and initialize an array to hold the months of a year Write code to declare and initialize an array to hold the number of days in each month of the year

week1023 How else do we fill an array? Any ideas about how else we could fill an array? A large number of the things we want to do with arrays involve manipulating the entire array. Unfortunately, there are very few built- in operations that are defined to work on the contents of an entire array. Instead, we must define those processes in terms of what they require us to do to the individual elements of the array. This task is then repeated once for each element of the array. Since the size of the array is fixed once the array is allocated, it is a natural application of a for-loop.

week1024 How else do we fill an array? Arrays and for-loops go together like peanut butter and jelly!  For filling, for printing values, for checking values, etc We could use a for loop and access each array element to set a value to it: int myArray = new int[10]; for(int i =0; i < myArray.length; i++){ myArray[i] = 5 * i; }  Note: unlike the String class when we access the length of the array we are not calling a method, rather we are accessing one of the array’s instance variables

week1025 Exercise Print out the names and lengths of each of the months

week1026 Mistakes It is not uncommon to make a programming mistake that causes the bounds of a loop to be mis-defined and the loop index variable to stray beyond the range of the array. For example, what if in your for loop you wrote: for(int i = 0; i <= array.length; i++){ System.out.println(array[i]); }

week1027 Lab/Homework Lab: LightsOut game - we will work on this in class today and Monday Homework: Chess Design Project - Due next Wednesday Note: Quizzes the next two Tuesdays.