Oops... The last example of the last lecture: private static void makeEx (Ex e) { e = new Ex(); e.field = 4; } public static void main (String[] args)

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

Introduction to Java 2 Programming Lecture 5 The Collections API.
This is Java Jeopardy.
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Introduction to Computer Programming Stringing Along – Using Character and String Data.
Introduction to Computers and Programming Lecture 7:
Introduction to Computers and Programming Lecture 7:
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Lecture 221 CS110 Lecture 22 Tuesday, April 20, 2004 Announcements –hw10 due Thursday, April 22 –exam next Tuesday, April 27 Agenda –Questions –Error handling.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Unit 201 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
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.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Intro to Object-Oriented (“OO”) Design. OO Design Simplified methodology 1. Write down detailed description of problem 2. Identify all (relevant) nouns.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
CHARACTERS AND UNICODE Java Data Types. Unicode Characters, like the letters of the alphabet and other printable symbols, are represented internally in.
Basic Concepts of OOP in C++ Darvay Zsolt. C++ 2 Outline  The namespace and its members  The using declaration and directive  The address operator.
Mathematical Calculations in Java Mrs. G. Chapman.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
3: Controlling Program Flow Using Java operators Mathematical operators Relational operators Logical operators –Primitive type: ALL (the same with C) –String:
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Mathematical Calculations in Java Mrs. C. Furman.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Output Programs These slides will present a variety of small programs. Each program has some type of array that was introduced in this chapter. Our concern.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Creating and Using Class Methods. Definition Class Object.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
String str1 = JOptionPane.showInputDialog(“enter a string”); String str1 = JOptionPane.showInputDialog(“enter a string”); String str2 = JOptionPane.showInputDialog(“enter.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Vector program patterns Vectors contain many elements, so loops are common. Counted processing // Print the first 3 elements. for (int i = 0; i < 3; i++)
CSCI 51 Introduction to Programming March 10, 2009.
CPSC 233 Tutorial Xin Liu Feb 14, Tips on keyboard input How to detect that the user just hit enter when prompted for a string import java.util.Scanner;
I/O in java and Revision. 2 I/O in Java Many packages and libraries associated with Java provide sophisticated ways to input and output information, e.g.:
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Introduction to programming in java
Today’s topic: Arithmetic expressions.
Introduction to Computer Science / Procedural – 67130
Maha AlSaif Maryam AlQattan
Building Java Programs
Multiple variables can be created in one declaration
Building Java Programs
Hash table another data structure for implementing a map or a set
A few bits of information
OPERATORS (2) CSC 111.
The compareTo interface
Java Lesson 36 Mr. Kalmes.
Things to Remember.
Control Structure Chapter 3.
Sieve of Eratosthenes The Sieve of Eratosthenes uses a bag to find all primes less than or equal to an integer value n. Begin by creating a bag an inserting.
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Building Java Programs
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Control Structure.
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
CSC 205 Java Programming II
Presentation transcript:

Oops... The last example of the last lecture: private static void makeEx (Ex e) { e = new Ex(); e.field = 4; } public static void main (String[] args) { Ex e = new Ex(); // Should have been just Ex e; makeEx(e); System.out.println(e.field); }

Comparing objects Simple comparisons can be done with ==, !=, >=, etc.: int i, j;... // Give values to i and j. if (i == j) System.out.println ("same"); The rules are the same for objects, but they don't work as you'd expect. Ex e = new Ex(); e.field = 2; Ex f = new Ex(); f.field = 2; if (e != f) System.out.println("not same"); The == operator compares the values of the expressions, and the values here are references.

More object equality tests You can use the == operator to compare the values of any pair of expressions, as long as the value is what you really want to compare. if (e.field == f.field) System.out.println("same"); e = f; // Now they refer to the same object. if (e == f) System.out.println ("same");

Comparing strings Strings are objects. Don't use == to compare them. String line1, line2; line1 = in.readLine(); line2 = in.readLine(); if (line1.equals(line2))... Use the equals( ) method to see if two strings are the same.

Which string is first? To find out which string comes first in the alphabet, don't use. Instead, use the compareTo( ) method, which returns an integer: int line1isInThisDirectionFromLine2 = line1.compareTo(line2); The value of line1.compareTo(line2) is: 0 if they're the same < 0 if line1 comes before line2 > 0 if line1 comes after line2 Use the compareTo( ) method to compare strings.

String comparison: some details Example: "zoo".compareTo("aardvark") > 0 String comparison uses "dictionary order", based on the collating sequence. Java's collating sequence is Unicode, which includes ASCII. These are the only things to remember about Unicode (or ASCII): blank comes before all other printable characters A.. Z, a.. z, are each contiguous and in order.

Washing machines with loads class WashingMachine { private String contents; public String toString() { return "Contents: " + contents; } public void add (String item) {contents = item;} } public class TestWash {...main... { WashingMachine wm = new WashingMachine(); wm.add("sock"); wm.add("hat"); System.out.println(wm); }} What's the output?

Keeping a list A Vector object (an object of the Vector class) can hold objects of any class. Vector list = new Vector(); list.addElement("sock"); list.addElement("hat"); To get an object back from a Vector: String item = (String) list.elementAt(0); System.out.println("First item in list: " + item); The cast in front of the elementAt( ) call is essential. item = (String) list.elementAt(1); System.out.println("Second item in list: " + item); The contents of the list are not changed by calling elementAt( ).

WashingMachine again class WashingMachine { private Vector contents; public WashingMachine () { contents = new Vector(); } public void add (String item) { contents.addElement(item); } public String toString() { String result = "Contents: "; for (int i = 0; i < contents.size(); i++) result += " "+(String) contents.elementAt(i); return result; } public class TestWash {...main... { WashingMachine wm = new WashingMachine(); wm.add("sock"); wm.add("hat"); System.out.println(wm); }}