Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java 1/31/2017 Back to Objects.

Similar presentations


Presentation on theme: "Java 1/31/2017 Back to Objects."— Presentation transcript:

1 Java 1/31/2017 Back to Objects

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

3 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?

4 // 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( ); Account account2 = new Account( ); 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,

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

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

7 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.

8 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.

9 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);

10 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());

11 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); }

12 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.

13 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


Download ppt "Java 1/31/2017 Back to Objects."

Similar presentations


Ads by Google