Exercises inherited by Task 1 of week 8 on java

Source: Internet
Author: User

[Java]
/*
* Start the program header annotation.
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: inherited exercise
* Author: Xue Guangchen
* Completion date: January 1, October 17, 2012
* Version No.: x1.0

* Description of tasks and Solutions
* Input description:
* Problem description: (1) encapsulate a People type with the height and weight attributes and the speakHello, averageHeight, and averageWeight functions.
 
(2) encapsulate a type of ChinaPeople that is a subclass of People. The chinaMartial function is added, and the override super class speakHello, averageHeight, and averageWeight functions are provided.
 
(3) encapsulate a class of AmericanPeople, which is a subclass of People. The AmericanBoxing function is added. The override superclasses include speakHello, averageHeight, and averageWeight.
 
(4) encapsulate the BeijingPeople type to be a subclass of ChinaPeople. The BeijingOpera function is added, and the override class speakHello, averageHeight, and averageWeight functions are added.
 
(5) Test the above objects with a program execution entry
* Program output:
* End the comment in the program Header
*/
// People class
 
Package xgc;
 
Public class People {
Private double height;
Private double weight;

Public People ()
{
This. height = 10;
This. weight = 10;
}

Public People (double height, double weight)
{
This. height = height;
This. weight = weight;
}

Public double getHeight (){
Return height;
}
Public void setHeight (double height ){
This. height = height;
}
Public double getWeight (){
Return weight;
}
Public void setWeight (double weight ){
This. weight = weight;
}

Public void speakHello ()
{
System. out. println ("Hello ");
}

Public void averageHeight ()
{
}

Public void averageWeight ()
{
}
}
 
// ChinaPeople
Package xgc;
 
Public class ChinaPeople extends People {

Public void chinaMartial ()
{
System. out. println ("ChinaPeople taiji very good ");
}

Public void speakHello ()
{
System. out. println ("Hello, ChinaPeople ");
}

Public void averageHeight ()
{
SetHeight (1.7 );
System. out. println ("ChinaPeople averageHeight =" + getHeight ());
}

Public void averageWeight ()
{
SetWeight (70 );
System. out. println ("ChinaPeople averageWeight =" + getWeight ());
}
 
}
 
// AmericanPeople
Package xgc;
 
Public class AmericanPeople extends People {
Public void AmericanBoxing (){
System. out. println ("AmericanPeople Boxing very good ");
}
 
Public void speakHello ()
{
System. out. println ("AmericanPeople say Hello ");
}

Public void averageHeight ()
{
SetHeight (1.8 );
System. out. println ("AmericanPeople averageHeight =" + getHeight ());
}

Public void averageWeight ()
{
SetWeight (75 );
System. out. println ("AmericanPeople averageWeight =" + getWeight ());
}
}
 
// BeijingPeople
Package xgc;
 
Public class BeijingPeople extends ChinaPeople {
Public void BeijingOpera (){
System. out. println ("BeijingOpera very good ");
}

Public void speakHello ()
{
System. out. println ("BeijingPeople say hello ");
}

Public void averageHeight ()
{
SetHeight (1.75 );
System. out. println ("ChinaPeople averageHeight =" + getHeight ());
}

Public void averageWeight ()
{
SetWeight (73 );
System. out. println ("AmericanPeople averageWeight =" + getWeight ());
}
 
}
 
// Test
Package xgc;
 
Public class Test {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
ChinaPeople cp = new ChinaPeople ();
Cp. speakHello ();
Cp. chinaMartial ();
Cp. averageHeight ();
Cp. averageWeight ();
System. out. println ();

AmericanPeople ap = new AmericanPeople ();
Ap. speakHello ();
Ap. AmericanBoxing ();
Ap. averageHeight ();
Ap. averageWeight ();
System. out. println ();

BeijingPeople bp = new BeijingPeople ();
Bp. speakHello ();
Bp. BeijingOpera ();
Bp. averageHeight ();
Bp. averageWeight ();
System. out. println ();
 
}
 
}
Running result:

 

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.