Looking inside classes Conditional Statements Week 4.

Slides:



Advertisements
Similar presentations
SOFTWARE AND PROGRAMMING 1 Lecture 3: Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site
Advertisements

JAVA Revision Lecture Electronic Voting System Marina De Vos.
Understanding class definitions Looking inside classes 5.0.
Looking inside classes Fields, Constructors & Methods Week 3.
Lab 10: Bank Account II Transaction List, error checking & aggregation.
Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Understanding class definitions Looking inside classes 3.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Understanding class definitions Looking inside classes.
Python quick start guide
1 Chapter 9 Scope, Lifetime, and More on Functions.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: Ms Mihaela Cocea (from ) Room London Knowledge Lab Emerald.
PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
1 CSC 222: Object-Oriented Programming Spring 2013 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Introduction to Java Java Translation Program Structure
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
1 CSC 221: Computer Programming I Fall 2004 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Understanding class definitions
1 COS 260 DAY 3 Tony Gauvin. 2 Agenda Questions? 1 st Mini quiz on chap1 terms and concepts –Today In BlackBoard –30 min., M/C and short answer, open.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 Opbygning af en Java klasse Abstraktion og modularisering Object interaktion Introduktion til Eclipse Java kursus dag 2.
Copyright by Scott GrissomCh 2 Class Definition Slide 1 Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Comp1004: Programming in Java I Variables - Primitives, Objects and Scope.
Lecture 3: More on Classes Textbook: Chapter 2 Recap: –Classes vs. Objects –Q: What comes first a class or an object? –Q: Do we need a class to create.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Understanding class definitions Exploring source code 6.0.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
SOFTWARE AND PROGRAMMING 1 Advert : NO TEST1 on 7/02: TEST1 will be 14/02 Lab: SH131, BBK536 6:00-7:30 (from ) [each student must have obtained.
SOFTWARE AND PROGRAMMING 1
CSC 222: Object-Oriented Programming Fall 2015
Class definitions CITS1001 week 2.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
More Sophisticated Behavior
Chapter 4 The If…Then Statement
Understanding class definitions
public class BankAccount{
Java Programming with BlueJ
Understanding class definitions
An Introduction to Java – Part II
Lecture Notes – Week 3 Lecture-2
COS 260 DAY 3 Tony Gauvin.
Building Java Programs
2 The anatomy of class definitions
Understanding class definitions
F II 3. Classes and Objects Objectives
COS 260 DAY 4 Tony Gauvin.
Building Java Programs
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Coming up Variables Quick look at scope Primitives Objects
Building Java Programs
Presentation transcript:

Looking inside classes Conditional Statements Week 4

Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors initialize objects. Methods implement the behavior of objects. Fields persist (remain) for the lifetime of an object. Parameters are used to receive values into a constructor or method. Java Class Definitions WHAT WE LOOKED AT LAST WEEK

Conditional Statements Local Variables Java Class Definitions CONCEPTS COVERED THIS WEEK

Java Class Definitions REFLECTING ON OUR TICKET MACHINES Their behavior is inadequate in several ways: –No checks on the amounts entered. –No change given. –No checks for a sensible initialization. How can we do better? –We need more sophisticated behavior.

Java Class Definitions MAKING CHOICES public void insertMoney(int amount) { if(amount > 0) { balance = balance + amount; } else { System.out.println("Use a positive amount: " + amount); }

if(perform some test) { Do the statements here if the test gave a true result } else { Do the statements here if the test gave a false result } Java Class Definitions MAKING CHOICES if’ keyword boolean condition to be tested - gives a true or false result actions if condition is true actions if condition is false else keyword

Making decisions in Java This is the code to test whether a student has passed or failed a module, assuming 40% is the pass mark: if ( studentMark >= 40 ) { result = "pass"; } else { result = "fail"; } // end if

Improved printTicket() Uses if else to check if there are sufficient funds to buy a ticket

EQUALITY AND RELATIONAL OPERATORS ExampleMeaning balance == price balance equal to price balance != price balance not equal to price balance > price balance greater than price balance < price balance less than price balance >= price balance greater or equal to price balance <= price balance less or equal to price

Examples int janet = 44, john = 12; janet == john john != 12 john <= 12 7 < john janet > john janet >= 44

Logical Operators Logical operator Java Logical operator cond-1 AND cond-2 && cond-1 OR cond-2 || cond-1 EXCLUSIVE OR cond-2 ^ NOT cond-1 ! e.g. if (gender == 1 && age >= 65) Note the use of the round brackets which MUST surround the whole condition. Extra sets of brackets CAN be used for clarity if desired.

Java Class Definitions LOCAL VARIABLES public int refundBalance() { return balance; balance = 0; } Why won’t this method compile? unreachable statement!

Java Class Definitions LOCAL VARIABLES Fields are only one sort of variable. –They store values through the life of an object. –They are accessible by all methods throughout the class. Methods can include other (shorter-lived) variables. –They exist only as long as the method is being executed. –They are only accessible from within the specific method.

Java Class Definitions LOCAL VARIABLES public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } A local variable No visibility modifier

Required Reading Objects First With Java – A Practical Introduction using BlueJ Reading for this week’s lecture Chapter 2