CS61B 2021spring HW0

Posted 临风而眠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CS61B 2021spring HW0相关的知识,希望对你有一定的参考价值。

HW 0: A Java Crash Course

文章目录


这个hw是optional,然后主要是给至少学过一门别的编程语言然后来迅速适应Java的设计的

This tutorial assumes that you have significant (one semester) experience with at least some programming language, and is intended only to highlight the Java way of doing some previously familiar things.

A Basic Program

public class ClassNameHere 
   public static void main(String[] args) 
      int x = 5;
      x = x + 1;
      System.out.println(x);
      int y = x;
      x = x + 1;
      System.out.println("x is: " + x);
      System.out.println("y is: " + y);      
   

这里先不关注

public static void 啥的,这里作者写的很有意思👇

You’ll find yourself looking at what is perhaps your first Java program. There sure is a lot of weird stuff here, like public class and public static void main(String[] args). We’ll discuss these in more detail later, but for this HW, you should ignore all of this mysterious garbage.

这确实是学习一门新语言的很好的思路

this link, or try this link ,这两个可视化的很牛

  • Unlike other programming languages (like Python, Scheme, and MATLAB), Java variables have a static type. By this, I mean that x will only ever be able to store an integer. If you tried to put a number like 5.3 into it, the code would fail.

  • Also unlike these other languages, every statement in Java must be followed by a semicolon(分号). The semicolon is very important, as it tells Java where one statement ends and another begins.

  • x is printed in the Program output box below using the rather verbose command name System.out.println.


  • 知识点就不记录于此了,因为毕竟之前也学过Java(虽然严重划水)
  • 主要记录题目

Creative Exercise 1a: Drawing a Triangle

Your goal is to create a program that prints the following figure. Your code should use loops (i.e. shouldn’t just be five print statements, that’s no fun).

*
**
***
****
*****
public class printTest 
    public static void main(String[] args) 

        for(int i = 0; i < 5; i++) 
            for (int j = 0; j <= i; j++) 
                System.out.print("*");
            
            System.out.println();
        
    
    


Creative Exercise 1b: DrawTriangle

Name this new method drawTriangle and give it a return type of void (this means that it doesn’t return anything at all).

The drawTriangle method should take one parameter named N, and it should print out a triangle exactly like your triangle from exercise 1a, but N asterisks tall instead of 5.

After writing DrawTriangle, modify the main function so that it calls DrawTriangle with N = 10.


public class DrawTraingle

    public static void drawTriangle(int N) 
        for(int i = 1; i <= N; i++)
        
            for(int j = 1; j<=i; j++)
            
                System.out.print("*");
            
            System.out.println();
        

    
    public static void main(String[] args) 
        drawTriangle(10);
    

Exercise 2、3

exercise2其实是想让用while循环,exercise3是for loop,我这里就写个for loop了

Using everything you’ve learned so far on this homework, you’ll now create a function with the signature public static int max(int[] m) that returns the maximum value of an int array. You may assume that all of the numbers are greater than or equal to zero.

Modify the code below (also found here) so that max works as described. Furthermore, modify main so that the max method is called on the given array and its max printed out (in this case, it should print 22).

  • 在这段代码的基础上改👇

    public class ClassNameHere 
       public static int max(int[] m) 
           
           return 0;
       
       public static void main(String[] args) 
          int[] numbers = new int[]9, 2, 15, 2, 22, 10, 6;      
       
    
    
  • My solution

    public class ClassNameHere 
       public static int max(int[] m) 
           int maxNum = m[0];
           for(int i = 0; i < m.length; i++)
           
              if(m[i] > maxNum)
                maxNum = m[i]; 
              
           
           System.out.println(maxNum);
           
           return 0;
       
       public static void main(String[] args) 
          int[] numbers = new int[]9, 2, 15, 2, 22, 10, 6;      
          max(numbers);
       
    
    

Optional: Exercise 4

This is a particularly challenging exercise, but strongly recommended.

Write a function windowPosSum(int[] a, int n) that replaces each element a[i] with the sum of a[i] through a[i + n], but only if a[i] is positive valued. If there are not enough values because we reach the end of the array, we sum only as many values as we have.

For example, suppose we call windowPosSum with the array a = 1, 2, -3, 4, 5, 4, and n = 3. In this case, we’d:

  • Replace a[0] with a[0] + a[1] + a[2] + a[3].
  • Replace a[1] with a[1] + a[2] + a[3] + a[4].
  • Not do anything to a[2] because it’s negative.
  • Replace a[3] with a[3] + a[4] + a[5].
  • Replace a[4] with a[4] + a[5].
  • Not do anything with a[5] because there are no values after a[5].

Thus, the result after calling windowPosSum would be 4, 8, -3, 13, 9, 4.

As another example, if we called windowPosSum with the array a = 1, -1, -1, 10, 5, -1, and n = 2, we’d get -1, -1, -1, 14, 4, -1.

  • the starter code

    public class BreakContinue 
        public static void windowPosSum(int[] a, int n) 
            /** your code here */
        
    
        public static void main(String[] args) 
            int[] a = 1, 2, -3, 4, 5, 4;
            int n = 3;
            windowPosSum(a, n);
            // Should print 4, 8, -3, 13, 9, 4
            System.out.println(java.util.Arrays.toString(a));
        
    
    
  • my solution

    我是分了三段处理,最后一个不处理, index 为 0 到 a.length-n-1的为一段,然后剩下的再为一段

    public class BreakContinue 
        public static void windowPosSum(int[] a, int n) 
            /** your code here */
            for(int i = 0; i < a.length-n; i++) 
                if(a[i]<0) 
                    continue;
                
                else 
                    for(int j=1;j<=n;j++) 
                        a[i]+=a[i+j];
                    
                
            
            for(int i = a.length - n; i < a.length-1 ; i++) 
                if(a[i]<0) 
                    continue;
                
                else
                    for( int j = i+1; j < a.length; j++) 
    
                        a[i] += a[j];
                    
                
    
            
            // 最后一个不处理
    
        
    
        public static void main(String[] args) 
            int[] a = 1, 2, -3, 4, 5, 4;
            int n = 3;
            windowPosSum(a, n);
            // Should print 4, 8, -3, 13, 9, 4
            System.out.println(java.util.Arrays.toString(a));
        
    
    

    不过看这个给的类的名字,应该是要用到break,再想想怎么用上break

    想想哈,正常的思路,什么地方需要停止呢,就是那个往后加n个,但是如果i+n越界了,那在越界之前的临界时刻需要break,所以怎么来写呢,要在下面这里做些modification

                    for(int j=1;j<=n;j++) 
                        a[i]+=a[i+j];
                    
    

    那么条件就是i+j超过长度的时候

    public class BreakContinue 
        public static void windowPosSum(int[] a, int n) 
            /** your code here */
            for(int i = 0; i < a.length; i++)
            
                if(a[i] < 0)
                
                    continue;
                
                for(int j = 1; j <= n; j++)
                
                    if(i + j >= a.length)
                    
                        break;
                    
                    a[i] += a[i + j];
                
            
        
    
        public static void main(String[] args) 
            int[] a = 1, 2, -3, 4, 5, 4;
            int n = 3;
            windowPosSum(a, n);
            // Should print 4, 8, -3, 13, 9, 4
            System.out.println(java.util.Arrays.toString(a));
        
    
    

Java可视化orz

表达积累

  • daisy-chained 串联的,串接的

  • curly braces(and conditionals)

    • 花括号(和条件句)
    • curly:卷曲的

参考资料

以上是关于CS61B 2021spring HW0的主要内容,如果未能解决你的问题,请参考以下文章

CS 61B lab11

cs61b lab10

CS 61B Project1

CS 61B homewok8

cs61b lab11

CS61b homework1