Posted By

AustinGoods on 10/12/18


Tagged


Versions (?)

Example cont. Pt 3


 / Published in: Java
 

........

  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("The following program will compare and contrast a few small differeneces between 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.  
  14. //Object name, still asssigned the same variable
  15.  
  16. //Honda Accord inputs - Just assigns information to its respective object
  17.  
  18. double hondaAccordEndurance; //Total time that can be driven until out of gas
  19.  
  20. hondaAccord.horsepower = 150;
  21. hondaAccord.topSpeed = 120;
  22. hondaAccord.milesPerGallon = 28.5;
  23. hondaAccord.modelYear = 2015;
  24. hondaAccord.seats = 5;
  25. hondaAccord.totalDistance = 400;
  26.  
  27. hondaAccordEndurance = hondaAccord.totalDistance / hondaAccord.milesPerGallon;
  28.  
  29. System.out.println("\n\nHonda Accords are much more reliable and last longer in the long term, and are produced for often families and day-to-day commutes");
  30. System.out.println("\n\nA Honda Accord holds around " + hondaAccord.seats + " people");
  31. System.out.println("\nA It has Horse Power of " + hondaAccord.horsepower);
  32. System.out.println("\nThe Honda Accord also has a top speed of " + hondaAccord.topSpeed + " mph");
  33. System.out.println("\n\nAnd a Honda Accord can also drive in a straight line for " + hondaAccordEndurance + " hours\n"); //Just calculates the information and prints the results
  34.  
  35. //Hellcat inputs - Assigns information
  36.  
  37. double hellcatEndurance;
  38.  
  39. hellcat.horsepower = 707;
  40. hellcat.topSpeed = 199;
  41. hellcat.milesPerGallon = 13;
  42. hellcat.modelYear = 2016;
  43. hellcat.seats = 2;
  44. hellcat.totalDistance = 175;
  45.  
  46. hellcatEndurance = hellcat.totalDistance / hellcat.milesPerGallon;
  47.  
  48. //The following area
  49.  
  50. 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.\n");
  51.  
  52. //Just calculates the information and prints the results
  53. System.out.println("The Dodge Challenger Hellcat can drive in a completely straight line for about " + hellcatEndurance + " hours");
  54.  
  55. System.out.println("\nA Hellcat has horse power of " + hellcat.horsepower);
  56. System.out.println("\nIt has a top speed of " + hellcat.topSpeed + " mph");
  57. System.out.println("\nThe Dodge Hellcat's model year is " + hellcat.modelYear);
  58. System.out.println("\nAnd a Dodge Hellcat holds up to " + hellcat.seats + " people");
  59. }
  60. }
  61.  
  62. //Class has to be associated with an object - create class defintion, then object up top
  63.  
  64. class Vehicle { //Define new variables in new class outside of main method
  65.  
  66. int horsepower; /* Template, so no numbers assigned. int used instead of double
  67.   because values are usually described as whole numbers*/
  68.  
  69. int topSpeed; //Miles per hour
  70. double milesPerGallon;
  71. int modelYear;
  72. int seats;
  73. int totalDistance; //Distance achievable by driving on a full tank
  74. }

Report this snippet  

You need to login to post a comment.