Java textbook answer (non-official)

//page 45 exercises

Time is money // 24
8;b //25
abcd//26 
abc // 27
def  
abc\ndef    // 28  because the // is for / not for n 
HELLO TONY// 29
2 + 2 = 4  // 30 because in the calculation the mah should be the     first 
2 + 2 = 22

// exercises 31 
public class Person {

public static void main(String[] args) {
    int sam = increaseAge(10);
    System.out.println(sam);

}

public static int increaseAge(int inAge){
    return inAge;
}
}//31

/*
exercises 32:
the test does not change to the lower case when it print out, it should change to " System.out.println(test.toLowerCase());" or "test = test.toLowerCase();" so that after the print out, the test would all be lower case.
*/

**page 80 **

chapter 7 Inheritance
// page 427 -- 461

  • Inheritance: a very general form of a class can be defined and compiled;
  • Inheritance is the process by which a new class - known as a derived class - is created from another class, called based class;

Question: 430-464

  • why get hire date needs to have the new; date(hireDate); does it because it has to create a new object?why it should call date method;
    && create new variable

  • derived class(subclass/ inheritance/child class): a class defined by adding instance variables and methods to an existing class;

  • base class(superclass/parent class): the existing class that the derived class is built upon;

    super(); // if this line is omitted, Java will still invoke the no-argument constructor for the base class;

Question: 433-467
does the parameter from the hourly employee passed from the based class and need the super (variables from the base class as well);
&& yes

  • all the public and private instance variables, and all the public and private static variables from the base class
  • the only members not inherited are private methods "Overriding a Method definition";

Question: 438-472

  • if they are same class, we can direct call by getName(); (even the class is from the based class);
    &&yes

  • ancestor class: a class that is a parent of a parent of a parent of another class (or some other number of "parent of" iterations);

  • if class A is an ancestor of class B, then class B is often called descendent class of class A;

  • overriding: the meaning of the method in the derived class is different from the one in the base class

  • final: to definition of a method, it indicates that the method may not be redefined in a derived class;

  • may not change a void method to a method that return a value, nor a method that returns a value to a void method;

  • if the returned type is a class type, then you may change the returned type to that of any descent class of the returned type;

  • cannot change permission to make them more restricted in the derived class. can change private to public, but cannot change public to private;

  • when overriding a method definition, the new method definition given in the derived class has the exact same number number and types of parameters;

  • if the method in the derived class were to have a different number of parameters or a parameter of a different type from the method in the base class, then the derived class would have both methods;

  • super constructor: a constructor for a derived class begins with an invocation of a constructor for the base class;

Exercises: (self-test exercises 443-477)

  1. An object of the class Discount will also have instance variables named price and numberOfItems--> get variables.
  2. Yes, an object of the class DiscountSale have methods name getTotal and getTax. The methods have to perform the exact same actions in the class DiscountSale as in the class Sale if the method does not overriding by the method in the derived class.
  3. there are two methods named getTax will the class DiscountSale has and "@Overriding" should be at the heading
  4. because the name as the instance variables extends into the subclass by super so that we don't need to have the getname to get the variable;

Question: ????

  • when write the override with the method; when adding the parameter in the overriden method, there are error on it because of the different parameters
    && non

  • the call to the base class constructor(super) must always be the first action taken in a constructor definition;

  • this: as a method name to invoke a constructor in the same class;

  • "is a" relationship: for example, the subclass is a base class;

Self-Text Exercises: 448-482

  1. The following program is illegal the cannot use the type of employee directly, it should use the extend to extend the method to the base class;

Question:
example: an enhanced StringTokenizer Class

  • super.nextToken is the version of nextToken defined in the base class StringTokenizer ???
  • String sentence = keyword.nextLine();

Self-Text Exercises: 454-488

  1. it is legal
  2. it would then be legal to add the method crazyMethod to the class HourlyEmployee

chapter 16
chapter 17

你可能感兴趣的:(Java textbook answer (non-official))