SetterGetter won't compile without errors?
This is a simple program that is supposed to represent a car with only two parameters; year (1970-2011), and price (0-100000). Can somebody fix this code?
First Class
Code:
Class Car
{
private int Year;
private double Price;
public void setYear(int Y)
{
if(Y<=2011 && Y>=1970)
{
Year = Y;
}
else
{
Year = 0;
}
}
public void getYear()
{
return Year;
}
public void setPrice(double P)
{
if(P <= 100000 && P >= 0)
{
Price = P;
}
else{
Price = 0;
{
}
public void getPrice()
{
return Price;
}
}
[B]Second Class[/B
Code:
import java.util.Scanner;
Class Attributes
{
public static void main(String[] args)
{
Scanner i = new Scanner(System.in);
Car x = new Car;
System.out.println("What is the year of the car?");
x.setPrice(i.nextInt());
System.out.println("What is the price of the car?");
x.setYear(i.nextDouble());
System.out.println("The Price of the car is: " + x.getPrice() + ", and the year of the car is: " + x.getYear())
}
}
Re: SetterGetter won't compile without errors?
Hi doreilly15, welcome to the forums!
I have added "code" tags to your post. The idea is you put [code] at the start of a section of code and [/code] at the end. That way the is properly formatted and more readable when it appears here.
Quote:
Can somebody fix this code?
I begin from the assumption that you can! You may need some help though. In particular, does that code compile? If not, and you can't understand the compiler's messages post them and someone is sure to explain what they mean and what you might do about them etc.
Re: SetterGetter won't compile without errors?
We're not here to fix your code for you, but having said that, we'll be more than happy to help you fix it. Please first tell us what errors you're seeing. Best to post the whole error message, and indicate with comments where in the code the errors are being generated.
Re: SetterGetter won't compile without errors?
Fixed it myself, thanks anyway.
Re: SetterGetter won't compile without errors?
Code:
public void getPrice() {
return Price;
}
your getters will return nothing, because it is void.