EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
8th Edition
ISBN: 9781305480537
Author: FARRELL
Publisher: CENGAGE LEARNING - CONSIGNMENT
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
This is the question I am stuck on - In the exercises in Chapter 6, you created a class named Purchase. Each Purchase contains an invoice number, amount of sale, amount of sales tax, and several methods. Add get methods for the invoice number and sale amount fields so their values can be used in comparisons. Next, write a program that declares an array of five Purchase objects and prompt a user for their values. Then, in a loop that continues until a user inputs a sentinel value of Z, ask the user whether the Purchase objects should be sorted and displayed in invoice number order or sale amount order. This is the code I have -  public class Purchase {    private int invoiceNumber;    private double saleAmount;    private double tax;    private static final double RATE = 0.05;    public void setInvoiceNumber(int num)    {       invoiceNumber = num;    }    public void setSaleAmount(double amt)    {       saleAmount = amt;       tax = saleAmount * RATE;    }    public double…
This is the question - In the exercises in Chapter 6, you created a class named Purchase. Each Purchase contains an invoice number, amount of sale, amount of sales tax, and several methods. Add get methods for the invoice number and sale amount fields so their values can be used in comparisons. Next, write a program that declares an array of five Purchase objects and prompt a user for their values. Then, in a loop that continues until a user inputs a sentinel value of Z, ask the user whether the Purchase objects should be sorted and displayed in invoice number order or sale amount order. This is the code I have. The programming isn't likeing what I have at all for the area where it says to write code -  public class Purchase {    private int invoiceNumber;    private double saleAmount;    private double tax;    private static final double RATE = 0.05;    public void setInvoiceNumber(int num)    {       invoiceNumber = num;    }    public void setSaleAmount(double amt)    {…
This is the question I am stuck on - In the exercises in Chapter 6, you created a class named Purchase. Each Purchase contains an invoice number, amount of sale, amount of sales tax, and several methods. Now, write a program that declares an array of five Purchase objects and prompt a user for their values. As each Purchase object is created, continuously prompt until the user enters an invoice number between 1000 and 8000 inclusive and a non-negative sale amount. Prompt the user for values for each object and then display all the values. This is the code that I am unsure where to fully start -  public class Purchase {    private int invoiceNumber;    private double saleAmount;    private double tax;    private static final double RATE = 0.05;    public void setInvoiceNumber(int num)    {       invoiceNumber = num;    }    public void setSaleAmount(double amt)    {       saleAmount = amt;       tax = saleAmount * RATE;    }    public double getSaleAmount()    {       return…
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
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
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