Question

In: Computer Science

Using NetBeans, Modify your sales application so that it polymorphically processes any account objects that are...

Using NetBeans, Modify your sales application so that it polymorphically processes any account objects that are created. Complete the following:

    Create a 2-item array of type Account.
    Store each account object created into the array.
    For each element in this array, call the calculateSales() method, and use the toString() method to display the results.
    Code should be fully commented.
    Program flow should be logical.

Code before revision:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package inheritance;

/**
*
* @author Aaron Drury
*/
public class Inheritance {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
abstract class Account {
private int accountID;

public Account(int accountID) {
this.accountID = accountID;
}

public int getAccountID() {
return accountID;
}

public void setAccountID(int accountID) {
this.accountID = accountID;
}

public abstract double calculateSales();

@Override
public String toString() {
return "Account [\naccountID: " + accountID + "]";
}

}

class Sales extends Account {
private int numberOfHours;
private double ratePerHour;

public Sales(int accountID, int numberOfHours, double ratePerHour) {
super(accountID);
this.numberOfHours = numberOfHours;
this.ratePerHour = ratePerHour;
}

public int getHours() {
return numberOfHours;
}

public void setfHours(int numberOfHours) {
this.numberOfHours = numberOfHours;
}

public double getRate() {
return ratePerHour;
}

public void setRate(double ratePerHour) {
this.ratePerHour = ratePerHour;
}

@Override
public double calculateSales() {
return numberOfHours * ratePerHour;
}

@Override
public String toString() {
return "Sales [\naccountID: " + super.getAccountID() + "\nnumberOfHours: " + numberOfHours + "\nratePerHour: "
+ ratePerHour + "]";
}
}

class Service extends Account {
private int numberOfItems;
private int pricePerItem;

public Service(int accountID, int numberOfItems, int pricePerItem) {
super(accountID);
this.numberOfItems = numberOfItems;
this.pricePerItem = pricePerItem;
}

public int getNumberOfItems() {
return numberOfItems;
}

public void setNumberOfItems(int numberOfItems) {
this.numberOfItems = numberOfItems;
}

public int getPricePerItem() {
return pricePerItem;
}

public void setPricePerItem(int pricePerItem) {
this.pricePerItem = pricePerItem;
}

@Override
public double calculateSales() {
return numberOfItems * pricePerItem;
}

@Override
public String toString() {
return "Service [\naccountID: " + super.getAccountID() + "\nnumberOfItems: " + numberOfItems
+ "\npricePerItem: " + pricePerItem + "]";
}
}

}
  
}

Solutions

Expert Solution

/*The java program that creates an array of Account type of size,2. Then, instantiate the Sales and Service class objects and set to to the accounts array. Then call the methods in a for loop and display the account details and sales amount on the console output.
* */

//Polymorphism.java
public class Polymorphism
{
   public static void main(String[] args)
   {
       //Create an array of type Account of size,2
       Account[] accounts=new Account[2];
       //Set the objects of the Sales and services to the Account objects
       accounts[0]=new Sales(1234, 40, 10);
       //Set the objects of the Sales and services to the Account objects
       accounts[1]=new Service(5678, 50, 10);
       //Display the account details and sales value on console
       for(int index=0;index<accounts.length;index++)
       {
           //call toString method on accounts object
           System.out.println("Account :"+accounts[index].toString());
           //call calculateSales method
           System.out.println("Sales :"+accounts[index].calculateSales());
       }
   }//end of main method
}//end of class

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

//Account.java
abstract class Account
{
   private int accountID;
   public Account(int accountID)
   {
       this.accountID = accountID;
   }
   public int getAccountID()
   {
       return accountID;
   }
   public void setAccountID(int accountID)
   {
       this.accountID = accountID;
   }
   public abstract double calculateSales();

   public String toString()
   {
       return "Account [\naccountID: " + accountID + "]";
   }
}

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

//Sales.java
class Sales extends Account
{
   private int numberOfHours;
   private double ratePerHour;
   public Sales(int accountID, int numberOfHours, double ratePerHour)
   {
       super(accountID);
       this.numberOfHours = numberOfHours;
       this.ratePerHour = ratePerHour;
   }
   public int getHours()
   {
       return numberOfHours;
   }
   public void setfHours(int numberOfHours)
   {
       this.numberOfHours = numberOfHours;
   }
   public double getRate()
   {
       return ratePerHour;
   }
   public void setRate(double ratePerHour)
   {
       this.ratePerHour = ratePerHour;
   }
   public double calculateSales()
   {
       return numberOfHours * ratePerHour;
   }
   public String toString()
   {
       return "Sales [\naccountID: " + super.getAccountID() + "\nnumberOfHours: " + numberOfHours + "\nratePerHour: "
               + ratePerHour + "]";
   }
}

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

//Service.java
class Service extends Account
{
   private int numberOfItems;
   private int pricePerItem;
   public Service(int accountID, int numberOfItems, int pricePerItem)
   {
       super(accountID);
       this.numberOfItems = numberOfItems;
       this.pricePerItem = pricePerItem;
   }
   public int getNumberOfItems()
   {
       return numberOfItems;
   }
   public void setNumberOfItems(int numberOfItems)
   {
       this.numberOfItems = numberOfItems;
   }
   public int getPricePerItem()
   {
       return pricePerItem;
   }
   public void setPricePerItem(int pricePerItem)
   {
       this.pricePerItem = pricePerItem;
   }
   public double calculateSales()
   {
       return numberOfItems * pricePerItem;
   }
   public String toString()
   {
       return "Service [\naccountID: " + super.getAccountID() + "\nnumberOfItems: " + numberOfItems
               + "\npricePerItem: " + pricePerItem + "]";
   }
}

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

Sample Output:

Account :Sales [
accountID: 1234
numberOfHours: 40
ratePerHour: 10.0]
Sales :400.0
Account :Service [
accountID: 5678
numberOfItems: 50
pricePerItem: 10]
Sales :500.0


Related Solutions

Using Netbeans update the Sales project so the input and the output are done through a...
Using Netbeans update the Sales project so the input and the output are done through a GUI of your choice. The classes design should not be changed, only the code in the test class. public abstract class Account { private int accountId; public Account(int id){ this.accountId=id; } //getters public int getAccountId() { return accountId; } //setters public void setAccountId(int accountId) { } //abstract method to calculate sales public abstract double calculateSales();    //to string method @Override public String toString() {...
CS 206 Visual Programming, Netbeans Understand and modify the attached GCDMethod.java so it can find the...
CS 206 Visual Programming, Netbeans Understand and modify the attached GCDMethod.java so it can find the greatest common divisor of three numbers input by the user.  Hints: modify the gcd() method by adding the third parameter. import java.util.Scanner; public class GCDMethod { /** Main method */ public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in); // Prompt the user to enter two integers System.out.print("Enter first integer: "); int n1 = input.nextInt(); System.out.print("Enter second integer: ");...
Enhance the Future Value application Modify this application so it uses a persistent session to save...
Enhance the Future Value application Modify this application so it uses a persistent session to save the last values entered by the user for 2 weeks.. Index.php -------------------------- <?php     //set default value of variables for initial page load     if (!isset($investment)) { $investment = '10000'; }     if (!isset($interest_rate)) { $interest_rate = '5'; }     if (!isset($years)) { $years = '5'; } ?> <!DOCTYPE html> <html> <head>     <title>Future Value Calculator</title>     <link rel="stylesheet" type="text/css" href="main.css"/> </head> <body>    ...
Modify the following 'MessageBoxes' application so it uses a single action listener for each button. This...
Modify the following 'MessageBoxes' application so it uses a single action listener for each button. This will require you to separate the single action listener logic into multiple listeners, one for each button. Then modify the code to provide additional options to two or more buttons. /* * The source code for this assignment started with * a sample from "Thinking in Java" 3rd ed. page 825 * by Bruce Eckel. I have finished adding the rest of the action...
Using NetBeans IDE, write a JavaFX application that allows theuser to choose insurance options. Use...
Using NetBeans IDE, write a JavaFX application that allows the user to choose insurance options. Use a ToggleGroup to allow the user to select only one of two insurance types—HMO (health maintenance organization) or PPO (preferred provider organization). Use CheckBoxes for dental insurance and vision insurance options; the user can select one option, both options, or neither option. As the user selects each option, display its name and price in a text field; the HMO costs $200 per month, the...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using the Account hierarchy created in the codes below. Create an array of Account references to SavingsAccount and CheckingAccount objects. For each Account in the array, allow the user to specify an amount of money to withdraw from the Account using method Debit and an amount of money to deposit into the Account using method Credit. As you process each Account, determine its type. If...
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first...
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first printing a request for the int, then creating an int x, and using the Scanner to read an int from the user and put it in x, like this: int x = scan.nextInt(); ☑ Next read in two doubles d1 and d2 from the user. This will look a lot like what you did for the int, but the scanner reads doubles using scan.nextDouble();...
Modify Java program to accept a telephone number with any numberof the letters, using the...
Modify Java program to accept a telephone number with any number of the letters, using the if else loop. The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits. Also, modify the program to process as many telephone numbers as the user wants.
What is required so that a typical SaaS application can be used by ANY browser?
What is required so that a typical SaaS application can be used by ANY browser?
Create a python application that inputs, processes and stores student data. Specifications: 1. Your application should...
Create a python application that inputs, processes and stores student data. Specifications: 1. Your application should be able to accept Student data from the user and add the information in a file type of your choice. 2. your application is a menu driven and allow the user to choose from the following menu Menu: 1 – Add students to file 2 – print all the student information 3 – print specific student information using studentID 4 – Exit the program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT