Download presentation
Presentation is loading. Please wait.
Published byFrancine Gardner Modified over 9 years ago
1
Lecture Notes – Classes and Objects (Ch 7-8) Yonglei Tao
2
Arrays Hold sequences of values of primitive types or objects When an array is created, its values are initialized zero, false, or null int [] scores; scores = new int [10]; for (int i = 0; i < scores.length; i++) scores[i] = i * i; for (int n: scores)// a “for each” loop System.out.println (n);
3
Arrays (Cont.) String[] names = {“Ed”, “Bob”, “Cindy”}; System.out.println( names[0] ); if ( names[0].equals(“Ed”)) … String[] list = new String[5]; list[0] = new String(“Tom”); … A two-dimensional array int rows = 10, cols = 20; int [][] table = new int[rows][cols];
4
Partially Filled Arrays - Loading int numValues = 0; Scanner in = new Scanner(System.in); while (in.hasNextDouble()) { if (numValues < values.length) { values[numValues] = in.nextDouble(); numValues ++; } for (int i = 0; i < numValues; i++) { System.out.println(values[i]); }
5
Class ArrayList A sequence of objects Can grow and shrink as needed A generic class with a type parameter ArrayList classlist = new ArrayList (); classlist.add ( “Ed” ); classlist.add ( “Bob” ); classlist.add ( “Cindy” );
6
ArrayList (Cont.) for (int i = 0; i < classlist.size(); i++ ) { String name = classlist.get(i); System.out.println (name); // Ed, Bob, Cindy } for ( String name: classlist ) System.out.println (name); classlist.set (1, “Tim”);// Ed, Tim, Cindy classlist.add (1, “Ron”);// Ed, Ron, Tim, Cindy classlist.remove (2);// Ed, Ron, Cindy
7
Wrapper Classes
8
Auto Boxing and Unboxing int n = 25; Integer num = n; // same as num = new Integer (n); Double rate = 5.25; double d = rate; rate = rate + i; Boolean isDone = new Boolean(false); ArrayList values = new ArrayList (); values.add(29.95); double x = values.get(0);// inefficient
9
Arrays vs. ArrayLists When do you use which? Element type Number of elements Basic operations Locate insert (at the front, in the middle, at the end) delete (the first, a middle one, the last)
10
Common Array Algorithm - Search int pos = 0; boolean found = false; while (pos < values.size() && !found) { if (values.get(pos) > 100) { found = true; } else { pos++; } } if (found) { System.out.println("Position: " + pos); } else { System.out.println("Not found"); }
11
Common Array Algorithm - Insert ArrayList: use method add Unordered array: if (numValues < values.length) { values[numValues] = newElement; numValues++; }
12
Common Array Algorithm - Insert Ordered array: if (numValues < values.length) { for (int i = numValues ; i > pos; i--) values[i] = values[i - 1]; values[pos] = newElement; numValues ++; }
13
Common Array Algorithm – Array Copy double[] values = new double[6];... // Fill array double[] prices = values;? double[] prices = Arrays.copyOf(values, values.length); values = Arrays.copyOf(values, 2 * values.length);
14
Classes Each has a responsibility Each provides services for clients Objects are instances of a class Members Instance variablesprivate Instance methodsprivate Instance methodspublic- interface
15
Method Call A call obj.doIt (num, name); Method definition public void doIt (int n, String s) { n += 4; System.out.println (“Start with “ + s.charAt(0)); } Every thing is passed by value What if having s = “Mr. “+ s; in method doIt()? “tom” 6 6
16
Overloaded Methods // Methods of class MyClass public void doIt (int n) { … } public void doIt (char c) { … } public void doIt (int n, String s) { … } // Method calls MyClass obj = new MyClass(); obj.doIt(5); obj.doIt(‘a’); obj.doIt(“xyz”);
17
Constructors public class A { private int num; private String name; public A () { this(10, “tom”); } public A (int n) { num = n; } public A (int n, String s) { num = n; name = s; } … } // use of constructors A ref1 = new A(); A ref2 = new A(5); A ref3 = new A(10, “tom”);
18
Static Members of a Class Instance variable One copy per object Static variable One copy per class Static Method Operating on class-wide data Examples Integer.MAX_VALUE, Integer.MIN_VALUE Math.pow(x, y), Math.sqrt(x)
19
Scope of Local Variables public class RectangleTester { public static double area(Rectangle rect) { double r = rect.getWidth() * rect.getHeight(); return r; } public static void main(String[] args) { Rectangle r = new Rectangle(5, 10, 20, 30); double a = area(r); System.out.println(r); } }
20
Scope of Local Variables if (x >= 0) { double r = Math.sqrt(x);... } // Scope of r ends here else { Rectangle r = new Rectangle(5, 10, 20, 30); // OK - it is legal to declare another r here... }
21
Overlapping Scope public class Coin {... public double getExchangeValue(double exchangeRate) { double value;... return value; } private String name; private double value; } Which one to refer to if using value in this method? How to refer to instance variable value in this method?
22
Built-in Reference this Referring to an object from within public A (int num, String name) { this.num = num; this.name = name; } Referring to the current object A x = new A(10, “bob”); A y = new A(5, “ed”); x.setNum(1);
23
Packages in Java PackagePurposeSample Class java.lang Language support Math java.util Utilities Random java.io Input and output PrintStream java.awt Abstract Windowing Toolkit Color java.applet Applets Applet java.net Networking Socket java.sql Database Access ResultSet javax.swing Swing user interface JButton omg.w3c.dom Document Object Model for XML documents Document
24
Packages - Organizing related classes To put classes in a package package cis500.proj1; public class MyClass {... } To use class with importing import cis500.proj1.MyClass; Or import cis500.proj1.*;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.