Modify your Account superclass so that it is abstract and contains the abstract
ID: 3827892 • Letter: M
Question
Modify your Account superclass so that it is abstract and contains the abstract method, computeSales(). If you added default behavior in the earlier task for computeSales(), remove this default behavior.
Leave your implementation of computeSales() in the subclasses as:
Supplies = office supplies sold dollar amount + books sold dollar amount + apparel sold dollar amount
Services = number of hours * rate per hour.
Paper = number of pounds * price per pound
Update your design document so that the UML class diagrams reflect the abstract class and abstract method.
Modify your application so that it polymorphically processes any account objects created. Store each account object created into an array of type Account. For each element in this array, call the computeSales() method and display the results. Use the example of polymorphically processing employee objects in Chapter 10 as inspiration.
Test your application and verify your results. Take screenshots to demonstrate that your application works.
Intermediate-level Java programming should be demonstrated in your application:
There should be implemented constructors for each class.
The toString() method should be overridden to provide readable string representation of each object.
Getters and setters need to be implemented to enforce data hiding.
Code should be fully commented.
Program flow should be logical.
Behavior should be encapsulated into methods avoiding all encompassing large main() methods.
Projects should be developed in NetBeans and zipped prior to submission.
Code should compile and run free of exceptions, indicating that debugging tools were used to eliminate any run time errors.
Here is my code so far, I need it to be modified to the above standards. Thanks.
import java.util.Scanner; // java utility package is imported
public class Main // main class for defineing main method
{
public static void main(String []args)// Main method
{
ServiceAccount ser = new ServiceAccount(); // object created for class ServiceAccount
System.out.println(ser.Calculate()); // Calculate method is called for ser object
SuppliesAccount sup = new SuppliesAccount(); // object created for class ServiceAccount
System.out.println(sup.Calculate()); //Calculate method is called for sup object
Paper par = new Paper(); //object for paper class is created
System.out.println(par.Calculate()); // Calculate method is called for par object
}
}
class Accounts // clas account is defined
{
// variables are declared
int AccountID; // accund id variable
float Calculate() // calculate method is defined
{return 0.0f;} //to return the values
public String toString() // toString method is defined
{ return "x";} // return the values
}
class ServiceAccount extends Accounts// class ServiceAccount inherits the Account class
{
public float Calculate() // Calculate method from the parent class is defined
{
Scanner sc = new Scanner(System.in); // Scanner class object is created
System.out.print(" Enter number of hours : "); // print statement for printing the no. of //hours
float var1 = sc.nextFloat();//for user input for no. of hours
System.out.print(" Enter rate per hour : "); // print statement to enter the rate of hours
float var2 = sc.nextFloat();// user input for no. of hours
return (var1*var2); // return the value
}
}
class SuppliesAccount extends Accounts // again class Accounts is inherited by class //SuppliesAccount
{
public float Calculate()// method from the parent class is defined
{
Scanner sc = new Scanner(System.in);// Scanner class object is created
// print statement for entering the sold dollar amount
System.out.print(" Enter the Office SuppliesAccount sold dollar amount : ");
float var1 = sc.nextFloat();// defined for user input
System.out.print(" Enter the book sold dollar amount : "); // print statement to enter the //amount for the book sold
float var2 = sc.nextFloat();// defined for user input
System.out.print(" Enter the apparel sold dollar amount : ");// print statement to print the //sold dollar amount
float var3 = sc.nextFloat(); // for user input
return (var1+var2+var3); // return the result as the product of all the three variable
}
}
class Paper extends Accounts// Class Account is again inherited by class paper
{
public float Calculate() //
{
Scanner sc = new Scanner(System.in); // Scanner class object
System.out.print(" Enter number of pounds : "); // print statement for printing the no. of //pounds
float var1 = sc.nextFloat();
System.out.print(" Enter price per pound : ");// Print satement to print the price per pound.
float var2 = sc.nextFloat();
return (var1*var2); // return the vaule as the product of both
}
}
Explanation / Answer
import java.util.Scanner; // java utility package is imported
public class Main // main class for defineing main method
{
public static void main(String []args)// Main method
{
Accounts[] account = new Accounts[3];//array of 3 objects of Accounts
Scanner sc = new Scanner(System.in); // Scanner class object is created
System.out.print(" Enter number of hours : "); // print statement for printing the no. of //hours
float hours = sc.nextFloat();//for user input for no. of hours
System.out.print(" Enter rate per hour : "); // print statement to enter the rate of hours
float ratePerHour = sc.nextFloat();// user input for no. of hours
account[0] = new ServiceAccount(1001,hours,ratePerHour);
System.out.print(" Enter the Office Supplies sold dollar amount : ");
float officeSuppliesSold= sc.nextFloat();// defined for user input
System.out.print(" Enter the book sold dollar amount : "); // print statement to enter the //amount for the book sold
float booksSold = sc.nextFloat();// defined for user input
System.out.print(" Enter the apparel sold dollar amount : ");// print statement to print the //sold dollar amount
float apparelSold = sc.nextFloat(); // for user input
account[1] = new SuppliesAccount(1002,officeSuppliesSold,booksSold,apparelSold); // object created for class ServiceAccount
System.out.print(" Enter number of pounds : "); // print statement for printing the no. of //pounds
float numPounds = sc.nextFloat();
System.out.print(" Enter price per pound : ");// Print satement to print the price per pound.
float pricePerPound = sc.nextFloat();
account[2]= new Paper(1003,numPounds,pricePerPound); //object for paper class is created
for(int i=0;i<3;i++)
{
System.out.println("Account : "+account[i]);
System.out.println("Sales : "+account[i].computeSales());
}
}
}
abstract class Accounts // class account is defined
{
// variables are declared
int AccountID; // accund id variable
public Accounts(){}
public Accounts(int AccountID)
{
this.AccountID = AccountID;
}
public String toString() // toString method is defined
{ return "x";} // return the values
public abstract float computeSales();
}
class ServiceAccount extends Accounts// class ServiceAccount inherits the Account class
{
private float hours;
private float ratePerHour;
public ServiceAccount(){}
public ServiceAccount(int AccountID,float hours,float ratePerHour)
{
super(AccountID);
this.hours = hours;
this.ratePerHour = ratePerHour;
}
public float computeSales() // Calculate method from the parent class is defined
{
return (hours*ratePerHour); // return the value
}
public String toString()
{
return " Hours : "+hours+" Rate per Hour : "+ratePerHour;
}
}
class SuppliesAccount extends Accounts // again class Accounts is inherited by class //SuppliesAccount
{
private float officeSuppliesSold;
private float booksSold;
private float apparelSold;
public SuppliesAccount(){}
public SuppliesAccount(int AccountID,float officeSuppliesSold,float booksSold,float apparelSold)
{
super(AccountID);
this.officeSuppliesSold = officeSuppliesSold;
this.booksSold = booksSold;
this.apparelSold = apparelSold;
}
public float computeSales()// method from the parent class is defined
{
return (officeSuppliesSold+booksSold+apparelSold); // return the result as the product of all the three variable
}
public String toString()
{
return " Office Supplies sold : "+officeSuppliesSold+" Books sold : "+booksSold+" Apparel Sold : "+apparelSold;
}
}
class Paper extends Accounts// Class Account is again inherited by class paper
{
private float numPounds;
private float pricePerPound;
public Paper(){}
public Paper(int AccountID,float numPounds,float pricePerPound)
{
super(AccountID);
this.numPounds = numPounds;
this.pricePerPound = pricePerPound;
}
public float computeSales() //
{
return (numPounds*pricePerPound); // return the vaule as the product of both
}
public String toString()
{
return " Number of pounds : "+numPounds+" Price per pound : "+pricePerPound;
}
}
Output:
Enter number of hours : 10
Enter rate per hour : 3.4
Enter the Office Supplies sold dollar amount : 456.7
Enter the book sold dollar amount : 344.2
Enter the apparel sold dollar amount : 217.5
Enter number of pounds : 100.9
Enter price per pound : 56.7
Account :
Hours : 10.0 Rate per Hour : 3.4
Sales : 34.0
Account :
Office Supplies sold : 456.7 Books sold : 344.2 Apparel Sold : 217.5
Sales : 1018.4
Account :
Number of pounds : 100.9 Price per pound : 56.7
Sales : 5721.0303
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.