The name actually is a place in memory that contains

Info icon This preview shows pages 8–11. Sign up to view the full content.

View Full Document Right Arrow Icon
- The name actually is a place in memory that contains a reference (pointer) to the array object. |2| |4| |8| |16| 0 1 2 3 array [2] -> 8 array [2] = 13 --> |2| |4| |13| |16| *memory location* Array Declaration - Arrays are declared by specifiying a data type followed by square brackets followed by a variable name: double [] mpg; - Arrays need to be initialized to a specified size: String [] groceries = new String [10]; - Array may be initialized with a static initializer int [] luckyNumbers = {9, 25, 16, 18}; - An double [] table1={1.7,2.4}; double [] table2 = {3.9,4.5}; table1=table2; ARRAY LIST - Is a dynamic array - not in the Java core language import java.util.ArrayList; - Declare using angled brackets to indicate the type of data being stored in the ArrayList. ArrayList<Integer> list= new ArrayList,Integer>(); - Methods add(int index, E Value) Add a value at index in the list
Image of page 8

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
remove(int index) Remove the value at the specificed position get(int index) Returns the value at the indexed position set(int index, E Value) Replaces the value at the index size(); Returns the size of list isEmpty(); Returns true if there are no elements in the list arrayList.remove(1); arrayList.add(1,32); -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ------------------------------- Classes 11/29/2017 fixedMonthlyPayment=Math.round(100.0xinitialPrincipalxperiodicInterestRate) (1-(1+peridiodicInterestRate)^-number of payments <----- Rounding ERROR -------------------------------------------------------------------------------- --------------- 100.0 Do not use the Java keyword STATIC Define CONTRUCTORS to initialize objects Use ACCESSOR METHODS to get and set variables - Classes encapsulate information and behavior - Information is stored in variables - Behavior is captured in methods - No other code is allowed in a class public class CoordinatePair { <----------- Class names begin with a capital letters and proceed in camel-case private double x = 0.0; <-----------Information is contained in Instance Variables, also known as Data Members private double y = 0.0; public String toString() { return format ("(%.3f, %.3f);, x, y ); <---------Behavior is captured in Methods } } public class CoordinatePair2 { private double x = 0.0; <-----------INSTANCE VARIABLES are declared as private public double getX () { <------------GETTERS are methods used to retrieve the value of instance variables return x; } public void setX (double newe1){ return x; <--------SETTERS are methods used to change the value of instance variables } } public class CoordinatePair3 { private double x = 0.0; <-----------INSTANCE VARIABLES are private so you have complete control over them private double y = 0.0;
Image of page 9
public Coordinate Pair3 (double newx, double newy) { <-----------------CONSTRUCTORS have no return type x = newx; y = newy; } } public class CoordinatePair4 { private double x = 0.0; <-----------INSTANCE VARIABLES are private so you have complete control over them private double y = 0.0; public static void main (String[]args){ CoordinatePair4 pair = new CoordinatePar( 256, 64); System.out.println("x="+ pair.x); } } OBJECTS - Objects are values whose data type was created by a Java programmer. - Objects are referred to by a reference (memory location) - Objects are created by the "new" command: Scanner scan = new Scanner ( System.in ); - Classes are used to define data types - Objects are instances of a class data type - null indicates that no object has been assigned to the variable - A null value is the default value for variables that have a class data type.
Image of page 10

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern