Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 5, Problem 8PP

Explanation of Solution

a.

Method heading for each method:

  • The method heading is “public void sellTickets(int number)” that takes a parameter “number” of integer type and update the number of tickets sold and total sales.
  • The method heading is “public void phoneSalesOver()” that takes no parameters and sales can be made at the venue only...

Explanation of Solution

b.

Preconditions and postconditions of each method:

  • Precondition and postcondition of “public void sellTickets(int number)” method.
    • Precondition: checks whether the input is positive and less than or equal to number of unsold tickets.
    • Postcondition: update the number of tickets sold and total sales.
  • Precondition and postcondition of “public void phoneSalesOver()” method.
    • Precondition: None.
    • Postcondition: This method is used to sell the tickets only at the venue.
  • Precondition and postcondition of “public int getTicketsSold()” method...

Explanation of Solution

c.

Test class for some Java statement:

//Create an object for ConcertPromoter

ConcertPromoter con = new ConcertPromoter();

//Call initialize () method

con.initialize("The Ducks", 100, 10.00, 12.00);

//Display the header message

System.out.println("Concert Promoter Sales Program started");

//Create an object for Scanner class

Scanner reader = new Scanner(System.in);

//Declare and initialize the flag variable

boolean done = false;

//Loop executes until the true

while (!done)

{

/*Call phoneSalesOnly() method to check whether sales through phone. */

    if (con.phoneSalesOnly())

        //Display the Sales details

System.out.println("Sell tickets (S), Change to venue sales (V), Finish selling (F)");

  /*Otherwise, display the modified sales details. */

    else

System.out.println("Sell tickets (S), Finish selling (F)");

    //Read the input from user

    String response = reader.next();

    //Check whether the input is equal to "F"

    if (response...

Explanation of Solution

d.

Implementation of class:

ConcertPromoter.java:

//Import the java package

import java.util.Scanner;

//Define the class

public class ConcertPromoter

{

    //Declare the required variables

    private String bandName;

    private int capacity;

    private int ticketsSold;

    private double costPhone;

    private double costVenue;

    private double totalSales;

    private boolean salesAreByPhone;

    //Define the sellTickets() method

    public void sellTickets(int number)

    {

/*Check whether the number is inbetween "0" and ticket left. */

        if (number > 0 && number <= getTicketsLeft())

        {

            //Add the ticket sold number

            ticketsSold += number;

/*Call getSaleCost() method to calculate the total sales...

Explanation of Solution

e.

List of extra methods and attributes needed for this implementation:

There is no extra attributes are needed for this implementation.

The extra methods that were used apart from the mentioned methods are,

  • public void initialize() method:
    • This method is used to initialize the declared variables.
  • public boolean phoneSalesOnly() method:
    • This method is used to return the true when user making sales through the phone. Otherwise, return false.
  • public String getSalesReport() method:
    • This method is used to return the sales report of sold tickets count and their amount...

Explanation of Solution

f.

Implementation of class:

ConcertPromoter.java:

//Import the java package

import java.util.Scanner;

//Define the class

public class ConcertPromoter

{

    //Declare the required variables

    private String bandName;

    private int capacity;

    private int ticketsSold;

    private double costPhone;

    private double costVenue;

    private double totalSales;

    private boolean salesAreByPhone;

    //Define the initialize() method

public void initialize(String band, int max, double costOverPhone, double costAtVenue)

    {

        //Initialize the declared variables

        bandName = band;

        capacity = max;

        ticketsSold = 0;

        costPhone = costOverPhone;

        costVenue = costAtVenue;

        totalSales = 0.0;

        salesAreByPhone = true;

    }

    //Define the doTicketSale() method

    public void doTicketSale()

    {

        //Read the maximum ticket to sell from user

System.out.println("How many tickets to sell? (Maximum " + getTicketsLeft() + ")");

        Scanner reader = new Scanner(System.in);

        int toSell = reader.nextInt();

        //Call sellTickets() method to initialize the flag

        boolean saleCompleted = sellTickets(toSell);

        //Check whether the flag is "true"

        if (saleCompleted)

/*Call getSaleCost() method to calculate the ticket cost and then display it. */

System.out.println("The cost of the tickets is " + getSaleCost(toSell));

        //Otherwise, display the error message

        else

System.out.println("Sorry, unable to complete that sale");

    }

    //Define the sellTickets() method

    public boolean sellTickets(int number)

    {

/*Check whether the number is in between "0" and ticket left. */

        if (number > 0 && number <= getTicketsLeft())

        {

            //Add the ticket sold count

            ticketsSold += number;

/*Call getSaleCost() method to calculate the total sales. */

            totalSales += getSaleCost(number);

            //Return the true

            return true;

        }

        //Otherwise, return the false

        else

            return false;

    }

    //Define the getSaleCost() method

    public double getSaleCost(int number)

    {

        //Declare the variable

        double saleCost = number;

        //Check whether sales by phone is "true"

        if (salesAreByPhone)

            //Calculate the sale cost with cost phone

            saleCost *= costPhone;

/*Otherwise, calculate the sale cost with cost venue. */

        else

            saleCost *= costVenue;

        //Return the sale cost

        return saleCost;

    }

    //Define the phoneSalesOver() method

    public void phoneSalesOver()

    {

        //Set the flag as "false"

        salesAreByPhone = false;

    }

    //Define the getTicketsSold() method

    public int getTicketsSold()

    {

        //Return the ticket sold

        return ticketsSold;

    }

    //Define the getTicketsLeft() method

    public int getTicketsLeft()

    {

        //Return the remaining ticket

        return capacity - ticketsSold;

    }

    //Define the getTotalSales() method

    public double getTotalSales()

    {

        //Return the total sales

        return totalSales;

    }

    //Define the phoneSalesOnly() method

    public boolean phoneSalesOnly()

    {

        //Return the phone sales

        return salesAreByPhone;

    }

    //Define the getSalesReport() method

    public String getSalesReport()

    {

        //Return the sales report

return "Sales for " + bandName + ":  Tickets sold: " + ticketsSold + " for " + totalSales;

    }

    //Define the main() method

    public static void main(String[] args)

    {

        //Create an object for ConcertPromoter

        ConcertPromoter con = new ConcertPromoter();

        //Call initialize () method

con...

Blurred answer
Students have asked these similar questions
Need Java method please. Thank you.
Need Java method please. Thank you.
3. Write two nested loops to generate the following output. (Note: There is one space between each number, and any extra line shown is intentional.) 12 10 8 6 18 15 12 24 20 30 2 3 3 6 48 12 5 10 15 20 6 12 18 24 30

Chapter 5 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
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
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
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
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
6 Stages of UI Design; Author: DesignerUp;https://www.youtube.com/watch?v=_6Tl2_eM0DE;License: Standard Youtube License