Mindtap Programming, 1 Term (6 Months) Printed Access Card For Farrell's Java Programming, 9th
Mindtap Programming, 1 Term (6 Months) Printed Access Card For Farrell's Java Programming, 9th
9th Edition
ISBN: 9781337397117
Author: FARRELL, Joyce
Publisher: Cengage Learning
bartleby

Concept explainers

bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 9, Problem 7PE

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()...

Blurred answer
Students have asked these similar questions
Management Information Systems
Q2/find the transfer function C/R for the system shown in the figure Re ད
Please original work select a topic related to architectures or infrastructures (Data Lakehouse Architecture). Discussing how you would implement your chosen topic in a data warehouse project Please cite in text references and add weblinks

Chapter 9 Solutions

Mindtap Programming, 1 Term (6 Months) Printed Access Card For Farrell's Java Programming, 9th

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
9.1: What is an Array? - Processing Tutorial; Author: The Coding Train;https://www.youtube.com/watch?v=NptnmWvkbTw;License: Standard Youtube License