first "oop" program

Reply

Join Date: Sep 2009
Posts: 69
Reputation: gibson.nathan is an unknown quantity at this point 
Solved Threads: 0
gibson.nathan gibson.nathan is offline Offline
Junior Poster in Training

first "oop" program

 
0
  #1
32 Days Ago
i am trying to make a program that takes an object oriented approach to calculating the miles per gallon that your car gets. this is early in the process an i am aware that alot of stuff is missing. i am just trying to get it to work enough that i can figure it out. i can only go by examples of oop design because my resources are limited, so if something doesnt make sense, sorry. anyway, here is my code:
  1. /**
  2.  * The purpose of this program is to demonstrate the use of a constructor
  3.  * that takes parameters. Notice that there is no problem with two constructors
  4.  * with the same name as long as their parameter lists are different. This is
  5.  * referred to as overloading a constructor,
  6.  *
  7.  * ©FLVS 2007
  8.  * @author B. Jordan
  9.  * @version 05/28/07
  10.  */
  11.  
  12. public class CarV5
  13. {
  14.  
  15.  
  16.  
  17. CarV5()
  18. {
  19. }
  20.  
  21. public int calcDistance(int distance, int endMiles, int startMiles)
  22. {
  23. distance = endMiles - startMiles;
  24. return distance;
  25. }
  26.  
  27. public double calcMPG(int mpg, int distance, int gallons)
  28. {
  29. mpg = distance/ gallons;
  30. return mpg;
  31.  
  32. }
  33.  
  34. //main method
  35. public static void main(String[] args)
  36. {
  37. int startMiles1 = 55;
  38. int endMiles1 = 250;
  39. int gallons1 = 15;
  40.  
  41. int distance1, mpg1;
  42.  
  43. distance1 = calcDistance(distance1,endMiles1,startMiles1);
  44. mpg1 = calcMPG(mpg1 ,distance1);
  45.  
  46. System.out.println(" Gas Mileage Calculations");
  47. System.out.println("Type of Car Start Miles End Miles Distance Gallons Miles/Gallons");
  48. System.out.println("==========================================================================");
  49. System.out.printf("Saturn %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f" + startMiles1 + endMiles1 + distance1 + gallons1 + mpg1);
  50. }
  51. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 61
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training
 
0
  #2
32 Days Ago
Do you have any specific questions?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 69
Reputation: gibson.nathan is an unknown quantity at this point 
Solved Threads: 0
gibson.nathan gibson.nathan is offline Offline
Junior Poster in Training
 
0
  #3
32 Days Ago
Originally Posted by chunalt787 View Post
Do you have any specific questions?
...i would assume so. yes, as you can probably tell, i cant figure out how to correctly call my methods. and if possible someone could not only help me get it to work so i can figure it out on my own, but also to help me understand exactly how this works as i havent been able to find any good insructional information on this particular topic.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 61
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training
 
0
  #4
32 Days Ago
Originally Posted by gibson.nathan View Post
i am trying to make a program that takes an object oriented approach to calculating the miles per gallon that your car gets. this is early in the process an i am aware that alot of stuff is missing. i am just trying to get it to work enough that i can figure it out. i can only go by examples of oop design because my resources are limited, so if something doesnt make sense, sorry. anyway, here is my code:
  1. /**
  2.  * The purpose of this program is to demonstrate the use of a constructor
  3.  * that takes parameters. Notice that there is no problem with two constructors
  4.  * with the same name as long as their parameter lists are different. This is
  5.  * referred to as overloading a constructor,
  6.  *
  7.  * ©FLVS 2007
  8.  * @author B. Jordan
  9.  * @version 05/28/07
  10.  */
  11.  
  12. public class CarV5
  13. {
  14.  
  15.  
  16.  
  17. CarV5()
  18. {
  19. }
  20.  
  21. public int calcDistance(int distance, int endMiles, int startMiles)
  22. {
  23. distance = endMiles - startMiles;
  24. return distance;
  25. }
  26.  
  27. public double calcMPG(int mpg, int distance, int gallons)
  28. {
  29. mpg = distance/ gallons;
  30. return mpg;
  31.  
  32. }
  33.  
  34. //main method
  35. public static void main(String[] args)
  36. {
  37. int startMiles1 = 55;
  38. int endMiles1 = 250;
  39. int gallons1 = 15;
  40.  
  41. int distance1, mpg1;
  42.  
  43. distance1 = calcDistance(distance1,endMiles1,startMiles1);
  44. mpg1 = calcMPG(mpg1 ,distance1);
  45.  
  46. System.out.println(" Gas Mileage Calculations");
  47. System.out.println("Type of Car Start Miles End Miles Distance Gallons Miles/Gallons");
  48. System.out.println("==========================================================================");
  49. System.out.printf("Saturn %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f" + startMiles1 + endMiles1 + distance1 + gallons1 + mpg1);
  50. }
  51. }
Java is purely object oriented meaning that member functions such as, calcDistance(...) and calcMPG(...) must be called on an object.

In your main you must construct an object of type CarV5 with the following syntax
  1. CarV5 car = new CarV5();
car is just an object name so it can be whatever you want. Similiar to when you say int myInt = 8. myInt can be changed to anything. The last set of parentheses are empty because in your constructor you haven't specified any paremeters.

Now that you have a car object you can call your functions on that object.
  1. distance1 = car.calcDistance(distance1,endMiles1,startMiles1);
  2. mpg1 = car.calcMPG(mpg1 ,distance1);
You are now saying go into the car object and perform the calcDistance function and put the result to the integer distance1 and something similiar for calcMPG.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 114
Reputation: KirkPatrick is an unknown quantity at this point 
Solved Threads: 3
KirkPatrick KirkPatrick is offline Offline
Junior Poster
 
0
  #5
32 Days Ago
Originally Posted by gibson.nathan View Post
but also to help me understand exactly how this works as i havent been able to find any good insructional information on this particular topic.
  1. public int calcDistance(int distance, int endMiles, int startMiles)
  2. {
  3. distance = endMiles - startMiles;
  4. return distance;
  5. }

the above will calculate the distance obviously. It has three parameters: distance, end miles, and start miles. Inside you have it calculate the distance automatically.


  1. public double calcMPG(int mpg, int distance, int gallons)
  2. {
  3. mpg = distance/ gallons;
  4. return mpg;
  5.  
  6. }

this could be explained almost exactly like the one above, but with different paramters

  1. //main method
  2. public static void main(String[] args)
  3. {
  4. int startMiles1 = 55;
  5. int endMiles1 = 250;
  6. int gallons1 = 15;
  7.  
  8. int distance1, mpg1;
  9.  
  10. distance1 = calcDistance(distance1,endMiles1,startMiles1);
  11. mpg1 = calcMPG(mpg1 ,distance1);
  12.  
  13. System.out.println(" Gas Mileage Calculations");
  14. System.out.println("Type of Car Start Miles End Miles Distance Gallons Miles/Gallons");
  15. System.out.println("==========================================================================");
  16. System.out.printf("Saturn %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f" + startMiles1 + endMiles1 + distance1 + gallons1 + mpg1);
  17. }
  18. }

