Advertisement
Rahmadnet

Java Class Asigtment

Mar 5th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package Class;
  2.  
  3. /* What Is a class?
  4.  *
  5.  * in the world, you'll often find many individual objects all of the
  6.  * same kind. There may be thousands of other bicycle in existence,
  7.  * all of the same make and model. Each bicycle was built from the
  8.  * same set of blueprints and therefore contains the same components.
  9.  * in object-oriented terms, we say that your bicycle is an instance of
  10.  * the class of objects are created.
  11.  *
  12.  * The following Bicycle class is one possible implementation of a bicycle:
  13.  */
  14.  
  15.  
  16. class Bicycle
  17. {
  18.     int cadence = 10;
  19.     int speed = 0;
  20.     int gear = 1;
  21.    
  22.     void changeCadence(int newValue)
  23.     {
  24.         cadence = newValue;
  25.     }
  26.    
  27.     void changeGear(int newValue)
  28.     {
  29.         gear = newValue;
  30.     }
  31.    
  32.     void speedUp(int increment)
  33.     {
  34.         speed = speed + increment;
  35.     }
  36.    
  37.     void applyBrakers(int decrement)
  38.     {
  39.         speed = speed - decrement;
  40.     }
  41.    
  42.     void printState()
  43.     {
  44.         System.out.println("cadence : " + cadence + "\n"
  45.                             + "speed   : " + speed + "\n"
  46.                             + "gear    : " + gear + "\n");
  47.     }
  48. }
  49.  
  50. /*The syntax of the java programming language will look to you,
  51.  * but the design of this class is based on the previous discussion of
  52.  * bicycle objects. The fields cadence, speed, and gear represent
  53.  * the object's state, and the methods (changeCadence,
  54.  * changeGear, sppedUp etc.) define its interaction with the outside world.
  55.  *
  56.  * You many have notice that the Bicycle class does not contain a
  57.  * main method. That's because it's not a complete application; it's just
  58.  * the blueprint for bicycles that might be used in an application. The
  59.  * responsibility of creating and using new Bicycle objects belongs to
  60.  * some other class in your application.
  61.  *
  62.  * Here's BicycleDemo class that creates two separate Bicycle
  63.  * objects and invokes their methods:
  64.  */
  65.  
  66.  
  67. public class BycycleDemo
  68. {
  69.     public static void main(String[] args)
  70.     {
  71.         // Create two different
  72.         // Bicycle objects
  73.         Bicycle bike1 = new Bicycle();
  74.         Bicycle bike2 = new Bicycle();
  75.        
  76.        
  77. //      The output of this test prints the ending pedal cadence, spped, and
  78. //      gear for the two bicycles:
  79.        
  80.         // Invoke methods on
  81.         // those objects
  82.         bike1.changeCadence(50);
  83.         bike1.speedUp(10);
  84.         bike1.changeGear(2);
  85.         bike1.printState();
  86.        
  87.         bike2.changeCadence(50);
  88.         bike2.speedUp(20);
  89.         bike2.changeGear(2);
  90.         bike2.changeCadence(40);
  91.         bike2.speedUp(10);
  92.         bike2.changeGear(3);
  93.         bike2.printState();
  94.        
  95.        
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement