Java third time job

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 because the parent class construction method is not placed in the first row in the constructor method.
Correction method: Put super () in the first line.
Because you have a parent class instance before you can have a subclass instance, you cannot reverse it.

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 ("Animal called!");
}
}
Class Dog extends animal{
public void Shout () {
System.out.println ("bark ...");
}
public void Sleep () {
System.out.println ("Dog Sleeps ...");
}
}
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 One: the Animal.sleep ()
statement does not have a sleep method and should be removed.
Error Two: Dog dog = animal;
This is because there is no sub-class that indicates a downward transition, and the dog type should precede anmial.

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 ("Zhang San", 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, which can be called automatically by the system.
(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;
}
Rerun the program, what is the execution result of the program? What's the problem?

Result is
Name: Zhang San, age: 20
Name: Zhang San, age: 20
Programs can automatically call the ToString method in the object method.

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 car class, containing the number, name, rent
(2): Define an interface that includes carrying capacity, cargo capacity, passenger train connection capacity, truck connection cargo capacity, pickup connection capacity and cargo capacity, and all inherit abstract classes.
(3): Car categories are defined by their own array, including all cars, car rental for the number of the addition and subtraction.
(4): Define the car rental class, declare the taxi class array, through the construction method in the parameters as well as on the transformation of the initialization.

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 ();
}
}
~~~
cannot be compiled by
The implementation interface needs to rewrite all the methods in the interface, and the method within the interface cannot change its original permissions, and the Eat method changes its permissions.

Experiment Summary:
1, when implementing the interface, the rewrite interface must not change its original permissions, otherwise it will not be able to implement the compilation
2, downward transformation must indicate the sub-class of downward transformation
3, the ToString method can be called by the system default
4, must first have the parent class to have the subclass, this is irreversible

1. Banks

Design and design Bank class, including static method Weicome (), deposit Method Depoist (), method of construction with parameters of the parameterless, withdrawal method withdrawal (), and static method Welcomenext ()
Design trading Trade () to simulate the user-to-bank process.

2. Employees

Design an employee parent class, with construction methods and display data methods, and design management and staff inheriting employee classes.

3. Graphics calculation

Design a plane abstract class, a three-dimensional abstract class, triangle, Rectangle, Circle inheritance plane abstract class, cylinder, ball, cone inherit the three-dimensional abstract class, generate random numbers, a test class is used to determine whether the calculation is correct, and give the correct answer.

4. Feeding

Design an animal abstract class. Create lions, monkeys, pigeons classes inherit animals, a feeder class contains breeder names and feeding various animal methods, test classes are tested.

Https://gitee.com/hebau_java_cs16/Java_CS01dxy/upload/master

Java 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.