Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Class;
- /* What Is a class?
- *
- * in the world, you'll often find many individual objects all of the
- * same kind. There may be thousands of other bicycle in existence,
- * all of the same make and model. Each bicycle was built from the
- * same set of blueprints and therefore contains the same components.
- * in object-oriented terms, we say that your bicycle is an instance of
- * the class of objects are created.
- *
- * The following Bicycle class is one possible implementation of a bicycle:
- */
- class Bicycle
- {
- int cadence = 10;
- int speed = 0;
- int gear = 1;
- void changeCadence(int newValue)
- {
- cadence = newValue;
- }
- void changeGear(int newValue)
- {
- gear = newValue;
- }
- void speedUp(int increment)
- {
- speed = speed + increment;
- }
- void applyBrakers(int decrement)
- {
- speed = speed - decrement;
- }
- void printState()
- {
- System.out.println("cadence : " + cadence + "\n"
- + "speed : " + speed + "\n"
- + "gear : " + gear + "\n");
- }
- }
- /*The syntax of the java programming language will look to you,
- * but the design of this class is based on the previous discussion of
- * bicycle objects. The fields cadence, speed, and gear represent
- * the object's state, and the methods (changeCadence,
- * changeGear, sppedUp etc.) define its interaction with the outside world.
- *
- * You many have notice that the Bicycle class does not contain a
- * main method. That's because it's not a complete application; it's just
- * the blueprint for bicycles that might be used in an application. The
- * responsibility of creating and using new Bicycle objects belongs to
- * some other class in your application.
- *
- * Here's BicycleDemo class that creates two separate Bicycle
- * objects and invokes their methods:
- */
- public class BycycleDemo
- {
- public static void main(String[] args)
- {
- // Create two different
- // Bicycle objects
- Bicycle bike1 = new Bicycle();
- Bicycle bike2 = new Bicycle();
- // The output of this test prints the ending pedal cadence, spped, and
- // gear for the two bicycles:
- // Invoke methods on
- // those objects
- bike1.changeCadence(50);
- bike1.speedUp(10);
- bike1.changeGear(2);
- bike1.printState();
- bike2.changeCadence(50);
- bike2.speedUp(20);
- bike2.changeGear(2);
- bike2.changeCadence(40);
- bike2.speedUp(10);
- bike2.changeGear(3);
- bike2.printState();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement