EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from

Slides:



Advertisements
Similar presentations
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,
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Building Java Programs Chapter 7 Arrays. 2 Can we solve this problem? Consider the following program (input underlined): How many days' temperatures?
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
1 Reference semantics. 2 A swap method? Does the following swap method work? Why or why not? public static void main(String[] args) { int a = 7; int b.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Building Java Programs Chapter 7
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
BUILDING JAVA PROGRAMS CHAPTER 7: ARRAYS 1. 2 OUT-OF-BOUNDS INDEXES The indexes that are legal to access in an array are those in the range of 0 to the.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 1 Arrays (review) slides created by Marty Stepp
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CSCI 162 – Introduction to Programming II William Killian
Building Java Programs
Lecture 13: More Arrays Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Dr. Kyung Eun Park Summer 2017
Building Java Programs
Building Java Programs
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Array traversals, text processing
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Building Java Programs
Building Java Programs Chapter 7
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Autumn 2016 Lecture 19: lists as Parameters
CSE 143 Lecture 6 References and Linked Nodes reading: 16.1
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from
Building Java Programs
Building Java Programs
Why did the programmer quit his job?
Object Oriented Programming in java
Building Java Programs
Building Java Programs Chapter 7
Lecture 9: Arrays Building Java Programs: A Back to Basics Approach
Lecture 10: Arrays AP Computer Science Principles
Lecture 10: Arrays II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
Why did the programmer quit his job?
CSE 143 Lecture 5 References and Linked Nodes
Suggested self-checks: Section 7.11 #1-11
Building Java Programs
Building Java Programs
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Lecture 6: References and linked nodes reading: 16.1
CSE 143 Lecture 7 References and Linked Nodes reading: 16.1
Building Java Programs
Presentation transcript:

EE 422C Day 2 Java, Eclipse

Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from "Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. " - Stan Kelly-Bootle

Announcements Homework 1 online

Today Start off Eclipse Write programs with if/else, for and while loops, methods, object input and return. Static methods in Math and Arrays classes.

Some self-help resources Cave of Programming on YouTube. Cave of Programming – Java basics taught using Eclipse. Eclipse and Java – Using the Debugger, Alex Taylor, 7 videos on YouTube Eclipse and Java – Using the Debugger, Alex Taylor Using the Eclipse Workbench, Alex Taylor, 6 videos on Youtube Using the Eclipse Workbench

JVM JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. Java is platform independent. The JVM performs following main tasks: – Loads code – Verifies code – Executes code

8

JRE (Java Runtime Environment) Used to provide runtime environment. Physical implementation of JVM. Contains set of libraries + other files that JVM uses at runtime.

Array declaration [] = new [ ]; –Example: int[] numbers = new int[10]; index value

Array declaration, cont. The length can be any non-negative integer expression. int x = 2 * 3 + 1; int[] data = new int[x % 5 + 2]; Each element initially gets a "zero-equivalent" value. TypeDefault value int0 double0.0 booleanfalse String or other object null (means, "no object")

Accessing elements [ ] // access [ ] = ; // modify –Example: numbers[0] = 27; numbers[3] = -6; System.out.println(numbers[0]); if (numbers[3] < 0) { System.out.println("Element 3 is negative."); } index value index value

clicker Question  What is output by the following code? String[] names = new String[5]; names[1] = "Olivia"; names[3] = "Isabelle"; System.out.print(names[0].length()); A.no output due to null pointer exception B.no output due to array index out of bounds exception C.no output due to a compile error D.0 E.6

Limitations of arrays You cannot resize an existing array: int[] a = new int[4]; a.length = 10; // error You cannot compare arrays with == or equals : int[] a1 = {42, -7, 1, 15}; int[] a2 = {42, -7, 1, 15}; if (a1 == a2) {... } // false! if (a1.equals(a2)) {... } // false! An array does not know how to print itself: int[] a1 = {42, -7, 1, 15}; System.out.println(a1); //

The Arrays class Class Arrays in package java.util has useful static methods for manipulating arrays: Syntax: Arrays. ( ) Method nameDescription binarySearch(, ) returns the index of the given value in a sorted array (or < 0 if not found) copyOf(, ) returns a new copy of an array equals(, ) returns true if the two arrays contain same elements in the same order fill(, ) sets every element to the given value sort( ) arranges the elements into sorted order toString( ) returns a string representing the array, such as "[10, 30, -25, 17]"

Arrays.toString Arrays.toString accepts an array as a parameter and returns a String representation of its elements. int[] e = {0, 2, 4, 6, 8}; e[1] = e[3] + e[4]; System.out.println("e is " + Arrays.toString(e)); Output: e is [0, 14, 4, 6, 8] –Must import java.util.Arrays;

Reference semantics 17

Value semantics value semantics: Behavior where values are copied when assigned, passed as parameters, or returned. –All primitive types in Java use value semantics. –When one variable is assigned to another, its value is copied. –Modifying the value of one variable does not affect others. int x = 5; int y = x; // x = 5, y = 5 y = 17; // x = 5, y = 17 x = 8; // x = 8, y = 17

Reference semantics (objects) reference semantics: Behavior where variables actually store the address of an object in memory. –When one variable is assigned to another, the object is not copied; both variables refer to the same object. –Modifying the value of one variable will affect others. int[] a1 = {4, 15, 8}; int[] a2 = a1; // refer to same array as a1 a2[0] = 7; System.out.println(Arrays.toString ( a1 ) ); // [7, 15, 8] index012 value4158 index012 value7158 a1 a2

References and objects Arrays and objects use reference semantics. Why? –efficiency. Copying large objects slows down a program. –sharing. It's useful to share an object's data among methods. DrawingPanel panel1 = new DrawingPanel(80, 50); DrawingPanel panel2 = panel1; // same window panel2.setBackground(Color.CYAN); panel1 panel2

Objects as parameters When an object is passed as a parameter, the object is not copied. The parameter refers to the same object. –If the parameter is modified, it will affect the original object. public static void main(String[] args) { DrawingPanel window = new DrawingPanel(80, 50); window.setBackground(Color.YELLOW); example(window); } public static void example(DrawingPanel panel) { panel.setBackground(Color.CYAN);... } panel window

Copy of a reference index012 value index012 value iq a Array variables are references A parameter is a copy of the same reference the argument stores. Changes made in the method to the elements are also seen by the caller. public static void main(String[] args) { int[] iq = {126, 167, 95}; increase(iq); System.out.println(Arrays.toString(iq)); } public static void increase(int[] a) { for (int i = 0; i < a.length; i++) { a[i] = a[i] * 2; } } –Output: [252, 334, 190]

Using new Array variables are references A parameter is a copy of the same reference the argument stores. Changes made in the method to the elements are also seen by the caller. public static void main(String[] args) { int[] iq = {1, 2, 3}; int [] iq2 = increase(iq); System.out.println(Arrays.toString(iq), Arrays.toString(iq2)); } public static int[] increase(int[] a) { for (int i = 0; i < a.length; i++) { a[i] = a[i] * 2; } a = new int [3]; // <- Create new array a[2] = a[2]*2; return a; } –Output: [2, 4, 6][0, 0, 0]