Presentation is loading. Please wait.

Presentation is loading. Please wait.

You can work in groups on this program.

Similar presentations


Presentation on theme: "You can work in groups on this program."— Presentation transcript:

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

2 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

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

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

5 // 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,

6 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

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

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

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

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

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); fraction2=fraction.mult(fraction2); }

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


Download ppt "You can work in groups on this program."

Similar presentations


Ads by Google