題目:Sum Calculator
Write a class with the name SimpleCalculator. The class needs two private fields (instance variables) with names firstNumber and secondNumber both of type double.
Write the following methods (instance methods):
*Method named getFirstNumber without any parameters, it needs to return the value of firstNumber field.
*Method named getSecondNumber without any parameters, it needs to return the value of secondNumber field.
*Method named setFirstNumber with one parameter of type double, it needs to set the value of the firstNumber field.
*Method named setSecondNumber with one parameter of type double, it needs to set the value of the secondNumberfield.
*Method named getAdditionResult without any parameters, it needs to return the result of adding the field values of firstNumber and secondNumber.
*Method named getSubtractionResult without any parameters, it needs to return the result of subtracting the field values of secondNumber from the firstNumber.
*Method named getMultiplicationResult without any parameters, it needs to return the result of multiplying the field values of firstNumber and secondNumber.
*Method named getDivisionResult without any parameters it needs to return the result of dividing the field values of firstNumber by the secondNumber. In case the value of secondNumber is 0 then return 0.
TEST CODE:
SimpleCalculator calculator = new SimpleCalculator();calculator.setFirstNumber(5.0);calculator.setSecondNumber(4);System.out.println("add= " + calculator.getAdditionResult());System.out.println("subtract= " + calculator.getSubtractionResult());calculator.setFirstNumber(5.25);calculator.setSecondNumber(0);System.out.println("multiply= " + calculator.getMultiplicationResult());System.out.println("divide= " + calculator.getDivisionResult());
OUTPUT:
add= 9.0subtract= 1.0multiply= 0.0divide= 0.0
TIPS:
*add= 9.0
is printed because 5.0 + 4 is 9.0
*subtract= 1.0
is printed because 5.0 - 4 is 1.0
*multiply= 0.0
is printed because 5.25 * 0 is 0.0
*divide= 0.0
is printed because secondNumber is set to 0
NOTE: All methods should be defined as public NOT public static.
NOTE: In total, you have to write 8 methods.
NOTE: Do not add the main method to the solution code.
題目:Person
Write a class with the name Person. The class needs three fields (instance variables) with the names firstName, lastName of type String and age of type int.
Write the following methods (instance methods):
*Method named getFirstName without any parameters, it needs to return the value of the firstName field.
*Method named getLastName without any parameters, it needs to return the value of the lastName field.
*Method named getAge without any parameters, it needs to return the value of the age field.
*Method named setFirstName with one parameter of type String, it needs to set the value of the firstName field.
*Method named setLastName with one parameter of type String, it needs to set the value of the lastName field.
*Method named setAge with one parameter of type int, it needs to set the value of the age field. If the parameter is less than 0 or greater than 100, it needs to set the age field value to 0.
*Method named isTeen without any parameters, it needs to return true if the value of the age field is greater than 12 and less than 20, otherwise, return false.
*Method named getFullName without any parameters, it needs to return the full name of the person.
*In case both firstName and lastName fields are empty, Strings return an empty String.
*In case lastName is an empty String, return firstName.
*In case firstName is an empty String, return lastName.
To check if s String is empty, use the method isEmpty from the String class. For example, firstName.isEmpty() returns true if the String is empty or in other words, when the String does not contain any characters.
TEST CODE:
Person person = new Person();person.setFirstName(""); // firstName is set to empty stringperson.setLastName(""); // lastName is set to empty stringperson.setAge(10);System.out.println("fullName= " + person.getFullName());System.out.println("teen= " + person.isTeen());person.setFirstName("John"); // firstName is set to Johnperson.setAge(18);System.out.println("fullName= " + person.getFullName());System.out.println("teen= " + person.isTeen());person.setLastName("Smith"); // lastName is set to SmithSystem.out.println("fullName= " + person.getFullName());
OUTPUT
fullName=teen= falsefullName= Johnteen= truefullName= John Smith
NOTE: All methods should be defined as public NOT public static.
NOTE: In total, you have to write 8 methods.
NOTE: Do not add the main method to the solution code.
參考答案:
Sum Calculator
public class Challenge_Sum_Calculator { private double firstNumber;
private double secondNumber; public double getFirstNumber() {
return this.firstNumber;
} public double getSecondNumber() {
return this.secondNumber;
} public void setFirstNumber(double firstNumber){
this.firstNumber = firstNumber;
} public void setSecondNumber(double secondNumber){
this.secondNumber = secondNumber;
} public double getAdditionResult(){
return firstNumber + secondNumber;
} public double getSubtractionResult(){
return firstNumber - secondNumber;
} public double getMultiplicationResult(){
return firstNumber * secondNumber;
} public double getDivisionResult(){
if(secondNumber==0){
return 0;
}
return firstNumber / secondNumber;
}
}
Person
public class Challenge_Person { private String firstName;
private String lastName;
private int age; public String getFirstName() {
return firstName;
} public void setFirstName(String firstName) {
this.firstName = firstName;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = (age > 0) && (age <= 100) ? age : 0;
} public boolean isTeen() {
return (age > 12) && (age < 20);
} public String getFullName() { if (firstName.isEmpty()) {
return getLastName();
} else if (lastName.isEmpty()) {
return getFirstName();
} else if (firstName.isEmpty() && lastName.isEmpty()) {
return "";
}
return getFirstName() + " " + getLastName(); }
}