Java third time job

Source: Internet
Author: User

"Java Technology" XX homework (a) study summary

1 Read the following procedure, analysis can be compiled through? 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 compile, reason: the call to make a function must be the first statement in the constructor. Put the statement super ("Hello.grandparent.") " The first sentence of the construction method placed in the subclass parent.

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 ("Barking ...!");
}
public void sleep () {
System.out.println ("The 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 ();
}
}
~~~
The sleep () method is a new method in the subclass, and animal cannot be called by the parent class.

The program after the change:
~~~
Class animal{
void Shout () {
System.out.println ("Animal called!");
}
void Sleep () {
System.out.println ("Sleeping");
}
}
Class Dog extends animal{
public void shout () {
SYSTEM.OUT.PRINTLN ("Wang!");
}
public void sleep () {
System.out.println ("The Dog Sleeps");
}
}
public class test{
public static void Main (String args[]) {
Animal Animal = new Dog ();
Animal.shout ();
Animal.sleep ();
Dog dog = (dog) animal;
Dog.sleep ();
Animal animal2 = new Dog ();
Dog = (dog) Animal2;
Dog.shout ();
}
}
~~~
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]
~~~
(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 ();
}
}
~~~

(3) Add the following method to the person class
~~~
Java public String toString () {return "name:" + THIS.name + ", Age:" + this.age;}
~~~

Operation Result:
Name: Zhang San, age: 20
Name: Zhang San, age: 20
Illustrates that the subclass person class repeats the ToString () method in the object class

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: Method in interface, default is public abstract so when subclasses implement abstract methods, they should be decorated with public

(ii) Experimental summary

A thought, a pattern thought of a two-layer pattern three-level image similar to a database

2. Complete the design of the class according to the following requirements (1) design a planar graphic abstract class (providing a method for calculating the perimeter and area of the object) and a stereoscopic graphic abstract class (providing a method to find the surface area and volume of the Object) (2) design ball, cylinder, cone, rectangle, triangle, Circle class, The abstract classes of planar graphics and three-dimensional graphics are inherited respectively. (3) Set up the test class and Test. Draw a class diagram. 3: Reference class diagram, refactor the following instance, analyze and understand the meaning and purpose of polymorphism (1) A zoo has a breeder Xiao Li, who needs to feed him a lion, five monkeys and 10 pigeons every day. Please use a program to simulate his feeding process (2) First refactoring, introducing inheritance, using abstract classes and object polymorphic refactoring programs, animal classes using abstract classes, merging Feeder class methods (3) Second refactoring, modifying feedanimals methods, Let it receive a animal array (iii) [Code hosting][email protected]: Java1601/first_java_operation.git
    • Code Cloud Commit History

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.