Question

In: Computer Science

repare a single compressed file in .zip format containing all the source files (.java files, NOT...

repare a single compressed file in .zip format containing all the source files (.java files, NOT .class files) and the output file generated by running your code (i.e., testsOutput.txt) and submit it through Moodle. To be more precise, your submitted .zip file should contain the following files: PhoneCard.java, SuperNA10Card, Global10Card, Global25Card, SuperPhoneCardInc.java, CardTable.java, CallZone.java, and testsOutput.txt.

Note:

(1) Your assignment will be given a zero mark if only the compiled files (.class files) are submitted. No exceptions. Please make sure to submit the source files (.java files).

(2) Please make sure your code compiles under the command line (i.e., without an IDE). All Java classes should be put under the default package. Do not put any package statement at the beginning of your source file. If you are using an IDE, this is especially important because some IDEs put your code under a particular package for your project. Any code that does not compile under the command line can only receive 20/100. (See below for the marking scheme).

(3) Finish only the parts as indicated; do not attempt to change other parts of the code.

(4) Please pay attention to variable naming. JavaDoc comments are required for both the code you write and the code already provided.

(5) Please pay close attention to the formatting of the output. For example, when asked to print charged X.XX, make sure only two digits are printed after the decimal point. (Hint: Use DecimalFormat or System.out.printf().)

Details

SuperPhoneCard Inc. sells phone cards for making cheap long distance calls around the world. In this assignment, you will write a (much simplified) Java program to manage their business.

SuperPhoneCard Inc. sells 3 types of phone cards: SuperNA10 cards, which cost $10 and are good only for calls in Canada and the USA, Global10 cards, which cost $10 and are good for overseas calls, and Global25 cards, which cost $25 and are also good for overseas calls. The per minute rates for each type of card and call zone are as follows:

SuperNA10 Global10 Global25
Canada $0.05 $0.07 $0.05
USA $0.10 $0.15 $0.10
Europe XXXXX $0.30 $0.20
Asia XXXXX $0.60 $0.40
Australia & NZ XXXXX $0.45 $0.30
Latin America XXXXX $0.45 $0.30
Africa XXXXX $0.60 $0.40

The initial balance on the cards and the weekly maintenance fee are indicated below:

SuperNA10 Global10 Global25
initial balance $10.00 $10.00 $25.00
weekly fee $0.50 $1.00 $1.00

Your main job in this assignement is to implement a hierarchy of classes to represent the different types of cards. At the top of the hierarchy, there is an abstract class PhoneCard with the following API:

  • public PhoneCard(long no, int passwd, double bal): a constructor (precondition: no and passwd must be positive);
  • public long getNumber(): an accessor returning the card number;
  • public int getPassword(): an accessor returning the card password;
  • public double getBalance(): an accessor returning the card balance;
  • public void setBalance(double bal): a mutator to set the card balance;
  • public abstract boolean allowed(String zone): a predicate that returns true if a call to the argument zone is allowed for the card;
  • public abstract double costPerMin(String zone): returns the cost per minute of a call to the argument zone;
  • public int getLimit(String zone): returns the maximum number of minutes that can be charged for a call to the argument zone given the card's balance (truncated down to the next integer);
  • public boolean charge(int minutes, String zone): tries to charge a call to the given zone with the given number of minutes to the card; if the balance is sufficient to cover it, it returns true and if the balance is not sufficient, it leaves it unchanged and returns false (precondition: minutes must be positive);
  • public abstract void deductWeeklyFee(): deducts the appropriate weekly fees from the card's balance, leaving it non-negative;
  • public String toString(): returns the string "card no no has a balance of X.XX".

There are also 3 classes, SuperNA10Card, Global10Card, and Global25Card that inherit from PhoneCard, and model the properties of the associated type of card as specified above, by defining the PhoneCard class's abstract methods. These classes should use the supplied CallZone.java class to check if the strings representing the call zones are valid.

Once you have defined these classes, you should then complete the application that SuperPhoneCard Inc. will use to manage its business. This application is implemented by the SuperPhoneCardInc class, that reads and processes a number of commands from the standard input stream for processing cards and calls, outputing the results on the standard output stream. The commands are:

  • add no passwd cardType: adds a card of the given type with the given number, password, and type (a cardType is either SuperNA10, Global10, or Global25);
  • getBalance no passwd: prints the balance of card no if the password passwd is valid;
  • getLimit no passwd callZone: prints the maximum number of minutes that can be charged to card no for a call to callZone given the card's balance if the password passwd is valid and calls to callZone are allowed for this card (a callZone is either CANADA, USA, EUROPE, ASIA, ANZ, LATINAM, or AFRICA);
  • charge no passwd callZone minutes: charges card no for a call of minutes to callZone if the password passwd is valid, the balance is sufficient, and calls to callZone are allowed for this card;
  • deductWeeklyFee: deducts the weekly fee from all cards;
  • printAll: prints the balance of all cards.

An incomplete definition for the SuperPhoneCardInc class is available here in SuperPhoneCardInc.java. You must fill out the missing parts of the definition, i.e. the code for handling the following commands:

  • getLimit no passwd callZone: if the command can be executed successfully, print "Result: card no limit for zone callZone is XX minutes", otherwise print error messages as follows: if the number of arguments or their type is wrong, print "Error: invalid arguments for getLimit command"; if the card does not exists, print "Error: card no no does not exist"; if the card's password is wrong, print "Error: password passwd incorrect"; and if calls to the given zone are not allowed for this card, print "Error: card no not allowed for zone callZone";
  • charge no passwd callZone minutes: if the command can be executed successfully, print "Result: card no charged X.XX, new balance is Y.YY", otherwise print error messages as follows: if the number of arguments or their type is wrong, print "Error: invalid arguments for charge command"; if the card does not exists, print "Error: card no no does not exist"; if the card's password is wrong, print "Error: password passwd incorrect"; and if calls to the given zone are not allowed for this card, print "Error: card no not allowed for zone callZone"; and if the card's balance is not sufficient to cover the call, print "Error: card no limit for zone callZone is XX minutes".

The SuperPhoneCardInc uses another class CardTable to manage a table of PhoneCards. An incomplete definition for the CardTable class is available here in CardTable.java. You must also fill out the missing parts of the definition of CardTable, i.e. the public PhoneCard get(long no) method that returns the first phone card in the table whose number matches the argument, or null if there is no card with this number.

Make sure your code compiles and runs under Java SE Development Kit 8 or above. Once you have implemented and thoroughly tested all of these classes, run your application on the test data file testsInput.txt and save your output in the file testsOutput.txt. You can do this by redirecting the standard input stream and standard output stream with the command:

java -ea SuperPhoneCardInc < testsInput.txt > testsOutput.txt

Then submit a zipped archive with all your source code files (containing PhoneCard.java, SuperNA10Card, Global10Card, Global25Card, SuperPhoneCardInc.java, CardTable.java, and CallZone.java) and testsOutput.txt by the deadline as specified above. The test output file corresponding to the given testsInput.txt is here. Note that we are going to use a different input file to test your program.

Solutions

Expert Solution

Hi,

Please find the classes below:

//////////////////

import java.text.DecimalFormat;

//PhoneCard.java
public abstract class PhoneCard {
   protected long number;
   protected int password;
   protected double balance;
   protected double costPerMinute;

   //PhoneCard constructor
   public PhoneCard (long no, int passwd, double bal){
       //(precondition: no and passwd must be positive);
       assert no >= 0;
       assert passwd >=0;
       this.number=no;
       this.password=passwd;
       this.balance=bal;
   }

   //an accessor returning the card number;
   public long getNumber(){
       return number;
   }

   //an accessor returning the card password;
   public int getPassword(){
       return password;
   }

   //an accessor returning the card balance;
   public double getBalance(){
       return balance;
   }

   // a mutator to set the card balance;
   public void setBalance(double bal){
       this.balance = bal;
   }
  
   // returns true if a call is allowed for the card;
   public abstract boolean allowed(String zone);
  
   //returns the cost per minute
   public abstract double costPerMin(String zone);
  
   //returns the maximum number of minutes
   public int getLimit(String zone) {
       double maximumMinutes = balance/costPerMin(zone);
         
        return (int)maximumMinutes;
   }

   // returns the cost of the call with the given number of minutes to the card
   public boolean charge(int minutes, String zone) {
       double charge = costPerMin(zone)*minutes;
            if(charge< balance){
              balance=balance-charge;
            
             return true;
            }
            else
              return false;
   }
  
   // deducts the appropriate weekly fees from the card's balance
   public abstract void deductWeeklyFee();
  
   //toString() method
   public String toString(){
       DecimalFormat df = new DecimalFormat();
       df.setMaximumFractionDigits(2);
       return "Card Number ="+ number + " , Balance= " + df.format(balance);
   }
}

///////////////////

//SuperNA10Card.java
public class SuperNA10Card extends PhoneCard {

   protected final double costPerMinuteCan = 0.05;
   protected final double costPerMinuteUsa = 0.10;
   protected final double weeklyFee=0.50;

   public SuperNA10Card(long no, int passwd) {
       super(no, passwd, 10.0);
   }

   @Override
   public boolean allowed(String zone) {
       if(zone.equals(CallZone.CANADA.toString()) || zone.equals(CallZone.USA.toString())){
           return true;
       }
       else {
           return false;
       }
   }

   @Override
   public double costPerMin(String zone) {
       double costPerMinute = 0.0;
       if(zone.equals(CallZone.CANADA)){
           costPerMinute = costPerMinuteCan;
       }
       if (zone.equals(CallZone.USA)){
           costPerMinute = costPerMinuteUsa;
       }
       return costPerMinute;
   }

   @Override
   public void deductWeeklyFee() {
       balance = balance-weeklyFee;
       if(weeklyFee < balance){
           balance = balance-weeklyFee;
       }
       else{
           balance = 0.00;
       }
   }
}


/////////////////////////

//CallZone.java
public enum CallZone
{ CANADA, USA, EUROPE, ASIA, AUSTRALIANZ, LATINAMERICA, AFRICA;

   public static boolean isValidZone(String zone)
   { if(CANADA.toString().equals(zone) ||
           USA.toString().equals(zone) ||
           EUROPE.toString().equals(zone) ||
           ASIA.toString().equals(zone) ||
           AUSTRALIANZ.toString().equals(zone) ||
           LATINAMERICA.toString().equals(zone) ||
           AFRICA.toString().equals(zone))
   { return true;
   }
   else
   { return false;
   }
   }

   public static CallZone convertToZone(String zone)
   { if(CANADA.toString().equals(zone))
   { return CANADA;
   }
   else if(USA.toString().equals(zone))
   { return USA;
   }
   else if(EUROPE.toString().equals(zone))
   { return EUROPE;
   }
   else if(ASIA.toString().equals(zone))
   { return ASIA;
   }
   else if(AUSTRALIANZ.toString().equals(zone))
   { return AUSTRALIANZ;
   }
   else if(LATINAMERICA.toString().equals(zone))
   { return LATINAMERICA;
   }
   else
   { assert AFRICA.toString().equals(zone);
   return AFRICA;
   }
   }
}


//////////////////////////

//Global10Card.java
public class Global10Card extends PhoneCard {

   protected final double costPerMinuteCan = 0.07;
   protected final double costPerMinuteUsa = 0.15;
   protected final double costPerMinuteEuro = 0.30;
   protected final double costPerMinuteAsia = 0.60;
   protected final double costPerMinuteAfrica = 0.60;
   protected final double costPerMinuteAusNz = 0.45;
   protected final double costPerMinuteLatin = 0.45;
   protected final double weeklyFee=1.00;

   public Global10Card(long no, int passwd, double bal) {
       super(no, passwd, 10.0);
   }

   @Override
   public boolean allowed(String zone) {
       if(zone==CallZone.CANADA.toString() || zone==CallZone.USA.toString() ||
               zone==CallZone.EUROPE.toString() || zone==CallZone.ASIA.toString() ||
               zone==CallZone.AFRICA.toString() || zone==CallZone.LATINAMERICA.toString()
               || zone==CallZone.AUSTRALIANZ.toString()){
           return true;
       }
       else {
           return false;
       }
   }

   @Override
   public double costPerMin(String zone) {
       if(zone.equals(CallZone.CANADA)){
           costPerMinute = costPerMinuteCan;
       }
       if (zone.equals(CallZone.USA)){
           costPerMinute = costPerMinuteUsa;
       }
       if (zone.equals(CallZone.EUROPE)){
           costPerMinute=costPerMinuteEuro;
       }
       if (zone.equals(CallZone.ASIA)){
           costPerMinute = costPerMinuteAsia;
       }
       if (zone.equals(CallZone.AFRICA)){
           costPerMinute = costPerMinuteAfrica;
       }
       if (zone.equals(CallZone.LATINAMERICA)){
           costPerMinute = costPerMinuteLatin;
       }
       if (zone.equals(CallZone.AUSTRALIANZ)){
           costPerMinute = costPerMinuteAusNz;
       }
       return costPerMinute;
   }

   @Override
   public void deductWeeklyFee() {
       balance = balance-weeklyFee;
       if(weeklyFee < balance){
           balance = balance-weeklyFee;
       }
       else{
           balance = 0.00;
       }
   }

}

////////////////////////

Screenshot:

------------------------------------------------------------------


Let me know if you need more help on this.

Hope this helps.


Related Solutions

Since you will be using zip or compressed files; Explain how to zip (compress) and unzip...
Since you will be using zip or compressed files; Explain how to zip (compress) and unzip (uncompress/extract) files. How to extract or uncompress files into a folder. Why do you need to uncompress/extract the files? The importance of knowing about zip/compressed files in digital forensics.
Download the following zip file Bag and complete the program. You will need to complete files...
Download the following zip file Bag and complete the program. You will need to complete files MyBag and . MyBag uses the java API class ArrayList as the underline data structure. Using the class variables given, complete the methods of the MyBag class. In your BagHand class you should set appropriate default values for select class variables in the constructor in the add method, use the MyBag object to add a PlayingCard type to the bag and find and set...
JAVA Start with the SelectionSort class in the zip file attached to this item. Keep the...
JAVA Start with the SelectionSort class in the zip file attached to this item. Keep the name SelectionSort, and add a main method to it. Modify the selectionSort method to have two counters, one for the number of comparisons, and one for the number of data swaps. Each time two data elements are compared (regardless of whether the items are in the correct order—we're interested in that a comparison is being done at all), increment the comparison counter. Each time...
Write a program that reads a Java source file and produces an index of all identifiers...
Write a program that reads a Java source file and produces an index of all identifiers in the file. For each identifier, print all lines in which it occurs. For simplicity, we will consider each string consisting only of letters, numbers, and underscores an identifer. Declare a Scanner in for reading from the source file and call in.useDelimiter("[^AZa-z0-9_]+"). Then each call to next returns an identifier. Java. Explain logic used lease.
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
I NEED IT PRINT IN THIS FORM JAVA How is a Java source file executed? 1....
I NEED IT PRINT IN THIS FORM JAVA How is a Java source file executed? 1. It is compiled and runs natively 2. It is interpreted but not compiled 3. It is compiled into byte code and executed by the Java Virtual Machine 4. It is magic Please enter your answer 1 Sorry that answer is not correct. The correct answer is 3 Correct output: How is a Java source file executed? 1. It is compiled and runs natively 2....
(c++)You will be given a data file containing data for 10 students. The format is as...
(c++)You will be given a data file containing data for 10 students. The format is as follows - grades are double precision numbers: Line 1: Header Information Line 2: Student Full name Line 3: Student ID Line 4: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5 Line 5: Student Full name Line 6: Student ID Line 7: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5 Line 8: Student Full name Line 9: Student ID Line 10: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5 Etc. Read the data into...
"You will need to compile each Java source file (provided below) separately, in its own file...
"You will need to compile each Java source file (provided below) separately, in its own file since they are public classes, to create a .class file for each, ideally keeping them all in the same directory." My class is asking me to do this and i am not understanding how to do it. I use JGRASP for all of my coding and i can't find a tutorial on how to do it. I just need a step by step tutorial...
In JAVA Write a brief program that writes your name to a file in text format...
In JAVA Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
Compilers and Assemblers translate each source file individually to generate object code files. Hence the object...
Compilers and Assemblers translate each source file individually to generate object code files. Hence the object files need to be linked together before they can be executed. Theoretically, however, it is possible to skip the linking step and directly have compilers generate the final executable file. What would be the downside of taking the latter approach
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT