首页 > 其他分享 >shujujiegou章节2.17

shujujiegou章节2.17

时间:2024-09-12 21:22:27浏览次数:10  
标签:章节 set return value public new 2.17 end shujujiegou

/** A class that maintains a shopping cart for an online store.

    @author Frank M. Carrano

    @version 4.0

*/

public class OnlineShopper

{

   public static void main(String[] args)

   {

      Item[] items = {new Item("Bird feeder", 2050),

                      new Item("Squirrel guard", 1547),

                      new Item("Bird bath", 4499),

                      new Item("Sunflower seeds", 1295)};


      BagInterface<Item> shoppingCart = new ArrayBag<>();

      int totalCost = 0;


      // Statements that add selected items to the shopping cart:

      for (int index = 0; index < items.length; index++)

      {

         Item nextItem = items[index]; // Simulate getting item from shopper

         shoppingCart.add(nextItem);

         totalCost = totalCost + nextItem.getPrice();  

      } // end for


      // Simulate checkout

      while (!shoppingCart.isEmpty())

         System.out.println(shoppingCart.remove());


      System.out.println("Total cost: " + "\t$" + totalCost / 100 + "." +

                         totalCost % 100);

   } // end main

} // end OnlineShopper


/*

Sunflower seeds $12.95

Bird bath       $44.99

Squirrel guard  $15.47

Bird feeder     $20.50

Total cost:     $93.91

*/

/**

    A class that represents a coin.

    @author Frank M. Carrano

    @version 4.0

*/

public class Coin

{

   private enum CoinSide {HEADS, TAILS}

   private CoinName myName;

   private int value; // In cents

   private int year;  // Mint year

    private CoinSide sideUp;


   /** Constructs an object for the coin having a given  

       value and mint year. The visible side of the new

        coin is set at random. */

   public Coin(int coinValue, int mintYear)

   {

      switch (coinValue)

      {

         case 1:

             myName = CoinName.PENNY;

             break;

         case 5:

             myName = CoinName.NICKEL;

             break;

         case 10:

             myName = CoinName.DIME;

             break;

         case 25:

             myName = CoinName.QUARTER;

             break;

         case 50:

             myName = CoinName.FIFTY_CENT;

             break;

         case 100:

             myName = CoinName.DOLLAR;

             break;

         default:  

             myName = CoinName.PENNY;

             break;

      } // end switch


      value = coinValue;

      year = mintYear;

        sideUp = getToss();

   } // end constructor


   /** Constructs an object for the coin having a given  

       name and mint year. The visible side of the new

        coin is set at random. */

   public Coin(CoinName name, int mintYear)

   {

      switch (name)

      {

         case PENNY:

             value = 1;

             break;

         case NICKEL:

             value = 5;

             break;

         case DIME:

             value = 10;

             break;

         case QUARTER:

             value = 25;

             break;

         case FIFTY_CENT:

             value = 50;

             break;

         case DOLLAR:

             value = 100;

             break;

         default:  

             value = 1;

             break;

      } // end switch


      myName = name;

      year = mintYear;

        sideUp = getToss();

   } // end constructor


   /** Returns the name of the coin. */

    public CoinName getCoinName()

   {

      return myName;

   } // end getCoinName


   /** Returns the value of the coin in cents. */

    public int getValue()

    {

        return value;

    } // end getValue


    /** Returns the coin's mint year as an integer. */

    public int getYear()

    {

        return year;

    } // end getYear


   /** Returns "HEADS" if the coin is heads-side up;

        otherwise, returns "TAILS". */

    public String getSideUp()

    {

   /*

        String result = "Tails";

        if (sideUp == CoinSide.HEADS)

            result = "Heads";

        return result;

   */

      return sideUp.toString();

    } // end getSideUp


    /** Returns true if the coin is heads-side up. */

    public boolean isHeads()

    {

        return sideUp == CoinSide.HEADS;    

    } // end isHeads


    /** Returns true if the coin is tails-side up. */

    public boolean isTails()

    {

        return sideUp == CoinSide.TAILS;    

    } // end isTails


    /** Tosses the coin; sideUp will be either

       HEADS or TAILS randomly. */

