问题
I'm supposed to make a code in Java out of the following class diagram.
In the description of my assignment the following is stated; "One thing is completely missing from the class diagram, methods for updating the attributes. This is because the client of the system has decided that it is not allowed to update them. However, we know that this requirement will change, at least for age. The task therefore also includes designing and implementing a method for updating the age. When you do so, keep in mind that age can only increase, never decrease."
My question is therefore How do I make a method to update attributes of a class - in this case increase age? :)
2 other requirements down below (so you know why I included it in my code)... "The tail length of a dog can be calculated with the neat formula: tail length = ålder⋅vikt10svanslängd = ålder⋅vikt10 This formula applies to all dogs except taxis. One tax always has the tail length 3.7. Since kennel values are international, both the Swedish word "tax" and the English "dachshund" must be handled correctly by the system."
Here is my code:
public class Dog {
private String name;
private String breed;
private int age;
private int weight;
public String getName() {
return name;
}
public String getBreed() {
return breed;
}
public int getAge() {
return age;
}
public int getWeight() {
return weight;
}
public double getTailLength() {
double length;
// Undantag för taxar/dachshunds (alltid svanslängd 3,7).
if (breed.equalsIgnoreCase ("Tax") || breed.equalsIgnoreCase("Dachshund")) {
length = 3.7;
} else {
//Formel för svanslängd
length = (double)(age * weight) / 10;
}
return length;
}
public String toString() {
return String;
}
}
回答1:
You should create a set method
public void setAge(int newAge) {
age = newAge;
}
回答2:
You can add a method for updating the age
public void updateAge(int age) {
if (age <= this.age) { // use < instead of <= if the age can remain the same
throw new RuntimeException("Invalid age");
}
this.age = age;
}
It validates that the age passed is atleast the current age. If not, it throws an exception. You can throw any appropriate exception here.
If you don't want to throw an exception, you can have the method return a boolean to reflect if the update succeeded or not.
public boolean updateAge(int age) {
if (age <= this.age) {
return false;
}
this.age = age;
return true;
}
回答3:
Methods to update attributes are usually called Setters. In Java you usually have a get/set method for each attribute. Therefore what you're looking to do is to create a setter for the age. This is very basic Java and I'd strongly recommend you search / read up on Getters and Setters instead of someone telling you exactly what to do (i.e writing the code for you) . It will help you a lot to understand and learn Java.
I just googled it but here's an example of a tutorial that might help you: https://www.codejava.net/coding/java-getter-and-setter-tutorial-from-basics-to-best-practices
回答4:
Following the conventions, you should have a setter
method to modify the value (as a getter
return the value), The basic one is :
public void setAge(int age) {
this.age = age;
}
You could add validation, like for ex if you want to be able to only increase the age
public void setAge(int age) { if(age > this.age) this.age = age; }
Or create a
oneMoreYear()
method to increase by onepublic void oneMoreYear() { age++; }
回答5:
This depends on the type of updating.
Allow to set any Age:
public void setAge(int newAge) {
if (newAge > age) {
this.age = newAge;
}
}
Allow to only increase by one year with each call:
public void increaseAge() {
this.age++;
}
回答6:
you can create a method, the method you can call a other name, like 'addAge', in this method, you do addAge, you can in this method to do service
回答7:
i think like age, the attribute cannot be change, but you must create setAge and in setAge, you do nothing or show wranning, create other methor to increase age
回答8:
Just keep the getters and setters as is - follow the java bean spec in this regard. You are trying to do more than setting the value - for eg in the case of setter for age attribute. In this case, it will be better to have another method ,which increments the age according to the logic that you want to put in. The choice to have a new method , will mandate the need to have proper Javadoc, as always.
来源:https://stackoverflow.com/questions/54124934/how-to-make-a-method-to-update-attributes-of-a-class