
Concept explainers
Explanation of Solution
Program:
File name: “Purchase.java”
//Define a class named Purchase
public class Purchase
{
//Declare the private variables
private int invoiceNumber;
private double saleAmount;
private double tax;
private static final double RATE = 0.05;
//Define a set method that takes the invoice number
public void setInvoiceNumber(int num)
{
//Assign the value
invoiceNumber = num;
}
//Define a set method that takes the amount of sale
public void setSaleAmount(double amt)
{
//Assign the value
saleAmount = amt;
//Compute the sales tax
tax = saleAmount * RATE;
}
//Define a get method that returns the amount of sale
public double getSaleAmount()
{
//Return the value
return saleAmount;
}
//Define a get method that returns the invoice number
public int getInvoiceNumber()
{
//Return the value
return invoiceNumber;
}
/*Define a method to display the invoice number,
amount of sale, and amount of sales tax*/
public void display()
{
//Print the result
System.out.println("Invoice #" + invoiceNumber +
" Amount of sale: $" + saleAmount + " Tax: $" + tax);
}
}
File name: “SortPurchasesArray.java”
//Import necessary header files
import java.util.Scanner;
//Define a class named SortPurchasesArray
public class SortPurchasesArray
{
//Define a main method
public static void main(String[] args)
{
//Declare an array of five Purchase objects
Purchase[] purchases = new Purchase[5];
//Declare the variables
int i;
String message;
char choice;
final char QUIT = 'Z';
int number;
double price;
//Create an object for Scanner class
Scanner keyboard = new Scanner(System.in);
//For loop to be executed until i exceeds 5
for(i = 0; i < purchases.length; ++i)
{
//Prompt the user to enter the invoice number
System.out.print("Enter invoice number >> ");
number = keyboard.nextInt();
//Prompt the user to enter the amount of sale
System.out.print("Enter sale amount >> ");
price = keyboard.nextDouble();
purchases[i] = new Purchase();
//Function call
purchases[i].setInvoiceNumber(number);
purchases[i].setSaleAmount(price);
}
keyboard.nextLine();
/*Prompt the user to enter whether the Purchase objects
should be sorted and displayed in invoice number
order or sale amount order*/
System.out.print("\nSort Purchases by (I)nvoice number, or (S)ale amount? ");
choice = keyboard.nextLine()...

Want to see the full answer?
Check out a sample textbook solution
Chapter 9 Solutions
Java Programming (MindTap Course List)
- Design and draw a high-level "as-is" process diagram that illustrates a current process related to a product or service offered through the SSDCI.gov database.arrow_forwardCompare last-mile connections for connecting homes and businesses to the Internetarrow_forwardExplain wireless networking standardsarrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning




