Question

In: Computer Science

Decision Structures in Java       You are the owner of a company that sells designer desks....

Decision Structures in Java

      You are the owner of a company that sells designer desks. Your program will create a user friendly interface (company name, labeled prompts, neat output) that calculates the cost of constructing desks for customers. After the customer enters the appropriate prompted values, the program will calculate the cost of the desk based on the specifications outlined below. The program should allow the user to order a second desk if desired.

  1. Define a java instance class as described below. Save the class definition in a file with an appropriate filename.

Class Name:         Desk

Instance Variables: length – float (representing inches)

(Fields)                     width – float (representing inches)

                                 type – char (valid values P, O, or M for Pine, Oak, or Mahogany)

                                 drawers – integer (representing the number of desk drawers)

Static Variable:        count – tracks the number of desks created

Instance Methods: A constructor that accepts no arguments and sets default values as follows: the length to 6, width to 12, Oak type, and 1 drawer

                                 A constructor that accepts 4 arguments, and sets the fields appropriately.

                                 getArea( ) A method to calculate and return the area of the desk.

                                 getType( ) A method to return desk type.

                                 getDrawers( ) A method to return the number of drawers.

                                 toString() Displays the object’s attributes

                                

  1. Create a program class, TestDesk, that contains a main() to test and use the above class. This process should contain methods as follows:

main(): This method should:

  1. Call createDesk() to get the inputs and create a Desk object.
  2. Call calculatePrice() to determine the cost of constructing a desk.
  3. Display the price of the desk along with the attribute values.
  4. Ask the user if they want another desk.
  5. If the answer is No, display the total price of the desk
  6. If the answer is Yes, implement steps 1 and 2 again (without a loop), then display:
  • the number of desks created (through a variable)
  • total cost of the desk(s), and
  • average desk cost.
  1. The program should then end.

Class methods:      createDesk( ) A function to prompt the user for inputs and accept the four (4) input values needed to create an instance of a Desk. It instantiates a Desk object and returns the object to the calling method.

                              CalculatePrice() This function has a Desk object as its formal argument and returns the price of the desk to be determined as follows:

  • The basic cost of a frame is $12.50 for P(ine), $26 for O(ak), and $37.75 for M(ahogany).
  • Any desk with an area greater than 75 square inches is considered large. Add $18.25 to the cost for a large desk.
  • The basic desk comes with one (1) drawer. There is an extra charge of $9.50 for each additional drawer. Desk’s are charged for a maximum of six (6) drawers. Any desk with more than 6 drawers is charged for 6 drawers.

Additional Notes:

  • Be sure to efficiently implement all 3 decision structures discussed in class
  • Implement sound program design so as to avoid duplicated code
  • An invalid wood type entry should default to Oak
  • Your program should accommodate upper and lower case user inputs
  • Dollar values should be displayed to 2 decimal places and labeled

Solutions

Expert Solution

Program: In this program, we implement the Desk class and TestDesk class. TestDesk class is the driver class, which has a main() method, createDesk() method and CalculatePrice() method. We take user inputs for desk specifications and then show the user with the total price, desk properties and also ask if the user wants to buy another desk or not.

Below is the implementation:

Code:

public class Desk {
    // Instance variables
    private float length;
    private float width;
    private char type;
    private int drawer;
    static int count = 0;

    // Default Constructor
    public Desk() {
        length = 6;
        width = 12;
        type = 'O';
        drawer = 1;
        count++;
    }

    // Parameterised Constructor
    public Desk(float length, float width, char type, int drawer) {
        this.length = length;
        this.width = width;
        this.type = type;
        this.drawer = drawer;
        count++;
    }

    // Function to get area
    public double getArea() {
        return length * width;
    }

    // Function to return type
    public char getType() {
        return type;
    }

    // Function to return drawers
    public int getDrawers() {
        return drawer;
    }

    // Function to display desk info
    @Override
    public String toString() {
        String deskType = "";
        if(type == 'O'){
            deskType = "Oak";
        }
        else if(type == 'P'){
            deskType = "Pine";
        }
        else{
            deskType = "Mahogany";
        }
        return "Desk Info: \n" + "Length: " + length + " inches\nWidth: "
                + width + " inches\nType: " + deskType + "\nDrawers: " + drawer;
    }
}

TestDesk.java:

Code:

import java.util.Scanner;

public class TestDesk {
   // Scanner class instance
    private static Scanner scanner;

    // Main()
    public static void main(String[] args) {
        scanner = new Scanner(System.in);

        // Create desk
        Desk desk = createDesk();

        // Store price
        double price = CalculatePrice(desk);

        // Display desk info and price
        System.out.println(desk);
        System.out.println(String.format("Total cost of desk: $%.2f", price));

        // Prompt user to buy another desk
        System.out.println("Do you want to buy another desk? (y/n)");
        String input = scanner.next();

        // If yes, then repeat above steps
        if (input.toLowerCase().equals("y")) {
            desk = createDesk();
            double price2 = CalculatePrice(desk);
            price += price2;
            System.out.println("Total number of desks purchased: " + desk.count);
            System.out.println(String.format("Total cost of desks: $%.2f", price));
            System.out.println(String.format("Average cost of desks: $%.2f",price / 2));
        }

        // End program
        System.out.println("Goodbye!");


    }

