cs106A-MIT-過濾掉了圖形部分

content from MIT-cs106A
枚舉胎撤,接口,搜索算法碗短,標(biāo)準(zhǔn)庫
課程內(nèi)容:163

/**
 * The Student class keeps track of the following pieces of data
 * about a student: the student's name, ID number, and the number
 * of credits the student has earned toward graduation.
 * All of this information is entirely private to the class.
 * Clients can obtain this information only by using the various
 * methods defined by the class.
 */

public class Student {
    
    /* Public constants */

    /** The number of units required for graduation */
    public static final double UNITS_TO_GRADUATE = 180.0;


    /**
     * Creates a new Student object with the specified name and ID.
     * @param studentName The student's name as a String
     * @param studentID The student's ID number as an int
     */
    public Student(String studentName, int studentID) {
        name = studentName;
        ID = studentID;
    }

    /**
     * Gets the name of this student.
     * @return The name of this student
     */
    public String getName() {
        return name;
    }

    /**
     * Gets the ID number of this student.
     * @return The ID number of this student
     */
    public int getID() {
        return ID;
    }

    /**
     * Sets the number of units earned.
     * @param units The new number of units earned
     */
    public void setUnits(double units) {
        unitsEarned = units;
    }

    /**
     * Gets the number of units earned.
     * @return The number of units this student has earned
     */
    public double getUnits() {
        return unitsEarned;
    }

    /**
     * Increments the number of units earned.
     * @param additionalUnits The additional number of units earned
     */
    public void incrementUnits(double additionalUnits) {
        unitsEarned += additionalUnits;
    }
    
    /**
     * Gets the number of units earned.
     * @return Whether the student has enough units to graduate
     */
    public boolean hasEnoughUnits() {
        return (unitsEarned >= UNITS_TO_GRADUATE);
    }
    
    
    /**
     * Creates a string identifying this student.
     * @return The string used to display this student
     */
    public String toString() {
        return name + " (#" + ID + ")";
    }


    /* Private instance variables */
    private String name;        /* The student's name         */
    private int ID;             /* The student's ID number    */
    private double unitsEarned; /* The number of units earned */

}
/**
 * File: Stanford.java
 * -------------------
 * The program provides an example of using Student objects
 */

import acm.program.*;

public class Stanford extends ConsoleProgram {
    
    /* Constants */
    private static final int CS106A_UNITS = 5;
    
    public void run() {
        setFont("Times New Roman-28");
                
        Student mehran = new Student("Mehran Sahami", 38000000);
        mehran.setUnits(3);
        printUnits(mehran);

        Student nick = new Student("Nick Troccoli", 57000000);
        nick.setUnits(179);
        printUnits(nick);
        
        println("Called tryToAddUnits to add to Nick's units...");
        tryToAddUnits(nick.getUnits(), CS106A_UNITS);
        printUnits(nick);
                
        takeCS106A(mehran);
        takeCS106A(nick);
        
        printUnits(mehran);
        printUnits(nick);
    }
    
    /**
     * Prints the name and number of units that student s has,
     * as well as whether the student can graduate
     * @param s The student who we will print information for
     */
    private void printUnits(Student s) {
        println(s.getName() + " has " + s.getUnits() + " units");
        println(s.toString() + " can graduate: " + s.hasEnoughUnits());
    }
    
    /**
     * BUGGY!! -- Tries to add to numUnits, but only adds to copy!
     * @param numUnits Original number of units
     * @param numUnitsToAdd Number of units to add to original
     */
    private void tryToAddUnits(double numUnits, double numUnitsToAdd) {
        numUnits += numUnitsToAdd;
    }

    /**
     * States that student s takes CS106A and increments number of units
     * @param s The student who will be taking CS106A
     */
    private void takeCS106A(Student s) {
        println(s.getName() + " takes CS106A!!");
        s.incrementUnits(CS106A_UNITS);
    }
}
file reading
file reading example
file reading code for example
/* 
 * FilesExample.java
 * -----------------
 * This program shows an example of reading a file.
 */

/* 
line==null can be put in while()
String line = rd.readLine();
while(line!=null) println();
 */

import acm.program.*;
import acm.util.*;
import java.io.*;

public class FilesExamples extends ConsoleProgram {
    
    public void run() {
        setFont("Times New Roman-24");
        
        try {
            BufferedReader rd = new BufferedReader(new FileReader("claire.txt"));
            while (true) {
                String line = rd.readLine();
                if (line == null) break;
                println("Read line: [" + line + "]");
            }
            rd.close();
            
        } catch (IOException ex) {
            throw new ErrorException(ex);
        }
   
    }
    
}
/* 
 * AnotherFileExample.java
 * -----------------------
 * This program shows an example of reading a file.
 */

