Results 1 to 11 of 11
Thread: How to use Polymorphism
- 01-13-2012, 11:38 AM #1
How to use Polymorphism
CyberCafe.java
Printing.javaJava 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(); }
Internet.javaJava 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; } }
myCyberCafeAPP.javaJava 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; } }
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...
- 01-13-2012, 11:51 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-13-2012, 11:59 AM #3
Re: How to use Polymorphism
in this case, what should i do..
can i declare only 1 array object to get total charge..
- 01-13-2012, 12:57 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 01-13-2012, 01:01 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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?
- 01-13-2012, 01:03 PM #6
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...
- 01-13-2012, 02:07 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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()).
- 01-13-2012, 02:11 PM #8
Re: How to use Polymorphism
thanks..
i'll try..
- 01-16-2012, 09:29 AM #9
- 01-16-2012, 12:29 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 01-16-2012, 02:15 PM #11
Similar Threads
-
Polymorphism - do you use it?
By N00Bie in forum New To JavaReplies: 10Last Post: 02-15-2011, 04:42 PM -
Polymorphism
By blug in forum New To JavaReplies: 3Last Post: 10-11-2010, 10:35 AM -
Polymorphism Help
By AWPtic in forum New To JavaReplies: 5Last Post: 04-06-2009, 04:13 PM -
what is polymorphism
By Nari in forum New To JavaReplies: 5Last Post: 04-04-2008, 03:14 AM -
what's polymorphism?
By christina in forum New To JavaReplies: 2Last Post: 08-05-2007, 10:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks