Java Third Experiment

Source: Internet
Author: User

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();    }}

Cannot be compiled, super statements should be placed in the first sentence.

As with this call to the constructor method, the super statement must be placed in the first row of the subclass construction method and cannot be reversed. Regardless of how the subclass operates, you will eventually have to call the constructor method in the parent class first. The Super statement directly accesses the properties in the parent class and accesses the parent class's methods directly.

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();    }}

(1) animal.sleep () error. Sleep is a method in a subclass that declares an object of the parent class, and the object of the parent class cannot call the Slepp method in the subclass directly.

(2) Dog dog = animal; error, downward transformation, direct definition is not correct, because there is no definition of who is the parent class, who is the child class, the parent class can have more than one subclass, should let the parent class clear which is the subclass to be transformed.
(3) Animal animal2 = new Animal ();
Dog = (dog) animal2; error, cast, declared object requires the parent class to point to the child class.
Modify:
1. ((Dog) animal). Sleep (); cast type, change to Dog object.
2. Change into dog dog = (dog) animal;
3. Modify to Animal Animal2 = new Dog ()

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]
~~~
The ToString () method is not written, but it can be called, but it is called the system default ToString, which is not related to the data.

(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 String toString () {
Return GetClass (). GetName () + "@" + integer.tohexstring (Hashcode ());
}
~~~
(3) Add the following method to the person class
~~~

Public String toString () {
Return "Name:" + THIS.name + ", Age:" + this.age;
}
~~~
All classes are subclasses of the object class, and when the class writes the ToString method, the method in the class is called, and if it is not, the default ToString method of the object class is called.

4. Car rental companies, taxi types have passenger cars, trucks and pickup three kinds, each car in addition to have a number, name, rent three basic attributes, passenger cars have capacity, goods vehicles have cargo capacity, pickup is both carrying capacity and cargo capacity. This paper analyzes the above problems with object-oriented programming, and represents them as appropriate classes, abstract classes or interfaces, and illustrates the design ideas. Now to create a list of rental cars, how should I create them?

(1) Define an abstract class with a number, name, and rent of three attributes;
(2) Define two interfaces: cargo capacity, capacity of passengers;
(3) Define three subclass inherits abstract class, bus class realizes carrying capacity interface, train class realizes cargo capacity interface, pickup class realizes carrying capacity and cargo capacity interface.

(ii) Experimental summary

1. Banks

Program Design ideas: To create a bank class and a banking class in the bank class to implement the banking class-based approach

In the 1.java while loop, the ture is different from C in the loop and cannot write "1".
2. Employees

Program Design ideas: Define an employee class, define a management class, define employee classes to inherit employees, define test classes to make calls and tests of various types.
Problems and Solutions:
The inheritance relationship is still a little blurry, declaring an object with the parent class, and cannot, directly in the test class, invoke the method of the child class with the object.
Double quotation marks are the words that pass through the string content in double quotes.

3. Calculation of planar graphs and stereoscopic graphs

The program Design idea: first defines two abstract classes, respectively defines the plane graph abstract class and the stereoscopic graph abstract class.
A method for calculating perimeter and area in a planar graph abstract class
A method for calculating surface area and volume in the abstract class of stereoscopic graphics
Then the corresponding classes are created for each graph and stereoscopic graph, and the data is calculated in the class.
Create a test class that is given the individual values of the selected graph in the test class, and answer according to the random data.

https://gitee.com/hebau_java_cs16/Java_CS01WangTianLun/tree/abb56e23b55edd48f3d6796d228b6725b01c7ff6/

Java Third Experiment

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.