    public void toss()

    {

        sideUp = getToss();

    } // end toss


   /** Returns the coin as a string in the form "value/year/side-up". */

   public String toString()

   {

      return value + "/" + year + "/" + sideUp;

   } // end toString


   // Returns a random value of either HEADS or TAILS.

    private CoinSide getToss()

    {

        CoinSide result;

        if (Math.random() < 0.5)

            result = CoinSide.HEADS;

        else

            result = CoinSide.TAILS;


        return result;

    } // end getToss

} // end Coin

public enum CoinName {PENNY, NICKEL, DIME, QUARTER, FIFTY_CENT, DOLLAR}


// NOTE: We have substituted the class ArrayBag from Chapter 2
// for the class Bag that is used in Chapter 1.
/**
    A class that implements a piggy bank by using a bag.
    @author Frank M. Carrano
    @version 4.0
*/
public class PiggyBank
{
	private BagInterface<Coin> coins;

	public PiggyBank() 
	{
		coins = new ArrayBag<>();
	} // end default constructor

	public boolean add(Coin aCoin) 
	{
		return coins.add(aCoin);
	} // end add

	public Coin remove() 
	{
		return coins.remove();
	} // end remove

	public boolean isEmpty() 
	{
		return coins.isEmpty();
	} // end isEmpty
} // end PiggyBank
/**
    A class that demonstrates the class PiggyBank.
    @author Frank M. Carrano
    @version 4.0
*/
public class PiggyBankExample
{
	public static void main(String[] args) 
	{
		PiggyBank myBank = new PiggyBank();
		
		addCoin(new Coin(1, 2010), myBank);
		addCoin(new Coin(5, 2011), myBank);
		addCoin(new Coin(10, 2000), myBank);
		addCoin(new Coin(25, 2012), myBank);
		
 		System.out.println("Removing all the coins:");
      int amountRemoved = 0;
      
      while (!myBank.isEmpty())
      {
         Coin removedCoin = myBank.remove();
         System.out.println("Removed a " + removedCoin.getCoinName() + ".");
         amountRemoved = amountRemoved + removedCoin.getValue();
      } // end while
      
		System.out.println("All done. Removed " + amountRemoved + " cents.");
	} // end main
	
	private static void addCoin(Coin aCoin, PiggyBank aBank) 
	{
		if (aBank.add(aCoin))
			System.out.println("Added a " + aCoin.getCoinName() + ".");
		else
			System.out.println("Tried to add a " + aCoin.getCoinName() + 
			                   ", but couldn't");
	} // end addCoin
} // end PiggyBankExample

/*
Added a PENNY.
Added a NICKEL.
Added a DIME.
Added a QUARTER.
Removing all the coins:
Removed a QUARTER.
Removed a DIME.
Removed a NICKEL.
Removed a PENNY.
All done. Removed 41 cents.
*/


/**

   An interface that describes the operations of a set of objects.


   @author Charles Hoot, Frank M. Carrano

   @version 4.0

*/

public interface SetInterface<T>

{

    /** Gets the current number of entries in this set.

       @return  The integer number of entries currently in the set. */

    public int getCurrentSize();


    /** Sees whether this set is empty.

       @return  True if the set is empty, or false if not. */

    public boolean isEmpty();


    /** Adds a new entry to this set, avoiding duplicates.

        @param newEntry  The object to be added as a new entry.

        @return  True if the addition is successful, or

                false if the item already is in the set. */

    public boolean add(T newEntry);


    /** Removes a specific entry from this set, if possible.

       @param anEntry  The entry to be removed.

       @return  True if the removal was successful, or false if not. */

    public boolean remove(T anEntry);


    /** Removes one unspecified entry from this set, if possible.

       @return  Either the removed entry, if the removal was successful,

                or null. */

    public T remove();


    /** Removes all entries from this set. */

    public void clear();


    /** Tests whether this set contains a given entry.

        @param anEntry  The entry to locate.

        @return  True if the set contains anEntry, or false if not .*/

    public boolean contains(T anEntry);


    /** Retrieves all entries that are in this set.

         @return  A newly allocated array of all the entries in the set. */