    // Method to create a desk
    private static Desk createDesk() {
        scanner = new Scanner(System.in);

        // Prompt user to enter length, width, type, drawers
        System.out.print("Please enter the length of desk: ");
        float length = scanner.nextFloat();
        System.out.print("Please enter the width of desk: ");
        float width = scanner.nextFloat();
        System.out.print("Please enter the type of desk: ");
        char type = scanner.next().charAt(0);

        // convert type to uppercase
        Character.toUpperCase(type);

        // Validate type
        if (type != 'P' && type != 'O' && type != 'M') {
            type = 'O';
        }
        System.out.print("Please enter the number of drawers of desk: ");
        int drawers = scanner.nextInt();

        // Create a Desk class object with all the values
        Desk desk = new Desk(length, width, type, drawers);

        // Return the desk object
        return desk;
    }

    // Method to calculate price of a desk
    public static double CalculatePrice(Desk desk) {

        // Initialise price
        double price = 0;

        // Store and check type of desk, update price accordingly
        char type = desk.getType();
        if (type == 'P') {
            price += 12.50;
        } else if (type == 'O') {
            price += 26;
        } else {
            price += 37.75;
        }

        // Store area of desk
        double area = desk.getArea();

        // If area is greater than 75, update price
        if (area > 75) {
            price += 18.25;
        }

        // If total drawers is greater than 6, add price of 6 drawers
        if (desk.getDrawers() > 6) {
            price += 6 * 9.50;
        }

        //else add price of drawers
        else {
            price += desk.getDrawers() * 9.50;
        }

        // Return price
        return price;
    }
}

Output 1:

Output 2:



Related Solutions

You are the owner of a company that produces and sells a new tool at hardware...
You are the owner of a company that produces and sells a new tool at hardware stores. During one month, when 'x' of those new tools were produced and sold, the following were the revenue and cost (in hundreds of dollars).   R(x)= -0.06x2 +13.12x + 65.8 C(x)=3.04x+ 7.6 5.Fill-in the blanks.Be sure to read the following carefully! HINT: do NOT use the TABLE from your graphing calculator, but DO use the graphing capabilities. a)The profit for that month was 79...
Question 1 Ruby Limited manufactures and sells desks. Price and cost data for the company are...
Question 1 Ruby Limited manufactures and sells desks. Price and cost data for the company are provided below: Selling price per unit                                                                        $250 Variable costs per unit Manufacturing Direct materials                                                          $82 Direct labour                                                               $40 Variable manufacturing overhead                            $60                                             Variable selling and administrative costs                            $16 Fixed manufacturing overhead $1,440,000 Fixed selling and administrative costs $2,070,000 Forecasted annual sales $17,500,000                     Annual fixed costs Ruby Limited pays income taxes of 30 percent. Required: What is Ruby Limited’s break‐even point in units?...
The SCO Company sells desks that have a demand of 4 units per month and cost...
The SCO Company sells desks that have a demand of 4 units per month and cost $25 each, the annual carrying charge is 30 percent of the product value, and it costs $15 to place an order. The company is considering installing either a Q or a P system for inventory management. The standard deviation of demand has been 4 units per month, and the replenishment lead time is two months, a 90 percent service level is desired. For Parts...
The TechMech Company produces and sells 6400 modular computer desks per year at a selling price...
The TechMech Company produces and sells 6400 modular computer desks per year at a selling price of $450 each. Its current production equipment, purchased for $1,650,000 and with a five-year useful life, is only two years old. It has a terminal disposal value of $0 and is depreciated on a straight-line basis. The equipment has a current disposal price of $500,000. However, the emergence of a new moulding technology has led TechMech to consider either upgrading or replacing the production...
Assume you are a business owner of a growing company that sells electronic goods, including calculators,...
Assume you are a business owner of a growing company that sells electronic goods, including calculators, MP3 players, computers, etc. You receive an email from a business contact in a country with an emerging market, such as Jamaica. She indicates she may be interested in a large purchase for a school in her country. Assume you know and trust this person and that the business deal is legitimate. However, doing business internationally, particularly in an emerging market, comes with uncertainty....
MARGINAL ANALYSIS - Economics for Business Decision You are a business owner of a firm that...
MARGINAL ANALYSIS - Economics for Business Decision You are a business owner of a firm that services trucks. A customer would like to rent a truck from you for one week, while you service his truck. You must decide whether or not to rent him a truck. You have an extra truck that you will not use for any other purpose during this week. This truck is leased for a full year from another company for $350/ week plus $.50...
The Damico Company produces and sells 5,300 modular computer desks per year at a selling price of $460 each.
The Damico Company produces and sells 5,300 modular computer desks per year at a selling price of $460 each. Its current production equipment, purchased for $1,700,000 and with a five-year useful life, is only two years old. It has a terminal disposal value of SO and is depreciated on a straight-line basis. The equipment has a current disposal price of $550,000. However, the emergence of a new molding technology has led Damico to consider either upgrading or replacing the production...
The TechAide Company produces and sells 6,000 modular computer desks per year at a selling price of $450 each.
The TechAide Company produces and sells 6,000 modular computer desks per year at a selling price of $450 each. Its current production equipment, purchased for $1,350,000 and with a five-year useful life, is only two years old. It has a terminal disposal value of $0 and is depreciated on a straight-line basis. The equipment has a current disposal price of $550,000. However, the emergence of a new molding technology has led TechAide to consider either upgrading or replacing the production...
Which are the three control structures used in java programming. Which of these three control structures...
Which are the three control structures used in java programming. Which of these three control structures is used in every program or application
The C-Gourmet Burger company sells burgers for $4.05 each. The owner of the this company pays...
The C-Gourmet Burger company sells burgers for $4.05 each. The owner of the this company pays workers $192 per day. The following table (Table 22) shows employment and production of burgers per day at this company. Table 22 Labor (number of workers) Quantity (Units of burgers per day) 0 0 1 74 2 136 3 189 4 236 5 270 6 300 Refer to Table 22. To maximize its profit, how many workers would the C-Gourmet Burger company employ? Show...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT