Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21
  1. #11

    Default Re: hi please help me.. using java,, INHERITANCE.... pleaseeeeee...


    Quote Originally Posted by javaslave View Post
    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 )
    > Wala ko kasabot asa dapita ang inheritance diri nga problem... 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

  2. #12
    Elite Member
    Join Date
    Jun 2010
    Gender
    Male
    Posts
    1,018

    Default Re: hi please help me.. using java,, INHERITANCE.... pleaseeeeee...

    Akong gamitan og inheritance ang object ni marcdaven.


    Code:
    import marcdavens.Animal;
    
    public class Dog extends Animal{
    
        public Dog(char gender){
             this.gender = gender;
            this.weight = 30.0;
            this.name = "Dog";
            this.type = "Mammal";
        }
    
        public void bark(){
             System.out.println("Arf");
        }
    
        public static void main(String args[]){
              Dog javaslave = new Dog('M');
              javaslave.bark();
              javaslave.printValues();
      
        }
    
    
    
    }
    Kalimot naman ko hapit og java so naay chance naay syntax error.

    Please notice the following points when an object is inheriting from another object.
    The use of "extends"
    Use of my own constructor for the new object.
    No need to redeclare used variables or methods from parent object but you are able to use them.
    You have the freedom to add your own method in my case "bark"

    One more note:
    Empty constructors are usually used by Frameworks (EJB) so they are able to use your entities or objects without any parameters.


    Last na lang though off topic gamay.

    We help because we want to encourage people to become programmers labi na kung course IT.
    Hunahunaa, nag IT mo unya padung ra mo mag office clerk, secretary, encoder or technical support.
    Kani ang mga nindot og career path, programmer or database admin. naa pa tingali lain.
    So kanang magpatabang nga dili makita namo ang effort to learn madiscourage tawon mi.
    Feeling namo gausik-usik ra mo sa inyong mga kaugmaon as well as our time.
    Last edited by Klave; 01-19-2012 at 10:53 PM.

  3. #13

    Default Re: hi please help me.. using java,, INHERITANCE.... pleaseeeeee...

    Quote Originally Posted by Klave View Post
    Akong gamitan og inheritance ang object ni marcdaven.


    Code:
    import marcdavens.Animal;
    
    public class Dog extends Animal{
    
        public Dog(char gender){
             this.gender = gender;
            this.weight = 30.0;
            this.name = "Dog";
            this.type = "Mammal";
        }
    
        public void bark(){
             System.out.println("Arf");
        }
    
        public static void main(String args[]){
              Dog javaslave = new Dog('M');
              javaslave.bark();
              javaslave.printValues();
      
        }
    
    
    
    }
    Kalimot naman ko hapit og java so naay chance naay syntax error.

    Please notice the following points when an object is inheriting from another object.
    The use of "extends"
    Use of my own constructor for the new object.
    No need to redeclare used variables or methods from parent object but you are able to use them.
    You have the freedom to add your own method in my case "bark"
    PERFECT example of Inheritance by Klave!
    Mao gyud ni pinaka basic example sa concept of Inheritance sa Java. There are many more examples out there as you encounter more complex ideas (such as design patterns, etc.). It's so important that you fully understand the basic lang sa gyud.

    Good luck javaslave!

  4. #14

    Default Re: hi please help me.. using java,, INHERITANCE.... pleaseeeeee...

    Inheritance allows you to create a class that's based on another class. When used correctly, inheritance can simplify the overall design of an application.

    When inheritance is used, a subclass inherits the fields, constructors, and methods of a superclass. Then, the objects that are created from the subclass can use these inherited members. The subclass can also provide its own members that extend the superclass, and it can override methods of the superclass by providing replacement definitions for them.

  5. #15

    Default Re: hi please help me.. using java,, INHERITANCE.... pleaseeeeee...

    Given the examples below,



    I'm not going to answer your problem but will give you idea.

  6. #16

    Default Re: hi please help me.. using java,, INHERITANCE.... pleaseeeeee...

    Try to run the example and check how the inheritance does in the program.

    BOOK CLASS THAT INHERITS PRODUCT

    Code:
    public class Book extends Product
    {
        private String author;
    
        public Book()
        {
            super();
    		author = "";
            count++;
        }
    
        public void setAuthor(String author)
        {
            this.author = author;
        }
    
        public String getAuthor(){
            return author;
        }
    
        @Override
        public String toString()
        {
            return super.toString() +
                "Author:      " + author + "\n";
        }
    }
    CLASS PRODUCT AS SUPERCLASS

    Code:
    import java.text.NumberFormat;
    
    public class Product
    {
        private String code;
        private String description;
        private double price;
        protected static int count = 0;
    
        public Product() {}
    
        public void setCode(String code)
        {
            this.code = code;
        }
    
        public String getCode(){
            return code;
        }
    
        public void setDescription(String description)
        {
            this.description = description;
        }
    
        public String getDescription()
        {
            return description;
        }
    
        public void setPrice(double price)
        {
            this.price = price;
        }
    
        public double getPrice()
        {
            return price;
        }
    
        public String getFormattedPrice()
        {
            NumberFormat currency = NumberFormat.getCurrencyInstance();
            return currency.format(price);
        }
    
        @Override
        public String toString()
        {
            return "Code:        " + code + "\n" +
                   "Description: " + description + "\n" +
                   "Price:       " + this.getFormattedPrice() + "\n";
        }
    
        public static int getCount()
        {
            return count;
        }
    }

    Code:
    import java.util.Scanner;
    
    public class ProductApp
    {
        public static void main(String args[])
        {
            // display a welcome message
            System.out.println("Weclome to the Product Selector");
            System.out.println();
    
            // perform 1 or more selections
            Scanner sc = new Scanner(System.in);
            String choice = "y";
            while (choice.equalsIgnoreCase("y"))
            {
                System.out.print("Enter product code: ");
                String productCode = sc.next();  // read the product code
                sc.nextLine();  // discard any other data entered on the line
    
                // get the Product object
                Product p = ProductDB.getProduct(productCode);
    
                // display the output
                System.out.println();
                if (p != null)
                    System.out.println(p.toString());
                else
                    System.out.println("No product matches this product code.\n");
    
                System.out.println("Product count: " + Product.getCount() + "\n");
    
                // see if the user wants to continue
                System.out.print("Continue? (y/n): ");
                choice = sc.nextLine();
                System.out.println();
            }
        }
    }
    CLASS PRODUCTDB

    Code:
    public class ProductDB
    {
        public static Product getProduct(String productCode)
        {
           
            Product p = null;
    
            if (productCode.equalsIgnoreCase("java") ||
               productCode.equalsIgnoreCase("jsps") ||
               productCode.equalsIgnoreCase("mcb2"))
            {
                Book b = new Book();
                if (productCode.equalsIgnoreCase("java"))
                {
                    b.setCode(productCode);
                    b.setDescription("Beginning Java");
                    b.setPrice(49.50);
                    b.setAuthor("Manny Pacquiao");
                }
                else if (productCode.equalsIgnoreCase("jsps"))
                {
                    b.setCode(productCode);
                    b.setDescription("Java Servlets and JSP");
                    b.setPrice(49.50);
                    b.setAuthor("Freddie Roach");
                }
                else if (productCode.equalsIgnoreCase("mcb2"))
                {
                    b.setCode(productCode);
                    b.setDescription("Visual Basic.NET 2010");
                    b.setPrice(59.50);
                    b.setAuthor("Messerchtmitt Longbow");
                }
                p = b; // set Product object equal to the Book object
            }
            else if (productCode.equalsIgnoreCase("txtp"))
            {
                Software s = new Software();
                s.setCode("txtp");
                s.setDescription("TextPad");
                s.setPrice(27.00);
                s.setVersion("4.7.3");
                p = s; // set Product object equal to the Software object
            }
            return p;
        }
    }
    Code:
    public class Software extends Product
    {
        private String version;
    
        public Software()
        {
            super();
    		version = "";
            count++;
        }
    
        public void setVersion(String version)
        {
            this.version = version;
        }
    
        public String getVersion(){
            return version;
        }
    
        @Override
        public String toString()
        {
            return super.toString() +
                "Version:     " + version + "\n";
        }
    }
    Hope you'll learn through these example....

  7. #17

    Default Re: hi please help me.. using java,, INHERITANCE.... pleaseeeeee...

    How to code constructors

    • The constructor must use the same name and capitalization as the name of the class.

    • If you don’t code a constructor, Java will create a default constructor that initializes all numeric types to zero, all boolean types to false, and all objects to null.

    • To code a constructor that has parameters, code a data type and name for each parameter within the parentheses that follow the class name.

    • The name of the class combined with the parameter list forms the signature of the constructor.

    • Although you can code more than one constructor per class, each constructor must have a unique signature.

    • The this keyword can be used to refer to an instance variable of the current object.

  8. #18

    Default Re: hi please help me.. using java,, INHERITANCE.... pleaseeeeee...

    Quote Originally Posted by rjcaballes View Post
    google is your friend...
    GOogle is everyone's friend.

  9. #19

    Default Re: hi please help me.. using java,, INHERITANCE.... pleaseeeeee...

    google rules hehe

  10. #20
    C.I.A.
    Join Date
    Apr 2009
    Gender
    Female
    Posts
    8,812
    Blog Entries
    39

    Default Re: hi please help me.. using java,, INHERITANCE.... pleaseeeeee...

    nagpahimo sha ug code ei. ahhaha wa sha magpatabang. hahahaha
    mao sad ni amo topic

  11.    Advertisement

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

 
  1. please help me how to connect my two computer for myDSL
    By rayfloydcabrera in forum Computer Hardware
    Replies: 13
    Last Post: 11-10-2013, 08:40 PM
  2. Replies: 9
    Last Post: 07-13-2006, 01:55 PM
  3. Sports Bra.. please help me out
    By nopeace in forum Trends & Fashion
    Replies: 4
    Last Post: 12-24-2005, 02:34 PM
  4. Please Help me!
    By SeventhSlave in forum Music & Radio
    Replies: 13
    Last Post: 09-05-2005, 01:12 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