问题
I'm trying to create a class in Java using BlueJ. My class is named Automobile. My goal is to be able to use my Constructor method to create cars with the variables: year, color, brand, number of doors, number of kilometers, if it's automatic (boolean), if it's sold (boolean), a description, and an identification number. All the variables have a set default value, a minimum and a maximum accepted value.
I have to use getVariablename and setVariablename for my methods. My color and brand variables are int, and I made methods to retrieve their String counterparts in a table in my class.
My issue is I don't understand the principle of setting my variable in one method and getting it in another (while making sure it's an accepted value). Also, once I have my Setter and Getter method, what do I have to write down in the creation of my Constructor method?
Up to now, I have this :
public class Automobile {
private static final String[] COLORS = { "Other", "Noir", "Blanc", "Bleu Nuit", "Bleu Clair", "Vert Pomme", "Vert Bouteille", "Taupe", "Argent", "Sable"};
private static final String[] BRANDS = { "Autre", "Mazda", "Toyota", "Ford", "GM", "Hyunday", "BMW", "SAAB", "Honda"};
public static final int COLOR_DEF = 8;
public static final int COLOR_MIN = 0;
public static final int COLOR_MAX = COULEURS.length - 1;
public static final int BRAND_DEF = 4;
public static final int BRAND_MIN = 0;
public static final int BRAND_MAX = MARQUES.length - 1;
public static final double KILO_DEFAULT = 55000;
public static final double KILO_MIN = 15000;
public static final double KILO_MAX = 140000;
public static final int TWO_DOORS = 2;
public static final int FOUR_DOORS = 4;
public static final int DOORS_DEFAULT = FOUR_DOORS;
public static final boolean AUTO_DEF = true;
public static final int YEAR_MIN = 1997;
public static final int YEAR_MAX = 2016;
public static final int YEAR_DEFAUT = 2007;
public static final String COMM_DEFAUT = "";
public static String color (int cou) {
String chainecolor = "";
if (cou >= COLOR_MIN && cou <= COLOR_MAX) {
chainecolor = COLORS[cou];
}
return chainecolor;
} //This method is to return the String value of a color from its int value using the COLORS table. If invalid it returns an empty chain.
public static String brand (int br) {
String chainebrand = "";
if (ma >= BRAND_MIN && ma <= BRAND_MAX) {
chainebrand = BRANDS[br];
}
return chainebrand;
} //same thing for the brand
public Automobile (int brand, int year, int color, boolean automatic, double kilometers,int nbrDoors, String description, boolean sold){
//To be completed
}
//here i'm supposed to create getters that return int values for everything but automatic, sold and description
public void setYear ( int year ) {
if (year >= YEAR_MIN && YEAR <= YEAR_MAX) {
year = year;
}
} // supposed to be the setter for my year, as long as it's within the accepted values
public void setMarque (int brand){
if (brand >= BRAND_MIN && brand <= BRAND_MAX) {
brand = brand;
}
} //same, for the brand
public void setColor (int color) {
if (color >= COLOR_MIN && color <= COLOR_MAX){
color = color;
}
}// same for the color
public void setNbrDoors (int p) {
if (p == TWO_DOORS || p == FOUR_DOORS){
p = p;
}
} // same for the door. I am forced to use (int p) as the variable for this method, which confuses me as to how I will refer to it from nbrDoors up in the Automobile constructor method
} // Automobile
So my difficulties lie in:
Are the examples of setters that I made valid for this purpose? I do not understand the need for p = p, or color = color...
How do I create a getter method that will be able to go pick up the Variable p from setNbrDoors, return its value and have it be used for nbrDoors in the Automobile constructor?
What am I supposed to write in the Constructor method, such as it will be able to get its values from the getters?
This is all because the second part is I will have to create a little code to ask the user to input all the values for variables, then create a table to stock the Cars the user creates.
P.S.: the work is originally in french, so I translated the variable and method names best I could for your better understanding. Also, the variable names, methods, etc are all imposed, I am FORCED to make the class this way exactly.
EDIT: As such, the use of static for brand and color conversion are also imposed. Those 2 methods are solely for returning a String of character from an int value. they are not used in the Constructor. Finally, the exceptions will be handled in the second part of the work using a separate validation loop. The Automobile class is really used solely to handle the creation of the "car" object.
回答1:
There are few issues with your code:
(1) You did not have any proper instance variables (like year
, brand
, etc..) for Automobile
(2) You did not use this.
to set the instance variables (because you did not create them)enter code here
. Just note that, this
always refers to the current object, refer here i.e., when you say this.year= year
, you are actually assigning the right hand side year
value to the current object's year
variable (left hand side).
You can refer the below code with comments:
public class Automobile {
private int year;
private int color;
private int brand;
//add other fields
public Automobile (int brand, int year, int color, boolean automatic, double kilometers,int nbrDoors, String description, boolean sold) {
if (year >= YEAR_MIN && year <= YEAR_MAX) {
this.year = year;
} else {
new IllegalArgumentException("Invalid Year Passed to construct Automobile");
}
//Similarly add other validations for brand, color, etc..
}
public void setYear ( int year ) {
if (year >= YEAR_MIN && YEAR <= YEAR_MAX) {
//USE 'this.' as shown below' to set the given year to 'this' object's year
this.year = year;
}
}
public int getYear() {
return year;
}
//Similarly add setters and getters for year, color, brand, etc...
}
回答2:
1-it's better to use this.p=p to attribute on your object.
2-setNbrDoors, return a void , you cant pick up a variable from it,you should create a getNbrDoors:int getNbrDoors() { return this.p; }
来源:https://stackoverflow.com/questions/40985875/getters-setters-and-constructors-in-java-making-a-car-and-stocking-it