Question

In: Computer Science

package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...

package compstore;
public class Desktop{
    private String brand;
    private float price;

    public Desktop(String brand, float price) {
        this.brand = brand;
        this.price = price;
    }

    public String getBrand() {        return brand;     }

    public float getPrice() {        return price;    }

}
package compstore;

public class DeskTopDeals{
    // assume proper variables and other methods are here

    public void dealOfTheDay(Desktop[] items, int numItems){

        /****************************
         *  your code would go here *
         ****************************/

    }
}

Given the above Desktop class, write code that should go in the area marked * your code would go here * to complete the “dealOfTheDay(Desktop[] items, int numItems)” method. The output should be printed to the console (screen). There are numItems Desktops in the array Desktop[] items. These items are in locations 0 to numItems - 1 of the array.

Each desktop item should have its brand and price output on a single line. There will be marks for properly right justifying the price and outputting two decimals. As you are printing the Desktop objects, you should keep track of the Desktop object with the lowest price. At the end print the brand name followed by the price, a shipping surcharge of 15% and the total price. For example, if the Desktop array had 3 Desktops, a SkyTech for $300, a Dell for $400, and HP for $500, we would get the following output.

 

Current Deals:

SkyTech 300.00

Dell 400.00

HP           500.00

The deal of the day is SkyTech.

Price: $300.00 Shipping: $45.00 Total: $ 345.00

Solutions

Expert Solution

Program: In this program, we implement the method dealOfTheDay() method in which we display all current deals and then the deal of the day. We format the output using System.out.printf() method and print it on console. To run the method we created the main() method as well in DeskTopDeals class.

Below is the implementation:

Desktop.java:

Code:

package compstore;

public class Desktop {

    // Instance variables
    private String brand;
    private float price;

    // Constructor
    public Desktop(String brand, float price) {
        this.brand = brand;
        this.price = price;
    }

    // Getters
    public String getBrand() {
        return brand;
    }

    public float getPrice() {
        return price;
    }
}

DeskTopDeals.java:

Code:

package compstore;

public class DeskTopDeals {
    // assume proper variables and other methods are here

    // Main method
    public static void main(String[] args) {
        // Create a variable to store array size
        int numItems = 3;

        // Create an array of Desktop type items
        Desktop[] items = new Desktop[numItems];

        // Pass values to items - brand name and brand price
        items[0] = new Desktop("SkyTech", 300);
        items[1] = new Desktop("Dell", 400);
        items[2] = new Desktop("HP", 500);

        // Create an object of this class
        DeskTopDeals deskTopDeals = new DeskTopDeals();

        // Call dealOfTheDay method, pass items and size
        deskTopDeals.dealOfTheDay(items,numItems);

    }

    public void dealOfTheDay(Desktop[] items, int numItems) {

        /****************************
         *  your code would go here *
         ****************************/

        // Create a Desktop class object to store the deal of the day
        Desktop dealOfTheDay = new Desktop("", 0);

        // Create a min variable to compare prices of brands
        float min = Float.MAX_VALUE;


        System.out.println("Current Deals:");

        // Traverse the items array
        for (int i = 0; i < numItems; i++) {

            // Display items
            System.out.printf("%s%10.2f\n",items[i].getBrand(),items[i].getPrice());

            // Check if current price of item is less than min
            if (min > items[i].getPrice()) {
                min = items[i].getPrice();
                dealOfTheDay = items[i];
            }
        }

        // Calculate tax
        float tax = (dealOfTheDay.getPrice() * 15) / 100;

        // Calculate total price of the product by adding price of item with tax
        float total = dealOfTheDay.getPrice() + tax;

        // Print the answer to console
        System.out.println("The deal of the day is " + dealOfTheDay.getBrand());
        System.out.printf("Prices: $%.2f Shipping: $%.2f Total: $%.2f\n", dealOfTheDay.getPrice(), tax, total);
    }
}

Output:

#Please ask for any doubts. Thanks.


Related Solutions

package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price)...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price) { this.contractor = contractor; this.price = price; } public String getContractor() { return contractor; } public float getPrice() { return price; } } package construction; public class ContractorBids{ // assume proper variables and other methods are here public void winningBid(Bid[] bids, int numBids){ /**************************** * your code would go here * ****************************/ } } You are doing renovations on your building, and multiple contractors...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d,...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d, int y) { dealerPrice = d; year = y; } public float getDealerPrice() { return dealerPrice; } public int getYear() { return year; } public abstract float getStickerPrice(); } In the space below write a concrete class Car in the dealership package that is a subclass of Vehicle class given above. You will make two constructors, both of which must call the superclass constructor....
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList;...
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList; public Postfix(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } public void reset(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } private boolean isOperator(char c) { if(c=='-'||c=='+'||c=='*'||c=='/') return true; return false; } private boolean isParenthesis(char c) { if(c == '(' || c==')') return true;...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
What is a package? What package is the Scanner class in? What package is the String class in?
What is a package? What package is the Scanner class in? What package is the String class in? 
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>();...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>(); } public void addEdge(String v, String w) { // Put v in w's SET and w in v's SET. if (!st.contains(v)) st.put(v, new SET<String>()); if (!st.contains(w)) st.put(w, new SET<String>()); st.get(v).add(w); st.get(w).add(v); } public Iterable<String> adjacentTo(String v) { return st.get(v); } public Iterable<String> vertices() { return st.keys(); } // See Exercises 4.5.1-4 for V(), E(), degree(), // hasVertex(), and hasEdge(). public static void main(String[] args)...
public class Person { private String name; public Person() { name = "No name yet"; }...
public class Person { private String name; public Person() { name = "No name yet"; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return this.name.equalsIgnoreCase(otherPerson.name); } } 2- Write a Program Patient. Java Class that extends Person to include  Social security Gender  Appropriate construtors, accessors, and mutators....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT