130 likes | 309 Vues
Lab 4 - Object Programming. Java Programming Laboratory. Lab 4 - Object Programming. Prelab requirements Review class structure, constructors and methods Problem Description Example Assignment. Prelab Requirements. Read chapter on Java classes and object for book for course.
E N D
Lab 4 - Object Programming • Java Programming Laboratory
Lab 4 - Object Programming • Prelab requirements • Review class structure, constructors and methods • Problem Description • Example • Assignment
Prelab Requirements • Read chapter on Java classes and object for book for course. • Review the Java class syntax. • Study the class notes on constructors and methods.
Object-oriented Concepts • Classes - create a new type, a “picture” • Instances of Classes - Objects • Variables - object’s state • Methods - object’s behavior, API
Java Object Structure • public class MyClass { <---- class declaration • } private int i; <---- variablesprivate Object obj; public MyClass() { <---- constructors // constructor} public int aMethod(int i) { <---- methods // object method} public static int someMethod() { // class method}
Constructors • Object constructors are of the form:public MyClass(arguments) { // body of constructor } • Constructors have no return types. • Multiple constructors can be used if you have different arguments.
Class Methods • declared as a public static method • does not require an object instance to execute • called by using the class name and method name: className.classMethod(arg1,arg2) • behaves like a C function • Examples:public static void main(String[] args)Character.isLetter(‘j’)Float.parseFloat(“1.2E-10”)
Instance Variables & Methods • A class Instance is an object created by a constructor using the new keyword. • Object variables should always be declared as private. • Object methods can be public or private, but require an object instance to execute. • The API for an object is all the public methods. • Object methods are usually named using a verb or verb and object or verb adverb as in:getSize send isEqual toString compareTo
Problem Description • You will construct a simulation that will maintain several objects. The goal is to simulate a coffee bar. You will have several interactions with the coffee bar that will make this program interesting. Your assignment is to construct two objects CoffePot and CoffeBar to complete this lab.
Example • Opening Cafe ... • Please order a cup of coffee: • 1. Columbian Supreme (32 oz) • 2. Kona Medium Roast (42 oz) • 3. Guatemalan Dark (16 oz) • Which do you prefer? 2 • What size, small (8 oz) or large (16 oz)? large • Please order a cup of coffee: • 1. Columbian Supreme (32 oz) • 2. Kona Medium Roast (26 oz) • 3. Guatemalan Dark (16 oz) • Which do you prefer? 0 • Thank you for coming!
Object CoffeePot • CoffeePot( name, amount ) is a constructor that makes the coffee in the pot with the name and amount. For instance:CoffeePot pot1 = new CoffeePot(“Columbian Supreme”, 32);would make the first pot of coffee in the example above. • getCoffeeName() returns the coffee name for the current pot of coffee • getCoffeeAmount() returns the amount of coffee left in the coffee pot • pourCup( amount ) pours one cup of coffee of size amount from the coffee pot • brewPot( amount ) fills the coffee pot with the amount of coffee specified. Coffee pots can hold any amount. If there was coffee in the pot, then it is added to the amount specified.
Object CoffeeBar • CoffeeBar( CoffeePot1, CoffeePot2, CoffeePot3 ) is a constructor a CoffeeBar object with three pots of coffee to serve. • getMenu() returns a String that contains the menu for the CoffeeBar. • getSmallCup() returns the number of ounces (oz) in a small cup • getLargeCup() returns the number of ounces (oz) in a large cup • serveSmallCup( pot ) serves a small cup of coffee (8 oz) from the number given by the argument pot. If the pot does not contain at least 8 oz, then the cup will not be served. • serveLargeCup( pot ) serves a large cup of coffee (16 oz) from the number given by the argument pot. if the pot does not contain at least 16 oz, then the cup will not be served.
Cafe.java (partial) • CoffeePot columbian = new CoffeePot("Columbian Supremo", 32); CoffeePot konaroast = new CoffeePot("Kona Medium Roast", 42); CoffeePot houseblend = new CoffeePot("Special House Blend", 64); CoffeeBar bar = new CoffeeBar(columbian, konaroast, houseblend); • cons.putString("\nOpening Cafe ...\n"); • do { cons.putString("\nPlease order a cup of coffee:\n"); cons.putString(bar.getMenu());