【无标题】

This question involves a system to analyze the inventory of different types of flowers sold by a florist. Flower types are represented by the following Flower class.

public class Flower

{

/** Returns the name of the flower (e.g., "rose", "tulip", etc.) */

public String getName()

{  /* implementation not shown */  }

/** Returns the quantity in stock of the flower */

public int getQuantity()

{  /* implementation not shown */  }

/** Sets the quantity in stock of the flower to newQ */

public void setQuantity(int newQ)

{  /* implementation not shown */  }

// There may be instance variables, constructors, and methods that are not shown.

}

In the following FlowerShop class, the array flowerInventory contains information about the flowers sold at a flower shop. You will implement two methods in the FlowerShop class.

public class FlowerShop

{

/** An array of the flowers sold at the flower shop, sorted alphabetically by flower name

  *   Guaranteed to be nonempty and to contain only non-null entries

*/

private Flower[] flowerInventory;

/** Returns a copy of Flower[] arr in which the array elements have been sorted in

  *  order from highest quantity to lowest quantity

*/

public Flower[] sortByQuantity(Flower[] arr)

{  /* implementation not shown */  }

/** Updates the Flower objects contained in flowerInventory, as described in

*   part (a)

  *   Precondition: newInventory has the same flower names in the same positions

*             as flowerInventory.

*  Postcondition: newInventory is unchanged.

*/

public void updateInventory(Flower[] newInventory)

{  /* to be implemented in part (a) */  }

/** Returns true if the top n flowers by quantity in stock at the flower shop are the

  *  same as the top n flowers by quantity in stock in otherInventory, as described

*  in part (b)

  *  Precondition: n is less than or equal to the lengths of flowerInventory and

*            otherInventory.

*  Postcondition: flowerInventory and otherInventory are unchanged.

*/

public boolean topNSame(int n, Flower[] otherInventory)

{  /* to be implemented in part (b) */  }

// There may be instance variables, constructors, and methods that are not shown.

}

(a) Write the FlowerShop method updateInventory, which increases the quantities of the flowers in flowerInventory by the quantities of the flowers in the array parameter newInventory. Each quantity of a flower in newInventory is added to the quantity of the corresponding flower in flowerInventory. The arrays flowerInventory and newInventory are the same length and contain the same flower names in the same positions.

For example, assume that sunnyFlowers has been declared as a FlowerShop object and newFlowers is a Flower array.

The array flowerInventory before the call sunnyFlowers.updateInventory(newFlowers):

"daffodil"225"rose"550"tulip"419

The array newFlowers:

"daffodil"200"rose"50"tulip"150

The array flowerInventory after the call sunnyFlowers.updateInventory(newFlowers):

"daffodil"425"rose"600"tulip"569

Complete method updateInventory.

/** Updates the Flower objects contained in flowerInventory, as described in

*   part (a)

  *   Precondition: newInventory has the same flower names in the same positions

*             as flowerInventory.

*  Postcondition: newInventory is unchanged.

*/

public void updateInventory(Flower[] newInventory)

(b) Write the FlowerShop method topNSame, which compares the flowers that appear in the first n positions of flowerInventory with the flowers that appear in the first n positions of otherInventory when both arrays are sorted in decreasing order by quantity in stock. The method returns true if the first n Flower objects in both arrays are the same and appear in the same position in both sorted arrays and returns false otherwise.

A helper method, sortByQuantity, has been provided. It returns a copy of its Flower[] parameter sorted in decreasing order by quantity in stock.

For example, assume that sunnyFlowers has been declared as a FlowerShop object and that stockB is an array of Flower objects.

The array flowerInventory in sunnyFlowers:

"carnation"50"daffodil"320"daisy"375"rose"550"tulip"419

The array stockB:

"carnation"250"daffodil"220"daisy"500"rose"525"tulip"310

The method call sunnyFlowers.topNSame(3, stockB) should return false. For sunnyFlowers, the top three flowers by quantity (in order) are "rose", "tulip", and "daisy". However, for stockB, the top three flowers by quantity (in order) are "rose", "daisy", and "tulip".

Complete method topNSame. You must use sortByQuantity appropriately to receive full credit.

/** Returns true if the top n flowers by quantity in stock at the flower shop are the

  *   same as the top n flowers by quantity in stock in otherInventory, as described

  *  in part (b)

  *  Precondition: n is less than or equal to the lengths of flowerInventory and

  *           otherInventory.

  *  Postcondition: flowerInventory and otherInventory are unchanged.

*/

