![Quote](images/metro/blue/misc/quote_icon.png)
Originally Posted by
javaslave
Inheritance
a. Create a java program for a class named Animal to allow the following private data members:
§ Include integer values named lifeExpectancy and weight.
§ Include a character value named gender and a String value called name.
§ Allow for a public String value called type.
§ Design an Animal() constructor with parameters to accept values for each data member.
§ Design a public void method printValues() with no parameters that displays all data members for the object of the Animal class.
Animal |
- lifeExpectancy: int
- weight: int
- gender: char
- name: String
+ type: String |
+ Animal(char, int, String, int, String)
+ printValues( ): void |
Sayun ra man ning assignment nimo bro. Let me help:
Code:
public class Animal {
private int lifeExpectancy;
private double weight;
private char gender;
private String name;
public String type;
public Animal(char gender, double weight, String name) {
this.gender = gender;
this.weight = weight;
this.name = name;
}
public Animal(){
}
public void printValues(){
System.out.println("Life Expectancy: "+lifeExpectancy);
System.out.println("Weight: "+weight);
System.out.println("Gender: "+gender);
System.out.println("Name: "+name);
System.out.println("Type: "+type);
}
public static void main(String[] args) {
Animal ka = new Animal('M', 66.6, "You");
ka.printValues();
}
}
Bro, naa koy gi-pang usab:
>
weight: it should be
double (Since weights are precision dependent)
>
lifeExpectancy and type: They do not have initialized values since wala man naka butang sa imong problem nga ipa initialize sila. (
As based from the method specification nimo)
>
default constructor: I-ask na lang ni sa inyong teacher why nagbutang lang ko ug empty default constructor. (Maglisud ko ug explain
![Cheesy](images/smilies/cheesy.gif)
)
> Wala ko kasabot asa dapita ang inheritance diri nga problem...
![Sad](images/smilies/sad.gif)
Inheritance man gud involves the extension and implemetation of one or multiple different classes. If you have a Java book, please read the concept about Inheritance.
NOTE: Sa sunod bro, don't be lazy to code because eventually that kind of attitude will eat you whole in the industry. Makaya ra man unta ni nimo even though basic concepts pa inyung gi-tackle. I hope you'll never forget what I said.
(Output)
Code:
Life Expectancy: 0
Weight: 66.6
Gender: M
Name: You
Type: null