March 29th Odds & Ends CS 239.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Programming Methodology (1). Implementing Methods main.
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
1 Value vs. reference semantics. Recall: Value semantics value semantics: Behavior where variables are copied when assigned to each other or passed as.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
INF 523Q Chapter 5: Enhancing Classes (Examples).
1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Introduction.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Mixing integer and floating point numbers in an arithmetic operation.
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.
CIS 270—Application Development II Chapter 18-Generics.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CSI 3125, Preliminaries, page 1 Compiling the Program.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Building java programs, chapter 3 Parameters, Methods and Objects.
CSE 1201 Object Oriented Programming ArrayList 1.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Classes - Intermediate
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Computer Programming Your First Java Program: HelloWorld.java.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Building Java Programs
Lecture 10: More on Methods and Scope
CMSC 202 Static Methods.
Control Statement Examples
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
String Objects & its Methods
TO COMPLETE THE FOLLOWING:
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays in Java What, why and how Copyright Curt Hill.
AP Java Warm-up Boolean Array.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Building Java Programs
More on Classes and Objects
Assignment 7 User Defined Classes Part 2
Java Lesson 36 Mr. Kalmes.
Parameter Passing in Java
Understanding Parameter Passing
CS2011 Introduction to Programming I Methods (II)
Java Inheritance.
slides created by Ethan Apter
Why did the programmer quit his job?
Building Java Programs
Scope of variables class scopeofvars {
Building Java Programs
slides created by Ethan Apter
A+ Computer Science PARAMETERS
Lecture 8-2: Object Behavior (Methods) and Constructors
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

March 29th Odds & Ends CS 239

Re: instance variables Public class DoSomething { int count; // count is an instance variable FACT 1: when a class is instantiated instance variables are initialized FACT 2: variables of primitive types within a method MUST be initialized by the programmer

Look back at ChangeMaker trace The detailed trace in the key only handles the j = 1 case in the initial call. Still needs to handle the j=2 case in the initial call.

Overloading When you overload a method the method names are the same but the parameter lists are different.

Strings Are immutable When you run the code on the next slide, you are not changing the value at the original storage location, you are being given a new storage location for the new String. There is no way to get the address of a String or any other java Object.

String example public class TestString { public static void main (String [] args) String animal = “cat”; System.out.println (animal); animal = animal.concat(“bird”); } // end main } // end class

Addresses in Java Java doesn’t want you to see addresses When we printed out numbers in class, what we were seeing was not an address. It was a hash code.

TestParameterPassing.java public class TestParameterPassing { public static void main (String [] args) { Integer c,d; ParameterPassing myParameterPassing; myParameterPassing = new ParameterPassing(); // c = new Integer(7); // d = new Integer (13); c = 7; d = 13; System.out.println (" c = " + c + " d = " + d); // before call to swap myParameterPassing.swap(c,d); System.out.println (" c = " + c + " d = " + d); // after call to swap } // end main } // end class In the current version of java, using c = 7; and d = 13; is equivalent to the commented out lines above. In either case, the swap method with two Integer parameters is the one that will be called.