public boolean topNSame(int n, Flower[] otherInventory)

(c) A programmer would like to add a method called getMostExpensiveFlower, which returns the name of the flower with the highest price.

Write a description of how you would change the Flower and FlowerShop classes in order to support this modification.

Make sure to include the following in your response.

  • Write the method header for the getMostExpensiveFlower method.
  • Identify any new or modified variables, constructors, or methods aside from the getMostExpensiveFlower method. Do not write the program code for this change.
  • Describe, for each new or revised variable, constructor, or method, how it would change or be implemented, including visibility and type. You do not need to describe the implementation of the getMostExpensiveFlower method. Do not write the program code for this change.
  • (由留学作业帮www.homeworkhelp.cc整理编辑)
    Bartleby|Bookrags|brainly.com|Coursehero|Chegg|eNotes|Ebook|gradebuddy|Grammerlly|Numerade |QuillBot|Oneclass|Studypool|SaveMyEaxms|Studymode|ScholarOne|SlideShare|SkillShare|Scribd|SolutionInn|Study.com|Studyblue|Termpaperwarehouse|and more…
  • Flower.java :
    
    // class made Comparable for sorting purpose
    public class Flower implements Comparable<Flower>
    {
            /** Returns the name of the flower (e.g., "rose", "tulip", etc.) */
            public String getName()
            {  /* implementation not shown */  }
            
            /** Returns the quantity in stock of the flower */
            public int getQuantity()
            {  /* implementation not shown */  }
            
            /** Sets the quantity in stock of the flower to newQ */
            public void setQuantity(int newQ)
            {  /* implementation not shown */  }
            
            // method added for comparison
            public int compareTo(Flower f) {
                    if (this.getQuantity() > f.getQuantity())
                            return -1;
                    else if (this.getQuantity() < f.getQuantity())
                            return 1;
                    return 0;
            }
            
            // There may be instance variables, constructors, and methods that are not shown.
    
    }
    FlowerShop.java :
    
    import java.util.Arrays;
    
    public class FlowerShop
    {
            
            /** An array of the flowers sold at the flower shop, sorted alphabetically by flower name
              *   Guaranteed to be nonempty and to contain only non-null entries
            */
            private Flower[] flowerInventory;
            
            /** Returns a copy of Flower[] arr in which the array elements have been sorted in
              *  order from highest quantity to lowest quantity
            */
            public Flower[] sortByQuantity(Flower[] arr)
            {  /* implementation not shown */  
                    Flower[] temp = arr;
                    Arrays.sort(temp);
                    return temp;
            }
            
            /** Updates the Flower objects contained in flowerInventory, as described in
            *   part (a)
              *   Precondition: newInventory has the same flower names in the same positions
            *             as flowerInventory.
            *  Postcondition: newInventory is unchanged.
            */
            public void updateInventory(Flower[] newInventory)
            {  /* to be implemented in part (a) */  
                    for (int i = 0; i < newInventory.length; ++i) {
                            flowerInventory[i].setQuantity(flowerInventory[i].getQuantity() + newInventory[i].getQuantity());
                    }
            }
            
            /** Returns true if the top n flowers by quantity in stock at the flower shop are the
              *  same as the top n flowers by quantity in stock in otherInventory, as described
            *  in part (b)
              *  Precondition: n is less than or equal to the lengths of flowerInventory and
            *            otherInventory.
            *  Postcondition: flowerInventory and otherInventory are unchanged.
            */
            public boolean topNSame(int n, Flower[] otherInventory)
            {  /* to be implemented in part (b) */  
                    Flower[] f = sortByQuantity(flowerInventory);
                    Flower[] o = sortByQuantity(otherInventory);
                    
                    for (int i = 0; i < n; ++i) {
                            if (f[i].getName() != o[i].getName())
                                    return false;
                    }
                    
                    return true;
                    
            }
            
            // There may be instance variables, constructors, and methods that are not shown.
    
    }
    For part c:
    
    We have to add a private price attribute in flower class and its appropriate getter & setter.
    
    In FlowerShop class we will add this: (This is only the method header)
    
    Flower getMostExpensiveFlower()

    Hope this helps, consider giving a like if it did! :)

    For any doubt, feel free to reach back!

    Cheers!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值