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
Create an original network topology consisting of at least seven routers and twelve links, assigning arbitrary positive weights to each link. Using this topology, apply Dijkstra's Link-State Algorithm to compute the shortest paths from a source router of your choice to all other routers in the network. Your topology must be entirely your own design and should not resemble any examples from the textbook, lecture slides, or other students' work. Al-generated topologies are not permitted. Create
x3003 x3008 1110 0000 0000 1100 1110 0010 0001 0000 0101 0100 1010 0000 x3004 0010 0100 0001 0011 x3005 0110 0110 0000 0000 X3006 0110 1000 0100 0000 x3007 0001 0110 1100 0100 0111 0110 0000 What does the following LC-3 program do? Trace Step by Step, SHOW ALL YOUR WORK. x3001 x3002 0000 x3009 0001 0000 0010 0001 X300A 0001 0010 0110 0001 x300B 0001 0100 1011 1111 x300C 0000 0011 1111 1000 X300D 1111 0000 0010 0101 x300E 0000 0000 0000 0101 x300F 0000 0000 0000 0100 x3010 0000 0000 0000 0011 x3011 0000 0000 0000 0110 x3012 0000 0000 0000 0010 x3013 x3014 0000 0000 0000 0000 0000 0100 0000 0111 x3015 0000 0000 0000 0110 x3016 0000 0000 0000 1000 x3017 0000 0000 0000 0111 x3018 0000 0000 0000 0101
2) Assume a local area network has four host computers (h1, h2, h3 & h4) and they are connected to the internet through a NAT router (s1). The host computers use private IP address space: 192.168.2/24. Each host is trying to establish 2 TCP connections to a remote webserver through the NAT router. The IP address of the webserver is: 130.12.11.9. Now do the following: 1 a. Assign IP addresses to the interfaces of the hosts and the router. For the router, assign arbitrary addresses. List these addresses. b. Now create a NAT translation table as taught in the class for all TCP connections. Assign arbitrary port numbers as required.

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