Posted By

AustinGoods on 10/12/18


Tagged


Versions (?)

Objects Example Continued Pt 2


 / Published in: Java
 

Etc. again

  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("Comparing and contrasting a Honda Accord and a Dodge Challenger Hellcat\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 Accords are much more reliable and last longer in the long term, and are produced for often families and day-to-day commutes");
  22. System.out.println("\n\nHonda Accord holds " + hondaAccord.seats + " people");
  23. System.out.println();
  24. System.out.println("Honda Accord has Horse Power of " + hondaAccord.horsepower);
  25. System.out.println();
  26. System.out.println("Honda Accord has a top speed of " + hondaAccord.topSpeed + " mph");
  27.  
  28. hellcat.horsepower = 707;
  29. hellcat.topSpeed = 199;
  30. hellcat.MilesPerGallon = 13;
  31. hellcat.modelYear = 2016;
  32. hellcat.seats = 2;
  33.  
  34. System.out.println("\n\n\nDodge Hellcats are known as one of the most aggresive looking and sounding cars on the general market. While not specifically made for daily driving, many owners use these as daily drivers.");
  35.  
  36. System.out.println("\n\n\n\nDodge Hellcat has horse power of " + hellcat.horsepower);
  37. System.out.println();
  38. System.out.println("Dodge Hellcat has a top speed of " + hellcat.topSpeed + " mph");
  39. System.out.println();
  40. System.out.println("Dodge Hellcat's model year is " + hellcat.modelYear);
  41. System.out.println();
  42. System.out.println("A Dodge Hellcat can hold up to " + hellcat.seats + " people");
  43. }
  44. }
  45.  
  46. //Class has to be associated with an object - create class defintion, then object up top
  47.  
  48. class Vehicle { //Define new variables in new class outside of main method
  49.  
  50. int horsepower; /* Template, so no numbers assigned. int used instead of double
  51.   because values are usually described as whole numbers*/
  52.  
  53. int topSpeed; //Miles per hour
  54. double MilesPerGallon;
  55. int modelYear;
  56. int seats;
  57. }

Report this snippet  

You need to login to post a comment.