CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2012
    Posts
    2

    polymorphism with different parameter

    CyberCafe.java
    Code:
    public abstract class CyberCafe{
        String cust_name;
        int pc_no;
         
        public CyberCafe(String cn, int pn){
            cust_name = cn;
            pc_no = pn;
        }
         
        public String getCustName(){
            return cust_name;
        }
         
        public int getPC(){
            return pc_no;
        }
         
        public abstract double calcTotCharge();
    }
    Printing.java
    Code:
    public class Printing extends CyberCafe{
        int no_of_print;
        boolean colour;
         
        public Printing(String cn, int pn, int p, boolean c){
            super(cn, pn);
            no_of_print = p;
            colour = c;
        }
         
        public int getNoOfPrint(){
            return no_of_print;
        }
         
        public boolean getColour(){
            return colour;
        }
         
        public double calcTotCharge(){
            double charge;
            if(colour){
                charge =  0.7 * no_of_print;
            }
            else{
                charge =  0.3 * no_of_print;
            }
            return charge;
        }
    }
    Internet.java
    Code:
    public class Internet extends CyberCafe{
        String start_time;
        String end_time;
        int no_of_files_downloaded;
         
        public Internet(String cn, int pn, String s, String e, int d){
            super(cn, pn);
            start_time = s;
            end_time = e;
            no_of_files_downloaded = d;
        }
         
        public String getStartTime(){
            return start_time;
        }
         
        public String getendTime(){
            return end_time;
        }
         
        public int getDownloadedFiles(){
            return no_of_files_downloaded;
        }
         
        public double calcTotCharge(){
            double charge = 0.0;
            int min = 0;
             
            int startH = Integer.parseInt(start_time.substring(0, 2));
            int startM = Integer.parseInt(start_time.substring(2));
            int endH = Integer.parseInt(end_time.substring(0, 2));
            int endM = Integer.parseInt(end_time.substring(2));
             
            //calculate minutes
            if(startH == endH){
                min = endM - startM;
            }
             
            else if(startH < endH){
                min = ((endH - startH -1) * 60) + endM + (60 - startM);
            }
     
            else{
                min = (((24 - startH) + endH - 1) * 60) + endM + (60 - startM);
            }
             
            //calculate charge
            if(min < 120){
                charge = 2.5;
            }
             
            else{
                charge = 2.5 + ((((min - 120) / 30) + 1) * 0.5);
            }
             
            //calculate total charges
            charge = charge + (1.5 * no_of_files_downloaded);
            return charge;
        }
    }
    myCyberCafeAPP.java
    Code:
    import java.util.Scanner;
    public class myCyberCafeAPP{
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            System.out.print("Enter no of customer: ");
            int cust = input.nextInt();
             
            CyberCafe[] myCafePrint= new CyberCafe[cust];
            CyberCafe[] myCafeInternet= new CyberCafe[cust];
            System.out.print("\n");
             
            for(int i=0; i<cust; i++){
                System.out.print("Customer Name: ");
                String name = input.next();
                System.out.print("Phone No: ");
                int phone = input.nextInt();
                 
                System.out.print("No of Print: ");
                int print = input.nextInt();
                System.out.print("Colour (Yes/No): ");
                String colour = input.next();
                boolean c;
                if(colour.equalsIgnoreCase("Yes")){
                    c = true;
                }
                else{
                    c = false;
                }
                 
                System.out.print("Start Time (eg: 1420): ");
                String start = input.next();
                System.out.print("End Time (eg: 1430): ");
                String end = input.next();
                System.out.print("No of Files Downloaded: ");
                int download = input.nextInt();
                 
                myCafePrint[i] = new Printing(name, phone, print, c);
                myCafeInternet[i] = new Internet(name, phone, start, end, download);
            }
             
            double total = 0.0;
            for(int j=0; j<cust; j++){
                total = total + myCafePrint[j].calcTotCharge() + myCafeInternet[j].calcTotCharge();
            }
            System.out.print("Total Charge: RM " + total);
        }
    }
    i have subclass "Printing" and "Internet"..
    how can i store both "Printing" and "Internet" data in the same array object...
    for example:
    array[1] = {customer name, phone no, no of print, color, start time, end time, no of download}

    should i use interface..i read a lot about it but i don't understand...
    can anyone teach me how to solve it with appropriate example...
    thanks in advance...

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: polymorphism with different parameter

    If your array is of type CyberCafe you can put either Printing or Internet objects into it.

    Note: The objects you retrieve from the array will be of type CyberCafe and so you will only be able to call the CyberCafe methods on the object unless you cast the objects back to their original type.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jan 2012
    Posts
    2

    Re: polymorphism with different parameter

    if i make array object like this "CyberCafe[] myCafe = new CyberCafe[cust];"...
    how can i store "Internet" and "Print" in myCafe[]
    for example:
    myCafe[0] = new Printing();
    myCafe[1] = new Internet();
    and so on.......

    i think my method wrong...
    CyberCafe[] myCafePrint= new CyberCafe[cust];
    CyberCafe[] myCafeInternet= new CyberCafe[cust];

    myCafePrint[i] = new Printing(name, phone, print, c);
    myCafeInternet[i] = new Internet(name, phone, start, end, download);

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: polymorphism with different parameter

    Why not try it and see if it works.

    Code:
    myCafe[0] = new Printing();
     myCafe[1] = new Internet();
    Yes that will work as long as Printing and Internet inherit from CyberCafe.

    If you have an array of type A you can put into it anything that ultimately inherits from type A.

    BTW: I'm not convinced your design is correct. The golden rule of inheritence is the "is a kind of" rule. So, does it make sense to say "Printing is a kind of CyberCafe" and/or "Internet is a kind of CyberCafe". Clearly not, Printing and Internet Access are actions/activities you undertake in a CyberCafe and are not kinds of CyberCafe.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: polymorphism with different parameter

    Sorry but I don't have the time to teach you. There are loads of on-line articles on the subject, I suggest you read some of them.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Jan 2011
    Posts
    24

    Re: polymorphism with different parameter

    Just a side note:

    In Java you should trying using camelCase with variable, method or class names e.g. no_of_files_downloaded should be written as noOfFilesDownloaded. not mandatory but this is java code convention and bean naming convention. There are lot of tool and framework in java which relies on bean naming convention when they access code using reflection. so following convention is good.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured