Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

RE: Authors Code

 
Greenhorn
Posts: 2
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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. }


 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Randy.

Can you avoid posting the same question more than once, please? This is a duplicate of https://coderanch.com/t/589984/java/java/Author-Code, and that post already has some replies. So you should continue the discussion there.

Thanks.
 
And then the flying monkeys attacked. My only defense was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic