UCB CS61B learning

homework1-1

package hw1;

/* OpenCommercial.java */

import java.net.*;
import java.io.*;
import java.util.*;/*package for Arraylist

/**  A class that provides a main function to read five lines of a commercial
 *   Web page and print them in reverse order, given the name of a company.
 */

class OpenCommercial {

  /** Prompts the user for the name X of a company (a single string), opens
   *  the Web site corresponding to www.X.com, and prints the first five lines
   *  of the Web page in reverse order.
   *  @param arg is not used.
   *  @exception Exception thrown if there are any problems parsing the 
   *             user's input or opening the connection.
   */
  public static void main (String[] arg) throws Exception {
    
	    BufferedReader keyboard;
	    String inputLine;
	
	    keyboard = new BufferedReader(new InputStreamReader(System.in));
	
	    System.out.print("Please enter the name of a company (without spaces): ");
	    System.out.flush();        /* Make sure the line is printed immediately. */
	    inputLine = keyboard.readLine();
	    
	    /* Replace this comment with your solution.  */ 
	    String Website = "http://www."+inputLine+".com";	
	    URL url = new URL(Website);
	    InputStream is = url.openStream();
	    InputStreamReader isr = new InputStreamReader(is,"utf-8");
	    BufferedReader bf = new BufferedReader(isr);
	    String data;
	    data = bf.readLine();
	    ArrayList<String> list = new ArrayList<String>();
	    int i = 0;
	    while(data!=null & i<5)  {
	    	list.add(data);
	    	data=bf.readLine();	 
            i+=1;
        }  
	    for (i=0;i<5;i++) {
		    System.out.println(list.get(4-i));	    	
	    }
	    /*
	    String[] arr = list.toArray(new String[0]);
	    System.out.println(Arrays.toString(arr));
	    */
    } 
    
  }

lab2

package lab2;

/* Fraction.java */

import java.io.*;

/** The Fraction class implements nonnegative fractions (rational numbers).
 */
class Fraction {

  /* private fields within a Fraction. */
  private static int numberOfFractions = 0;/*static is important*/

  private int numerator;
  private int denominator;

  /** Constructs a Fraction n/d. 
   *  @param n is the numerator.  Must be nonnegative.
   *  @param d is the denominator.  Must be positive.
   */
  
  public int Judge(int n) {
	if (n < 0) {
	  System.out.println("Fatal error:  Negative numerator.");
	  System.exit(0);
	}
	return 1;
  }
  
  public Fraction(int n, int d) {
	Judge(n);
	Judge(d);
    numberOfFractions++;
    numerator = n; 
    denominator = d;
    System.out.println(numberOfFractions);
  }

  /** Constructs a Fraction n/1. 
   *  @param n is the numerator.  Must be nonnegative.
   */
  public Fraction(int n) {
    this(n, 1);
  }

  /** Constructs a Fraction 0/1. 
   */
  public Fraction() {
    numberOfFractions++;
    numerator = 0;
    denominator = 1;
    System.out.println(numberOfFractions);
  }

  /** Copies the Fraction "original".
   */
  public Fraction(Fraction original) {
    numberOfFractions++;
    numerator = original.numerator;
    denominator = original.denominator;
    System.out.println(numberOfFractions);
  }

  /** Converts this Fraction to a string format:  "numerator/denominator."
   *  Fractions should be printed in reduced form (part of your assignment is
   *  to make this true).
   *  @return a String representation of this Fraction.
   */
  public String toString() {
    int thisGcd = gcd(numerator, denominator);

    return (numerator / thisGcd + "/" + denominator / thisGcd);
  }

  /** Return the sum of two fractions.
   *  @param f2 is the Fraction to be added.
   *  @return the result of adding f2 to this Fraction.
   */
  public Fraction add(Fraction f2) {
    Fraction r = new Fraction((numerator * f2.denominator) +
			      (f2.numerator * denominator),
			      denominator * f2.denominator);
    return r;
  }

  /** Replaces this Fraction's numerator with a new value.
   *  @param numerator is the new numerator.  Must be nonnegative.
   */
  public void changeNumerator(int numerator) { // DO NOT CHANGE THIS SIGNATURE!
    // Fix the bug that prevents this method from working correctly.
    if (numerator < 0) {
      System.out.println("Fatal error:  Negative numerator.");
      System.exit(0);
    }
    this.numerator = numerator;/*this is importtant key*/
  }

  /** Returns the number of Fraction objects in existence.
   *  @return the number of Fraction objects in existence.
   */
  public int fracs() {                         // DO NOT CHANGE THIS SIGNATURE!
    // Fix the bug that prevents this method from working correctly.
    return numberOfFractions;
  }

  /** Computes the greatest common divisor (gcd) of the two inputs.
   * @param x must be nonnegative
   * @param y must be nonnegative
   * @return the gcd of x and y
   */
  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);
	}
  }

  /** Put the Fraction class through some tests.
   * @param argv is not used.
   */
  public static void main(String[] argv) {

    /* Test all four contructors and toString. */
    Fraction f0 = new Fraction();
    Fraction f1 = new Fraction(3);
    Fraction f2 = new Fraction(12, 20);
    Fraction f3 = new Fraction(f2);

    System.out.println("\nTesting constructors and toString():");
    System.out.println("The fraction f0 is " + f0.toString());
    System.out.println("The fraction f1 is " + f1);    // toString is implicit.
    System.out.println("The fraction f2 is " + f2);
    System.out.println("The fraction f3 is " + f3 + ", which should equal f2");

    /* Test the add method. */
    System.out.println("\nTesting add:");

    
    Fraction sumOfTwo = f1.add(f2);              // Sum of f1 and f2.
    Fraction sumOfThree = f0.add(sumOfTwo);             // Sum of f0, f1, and f2.

    System.out.println("The sum of " + f1 + " and " + f2 + " is " + sumOfTwo);
    System.out.println("The sum of " + f0 + ", " + f1 + " and " + f2 + " is " +
                       sumOfThree);
    
    
	
    /* Test the methods used in Part III. */
    System.out.println("\nTesting changeNumerator and fracs:");

    f3.changeNumerator(7);
    System.out.println("Now f3 is " + f3 + ", which should be 7/20");
    System.out.println("The total number of Fraction objects is " +
                       f3.fracs());

    /* Test gcd function (static method). */
    System.out.println("\nTesting gcd:");
    System.out.println("The gcd of 2 and 10 is: " + gcd(2, 10));
    System.out.println("The gcd of 15 and 5 is: " + gcd(15, 5));
    System.out.println("The gcd of 24 and 18 is: " + gcd(24, 18));
    System.out.println("The gcd of 10 and 10 is: " + gcd(10, 10));
    System.out.println("The gcd of 21 and 400 is: " + gcd(21, 400));
  }
}


homework1-2

package hw1;

import java.io.*;

public class Nuke2 {
	  public static void main(String[] arg) throws Exception {
		    
		    BufferedReader keyboard;
		    String inputLine;
		
		    keyboard = new BufferedReader(new InputStreamReader(System.in));
		
		    System.out.print("Please enter a string: ");
		    System.out.flush();        /* Make sure the line is printed immediately. */
		    inputLine = keyboard.readLine();
		    String output ="";
		    for (int i=0;i<inputLine.length();i++) {
		    	if(i!=1) {
		    		output += inputLine.charAt(i);
		    	}
		    }
		    System.out.println(output);
	  }
}

lab1

package lab1;//新建的package缺少声明

/* Names.java */

import java.io.*;

/** The Names class provides a single function, main, that will
 *   perform various manipulations of the name John Fitzgerald Kennedy. 
 *   This is a modification of the program on page 43 of Arnow and Weiss.
 */

class Names {

/** Performs various string operations on the name John Fitzgerald Kennedy.
 *  @param arg is not used.
 */
  public static void main(String arg[]) {
    String first = "John";
    String middle = "Fitzgerald";
    String last = "Kennedy";
    String initials;
    String firstInit, middleInit, lastInit;

    firstInit = first.substring(0,1);
    middleInit = middle.substring(0,1);
    lastInit = last.substring(0,1);
    initials = firstInit.concat(middleInit);
    initials = initials.concat(lastInit);

    System.out.println();//缺少逗号
    System.out.println(first + " " + middle + " " + last + " ");
    System.out.println(initials);
    System.out.println(last + ", " + first + " " + middle);
    System.out.println(last + ", " + first + " " + middleInit +".");
    System.out.println(first.toUpperCase() + " " + last.toUpperCase());//缺少括号

    System.out.println(first + " equals john is " + first.equals("john"));
    System.out.println(first + " equals john (ignoring case) is " 
		       + first.equalsIgnoreCase("john"));
    
    
    System.out.println("The character at index 3 in " + middle + " is " +
		       middle.substring(3,4));//参数错误
    System.out.println("The index of \"gerald\" within " + middle + " is " +
		       middle.indexOf("gerald"));
    System.out.println("The index of \"gerald\" within " + last + " is " +
		       last.indexOf("gerald"));
	
    
    System.out.println();
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值