This main method sets the start miles, end miles, and gallons with specific integers. Distance and mpg have no specific integers but will receive them once they are plugged in.


  1. distance1 = calcDistance(distance1, endMiles1, startMile1);

This is basically saying this: calcDistance(distance1, 250, 55);
Now remember calcDistance figures out the distance1 for you due to the calculation inside the method. So distance1 will now equal 195.


  1. mpg1 = calcMPG(mpg1, distance1);

This will be: calcMPG(mpg1, 195);
So plug that into the equation: mpg = 195/15 which is 13.


I'm not sure if that's what you were looking for, but hopefully it was. If you need it broken down more, state so.


*Edit*
Just noticed chunalt787 replied
Last edited by KirkPatrick; 32 Days Ago at 3:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 69
Reputation: gibson.nathan is an unknown quantity at this point 
Solved Threads: 0
gibson.nathan gibson.nathan is offline Offline
Junior Poster in Training
 
0
  #6
32 Days Ago
alright. quick question. this is pretty off topic to my last post. i have gotten most of that figured out and am now trying to advance my code to true oop. i have a question about my constructor.
  1. public class CarV5
  2. {
  3. String carType;
  4. int endMiles;
  5. int startMiles;
  6. double gallonsUsed;
  7. double pricePerGallon;
  8.  
  9. CarV5()
  10. {
  11.  
  12. }
  13.  
  14. CarV5 car1 = new CarV5(String carType1, double endMiles1, double startMiles1, double gallonsUsed1, double pricePerGallon1);
  15. {
  16. cT1 = carType1;
  17. eM1 = endMiles1;
  18. sM1 = startMiles11;
  19. g1 = gallonsUsed1;
  20. ppg1 = pricePerGallon1;
  21. }
it says that i need ")" what does that mean?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #7
32 Days Ago
  1. CarV5 car1 = new CarV5(String carType1, double endMiles1, double startMiles1, double gallonsUsed1, double pricePerGallon1);
  2. {
  3. cT1 = carType1;
  4. eM1 = endMiles1;
  5. sM1 = startMiles11;
  6. g1 = gallonsUsed1;
  7. ppg1 = pricePerGallon1;
  8. }

It's because the above code does not have valid syntax. Are you trying to create a method? If so, it has to be in a form such as:


  1. public void doSomething(String carType1, double endMiles1, double startMiles1, double gallonsUsed1, double pricePerGallon1)
  2. {
  3. cT1 = carType1;
  4. eM1 = endMiles1;
  5. sM1 = startMiles11;
  6. g1 = gallonsUsed1;
  7. ppg1 = pricePerGallon1;
  8. }


If you're trying to put method calls and use variables, etc, it has to be inside of a method.

  1. CarV5 car1 = new CarV5(String carType1, double endMiles1, double startMiles1, double gallonsUsed1, double pricePerGallon1);

^ The above is creating a new Object, but it is not inside of a method, it is being done inside of the class. You have to put it inside of a method, if you want it to be done when you 'run the class', then you'd put it inside of that class's main method.
Out.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC