📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
楼主: 小亩_0718
跳转到指定楼层
上一主题 下一主题
收起左侧

[入门|算法|数据结构] CS 61B Data Structures 2014Spring 每日打卡

🔗
liujinjin123 2020-5-23 04:52:59 | 只看该作者
全局:
目前刚刚做完PJ1,求个队友一起学习啊~

评分

参与人数 1大米 +1 收起 理由
小亩_0718 + 1 留个vx

查看全部评分

回复

使用道具 举报

无效楼层,该帖已经被删除
🔗
 楼主| 小亩_0718 2020-5-26 02:54:06 | 只看该作者

RE: CS 61B Data Structures 2014Spring 每日打卡

全局:
本帖最后由 小亩_0718 于 2020-5-26 02:55 编辑

LAB5-Part2

Suppose a subclass(CAT) inherits a method implementation(eat) from a superclass (Animal) and implements a Java interface(Pet) (that's the "interface" keyword) that contains a method with the same name and prototype.

  1. public class Animal{
  2.          public void eat(){
  3.                 System.out.println("Eating....");
  4.         }
  5. }
复制代码


-----
  1. public interface Pet{
  2.           public abstract void eat();  
  3. }
复制代码


-----
  1. public class Cat extends Animal implements Pet{
  2.         public static void main(String[] args){
  3.                  Cat mi = new Cat();
  4.                  mi.eat();
  5.          }
  6. }
复制代码


------
###a) Will Java compile the result?
- Yes, because Cat inherits the eat method from Animal, the eat method in the Pet interface doesn’t lack of implementation.

###b) What if the method declaration in the interface has a different return type
- Compile error.  Return type Long is not compatible with int.
  1. public class Animal{
  2.         public int count(){
  3.                 return 0;
  4.         }
  5. }
复制代码


----
  1. public interface Pet{
  2.         long count();
  3. }
复制代码


-----
  1. public class Cat extends Animal implements Pet{
  2.         public long count(){
  3.                 return 1;
  4.         }
  5. }
复制代码



###C) What if the method declaration in the interface has the same return type, but a signature with a different parameter type?
-不影响,编译可过。
###d)What if the method declaration in the interface has the same return type, and the same number of parameters and parameter types, but those parameters have different names?
- 不影响
回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-5-26 03:02:43 | 只看该作者
全局:
##Part 3
Suppose a subclass inherits a “public static final” constant from a superclass, and implements a Java interface that contains a “public static final” constant with the same name.

###a)Will Java compile the result? Does it make any difference whether the constant in the superclass and the constant in the interface have the same value?
- 可以,没有difference。
public class Animal{
public static final String name ="zzz";
}
--------
public interface Pet{
public static final String name ="KKK";
}
------
public class Cat extends Animal implements Pet{  
}

###b) Write a main( ) method in the subclass that accesses the constant using the same name used in the superclass and the Java interface. Will Java compiler the result? Does it make any difference whether the constant in the superclass and the constant in the interface have the same value?
-won’t compile. _this.name_ means _cat.name_, but cat doesn’t have name. Java compiler is forced to check the animal part and pet part of the cat, and finds out there are two variables named name which confuses it. Error msg: reference to name is ambiguous.
- No difference whether the constant in the superclass and the constant in the interface have the same value.
----------
public class Animal{
public static final String name ="zzz";
}
---------
public interface Pet{
public static final String name = "zzz";
}
---------
public class Cat extends Animal implements Pet{

public void print(){
  System.out.println(this.name);
}
       
public static void main(String[] args){
  Cat cat = new Cat();
  cat.print();
}
}
---------
###c)Figure out how to modify your main() method so that it accesses and prints one of the two conflicting values.
- change this.name to Pet.name or Animal.name
回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-5-26 05:35:56 | 只看该作者
全局:
##Part4
Consider a subclass that has a method that overrides a method with the same prototype in its superclass.

### a)Define a variable whose static type is the subclass and which references an object of the subclass. If we cast the variable to the superclass type before calling the overridden method.
        ((Superclass) subclass variable).method();
Does Java call the superclass method or the subclass method?
- Subclass
-------
public class Animal{
        public void print(){
                System.out.println("Animal");
        }
}
------
public interface Pet{
        public static final String name = "pet";
}
-----
public class Cat extends Animal implements Pet{

        public void print(){
                System.out.println("Cat");
        }
       
        public static void main(String[] args){
                Cat cat = new Cat();
                ((Animal)cat).print();
        }
               
}
-------
###b) Define a variable whose static type is the superclass and which references an object of the superclass(but not the subclass). If we cast the variable to the subclass type before calling the method, does Java call the superclass method or the subclass method?
- pass compile time but not run time because Animal cannot be cast to Cat!
---------
public static void main(String[] args){
                Animal ani = new Animal();
                ((Cat)ani).print();
}
-----------
###c)Suppose you have an object whose class(dynamic type) is the subclass. Can you figure out a way to call the superclass method on that object without having to go through the subclass method of the same name?
- 不行吧
回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-5-26 08:26:45 | 只看该作者
全局:
小亩_0718 发表于 2020-3-31 09:21
lab1_part II _1.Copy the lab1 blablabla, 呃...一上来就卡壳啊 T_T
它那些文件要先下下来吧...
https: ...

wget -r -np -R "index.html*" https://people.eecs.berkeley.edu/\~jrs/61b/hw/pj1/
回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-6-8 01:17:21 | 只看该作者
全局:
本帖最后由 小亩_0718 于 2020-6-8 01:27 编辑

Project1 - Part 1 _ PixImage.java
public class PixImage {
   //instance variables
   Pixel [][] image;
   private final int width;
   private final int height;
   //static variables
  private static final int[][] gxCons={{1,0,-1},{2,0,-2},{1,0,-1}};
  private static final int[][] gyCons={{1,2,1},{0,0,0},{-1,-2,-1}};
  //constructor
  public PixImage(int width, int height) {
    this.image = new Pixel [width][height];
    for (int i = 0; i<width; i++){
      for (int j = 0; j<height; j++){
        image[j]=new Pixel();
      }
    }
   
this.width=width;
    this.height=height;
   }
  //getter & setter methods
   public int getWidth() {
    return this.width;
  }
    public int getHeight() {
      return this.height;
  }

   public short getRed(int x, int y) {
    return this.image[x][y].red;
  }
   public short getGreen(int x, int y) {
    return this.image[x][y].green;
  }
  
  public short getBlue(int x, int y) {
    return this.image[x][y].blue;
  }

  public void setPixel(int x, int y, short red, short green, short blue) {
    this.image[x][y].red = red;
    this.image[x][y].green = green;
    this.image[x][y].blue = blue;
  }


   //boxBlur method
  1. public PixImage boxBlur(int numIterations) {
  2.                   if (this.width==0||this.height==0){
  3.                           return this;
  4.                   }
  5.                 PixImage source = this;
  6.                 for(int k=0;k<numIterations;k++){  
  7.                    PixImage target = new PixImage(this.width,this.height);
  8.             for(int i=0;i<source.width;i++){
  9.                                 for(int j=0; j<source.height;j++){
  10.                                         short r=source.calculateBlurRed(i,j);
  11.                                         short g=source.calculateBlurGreen(i,j);
  12.                                         short b=source.calculateBlurBlue(i,j);
  13.                                         target.setPixel(i,j,r,g,b);
  14.                                 }               
  15.                         }
  16.                         source=target;
  17.                 }
  18.                 return source;               
  19.   }
  20.   
  21.   private short calculateBlurRed(int x, int y) {   //calling obj is PixelImage
  22.           short sum = 0, count = 0;
  23.           for(int i=x-1;i<=x+1;i++){
  24.                   for(int j=y-1;j<=y+1;j++){
  25.                           if(i>=0 && i<this.width && j>=0 && j < this.height) {
  26.                                   count++;
  27.                                   sum+=this.getRed(i,j);
  28.                           }
  29.                   }
  30.           }
  31.           return (short)(sum/count);
  32.   }

  33.   private short calculateBlurGreen(int x, int y) {
  34.           short sum = 0, count = 0;
  35.           for(int i=x-1;i<=x+1;i++){
  36.                   for(int j=y-1;j<=y+1;j++){
  37.                           if(i>=0 && i<this.width && j>=0 && j < this.height) {
  38.                                   count++;
  39.                                   sum+=this.getGreen(i,j);
  40.                           }
  41.                   }
  42.           }
  43.           return (short)(sum/count);
  44.   }

  45.   private short calculateBlurBlue(int x, int y) {
  46.           short sum = 0, count = 0;
  47.           for(int i=x-1;i<=x+1;i++){
  48.                   for(int j=y-1;j<=y+1;j++){
  49.                           if(i>=0 && i<this.width && j>=0 && j < this.height) {
  50.                                   count++;
  51.                                   sum+=this.getBlue(i,j);
  52.                           }
  53.                   }
  54.           }
  55.           return (short)(sum/count);
  56.   }
复制代码


//mag2Gray method
  1. private static short mag2gray(long mag) {
  2.     short intensity = (short) (30.0 * Math.log(1.0 + (double) mag) - 256.0);

  3.     // Make sure the returned intensity is in the range 0...255, regardless of
  4.     // the input value.
  5.     if (intensity < 0) {
  6.       intensity = 0;
  7.     } else if (intensity > 255) {
  8.       intensity = 255;
  9.     }
  10.     return intensity;
  11.   }
复制代码




//sobelEdges metho
  1. public PixImage sobelEdges() {
  2.     PixImage copy = new PixImage(this.width,this.height);
  3.     //loop every pixel,
  4.     for (int i = 0; i<this.width; i++){
  5.             for (int j = 0; j<this.height; j++){
  6.                     //set R,G,B value on pixel
  7.                     short value=mag2gray(this.calculateEnergy(i,j));
  8.                     copy.setPixel(i,j,value,value,value);
  9.             }
  10.     }
  11.     return copy;
  12.   }
复制代码


//calculateEnergy method
  1.   private long calculateEnergy(int x, int y){
  2.                   long gxSumRed=0,gySumRed=0;
  3.                   long gxSumGreen=0,gySumGreen=0;
  4.                   long gxSumBlue=0,gySumBlue=0;

  5.             for(int i = 0; i<3; i++){
  6.                     for(int j = 0; j<3;j++){
  7.                             int m=i+(x-1),n=j+(y-1);
  8.                             if(m<0){                        //if m is invalid, let m equals to 0
  9.                                     m=0;
  10.                             }else if(m>=this.width){
  11.                                     m=this.width-1;
  12.                             }
  13.                             if(n<0){                        //if n is invalid, let n equals to 0
  14.                                     n=0;
  15.                             }else if(n>=this.height){
  16.                                     n=this.height-1;
  17.                             }
  18.                             int red=this.image[m][n].red;
  19.                                    int green=this.image[m][n].green;
  20.                             int blue=this.image[m][n].blue;

  21.                             gxSumRed+=gxCons[i][j]*red;
  22.                             gySumRed+=gyCons[i][j]*red;

  23.                             gxSumGreen+=gxCons[i][j]*green;
  24.                             gySumGreen+=gyCons[i][j]*green;
  25.                     
  26.                             gxSumBlue+=gxCons[i][j]*blue;
  27.                             gySumBlue+=gyCons[i][j]*blue;
  28.                     }
  29.             }

  30.             long energy = 0;
  31.             energy += gxSumRed * gxSumRed;
  32.             energy += gySumRed * gySumRed;
  33.             energy += gxSumGreen * gxSumGreen;
  34.             energy += gySumGreen * gySumGreen;
  35.             energy += gxSumBlue * gxSumBlue;
  36.             energy += gySumBlue * gySumBlue;
  37.                   
  38.                   return energy;
  39.   }
复制代码

[/i][/i][/i][/i][/i][/i]
[i][i]
---------------------------------------------Pixel.java----------
  1. public class Pixel {
  2.         short red;
  3.         short green;
  4.         short blue;
  5. }

复制代码


[/i][/i]



回复

使用道具 举报

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

本版积分规则

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