| | |
first "oop" program
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 69
Reputation:
Solved Threads: 0
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:
Java Syntax (Toggle Plain Text)
/** * The purpose of this program is to demonstrate the use of a constructor * that takes parameters. Notice that there is no problem with two constructors * with the same name as long as their parameter lists are different. This is * referred to as overloading a constructor, * * ©FLVS 2007 * @author B. Jordan * @version 05/28/07 */ public class CarV5 { CarV5() { } public int calcDistance(int distance, int endMiles, int startMiles) { distance = endMiles - startMiles; return distance; } public double calcMPG(int mpg, int distance, int gallons) { mpg = distance/ gallons; return mpg; } //main method public static void main(String[] args) { int startMiles1 = 55; int endMiles1 = 250; int gallons1 = 15; int distance1, mpg1; distance1 = calcDistance(distance1,endMiles1,startMiles1); mpg1 = calcMPG(mpg1 ,distance1); System.out.println(" Gas Mileage Calculations"); System.out.println("Type of Car Start Miles End Miles Distance Gallons Miles/Gallons"); System.out.println("=========================================================================="); System.out.printf("Saturn %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f" + startMiles1 + endMiles1 + distance1 + gallons1 + mpg1); } }
•
•
Join Date: Sep 2009
Posts: 69
Reputation:
Solved Threads: 0
0
#3 Oct 27th, 2009
...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.
•
•
Join Date: Apr 2008
Posts: 61
Reputation:
Solved Threads: 1
0
#4 Oct 27th, 2009
•
•
•
•
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:
Java Syntax (Toggle Plain Text)
/** * The purpose of this program is to demonstrate the use of a constructor * that takes parameters. Notice that there is no problem with two constructors * with the same name as long as their parameter lists are different. This is * referred to as overloading a constructor, * * ©FLVS 2007 * @author B. Jordan * @version 05/28/07 */ public class CarV5 { CarV5() { } public int calcDistance(int distance, int endMiles, int startMiles) { distance = endMiles - startMiles; return distance; } public double calcMPG(int mpg, int distance, int gallons) { mpg = distance/ gallons; return mpg; } //main method public static void main(String[] args) { int startMiles1 = 55; int endMiles1 = 250; int gallons1 = 15; int distance1, mpg1; distance1 = calcDistance(distance1,endMiles1,startMiles1); mpg1 = calcMPG(mpg1 ,distance1); System.out.println(" Gas Mileage Calculations"); System.out.println("Type of Car Start Miles End Miles Distance Gallons Miles/Gallons"); System.out.println("=========================================================================="); System.out.printf("Saturn %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f" + startMiles1 + endMiles1 + distance1 + gallons1 + mpg1); } }
In your main you must construct an object of type CarV5 with the following syntax
Java Syntax (Toggle Plain Text)
CarV5 car = new CarV5();
Now that you have a car object you can call your functions on that object.
Java Syntax (Toggle Plain Text)
distance1 = car.calcDistance(distance1,endMiles1,startMiles1); mpg1 = car.calcMPG(mpg1 ,distance1);
Hope this helps.
•
•
Join Date: Apr 2009
Posts: 114
Reputation:
Solved Threads: 3
0
#5 Oct 27th, 2009
•
•
•
•
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.
java Syntax (Toggle Plain Text)
public int calcDistance(int distance, int endMiles, int startMiles) { distance = endMiles - startMiles; return distance; }
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.
java Syntax (Toggle Plain Text)
public double calcMPG(int mpg, int distance, int gallons) { mpg = distance/ gallons; return mpg; }
this could be explained almost exactly like the one above, but with different paramters
java Syntax (Toggle Plain Text)
//main method public static void main(String[] args) { int startMiles1 = 55; int endMiles1 = 250; int gallons1 = 15; int distance1, mpg1; distance1 = calcDistance(distance1,endMiles1,startMiles1); mpg1 = calcMPG(mpg1 ,distance1); System.out.println(" Gas Mileage Calculations"); System.out.println("Type of Car Start Miles End Miles Distance Gallons Miles/Gallons"); System.out.println("=========================================================================="); System.out.printf("Saturn %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f" + startMiles1 + endMiles1 + distance1 + gallons1 + mpg1); } }
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.
java Syntax (Toggle Plain Text)
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.
java Syntax (Toggle Plain Text)
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; Oct 27th, 2009 at 3:10 pm.
•
•
Join Date: Sep 2009
Posts: 69
Reputation:
Solved Threads: 0
0
#6 Oct 27th, 2009
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.
it says that i need ")" what does that mean?
Java Syntax (Toggle Plain Text)
public class CarV5 { String carType; int endMiles; int startMiles; double gallonsUsed; double pricePerGallon; CarV5() { } CarV5 car1 = new CarV5(String carType1, double endMiles1, double startMiles1, double gallonsUsed1, double pricePerGallon1); { cT1 = carType1; eM1 = endMiles1; sM1 = startMiles11; g1 = gallonsUsed1; ppg1 = pricePerGallon1; }
•
•
Join Date: Sep 2008
Posts: 1,595
Reputation:
Solved Threads: 200
0
#7 Oct 27th, 2009
Java Syntax (Toggle Plain Text)
CarV5 car1 = new CarV5(String carType1, double endMiles1, double startMiles1, double gallonsUsed1, double pricePerGallon1); { cT1 = carType1; eM1 = endMiles1; sM1 = startMiles11; g1 = gallonsUsed1; ppg1 = pricePerGallon1; }
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:
Java Syntax (Toggle Plain Text)
public void doSomething(String carType1, double endMiles1, double startMiles1, double gallonsUsed1, double pricePerGallon1) { cT1 = carType1; eM1 = endMiles1; sM1 = startMiles11; g1 = gallonsUsed1; ppg1 = pricePerGallon1; }
If you're trying to put method calls and use variables, etc, it has to be inside of a method.
Java Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Hidden program installs .dlls with randomly generated names in random "notify" reg. (Viruses, Spyware and other Nasties)
- google "keyword" question (Search Engine Optimization)
- Need Help. "Macro" program (C)
- Looking for an "Alert" Program (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: java help please?
- Next Thread: How to put userinput into a list Java
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number objects online oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time title tree tutorial-sample ubuntu update windows working






