Everyone:
I am writing about formatting and repairing this code that the author has written. I would someone to rewrite the code so it is correct. I am so green that I do not quite know what I am doing yet, but I really want to learn
Java. I have not reached contructors in the book yet. I thinik the author uses this same kind of format throught out his text book. I trying to learn Java from "Java A Beginners Guide" Fifth Edition by Herber Schildt. It is his latest edition.
I am using the latest Eclipse and on line 3 Eclipse is saying "The type Vehicle is already defined".
On line 31 Eclipse is stating "Exception in
thread "main" java.lang.NoSuchFieldError: fuelcap
at AddMeth.main(AddMeth.java:31)"
I have tried to download the Java code so that someone can put it into their
IDE but it this website will not let medownload the java file with the .java extension or any other extension for that matter so I copied back into the letter. If there is a way I can attach the code for someone to put in their IDE I would like to know that too.
The Code is below
1. // Add range to Vehicle
2.
3. class Vehicle
4. {
5. int passengers; // number of passengers
6. int fuelcap; // fuel capacity in gallons
7. int mpg; // fuel consumption in miles per gallon
8.
9. // Display the range
10. void range()
11. {
12. System.out.println("Range is " + fuelcap * mpg);
13. }
14. }
15.
16. public class AddMeth
17. {
18.
19. /**
20. * @param args
21. */
22. public static void main(
String[] args)
23. {
24. Vehicle minivan = new Vehicle();
25. Vehicle sportscar = new Vehicle();
26.
27. // int range1, range2;
28.
29. // Assign values to fields in minivan
30. minivan.passengers = 7;
31. minivan.fuelcap = 16;
32. minivan.mpg = 21;
33.
34. // Assign values to fields in sportscar
35. sportscar.passengers = 2;
36. sportscar.fuelcap = 14;
37. sportscar.mpg = 12;
38.
39. System.out.print("Minivan can carry " + minivan.passengers + ". ");
40.
41. minivan.range(); // display range of minivan
42.
43. System.out.print("Sportscar can carry " + sportscar.passengers + ". ");
44.
45. sportscar.range(); // display range of sportscar
46. }
47.
48. }