//First
1 Public classcardemo{2 Public Static voidMain (string[] args) {3Car Carwind =NewCar ("Red", "West");4Carwind.speed = 60;5Carwind.color = "Red";6Carwind.direction = "West";7 8System.out.println ("The speed of car" +carwind.speed);9SYSTEM.OUT.PRINTLN ("The color of car is" +carwind.color);TenSystem.out.println ("The direction of car is" +carwind.direction); One } A } - classcar{ - //define four member variables (attributes) the intSpeed ; - String color; - String name; - String direction; +}
A second
Public classcardemo{ Public Static voidMain (string[] args) {Car carwind=NewCar ("Red", "West");//Create a reference to a car classCarwind.speed = 60; Carwind.color= "Red"; Carwind.direction= "West"; System.out.println ("The speed of car" +carwind.speed); System.out.println ("The color of car is" +Carwind.color); System.out.println ("The direction of car is" +carwind.direction); Carwind.drivecar (); Carwind.speedup (5); System.out.println ("The speed of car" +carwind.speed); }}classcar{//define four member variables (attributes) intSpeed ; String color; String name; String direction; Car (intSpeed , string color, string direction) { This. Speed =Speed ; This. color =color; This. Direction =direction; } Public voidDrivecar () {System.out.println ("Here we go!"); } Public voidSpeedupintaspeed) { This. Speed = speed +Aspeed; }}
Java classes and objects, using programs to interpret