/    Sign up×
Community /Pin to ProfileBookmark

Dependent Class in same file??

Hello all,

I am learning Java and I have run into a bit of a snag. I am trying to get a dependent main class to call functions from other classes in the same java file. The file is compiling with no error but I cant execute the file? I keep getting the error message: Exception in thread “main” java.lang.NoSuchMethodError: main.
??
I am very new to Java so I am not really sure what I am doing wrong. Here is the code that I have written. Just not sure what syntax I may be missing. I have been looking at this for 2 days and my head hurt, lol.

If anyone can give this a quick look and mayhaps point me in the right direction I will be very greatful! ?

Thanx

[QUOTE]

/* Car.java */

class Car
{
String make;
String model;
int year;
double invoice;
double price;

void showCar() {
System.out.println(“Make => ” + make);
System.out.println(“Model => ” + model);
System.out.println(“Year => ” + year);
}

int mpg;

int milesPerGallon(int miles, int gallons)
{
mpg = miles / gallons;
return mpg;
}

void setYear(int date){
year = date;
}
void setColor(String newColor){
color = newColor;
}

/* 1. Declare a field of the Car class named ‘color’ of type String.
(1 Point) */

String color;

/* 2. Define a method of the Car class named ‘getProfit’. The method should
return the difference of the price and invoice fields as a double.
(1 Point) */

double getProfit(double price, double invoice){
double newPrice;
newPrice = price – invoice;
return newPrice;
}

/* 3. Define a constructor method of the Car class that will initialize the
fields of the class as follows: (1 Point) *

make = “Ford”
model = “Escort”
year = 1997
invoice = 10500
price = 14500
color = “blue” */

Car(){
make = “Ford”;
model = “Escort”;
year = 1997;
invoice = 10500;
price = 14500;
color = “blue”;
}

/* 4. Define a constructor for the Car class that will initialize all the
fields of the Car from values that would be passed in as input
parameters. (1 Point) */

Car(String makeCar,String modelCar,int yearCar, double invoiceCar, double priceCar, String colorCar){
make = makeCar;
model = modelCar;
year = yearCar;
invoice = invoiceCar;
price = priceCar;
color = colorCar;
}

} // class Car

class CarDemo
{
public static void main(String Args[])
{

/* 5. Create a object of the Car class named ‘car02’ from the constructor
defined in problem #3. (1 Point) */

Car car02 = new Car();

/* 6. Write the line of code to assign the value 2002 to the year field
of the‘car02’ object. (1 Point) */

car02.setYear(2002);

/* 7. Write the line of code to assign the value “red” to the color field
of the‘car02’ object. (1 Point) */

car02.setColor(“red”);

/* 8. Write the line of code to call the showCar method of the ‘car02’
object. (1 Point) */

car02.showCar();

/* 9. Write the line of code to call the milesPerGallon method of the
‘car02’ object. Pass in the value 360 for the miles parameter and
12 for the gallons parameter. Assign the result to a variable
called ‘mpg’ of type int. (1 Point) */

System.out.println(“Miles Per Gallon: ” + car02.milesPerGallon(360, 12));

/* 10. Declare a reference to the Car class named ‘car95’ and create a new
instance of the class by utilizing the constructor from problem #4.
The new instance should have a make of “Volkswagen”, a model of
“Jetta”, a year of 1995, an invoice of 14000, a price of 17500 and
a color of “black”. (1 Point) */

Car car95 = new Car(“Volkswagen”,”Jetta”,1995,14000,17500,”black”);

} // main

} // class CarDemo

[/QUOTE]

to post a comment
Java

1 Comments(s)

Copy linkTweet thisAlerts:
@forty2Aug 08.2007 — You need public class in this file.Java execute static main() method of the unique public class in file.

Make this class public:
<i>
</i>public class CarDemo

and save this file with name CarDemo.java

And here:
<i>
</i>int mpg;

int milesPerGallon(int miles, int gallons)
{
mpg = miles / gallons;
return mpg;
}

You need double or float, because now is wrong calculation.

Sorry for my english.
×

Success!

Help @Ronen1313 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...