More on Classes and Objects

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

Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Looking inside classes Fields, Constructors & Methods Week 3.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Written by: Dr. JJ Shepherd
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Writing Classes (Chapter 4)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
CSCI 3327 Visual Basic Chapter 9: Object-Oriented Programming: Classes and Objects UTPA – Fall 2011.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Written by: Dr. JJ Shepherd
Java Methods 11/10/2015. Learning Objectives  Be able to read a program that uses methods.  Be able to write a write a program that uses methods.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
CSC111 Quick Revision.
Building Java Programs
Yanal Alahmad Java Workshop Yanal Alahmad
University of Central Florida COP 3330 Object Oriented Programming
Java Methods Making Subprograms.
Control Statement Examples
CS139 – Fall 2010 How far we have come
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Enter your code from FRQ to Shell
You can work in groups on this program.
Defining Your Own Classes Part 1
Introduction to Classes and Methods
Building Java Programs
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Java Methods Making Subprograms.
Building Java Programs
Chapter 5: Classes and Objects in Depth
Java Methods Making Subprograms.
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Building Java Programs
JAVA CLASSES.
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Object Oriented Programming in java
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Building Java Programs
Java 1/31/2017 Back to Objects.
Dr. R Z Khan Handout-3 Classes
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Review for Midterm 3.
Presentation transcript:

More on Classes and Objects 2-15-2018 Review Dry running a program that uses objects and a client Finish up the Car and Fraction Classes

Dry run the above code using the following values for input: 25, 123 import java.util.Scanner; public class AccountTest { public static void main( String args[] ) Account account1 = new Account( 50 ); Account account2 = new Account( -7 ); System.out.print(account1.getBalance() ); System.out.println(account2.getBalance() ); Scanner input = new Scanner( System.in ); System.out.print( "Enter deposit amount for account1: " ); int depositAmount = input.nextInt(); account1.credit( depositAmount ); System.out.print( "Enter deposit amount for account2: " ); depositAmount = input.nextDouble(); account2.credit( depositAmount ); } // end main } // end class AccountTest Dry run the above code using the following values for input: 25, 123 public class Account { private double balance=0; public Account( double initialBalance ) if ( initialBalance > 0.0 ) balance = initialBalance; } public void credit( double amount ) balance = balance + amount; public double getBalance() return balance;

Review: Are you allowed to define a class with no constructors? Can a class have two different no-args constructors? What is a good style for naming constructors? What is the common Java style for method names? Yes. Then Java provides one default no-args constructor No. Constructors must have different numbers or types of parameters There is no choice. The names of all of the constructors must match the name of the class. Are you allowed to define a class with no constructors? Yes. Then Java provides one default no-args constructor. Can a class have two different no-args constructors? No. Constructors must have different numbers or types of parameters. What is a good style for naming constructors? There is no choice: the names of all constructors are the same as the name of the class. What is the common Java style for method names? Start with a lowercase letter, with subsequent words capitalized. Methods should sound like verbs. Start with a lowercase letter, with subsequent words capitalized. Methods should sound like verbs.

Review (cont’d): Are parameters of primitive data types passed to methods “by value” or “by reference”? Can a method have more than one return statement? Can a method have no return statements? Can a method create a new object and return it to the calling method? By value. Yes, but all except one must be inside an if or else (or a switch). Are parameters of primitive data types passed to methods “by value” or “by reference”? By value. Can a method have more than one return statement? Yes, but all except one must be inside an if or else (or a switch). Can a method have no return statements? Yes, a void method doesn’t have to have a return. Can a method create a new object and return it to the calling method? Yes. Yes, a void method doesn’t have to have a return. Yes

Review (cont’d): When is it appropriate to define overloaded methods? Describe the difference between static and instance variables. What is the syntax for referring to a public static field outside the class? When two methods perform similar tasks for different types or numbers of parameters. A class variable (declared static) is shared by all objects of the class; an instance variable may have different values in different objects. When is it appropriate to define overloaded methods? When two methods perform similar tasks for different types or numbers of parameters. Describe the difference between class and instance variables. A class variable (declared static) is shared by all objects of the class; an instance variable may have different values in different objects. What is the syntax for referring to a public static field outside the class? ClassName.fieldName ClassName.fieldName

Review (cont’d): Can non-static methods access static fields? Can static methods access instance variables? Can static methods have local variables? Yes. No. Static methods are not associated with any particular object, so how would they know which object’s instance variables to access? Can non-static methods access static fields? Yes. Can static methods access instance variables? No. Static methods are not associated with any particular object, so how would they know which object’s instance variables to access? Can static methods have local variables? Sure, why not? Yes

Parking that Car Instance Variables Constructors Methods Model, numberOfPassengers, amountOfGas Constructors Default, given the above information Methods addPassenger removePassenger fillTank outOfGas toString Accessors and Modifiers (Mutators) for all instance variables amountOfGasPerPassenger

Code Break: Complete the Fraction Class Constructors Default given numerator and denominator Given a numerator Given another Fraction object (Will make a duplicate Fraction) Multiply (overloaded) Multiply by a Fraction object Multiply by an integer Multiply given a numerator and denominator toString() Divide By a Fraction Object By an integer add Pushes subtract Reduce