/*
open or create file in a method, two try-catch, convenient for debugging
*/

import acm.program.*;
import acm.util.*;
import java.io.*;

public class AnotherFileExample extends ConsoleProgram {
    
    public void run() {
        setFont("Times New Roman-24");
        
        BufferedReader rd = openFile("Please enter filename: ");
        
        try {   
            while (true) {
                String line = rd.readLine();
                if (line == null) break;
                println("Read line: [" + line + "]");
            }
            rd.close();
        } catch (IOException ex) {
            throw new ErrorException(ex);
        }   
        
    }

    private BufferedReader openFile(String prompt) {
        BufferedReader rd = null;
        
        while (rd == null) {
            try {
                String filename = readLine(prompt);
                rd = new BufferedReader(new FileReader(filename));
            } catch (IOException ex) {
                println("Can't open that file, chief.");
            }
        }
        return rd;
    }
    
    
}
/* 
 * CopyFile.java
 * -------------
 * This program shows an example of copying a text file
 * line by line.
 */

import acm.program.*;
import acm.util.*;
import java.io.*;

public class CopyFile extends ConsoleProgram {
    
    public void run() {
        setFont("Times New Roman-24");
        BufferedReader rd = openFile("Please enter filename: ");
        
        try {
            PrintWriter wr = new PrintWriter(new FileWriter("copy.txt"));
            
            while (true) {
                String line = rd.readLine();
                if (line == null) break;
                println(line);  // console
                wr.println(line);  // file
            }
            
            rd.close();
            wr.close();
            
        } catch (IOException ex) {
            throw new ErrorException(ex);
        }           
    }

    private BufferedReader openFile(String prompt) {
        BufferedReader rd = null;
        
        while (rd == null) {
            try {
                String filename = readLine(prompt);
                rd = new BufferedReader(new FileReader(filename));
            } catch (IOException ex) {
                println("Nice try punk.  That file doesn't exist.");
            }
        }
        return rd;
    }
    
    
}
/* 
 * SimpleArrayListExample.java
 * ---------------------------
 * This program shows an example of using an ArrayList.
 */

import acm.program.*;
import java.util.*;

public class SimpleArrayListExample extends ConsoleProgram {
    
    
    public void run() {
           setFont("Courier New-bold-24");

           ArrayList<String> strList = new ArrayList<String>();
           readStringList(strList);
           printArrayList(strList);
        
        ArrayList<Integer> intList = new ArrayList<Integer>();
            readIntList(intList);
        printArrayList(intList);
    }
    
    private void readStringList(ArrayList<String> list) {
        while (true) {
            String line = readLine("Next line: ");
            if (line.equals("")) break;
            list.add(line);
        }
    }
    
    private void printArrayList(ArrayList list) {
        println("List contains "  + list.size() + " elements");
        for(int i = 0; i < list.size(); i++) {
            println(list.get(i));   // unboxes value if needed (e.g., int)
        }
    }

    private void readIntList(ArrayList<Integer> list) {
        while (true) {
            int value = readInt("Next integer (-1 to stop): ");
            if (value == -1) break;
            list.add(value);    // boxes value (int) to Integer
        }
    }


}   
/* 
 * IntegerArrayListExample.java
 * ---------------------------
 * This program shows an example of using an Integer ArrayList.
 */

import acm.program.*;
import java.util.*;

public class IntegerArrayListExample extends ConsoleProgram {
    
    public void run() {
               setFont("Courier New-bold-24");

               ArrayList<Integer> intList = new ArrayList<Integer>();
            readList(intList);
        printArrayList(intList);
        
            readList(intList);
        printArrayList(intList);
    }
    
    private void readList(ArrayList<Integer> list) {
        while (true) {
            int value = readInt("Next number: ");
            if (value == -1) break;
            list.add(value);    // boxes value (int) to Integer
        }
    }
    
    private void printArrayList(ArrayList list) {
        println("List contains "  + list.size() + " elements");
        for(int i = 0; i < list.size(); i++) {
            println(list.get(i));  // unboxes Integer to int
        }
    }

}   
/* 
 * File: AverageScores.java
 * ------------------------
 * This program shows an example of using arrays.
 */

import acm.program.*;

public class AverageScores extends ConsoleProgram {
    
    private static final int SENTINEL = -1;
    private static final int MAX_SCORES = 1000; // actual size
    
    public void run() {
        setFont("Courier New-bold-24");

        int[] midtermScores = new int[MAX_SCORES];
        int numScores = 0;              // effective size
        
        while (true) {
        int score = readInt("Score: ");
        if (score == SENTINEL) break;
        midtermScores[numScores++] = score;
    }
        
        double averageScore = computeAverage(midtermScores, numScores);
        
        println("Average score: " + averageScore);
    }

    private double computeAverage(int[] arr, int numScores) {
        double average = 0;
        for(int i = 0; i < numScores; i++) {
            average += arr[i];
        }
        average = average / numScores;
        return average;
    }
    
}
/* 
 * SwapExample.java
 * -----------------
 * This program shows an example of using arrays when swapping values.
 */

import acm.program.*;

public class SwapExample extends ConsoleProgram {
        
    public void run() {
        setFont("Courier New-bold-24");

        int[] array = new int[5];
        array[0] = 1;
        array[1] = 2;
        
        println("Buggy swap results:");
        swapElementsBuggy(array[0], array[1]);
        println("array[0] = " + array[0]);
        println("array[1] = " + array[1]);
        
        println("Happy swap results:");
        swapElementsHappy(array, 0, 1);
        println("array[0] = " + array[0]);
        println("array[1] = " + array[1]);
    }

    private void swapElementsBuggy(int x, int y) {
        int temp = x;
        x = y;
        y = temp;
    }
    
    private void swapElementsHappy(int[] arr, int pos1, int pos2) {
        int temp = arr[pos1];
        arr[pos1] = arr[pos2];
        arr[pos2] = temp;
    }
    
}
/* 
 * File: TwoDimensionalArrayExample.java
 * -------------------------------------
 * This program shows an example of using 2-dimensional arrays.
 */

import acm.program.*;

public class TwoDimensionalArrayExample extends ConsoleProgram {
 
    public static final int ROWS = 2;
    public static final int COLS = 3;
    
    public void run() {
            setFont("Courier New-bold-24");

        int[][] arr = new int[ROWS][COLS];
        initMatrix(arr);
        printMatrix(arr);
    }

    private void initMatrix(int[][] matrix) {
        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix[0].length; j++) {
                matrix[i][j] = j;
            }
        }
    }
    
    private void printMatrix(int[][] matrix) {
        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix[0].length; j++) {
                print(matrix[i][j] + " ");
            }
            println();
        }
    }
 
    
}
import acm.program.*;


public class TestScores extends ConsoleProgram {

    // Maximum number of students in a course
    private static final int MAX_STUDENTS = 1000;

    public void run() {
        setFont("Times New Roman-24");
        
        int numScores = readInt("Number of scores per student: ");
        scores = new int[MAX_STUDENTS][numScores];
        
        initScores();
        
        println("Scores[0] before increment");
        printList(scores[0]);

        incrementScoreList(scores[0]);
        println("Scores[0] after increment");
        printList(scores[0]);
    }

    // Initialized score grid to all be 0
    private void initScores() {
        for(int i = 0; i < scores.length; i++) {
            for(int j = 0; j < scores[0].length; j++) {
                scores[i][j] = 0;
            }
        }
    }
    
    // Prints every element of list on a separate line
    private void printList(int[] list) {
        for(int i = 0; i < list.length; i++) {
            println(list[i]);
        }
    }
    
    // Adds 1 to every element of list
    private void incrementScoreList(int[] list) {
        for(int i = 0; i < list.length; i++) {
            list[i]++;
        }
    }
    
    /* private instance variable */
    private int[][] scores;
}
/*
 * File: HashMapExample.java
 * -------------------------
 * This program shows an example of using a HashMap to keep
 * track of a simple phonebook (mapping names to phone numbers).
 */

import acm.program.*;
import java.util.*;

public class HashMapExample extends ConsoleProgram {
    
    public void run() {
        setFont("Times New Roman-24");
        
        println("Reading in phone numbers");
        readPhoneNumbers();

        println("Displaying phone numbers");
        displayAllNumbers();

        println("Looking up phone numbers");
        lookUpNumbers();
    }
    
    // Ask the user for phone numbers to store in phonebook.
    private void readPhoneNumbers() {
        while (true) {
            String name = readLine("Enter name: ");
            if (name.equals("")) break;
            int number = readInt("Phone number (as int): ");
            phonebook.put(name, number);
        }
    }
    
    // Print out all the names/phone numbers in the phonebook.
    // (This version of the method uses iterators.)
    private void displayAllNumbers() {
        Iterator<String> it = phonebook.keySet().iterator();
        while (it.hasNext()) {
            String name = it.next();
            Integer number = phonebook.get(name);
            println(name + ": " + number);
        }
    }

    // Print out all the names/phone numbers in the phonebook.
    // (This version of the method uses a foreach loop.)
    private void displayAllNumbers2() {
        for(String name : phonebook.keySet()) {
            Integer number = phonebook.get(name);
            println(name + ": " + number);
        }
    }
    
    // Allow the user to lookup phone numbers in the phonebook
    // by looking up the number associated with a name.
    private void lookUpNumbers() {
        while (true) {
            String name = readLine("Enter name to lookup: ");
            if (name.equals("")) break;
            Integer number = phonebook.get(name);
            if (number == null) {
                println(name + " not in phonebook");
            } else {
                println(number);
            }
        }
    }


    /* Private instance variable */
    private Map<String,Integer> phonebook = new HashMap<String,Integer>();
}   
屏幕快照 2016-12-26 上午4.58.26.png

屏幕快照 2016-12-26 上午5.02.58.png

屏幕快照 2016-12-26 上午5.03.07.png

primitives passes by value

屏幕快照 2016-12-26 上午7.03.59.png

instance variables

屏幕快照 2016-12-26 上午7.06.53.png

屏幕快照 2016-12-26 上午7.07.40.png

屏幕快照 2016-12-26 上午7.08.44.png

屏幕快照 2016-12-26 上午7.12.10.png
屏幕快照 2016-12-26 下午5.54.44.png

屏幕快照 2016-12-26 下午5.54.51.png

屏幕快照 2016-12-26 上午7.22.42.png

屏幕快照 2016-12-26 上午7.39.40.png

屏幕快照 2016-12-26 上午7.39.48.png

屏幕快照 2016-12-26 上午7.40.00.png

屏幕快照 2016-12-26 上午7.41.18.png

屏幕快照 2016-12-26 上午7.41.29.png

屏幕快照 2016-12-26 上午7.41.38.png

屏幕快照 2016-12-26 上午7.41.46.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市题涨,隨后出現(xiàn)的幾起案子偎谁,更是在濱河造成了極大的恐慌,老刑警劉巖纲堵,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件巡雨,死亡現(xiàn)場離奇詭異,居然都是意外死亡席函,警方通過查閱死者的電腦和手機铐望,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來茂附,“玉大人正蛙,你說我怎么就攤上這事∮” “怎么了夫晌?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵婴洼,是天一觀的道長蒙揣。 經(jīng)常有香客問我庆亡,道長,這世上最難降的妖魔是什么蒜危? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任虱痕,我火速辦了婚禮,結(jié)果婚禮上辐赞,老公的妹妹穿的比我還像新娘。我一直安慰自己硝训,他們只是感情好响委,可當(dāng)我...
    茶點故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布新思。 她就那樣靜靜地躺著,像睡著了一般赘风。 火紅的嫁衣襯著肌膚如雪夹囚。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天邀窃,我揣著相機與錄音荸哟,去河邊找鬼。 笑死瞬捕,一個胖子當(dāng)著我的面吹牛鞍历,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播肪虎,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼劣砍,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了扇救?” 一聲冷哼從身側(cè)響起刑枝,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎迅腔,沒想到半個月后装畅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡沧烈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年掠兄,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片掺出。...
    茶點故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡徽千,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出汤锨,到底是詐尸還是另有隱情双抽,我是刑警寧澤,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布闲礼,位于F島的核電站牍汹,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏柬泽。R本人自食惡果不足惜慎菲,卻給世界環(huán)境...
    茶點故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望锨并。 院中可真熱鬧露该,春花似錦、人聲如沸第煮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至撵摆,卻和暖如春底靠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背特铝。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工暑中, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鲫剿。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓鳄逾,卻偏偏與公主長得像,于是被迫代替她去往敵國和親牵素。 傳聞我的和親對象是個殘疾皇子严衬,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,161評論 25 707
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)笆呆,斷路器请琳,智...
    卡卡羅2017閱讀 134,659評論 18 139
  • 樓上不知哪家鄰居,近幾個月開始學(xué)彈鋼琴赠幕,我上班在彈俄精,下班了也在彈,日出而作榕堰,日落還作竖慧,一直在輕輕的彈,輕輕的練逆屡,雖...
    愛跑步的小懶閱讀 1,072評論 0 0
  • 訴說一個人的傾聽圾旨,想起全國際的自己,問此生魏蔗,多少緣砍的,多少自己,國際那么大莺治,人生那么遠廓鞠,誰是誰的誰,錯失就是終身谣旁,失...
    mikasi35閱讀 316評論 0 0
  • 時間不停流轉(zhuǎn)榄审,冷眼觀看燈火從明亮閃爍到消失殆盡的循環(huán)砌们。金屬制成的小針在鐘盤里按部就班的朝著前面的每個刻度一步一步挪...
    愛笑的魚613閱讀 649評論 0 3