"Java Technology" third time job

Source: Internet
Author: User

(a) Summary of activities

1. Read the procedure below to see if you can compile the pass? If not, explain why. How should I modify it? What is the result of running the program?
Why do I have to call the construction method of the parent class before the constructor of the subclass is run? Can you turn around?

class Grandparent {    public Grandparent() {        System.out.println("GrandParent Created.");    }    public Grandparent(String string) {        System.out.println("GrandParent Created.String:" + string);    }}class Parent extends Grandparent {    public Parent() {                System.out.println("Parent Created");        super("Hello.Grandparent.");    }}class Child extends Parent {    public Child() {        System.out.println("Child Created");    }}public class Test{    public static void main(String args[]) {        Child c = new Child();    }}

A: No, the parent constructor method is not placed in the first row.
Modified: Will Super ("Hello.grandparent.") On the first line of the parent constructor method.
Running result: Grandparent Created.String:Hello.Grandparent.
Parent Created
Child Created
The inheritance principle of the construction method:
1) subclasses inherit unconditionally the parent class's constructor without parameters.
2) The Kawai class does not define its own constructor method, it inherits the parent class parameterless construction method as its own constructor method.
3) The Kawai class defines its own construction method, which executes the parameterless construction method inherited from the parent class, and then executes its own constructor method.
4) The parent class is constructed with parameters, and the subclass can invoke it by using the Super keyword in defining its own construction method, but the calling statement must be the first executable statement of the subclass construction method.
5) The subclass construction method does not explicitly call the parent class construction method, and the parent class does not have a parameterless constructor method when the compilation error occurs.

2. Read the procedure below, analyze what errors exist in the program, explain why, and how to correct them? What is the result of running the correct program?

class Animal{  void shout(){      System.out.println("动物叫!");  }}class Dog extends Animal{      public void shout(){            System.out.println("汪汪......!");       }      public void sleep() {       System.out.println("狗狗睡觉......");      } }public class Test{    public static void main(String args[]) {        Animal animal = new Dog();         animal.shout();        animal.sleep();        Dog dog = animal;        dog.sleep();         Animal animal2 = new Animal();        dog = (Dog)animal2;        dog.shout();    }

Error: Animal.sleep ();
Dog dog = animal;
The first line of error is the result of a transition, only a subclass inherited or overwrite method is called, and there is no sleep method. To remove it
The second line of error is due to the need to add "(type)" to the next transformation, correct: Dog dog = (dog) animal;
Animal is a subclass of dog's upper transformation object, on which the transformed object cannot manipulate the newly added member variables of the subclass, and cannot use the new method of the subclass. The parent class object is defined as a subclass object called down transformation, and for a downward transformation, a forced transformation is required, that is, the subclass type to be transformed must be explicitly specified: format: Subclass Name Subclass Object = (subclass) Parent class instance;

3. Run the following procedure

class Person {    private String name ;    private int age ;    public Person(String name,int age){          this.name = name ;          this.age = age ;    } }public class Test{        public static void main(String args[]){              Person per = new Person("张三",20) ;              System.out.println(per);             System.out.println(per.toString()) ;   } }

(1) The operation result of the program is as follows, what is the problem?

[email protected][email protected]

Reason System.out.println (per); The ToString method of calling the parent class object by default
(2) So, what is the result of the program running? Use Eclipse to open the source code of the println (per) method and see which methods are called in the method, can you explain the results of this example?

public void println(Object x) { String s = String.valueOf(x); synchronized (this) { print(s); newLine(); }

}
ValueOf (x)//if the argument is null and then a string equal to "null";
Otherwise, the value of Obj.tostring () is returned.

Returns null if the argument is an empty string, otherwise returns the return value of ToString ().
ToString () returns a string that describes the current object, returning the exact content: the class name @ object's hash code hexadecimal representation.

(3) Add the following method to the person class

public String toString(){     return "姓名:" + this.name + ",年龄:" + this.age ;  

Rerun the program, what is the execution result of the program? What's the problem?
Results

姓名:张三,年龄:20姓名:张三,年龄:20

The ToString method is override, and the calling is the overwrite method, and the print class name also defaults to the Overwrite method.

4. Car rental companies, taxi types have bus, truck and pickup three kinds, each car in addition to have a number, name, rent three basic attributes,
The passenger car has the capacity, the lorry has the cargo capacity, the pickup truck also has the capacity and the cargo capacity. This paper analyzes the above problems with object-oriented programming thought,
It is represented as an appropriate class, abstract class, or interface to illustrate the design idea. Now to create a list of rental cars, how should I create them?

Solution: Define an abstract class (or interface) "Taxi", there is an abstract method carrying capacity, define bus class, train class, pickup class to inherit the taxi (or implement the interface), to implement the abstract method in the parent class,
The bus realizes abstract method, print carrying capacity, truck realizes abstract method, print cargo capacity, pickup implements abstract method, print carrying capacity and cargo capacity,
Define a car hire class, declare an array of taxi classes, initialize by constructing method parameters and on-transition.

5. Read the procedure below to analyze if the code can be compiled, if not, explain why, and make corrections. If you can, list the results of the operation

interface Animal{        void breathe();    void run();    void eat();}class Dog implements Animal{    public void breathe(){        System.out.println("I‘m breathing");    }    void eat(){        System.out.println("I‘m eating");    }}public class Test{    public static void main(String[] args){        Dog dog = new Dog();        dog.breathe();        dog.eat();    }}

No
In the declaration of a class, the Implements clause is used to denote a class using an interface, the constants defined in the interface can be used in a class, and all methods defined in the interface must be implemented.
When implementing a method defined by an interface in a class, you must explicitly use the public modifier or you will be warned by the system to reduce the access control scope of the methods defined in the interface.

(ii) Experimental summary
1. Bank new customer cash business processing
Design ideas:
? (1) Define Banking bank: Bank name Bankname (static variable), username name, password password, account balance balance, turnover turnover.
The following methods are included:
static method Welcome (): Print welcome words
Construction method: Realize new user's account opening. Includes user name, password, turnover. When opening an account, 10 yuan card fee is deducted.
Deposit Method Deposit (): Modify the account balance according to the deposit amount. Output related information.
Withdrawal method Withdrawal (): To verify the user's password, incorrect password or the amount of withdrawal is greater than the balance, can not transact business, and prompt users. Otherwise, modify the user balance.
static method Welcomenext (): Output Welcome to the next visit.
? (2) Define a user trade category, and simulate a scenario where a new user is in a bank for business.

2. Define the employee class, with name, age, and gender attributes, and have a construction method and display data method.
Design ideas:
Define management classes, inherit employee classes, have their own property titles and annual salary.
Define the staff class, inherit the employee class, and have their own attributes for the department and monthly salary.
? Define a test class to test.

Note that if you want to succeed, you must assign the set method after the data is entered, otherwise the pass-through function is not a parameter.

3. Complete the design of the class according to the following requirements
Design ideas:
? (1) Design a planar graphic abstract class (which provides methods for calculating the perimeter and area of the object) and a stereoscopic abstract class (provides a method for finding the surface area and volume of such objects)
? (2) design ball, cylinder class, Cone class, Rectangle class, Triangle class, Circle class, respectively inherit the plane graph abstract class and the stereoscopic graphics abstract class.
? (3) Design a test class, randomly generated ball, cylinder, cone, rectangle, triangle, circle size, mock exam, ask the user to answer their volume and surface area (or circumference and area), and determine whether the user's answer is correct.

(c) Code hosting (be sure to link to your project)

"Java Technology" third time job

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.