复习CS61B project1之前的题目

lab 1 考点:

substring/concat/equals/equalsIgnoreCase/indexOf

all index from 0 not 1.

homework 1 考点:

opencommerical是老师上课讲过的例子,现学现卖/怎样读取网页检查元素/readline()会自动向下一个进行读取

 1 class OpenCommerical {
 2
 3   /** Prompts the user for the name X of a company (a single string), opens
 4    *  the Web site corresponding to www.X.com, and prints the first five lines
 5    *  of the Web page in reverse order.
 6    *  @param arg is not used.
 7    *  @exception Exception thrown if there are any problems parsing the
 8    *             user‘s input or opening the connection.
 9    */
10   public static void main(String[] arg) throws Exception {
11
12     BufferedReader keyboard;
13     String inputLine;
14
15     keyboard = new BufferedReader(new InputStreamReader(System.in));
16
17     System.out.print("Please enter the name of a company (without spaces): ");
18     System.out.flush();        /* Make sure the line is printed immediately. */
19     inputLine = keyboard.readLine();
20
21     /* Replace this comment with your solution.  */
22     URL u = new URL("http://www."+inputLine+".com/");
23     InputStream ins = u.openStream();
24     InputStreamReader isr = new InputStreamReader(ins);
25     String[] FiveLines = new String[5];
26     BufferedReader Baidu = new BufferedReader(isr);
27     for(int i=0; i<5 ;i++){
28         FiveLines[i] = Baidu.readLine();
29     }
30     for(int j=4;j>=0;j--){
31     System.out.println(FiveLines[j]);
32     }
33    }
34 }

nuke2 substring运用

lab2 考点

gcd辗转相除

  static private int gcd (int x, int y) {  //辗转相除法
    /* Replace the following line with your solution. */
      if(y==0)
          return x;
      else
          return gcd(y,x%y);
  }

构造函数

System.out.println("..." + d1);//这里d1会自动调用toString(); 可以不用写出来。

  1 class Fraction {
  2
  3   /* private fields within a Fraction. */
  4   static private int numberOfFractions = 0;
  5
  6   private int numerator;
  7   private int denominator;
  8
  9   /** Constructs a Fraction n/d.
 10    *  @param n is the numerator.  Must be nonnegative.
 11    *  @param d is the denominator.  Must be positive.
 12    */
 13   public Fraction(int n, int d) {
 14     if (n < 0) {
 15       System.out.println("Fatal error:  Negative numerator.");
 16       System.exit(0);
 17     }
 18     if (d < 1) {
 19       System.out.println("Fatal error:  Non-positive denominator.");
 20       System.exit(0);
 21     }
 22     numberOfFractions++;
 23     numerator = n;
 24     denominator = d;
 25   }
 26
 27   /** Constructs a Fraction n/1.
 28    *  @param n is the numerator.  Must be nonnegative.
 29    */
 30   public Fraction(int n) {
 31     this(n, 1);    //call the two-parameters constructor
 32   }
 33
 34   /** Constructs a Fraction 0/1.
 35    */
 36   public Fraction() {
 37     this(0,1);   // call the two-parameters constructor
 38   }
 39
 40   /** Copies the Fraction "original".
 41    */
 42   public Fraction(Fraction original) {
 43     this(original.numerator,original.denominator);
 44   }
 45
 46   /** Converts this Fraction to a string format:  "numerator/denominator."
 47    *  Fractions should be printed in reduced form (part of your assignment is
 48    *  to make this true).
 49    *  @return a String representation of this Fraction.
 50    */
 51   public String toString() {
 52     int thisGcd = gcd(numerator, denominator);
 53
 54     return (numerator / thisGcd + "/" + denominator / thisGcd);
 55   }
 56
 57   /** Return the sum of two fractions.
 58    *  @param f2 is the Fraction to be added.
 59    *  @return the result of adding f2 to this Fraction.
 60    */
 61   public Fraction add(Fraction f2) {
 62     Fraction r = new Fraction((numerator * f2.denominator) +
 63                   (f2.numerator * denominator),
 64                   denominator * f2.denominator);
 65     return r;
 66   }
 67
 68   /** Replaces this Fraction‘s numerator with a new value.
 69    *  @param numerator is the new numerator.  Must be nonnegative.
 70    */
 71   public void changeNumerator(int numerator) { // DO NOT CHANGE THIS SIGNATURE!
 72     // Fix the bug that prevents this method from working correctly.
 73     if (numerator < 0) {
 74       System.out.println("Fatal error:  Negative numerator.");
 75       System.exit(0);
 76     }
 77     this.numerator = numerator;
 78
 79   }
 80
 81   /** Returns the number of Fraction objects in existence.
 82    *  @return the number of Fraction objects in existence.
 83    */
 84   public int fracs() {                         // DO NOT CHANGE THIS SIGNATURE!
 85     // Fix the bug that prevents this method from working correctly.
 86     return numberOfFractions;
 87   }
 88
 89   /** Computes the greatest common divisor (gcd) of the two inputs.
 90    * @param x must be nonnegative
 91    * @param y must be nonnegative
 92    * @return the gcd of x and y
 93    */
 94   static private int gcd (int x, int y) {  //辗转相除法
 95     /* Replace the following line with your solution. */
 96       if(y==0)
 97           return x;
 98       else
 99           return gcd(y,x%y);
100   }
101
102   /** Put the Fraction class through some tests.
103    * @param argv is not used.
104    */
105   public static void main(String[] argv) {
106
107     /* Test all four contructors and toString. */
108     Fraction f0 = new Fraction(0,1);
109     Fraction f1 = new Fraction(3);
110     Fraction f2 = new Fraction(12, 20);
111     Fraction f3 = new Fraction(f2);
112
113     System.out.println("\nTesting constructors and toString():");
114     System.out.println("The fraction f0 is " + f0.toString());
115     System.out.println("The fraction f1 is " + f1);    // toString is implicit.
116     System.out.println("The fraction f2 is " + f2);
117     System.out.println("The fraction f3 is " + f3 + ", which should equal f2");
118
119     /* Test the add method. */
120     System.out.println("\nTesting add:");
121
122
123
124     Fraction sumOfTwo = f1.add(f2);              // Sum of f1 and f2.
125     Fraction sumOfThree = f0.add(f1).add(f2);             // Sum of f0, f1, and f2.
126
127     System.out.println("The sum of " + f1 + " and " + f2 + " is " + sumOfTwo);
128     System.out.println("The sum of " + f0 + ", " + f1 + " and " + f2 + " is " +
129                        sumOfThree);
130
131
132     /* Test the methods used in Part III. */
133     System.out.println("\nTesting changeNumerator and fracs:");
134
135     f3.changeNumerator(7);
136     System.out.println("Now f3 is " + f3 + ", which should be 7/20");
137     System.out.println("The total number of Fraction objects is " +
138                        f3.fracs());
139
140     /* Test gcd function (static method). */
141     System.out.println("\nTesting gcd:");
142     System.out.println("The gcd of 2 and 10 is: " + gcd(2, 10));
143     System.out.println("The gcd of 15 and 5 is: " + gcd(15, 5));
144     System.out.println("The gcd of 24 and 18 is: " + gcd(24, 18));
145     System.out.println("The gcd of 10 and 10 is: " + gcd(10, 10));
146     System.out.println("The gcd of 21 and 400 is: " + gcd(21, 400));
147   }
148 }

homework2考点

闰年判断

  public static boolean isLeapYear(int year) {
      if((year%4==0&&year%100!=0)||(year%400==0)){
          return true;
          }else return false;                     // replace this line with your solution
  }

正则表达+split分割字符串+Integer.parseInt()方法将String转化为int

  public Date(String s) {
      if(s.matches("\\d{1,2}\\/\\d{1,2}\\/\\d{1,4}")){
          String[] d=s.split("/");
          month=Integer.parseInt(d[0]);
          day=Integer.parseInt(d[1]);
          year=Integer.parseInt(d[2]);
      }else System.exit(0);
  }

string int 相互转换知识补充

http://blog.csdn.net/memray/article/details/7312817/  转自MemRay博客

String ==》int

1). int i = Integer.parseInt([String]); 或

i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

int ==》String

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i);

3.) String s = "" + i;

注: Double, Float, Long 转成字串的方法大同小异.

未完......................

时间: 2024-10-29 12:57:15

复习CS61B project1之前的题目的相关文章

cs61b project1

Debug到死系列,感觉每一个方法都查了了好几遍...感觉完成project1之后对List和Array的理解加深了不少,而且debug的能力明显增强23333 . 代码很多写的也比较繁琐就不贴了,挑几个写的少的贴上吧,debug到最后自己经常都搞晕了,简单说下思路 part1: 设置了一个pixel的private class其中包含Red,Green,Yellow三个颜色的值,然后建立一个pixel的矩阵,就是基本的数据结构了. Blur和Sobel方法:最好将原矩阵外扩一圈0,便于简化代码

南开大学软件学院夏令营(上)

2017年7月5日 今天是2017年7月5日,也是我们参加南开大学软件学院夏令营的第二天.今天早上九点钟,在中科院软件所QQ群听说软件所得夏令营名单出来,我去官网查看,果然又是意料中的没进. 上午参加了夏令营的入营典礼.院长介绍南开大学软件学院的历史以及由来.我了解到原来南开大学并不是独立院校.它跟计算机学院是联系在一起.跟国内其他985的示范性软件学院一样,软件学院的师资力量还是跟计算机学院是同一拨人.今天下午去津南小区参观的软件学院的大楼也是和计算机学院独立分开的.从张院长的讲话中,我知道了

最大公倍数

华为机试题,以前做过,复习了一点思路. //题目描述 // //正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数. // //输入描述 : //输入两个正整数A和B. // // //输出描述 : //输出A和B的最小公倍数. // //输入例子 : //5 //7 // //输出例子 : // 35 //最大公因数,也称最大公约数.最大公因子,指两个或多个整数共有约数中最大的一个.a,b的最大公约数记为(a,b),同样的,a,b,c的

大数的运算--阶乘

今天复习了一下求阶乘 题目:编写一个程序,可以输出10000内的阶乘 #include <cstdio> #include <cstdlib> #include <cstring> #define max_w 8002 #define base 100000 int ans[10002][max_w]; int wei[10002]; int calMulti(int n) { int ci = 0; for(int i = 0; i < wei[n-1]; i+

POJ 2370 Democracy in danger(简单贪心)

Democracy in danger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3388   Accepted: 2508 Description In one of the countries of Caribbean basin all decisions were accepted by the simple majority of votes at the general meeting of citize

关于红帽认证

介绍 编辑 Redhat认证是由服务器系统领域著名的厂商--Redhat公司推出的.红帽认证分为三个层次,初级的RHCT(红帽官方在2011年1月1号,取消RHCT的考试,改为RHCSA),中级的RHCE,高级的RHCA.另外在2005年,红帽又推出了一个新的安全领域的高级认证:RHCSS. RHCSA,是红帽认证系统管理员的简称.它是Red Hat的入门级认证,通过此项认证表明你可以独立完成Red Hat Linux 本地客户的配置,包括安装调配Linux的本地使用.本地网络客户端和本地系统的

Java之循环语句练习1

最近在猛复习Java,猛刷题目ing,好了,不说了,我要去搬砖了. 输入整数 a,输出结果 s,其中s与a的关系是:s=a+aa+aaa+aaaa+aa...a,最后为a个a.例如:a=2时,s=2+22=24. 要求:使用循环结构语句实现. package com.cdp.SuShu; import java.util.Scanner; public class task4 { public static void main(String[] args) { Scanner input = n

软件工程第三周结对编程

结对编程之四则运算 1.项目成员 连燕波:201521123081 曾飞远:201521123080 队员风采展示 结对编程码云地址:https://gitee.com/Yanboooooooo/software_engineering_201521123081 2.需求分析 随着课余娱乐项目的增加,现在的小孩子在学习上变得越来越不自觉,需要老师.家长在背后催着才会做作业,因此需要实现一个计时功能,以时间的流逝来给孩子一些紧迫感.另外,在经过长期的运算训练之后,如何才知道学生的运算水平是否有显著

Android 开发面经,历时两月斩获BAT+头条四个公司 Offer

拿了 BAT+头条 四个 offer:同时也在帮公司招聘,筛选简历并面试.对技术面试这回事有一些体会,在此分享. 坦白说,我对我个人在这次求职中的表现并不十分满意,面试前没有做足够充分的准备--数次被面试官出的题目"虐".应对面试的压力时没能做到沉着冷静.在面试中未能完整地把自己的积累与优势表现出来--所以本篇文章并不是一个"成功者"的经验分享,而是一个普通人经历过各种面试后的一个总结与反思. 一.面试前的准备1.1 简历在替公司筛选简历时我对同事说,简历是向陌生的