Java 1/31/2017 Back to 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.
Adding and Subtracting Fractions with Like Denominators.
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 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.
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Computer Programming Lab(4).
Computer Programming Lab(5).
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Adding and Subtracting Fractions Review
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)
Introduction to Object-Oriented Programming
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
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.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
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.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
Adding & Subtracting Whole Number and Fractions
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.
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.
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 - Intermediate
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Unified Modeling Language (UML)
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Common Denominators. Why do we need to know this? Comparing fractions >,
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.
CSC111 Quick Revision.
Yanal Alahmad Java Workshop Yanal Alahmad
Fractions: Adding and Subtracting Like Denominators
Implementing Classes Yonglei Tao.
Java Methods Making Subprograms.
Chapter Three - Implementing Classes
Java Enter your code from FRQ to Shell
You can work in groups on this program.
Adding and Subtracting Fractions
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Java Methods Making Subprograms.
CSE 1020:Programming by Delegation
Sorts on the AP Exam Insertion Sort.
Anatomy of a Method.
AP Java Review If else.
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.
AP Java Review If else.
Introduction to Object-Oriented Programming
Array Review Selection Sort
Random Numbers while loop
2.2 Adding Rationals Adding Fractions Adding Decimals 1. You need to have the same denominator when you add fractions If not, find the LCD 2. Multiply.
Adding and Subtracting Fractions
Presentation transcript:

Java 1/31/2017 Back to Objects

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=0; 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 What is returned? What is sent? Write the code for the constructors. We’ll go over it in class in a bit.

What Can Fractions Do? Multiply Who can use this ? (public or private) What is returned? (Return type); Do you want to modify the fraction object? Do you want to return a new Fraction object that is the result of the multiplication? How do you do this?(Code) Add a multiply method to the Fraction class. (Polymorphism) You can use the same method name as long as the parameters are different. Multiply by an integer Fraction1.multiply(5); Fraction answer = fraction1.mult(5); Multiply by a Fraction object fraction1.mult(fraction3); Fraction answer2 = fraction1.mult(fraction3);

Now add the following methods. toString() This method is part of every class since they are descendants of the Object class. 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());

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); System.out.println(fraction); //Automatically calls the toString method fraction=fraction.mult(fraction2); //or fraction.mult(fraction2); }

For today: Fraction Class Constructors Default constructor Constructor given a numerator and denominator Add the following methods to the fraction class. Multiply (integer, Fraction) toString() Divide (integer, Fraction) Add (Integer, Fraction) Pushes subtract() reduce() .equals() .compareTo() Test it using the Object Bench in BlueJ or by using a driver class.

Writing an Application for that uses the Fraction Class Write an application that will input an unknown number of Fractions using your Fraction class Output: The Total The average Pushes: Find the greatest and smallest fraction Turn in the Fraction class and the Application