首页 > 代码库 > 复习CS61B project1之前的题目

复习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 }
View Code

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 }
View Code

 

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 转成字串的方法大同小异.

 

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

 

复习CS61B project1之前的题目