查看: 2610| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

CS61B Spring 2015 hw4

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 Casualet 于 2015-8-12 21:02 编辑

//最后的效果是按a或者c键可以产生不同的音调,additional部分暂时无力写了,希望有更多人发一下解法,即使没写完,也可以互相参考。。。。
ArrayRingBuffer.java
// Make sure to make this class a part of the synthesizer package
//package <package name>;
package synthesizer;
public class ArrayRingBuffer extends AbstractBoundedQueue {
  /* Index for the next dequeue or peek. */
  private int first;           
  /* Index for the next enqueue. */
  private int last;            
  /* Array for storing the buffer data. */
  private double[] rb;

  /** Create a new ArrayRingBuffer with the given capacity. */
  public ArrayRingBuffer (int capacity){
    // TODO: Create new array with capacity elements.
    //       first, last, and fillCount should all be set to 0.
    //       this.capacity should be set appropriately. Note that the local variable
    //       here shadows the field we inherit from AbstractBoundedQueue.
        this.capacity = capacity;         
        this.rb = new double[capacity];
        this.fillCount= 0;
        this.first = 0;
        this. last = 0;
  }

  /** Adds x to the end of the ring buffer. If there is no room, then
    * throw new RuntimeException("Ring buffer overflow")
    */
  public void enqueue(double x) {
    // TODO: Enqueue the item. Don't forget to increase fillCount and update last.
    // is there room?
    if(this.capacity==this.fillCount)
           throw new RuntimeException("try to enqueue a full queue");
    else{
       this.rb[this.last]=x;
       this.last=(this.last+1)%this.capacity;
       this.fillCount+=1;
    }     

  }

  /** Dequeue oldest item in the ring buffer. If the buffer is empty, then
    * throw new RuntimeException("Ring buffer underflow");
    */
  public double dequeue() {
    // TODO: Dequeue the first item. Don't forget to decrease fillCount and update first.
    if(this.fillCount==0)
        throw new RuntimeException("try to dequeue in an empty queue");
    else{
        double result=this.rb[first];
        this.first=(this.first+1)%this.capacity;
        this.fillCount--;
        return result;
    }
  }

  /** Return oldest item, but don't remove it. */
  public double peek() {
    // TODO: Return the first item. None of your instance variables should change.
    if(this.fillCount==0)
        throw new RuntimeException("try to peek an empty queue");
    else{
        return this.rb[first];
     }
  }
  public boolean isFull(){
    if(this.fillCount==this.capacity)
    return true;
    else
    return false;
  }
  public boolean isEmpty(){
    if(this.fillCount==0)
    return true;
    else
    return false;
  }
  public int fillCount(){
    return this.fillCount;
  }
}

上一篇:数学大二有将来申DM/ML方向phD求建议
下一篇:【活动】我在美国上机器学习(Machine Learning)课程的感受和总结
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表