Results 1 to 11 of 11
  1. #1
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default How to use Polymorphism

    CyberCafe.java
    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
    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
    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
    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);
    	}
    }

    is this true?
    should i declare 2 array object (line 8 & 9 in myCyberCafeAPP.java)..
    help me..i don't really understand polymorphism...

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to use Polymorphism

    Polymorphism is calling the same method (i.e. same name, same number of parameters, same types) on two different objects. The implementation of the method in the two different objects can differ of course. If two different classes implement one interface, the methods from the interface can be called polymorphically. If one class extends another class and overrides a method, the implementations of the methods in the sub class and the super class can also be called polymorphically.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: How to use Polymorphism

    in this case, what should i do..
    can i declare only 1 array object to get total charge..

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to use Polymorphism

    Quote Originally Posted by nfsmwbe View Post
    in this case, what should i do..
    can i declare only 1 array object to get total charge..
    I really don't know what arrays have to do with polymorphism; care to explain what you have in mind?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to use Polymorphism

    I htink this has little to do with polymorphism as such.
    Your inheritance model (ie X is-a Y) is suspect.
    Printing is-a CyberCafe?
    Internet is-a CyberCafe?

  6. #6
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: How to use Polymorphism

    i mean the right and simplest way to get total charge
    using concept of inheritance n polymorphism..
    just give me hint..i'm new in java..
    thanks in advance...

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to use Polymorphism

    Have an interface, ChargeableItem, with a single method, "double calcTotCharge();".
    Have all things that are Chargeable implement that interface.
    Stick those in a List<ChargeableItem> for whoever is using the CyberCafe.
    When you want to total up the cost to the client loop round the List calling calcTotCharge() (though I would probably call it getCharge()).

  8. #8
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: How to use Polymorphism

    thanks..
    i'll try..

  9. #9
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: How to use Polymorphism

    Quote Originally Posted by Tolls View Post
    Stick those in a List<ChargeableItem> for whoever is using the CyberCafe.
    what does it mean..
    can u give an example...
    i don't know how to start...

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to use Polymorphism

    OK.
    SOmeone goes into your CYberCafe and buys some things (time on the internet, some printing, and a coffee)...these (as far as I can make out in what you;re doing) get associated with that person. That's your List of ChargeableItems.

  11. #11
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: How to use Polymorphism

    sorry, i don't get it..
    i already read a lot about interface but i don't understand how to apply in this situation...
    can you explain in simple code...
    what difference between "double calcTotCharge();" and "ChargeableItem" ?

Similar Threads

  1. Polymorphism - do you use it?
    By N00Bie in forum New To Java
    Replies: 10
    Last Post: 02-15-2011, 05:42 PM
  2. Polymorphism
    By blug in forum New To Java
    Replies: 3
    Last Post: 10-11-2010, 11:35 AM
  3. Polymorphism Help
    By AWPtic in forum New To Java
    Replies: 5
    Last Post: 04-06-2009, 05:13 PM
  4. what is polymorphism
    By Nari in forum New To Java
    Replies: 5
    Last Post: 04-04-2008, 04:14 AM
  5. what's polymorphism?
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 11:29 PM

Posting Permissions

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