Posted By

AustinGoods on 10/12/18


Tagged


Versions (?)

Objects Example Continued


 / Published in: Java
 

Etc.

  1. public class Goods{
  2.  
  3. //Object goes in main method, program executiong begins here
  4.  
  5. public static void main(String []args){
  6. System.out.println("Class 1 by Jacolby Goods\n\n");
  7. System.out.println("Define a car\n\n");
  8.  
  9. Vehicle hondaAccord = new Vehicle(); //Creates object of class vehicle
  10. //hondaAccord now associated with horsepower,topSpeed, MPG and modelYear variables
  11.  
  12. Vehicle hellcat = new Vehicle();
  13. //Object name, still asssigned the same variable
  14.  
  15. hondaAccord.horsepower = 150;
  16. hondaAccord.topSpeed = 120;
  17. hondaAccord.MilesPerGallon = 28.5;
  18. hondaAccord.modelYear = 2015;
  19. hondaAccord.seats = 5;
  20.  
  21. System.out.println("Honda Accord holds " + hondaAccord.seats);
  22. System.out.println();
  23. System.out.println("Honda Accord has Horse Power of " + hondaAccord.horsepower);
  24. System.out.println();
  25. System.out.println("Honda Accord has a top speed of " + hondaAccord.topSpeed);
  26.  
  27. hellcat.horsepower = 707;
  28. hellcat.topSpeed = 199;
  29. hellcat.MilesPerGallon = 13;
  30. hellcat.modelYear = 2016;
  31. hellcat.seats = 2;
  32.  
  33. System.out.println();
  34. System.out.println("Dodge Hellcat has horse power of " + hellcat.horsepower);
  35. System.out.println();
  36. System.out.println("Dodge Hellcat has a top speed of " + hellcat.topSpeed);
  37. System.out.println();
  38. System.out.println("Dodge Hellcat's model year is " + hellcat.modelYear);
  39. System.out.println();
  40. System.out.println("Dodge Hellcat has a passenger total of " + hellcat.seats);
  41. }
  42. }
  43.  
  44. //Class has to be associated with an object - create class defintion, then object up top
  45.  
  46. class Vehicle { //Define new variables in new class outside of main method
  47.  
  48. int horsepower; /* Template, so no numbers assigned. int used instead of double
  49.   because values are usually described as whole numbers*/
  50.  
  51. int topSpeed; //Miles per hour
  52. double MilesPerGallon;
  53. int modelYear;
  54. int seats;
  55. }

Report this snippet  

You need to login to post a comment.