bluej some questions about a simple car details class
Hallo ,
this is a class in bluej about car details i created
the problems so far in the class are that it allows any number in the transmition when i go to create the class , it should only allow 1 and 2(i dont really need to do it , but i am asking if there is a way around it ) and also in the toString method to show the manual and automatic and not 1 and 2 in the type
thanks for all the help
Code :
public class Car
{
// set up the two constants for the transmition
public static final int manual = 1;
public static final int automatic = 2;
// instance variables - replace the example below with your own
private static int engineSize;
private int fuel;
private final int type ;
/**
* Constructor for objects of class Car
*/
public Car(int newFuel , int newEngineSize , int Trans ) {
fuel = newFuel;
engineSize = newEngineSize;
type = Trans;
}
/**
Change the fuel
*/
public void getFuel()
{
System.out.println("The fuel of this car is " + fuel );
}
public void getEngineSize()
{
System.out.println("The engineSize of this car is " + engineSize);
}
public void setFuel( int newfuel)
{
fuel = newfuel;
}
public String getTypeAsString (){
if (type == automatic){
return "automatic";
}
else if (type == manual){
return "manual";
}
return "";
}
public String toString(){
return ("fuel " + fuel + " Engine Size " + engineSize + " Transmition " + type ) ;
}
}
Re: bluej some questions about a simple car details class
You can validate the data when it is received in the constructor and throw an exception if the data is invalid. Many java classes do that. See the Integer class for an example.
You would need to add a throws clause to the constructor's definition.
Re: bluej some questions about a simple car details class
thanks i will try
where can i find the integer class ?
Re: bluej some questions about a simple car details class
The doc for all the java SE classes:
Java Platform SE 6
Find the class in the lower left, click it and the doc will show in the main frame.
Re: bluej some questions about a simple car details class
excuse me im the beginner, i want to ask how entering source code like that, thank you
Re: bluej some questions about a simple car details class
Quote:
Originally Posted by
erdy_rezki
excuse me im the beginner, i want to ask how entering source code like that, thank you
When you post select the Go Advanced button, copy and paste your code and use the highlight tags, or select it and then click the "#" button above the text area. Here is the list of all the tags and how to use them.