    public T[] toArray();

} // end SetInterface


标签:章节,set,return,value,public,new,2.17,end,shujujiegou
From: https://blog.51cto.com/u_15892225/11993517

相关文章

  • 【OCPP】ocpp1.6协议第5.16 Set Charging Profile章节的介绍及翻译
    目录5.16设置充电配置SetChargingProfile-概述主要内容1.概要2.操作流程3.参数说明4.配置示例实际应用结论5.16设置充电配置SetChargingProfile-原文译文5.16.1在订单开始时设置一个chargingprofile5.16.2在RemoteStartTransaction请求中设置一个charging......
  • glibc-2.17 编译失败
    报错如下:Error:`_obstack@GLIBC_2.2.5'can'tbeversionedtocommonsymbol'_obstack_compat'修复方法:修改代码diff--gita/malloc/obstack.cb/malloc/obstack.cindex5786da0aa4..c27a422077100644---a/malloc/obstack.c+++b/malloc/obstack.c@@......
  • 大模型提示词工程技术1-《大模型提示词工程技术》创作与前沿章节介绍
    大模型提示词工程技术1-《大模型提示词工程技术》创作与前沿章节介绍,《大模型提示词工程技术》的作者:微学AI,这是一本专注于提升人工智能大模型性能的著作,它深入浅出地讲解了如何通过优化输入提示词来引导大模型生成高质量、准确的输出。书中不仅涵盖了提示词工程的基本概念......
  • FastReport .NET WPF 24.2.17 Crack
    FastReport.NETWPFAsetofhigh-performancecomponentsforcreatingreportsanddocumentsinWPFprojectsBuyTryforfreeWhatreportscanFastReport.NETWPFdo?Practicallyany:invoices,financialreports,productcatalogswithcolorprofilesuppor......
  • 考研数学针对性训练,按章节刷题
    前言考研数学目前大家都在如火如荼地进行强化,有一些同学已经遇到了一些问题,某个章节学不会,想专项复习一下,但是又不知道怎么搞,之前也有很多小伙伴问我如何按章节刷题,那么在此给大家解答一下。方法一在考研数学欧几里得里的刷题模块,可以按章节刷题,各个大章以及小节都可以选择......
  • 章节七:215 +个 ChatGPT优秀提示以及如何编写自己的提示
    关注的人一天可以看到10篇文章!章节一:章节一:215+个ChatGPT优秀提示以及如何编写自己的提示-CSDN博客章节二:章节二:215+个ChatGPT优秀提示以及如何编写自己的提示-CSDN博客章节三:章节三:215+个ChatGPT优秀提示以及如何编写自己的提示-CSDN博客章节四:章节四:215+个Chat......
  • 【OCPP】ocpp1.6协议第5.11章节Remote Start Transaction的介绍及翻译
    目录5.11RemoteStartTransaction-概述1.目的2.消息类型2.1RemoteStartTransaction.req2.2RemoteStartTransaction.conf3.流程描述4.状态和值域5.特殊情况5.11远程启动交易RemoteStartTransaction-原文译文5.11RemoteStartTransaction-概述在OCPP......
  • WPS Office 2023专业版 v12.8.2.17149v2 精简优化版
    概述WPSOffice是由金山软件股份有限公司自主研发的一款办公软件套装,可以实现办公软件最常用的文字、表格、演示等多种功能。具有内存占用低、运行速度快、体积小巧、强大插件平台支持、免费提供海量在线存储空间及文档模板、支持阅读和输出PDF文件、全面兼容微软Office97-2010格......
  • 算法力扣刷题总结篇 ——【五】二叉树章节
    前言从记录三十五【二叉树基础和递归遍历】到记录六十二【538.把二叉搜索树转换为累加树】28篇文章都是二叉树的章节。所以总结的任务量有点大啊。但还是要做的。完成之后,开启新章节……继续。一、题目回顾1-5:遍历方式模版题。6-14:确定遍历顺序。后序居多——先统......
  • day014(二叉树章节)+北境互娱游戏开发一面
    222.完全二叉树节点的个数给你一棵完全二叉树的根节点root,求出该树的节点个数。完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第h层,则该层包含1~ 2h 个......