Comparing objects booleans &&, ||and !

Slides:



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

Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
Chapter 2 Review Questions
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Understanding class definitions Looking inside classes.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
UML Basics & Access Modifier
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Java – Methods Part II Lecture Notes 6. Objects, Classes and Computer Memory When a Java program is executing, the memory must hold: 1. Templates for.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Overriding toString()
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
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.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
Program Organization Sequential Execution: One line done after the other Conditional Execution: If a test is true, one section is done, otherwise another.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Classes and Objects - Part I. What is an Object? An Object has two primary components: state – properties of the object behavior – operations the object.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Decisions (If Statements) And Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
Midterm preview.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Lecture 4b Repeating With Loops
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CSC111 Quick Revision.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Boolean Expressions and Conditionals (If Statements)
Elementary Programming
Chapter 2 Elementary Programming
Chapter Goals To implement decisions using if statements
Starting Out with Java: From Control Structures through Objects
Conditional Loops.
An Introduction to Java – Part I, language basics
Variables ICS2O.
The Boolean (logical) data type boolean
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
class PrintOnetoTen { public static void main(String args[]) {
Chapter 5 server Side Scripting Date: 01 October 2017.
Lecture Notes – Week 2 Lecture-2
Object based programming in Java
Object based programming in Java
Building Java Programs
Names of variables, functions, classes
CS Week 2 Jim Williams, PhD.
The for-statement.
Methods/Functions.
Presentation transcript:

Comparing objects booleans &&, ||and !

Elevator Redux—3 options if (...) { System.out.println("Error: There is no thirteenth floor."); } else if (...) System.out.println("Error: The floor must be between 1 and 18."); else // Now we know that the input is valid int actualFloor = floor; actualFloor = floor - 1; System.out.println("The elevator will travel to the actual floor " + actualFloor); Or operator: ||

Logical operators and their return values

Practice with logical expressions

Code using logic—test that a letter is a vowel String letter = letters.substring(i, i + 1); // A little piece of code to get you started // Check 'a', 'e', 'i', 'o', 'u' if (letter.equals("a")||___________) { return _______; } else return ________;

Strings are objects– do not use == public class EqualStrings { public static void main(String[] args) String first = "Java"; String second = "licious"; String firstAndSecond = first + second; String third = "Javalicious"; if (firstAndSecond==third) System.out.println("They are the same"); } else System.out.println("firstAndSecond is " + firstAndSecond); Output???

Objects and == firstAndSecond "Javalicious"; third "Javalicious"; == compares the memory locations stored in the two open boxes, not the contents stored in the blue boxes

Strings and other objects use equals() if (firstAndSecond.equals(third) { System.out.println("They are the same"); } else System.out.println("firstAndSecond is " + firstAndSecond);

box 1 == box2 returns true box 1 == box3 returns false

How do we determine if two rectangles are equal? Rectangle box = new Rectangle(5, 10, 20, 30); Rectangle box2 = box; Rectangle box3 = new Rectangle(5, 10, 20, 30); box.setColor(Color.RED); box3.setColor(Color.RED); box.draw(); box3.draw();

Write equals() method for Rectangle Class—ignore color public boolean equals(Rectangle other) { if( _________&& __________ && _____&&______) return true; } else return false; this keyword is helpful!

This keyword in action /** Constructs a new Color object. @param red the red value of the color (between 0 and 255) @param green the green value of the color (between 0 and 255) @param blue the blue value of the color (between 0 and 255) */ public Color(int theRed, int theGreen, int theBlue) { red = theRed; green = theGreen; blue = theBlue; }

More this and other /** Constructs a new Color object. */ public Color(Color other) { this.red = other.red; this.green = other.green; this.blue = other.blue; } Not calling methods, accessing instance variables

What do you expect to see? public class DayTester { public static void main(String[] args) Day theFourth = new Day(2013, 7, 4); Day julyFour = new Day(2013, 7, 4); String answer = "false"; if (theFourth == julyFour){answer = "true";} System.out.println("Actual: "+answer); System.out.println("Expected: true"); }

How would we write an equals method for testing two days: eg: theFourth.equals(julyFour); month = 7 year = 2013 day = 4