Results 1 to 10 of 10

Thread: Inheritance

  1. #1

    Default Inheritance


    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.

    please help me this a kinda problem...

    cheers.....

  2. #2
    assignment na sad na bro? hehehehehe....

  3. #3
    Murag assignment jud ni...hehehe
    Activity #1 on inheritance...hehehe
    Nkasugat mn pud ko ani animal nga activity kato student pako.
    hehehe

  4. #4
    *Sigh*

    another assignment thread...maybe we should create one thread dedicated for assignments...hehehehehe.

    anyways...
    Code:
    public class Animal {
    int weight;
    int LifeExpectancy;
    char gender;
    String name;
    public String type;
    
    Animal(int wt, int life, char ***, String n, String t){
    weight = wt;
    LifeExpectancy = life;
    gender = ***;
    name = n;
    type = t;
    }
    
    public void printValues(){
      System.out.println("Values:\n");
      System.out.println("Weight : "+weight);
      System.out.println("Life Expectancy: "+LifeExpectancy);
      System.out.println("Gender: "+gender);
      System.out.println("Name : "+name);
      System.out.println("Type: "+type);
    }
    public static void main(String args[]) {
                   Platypus p1 = new Animal();
           }
    }
    blah blah blah......


    maybe that's it. I maybe correct, but if its wrong...just correct them. Ay wait, asa man diay ang Inheritance ani? hihihi.....
    Last edited by ChaosOrb; 07-23-2008 at 11:13 PM.

  5. #5
    hope this would help..

    Code:
    public class Animal {
    	private Integer lifeExpectancy;
    	private Integer weight;
    	private Character gender;
    	private String name;
    	public String type;
    	
    	public Animal() {
    	  this.lifeExpectancy = 100;
    	  this.weight = 140;
    	  this.gender = "male";
    	  this.name = "charles";
    	  this.type = "o";
    	}
    	
          public void printValues()  {
             System.out.println("LifeExpectancy: " + this.lifeExpectancy);
             System.out.println("Weight: " + this.weight);
             System.out.println("Gender: " + this.gender);
             System.out.println("Name: " + this.name);
             System.out.println("Type: " + this.type);
    	}
    	
    	public static void main(String[] args) {
    		Animal anim = new Animal();
    		anim.printValues();
    	}
    	
    }

  6. #6
    tnxs 4 this code i try it..

    cheers....

  7. #7
    i dont see any inheritane there

  8. #8
    mao, wala may inheritance na...

  9. #9
    hehehe. mao gyod. ang iya gihimo ang parent class raman. corrent me if i'm wrong. dili mahitabo ang inheritance kong wla ang parent and child relationship or "is a" relationship(an elephant is an animal).

    Then mura ang problem mao naa problema. Unless naa sumpay ang problem.

  10. #10
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    google it bro... madami examples sa net..

    Inheritance (computer science - Wikipedia, the free encyclopedia)

    in short parang ganito:

    Code:
        class BaseClass(object):
           <statement>
           .
           .
    
        class DerivedClass(BaseClass):
            <statement>
            .
            .
            .
            <statement>
    pacnxa na...python gamit ko. para mabilis ng kunti. another way to understand is for example.

    class SQUARE - my properties ka sa sides nya. parang getPerimeter, getLength, getArea etc... }
    class TRIANGLE - my properties ka rin, etc....getPerimeter, getArea etc...}
    class PYRAMID(SQUARE, TRIANGLE) - so from here you are using inheritance. you are actually making a new class inherited from other class which is called a based class.

    Based class are: SQUARE & TRIANGLE
    Derived Class is: PYRAMID

    pwede mo parin makuha ang perimeter nya. by calling one function only. like
    PYRAMID.getPerimeter = this will sum up all the perimeters. bali 1 SQUARE at 4 Triangle. or
    PYRAMID.SQUARE.getPerimeter = this will output only the square part of the Pyramid
    from here you will enjoy how powerful inheritance and polymorphism.

    now go back to the derived class which is PYRAMID. we can add new method. which is getVolume. which is strictly for the new derived class only.

    in some 3d modelling software they construct their classes like these:

    Class Point(x,y) - (x and y are location of your point)
    Class Line(P1 as Point, P2 as Point) - you have two points here, startpoint and endpoint which makes a LINE
    Class Rectangle(Width as Line, Height as Line) - your rectangle inherit the properties of the lines

    Hope this may help.


    Mark

  11.    Advertisement

Similar Threads

 
  1. Replies: 20
    Last Post: 02-10-2012, 11:30 PM
  2. Inheritance: please help i wanna understand
    By javaslave in forum Programming
    Replies: 3
    Last Post: 01-17-2012, 09:20 AM
  3. Inheritance land problem
    By heartproblem in forum General Discussions
    Replies: 16
    Last Post: 06-13-2011, 06:51 PM
  4. Merged: Filipina maid inherits millions from employer
    By linogzkie in forum Politics & Current Events
    Replies: 73
    Last Post: 07-27-2010, 01:44 PM
  5. The Inheritance Cycle by Chris Paolini
    By samokan in forum Arts & Literature
    Replies: 22
    Last Post: 02-04-2008, 11:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top