You can work in groups on this program.

Slides:



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

1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Computer Programming Lab(4).
Computer Programming Lab(5).
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Writing Classes (Chapter 4)
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Introduction to Object-Oriented Programming
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
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.
Adding & Subtracting Whole Number and Fractions
Measurement Multiplying and Dividing Fractions.  We can add and subtract fractions with the same (common) denominator easily. Adding and Subtracting.
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 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
Programming Languages and Paradigms Activation Records in Java.
Measurement Adding and Subtracting Fractions with Different Denominators.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Fill In The Blank Multiplying Fractions 1. Fraction Form 2. Multiply Numerators Simplify.
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.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Rational Expressions – Equivalent Forms
CSC111 Quick Revision.
Fractions: Adding and Subtracting Like Denominators
Maha AlSaif Maryam AlQattan
Implementing Classes Yonglei Tao.
Java Methods Making Subprograms.
Chapter Three - Implementing Classes
Java Enter your code from FRQ to Shell
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Java Variables, Types, and Math Getting Started
Java Methods Making Subprograms.
CSE 1020:Programming by Delegation
Sorts on the AP Exam Insertion Sort.
Anatomy of a Method.
More on Classes and Objects
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall 2012 This set of slides is revised from lecture.
Fractions: Adding and Subtracting Like Denominators
Java Methods Making Subprograms.
JAVA CLASSES.
Making Equivalent Fractions.
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Introduction to Object-Oriented Programming
Java 1/31/2017 Back to Objects.
Array Review Selection Sort
Random Numbers while loop
Presentation transcript:

You can work in groups on this program. Java 12/10/2015 Objects

Demonstrate Chatbots Chatbot 2-3: Cat/Dog/Teacher/Blank/+ 3 Responses, + 3 Keywords Chatbot 4: I want…/ I like… Chatbot 5: Response Array / + 4 more random responses

Learning Objectives Be able to read a program that uses objects, classes and methods. Be able to develop and test a class from scratch.

public class Account { private double balance; public Account( double initialBalance ) if ( initialBalance > 0.0 ) balance = initialBalance; } public void credit( double amount ) balance = balance + amount; // add amount to balance public double getBalance() return balance; Find the … Class name Constructor Methods Instance Variable What does the constructor do?

// Fig. 3.14: AccountTest.java // Create and manipulate an Account object. import java.util.Scanner; public class AccountTest { public static void main( String args[] ) Account account1 = new Account( 50.00 ); Account account2 = new Account( -7.53 ); System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() ); System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() ); Scanner input = new Scanner( System.in ); double depositAmount; depositAmount = input.nextDouble(); System.out.printf( "\nadding %.2f to account1 balance\n\n", depositAmount ); account1.credit( depositAmount ); System.out.print( "Enter deposit amount for account1: " ); System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() ); System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() ); System.out.print( "Enter deposit amount for account2: " ); depositAmount = input.nextDouble(); System.out.printf( "\nadding %.2f to account2 balance\n\n", depositAmount ); account2.credit( depositAmount ); System.out.printf( "account2 balance: $%.2f\n", account2.getBalance() ); } // end main } // end class AccountTest Dry run the above code using the following values for input: 25.53, 123.45

Checking the Dry Run account1 balance: $50.00 account2 balance: $0.00 Enter deposit amount for account1: 25.53 Adding 25.53 to account1 balance account1 balance: $75.53 Enter deposit for account2: 123.45 Adding 123.45 to account2 balance account2 balance: $123.45

Making a Class from Scratch We will develop a class representing Fractions.

Going from idea to code: Fractions What attributes does a fraction have. Data, information, variables, … Numerator Denominator How should the fraction be created? Constructors Given a numerator and denominator A default value if nothing is given. What behaviors does a fraction have? Verbs, operations, functions, methods. Public +, -, *, / Private Reduce Find the greatest common factor to help reducing.

For starters Write the fraction class Create constructors Sample call statement Fraction fraction = new Fraction(25, 30); Fraction fraction2 = new Fraction(); Show fraction Multiply fractions Write the code for the constructors. We’ll go over it in class in a bit.

Now add the following methods. toString() Will return a String representation of the Fraction For example if the numerator = 5 and the denominator = 6 it will return “5/6” Sample call statement from the main body System.out.println(fractionObject.toString(); mult To multiply one fraction by another. Sample call statement. fractionObject1.mult(fractionObject2);

Sample FractionTest program //FractionTest.java //This program is to check the Fraction class public class FractionTest { public static void main(String args[] ) Fraction fraction = new Fraction(25, 30); System.out.println(fraction.toString()); Fraction fraction2 = new Fraction(3,4); fraction2=fraction.mult(fraction2); }

For today Add the following methods to the fraction class. Pushes toString() divide Add (Include creating common denominators) Pushes subtract Reduce Test it using the Object Bench in BlueJ or by using a driver class.