面向对象程序设计(课堂源代码留档)
in 课程相关Java笔记 with 0 comment

面向对象程序设计(课堂源代码留档)

in 课程相关Java笔记 with 0 comment

HelloProj1

HelloWorld.java

package com.study.hello;

public class HelloWorld {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Hello, Java world.");
    }

}

HelloProj2

HelloWorld2.java

package com.study.hello;

import java.util.Scanner;

public class HelloWorld2 {
    public static String input() {
        Scanner obj = new Scanner(System.in);
        String aName = "";
        try {
            System.out.print("Plese input your name.");
            aName = obj.next();
        } catch(Exception e) {
            aName = "nobody";
        } finally {
            obj.close();
        }
        
        return aName;
    }
    
    public static void output(String pName) {
        System.out.println("Hello,"+pName);
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String myName;
        myName = input();
        output(myName);
    }

}

HelloProj3

Greeting.java

package com.study.hello;

import java.util.Scanner;

public class Greeting {
    private String aName;

    public void input() {
        Scanner obj = new Scanner(System.in);
        try {
            System.out.print("Plese input your name.");
            
//Scanner是一个扫描器,我们录取到键盘的数据,先存到缓存区等待读取,它判断读取结束的标示是  空白符;比如空格,回车,tab 等等。
//next()方法读取到空白符就结束;
//nextLine()读取到回车结束也就是“\r”;
//需要输入带空格的字符串时,用nextLine();            
            aName = obj.nextLine();
        } catch (Exception e) {
            aName = "nobody";
        } finally {
            obj.close();
        }
    }

    public void output() {
        System.out.println("Hello," + aName);
    }
}

HelloWorld3.java

package com.study.hello;

public class HelloWorld3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Greeting obj = new Greeting();
        obj.input();
        obj.output();
    }

}

ScoresProj

MainClass.java

package com.study.socres;

public class MainClass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ScoreTransfer obj = new ScoreTransfer();
        obj.transfer("E");
    }

}

ScoreTransfer.java

package com.study.socres;

public class ScoreTransfer {
    public void transfer(String pGrade) {
        int score;

        switch (pGrade) {
        case "A":
            score = 95;
            break;
        case "B":
            score = 85;
            break;
        case "C":
            score = 75;
            break;
        case "D":
            score = 65;
            break;
        case "E":
            score = 35;
            break;
        default:
            score = 0;
        }
        System.out.println("The score is " + Integer.toString(score));
    }

    public void transfer(int pScore) {
        String grade;

        if (pScore >= 90 && pScore <= 100) {
            grade = "A";
        } else if (pScore >= 80 && pScore <= 89) {
            grade = "B";
        } else if (pScore >= 70 && pScore <= 79) {
            grade = "C";
        } else if (pScore >= 60 && pScore <= 69) {
            grade = "D";
        } else if (pScore >= 0 && pScore <= 59) {
            grade = "E";
        } else {
            grade = "Error";
        }
        System.out.println("The grade is " + grade);
    }
}

SavingMoney

MainClass.java

package com.study.savingmoney;

public class MainClass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Saver obj = new Saver(200,20,24);
        obj.showResult(false);
    }

}

Saver.java

package com.study.savingmoney;

public class Saver {
    private double initial;
    private double increase;
    private int months;
    private double[] saved;

//    public Saver() {
//        this.initial = 0;
//        this.increase = 0;
//        this.months = 0;
//        this.saved = new double[this.months];
//        this.compute();
//    }

    public Saver(double pInitial, double pIncrease, int pMonths) {
        this.initial = pInitial;
        this.increase = pIncrease;
        this.months = pMonths;
        this.saved = new double[this.months];
        this.compute();
    }

    public void compute() {
        this.saved[0] = this.initial;
        for (int i = 1; i < this.months; i++) {
            this.saved[i] = this.saved[i - 1] + this.increase;
        }
    }

    public void showResult() {
        double total = 0;
        for (double every : saved) {
            System.out.println("Saved: " + every);
            total += every;
        }
        System.out.println("Total saved: " + total);
    }

    public void showResult(boolean withMonthSave) {
        int i = 0;
        double total = 0;

        while (i < this.months) {
            if (withMonthSave) {
                System.out.println("Saved: " + this.saved[i]);
            }
            total += this.saved[i];
            i++;
        }
        System.out.println("Total saved: " + total);
    }
}

FileProj

注意:如果你的“Workspace”是默认路径,那么本节课代码运行后,“MyData.txt”的路径为:C:\Users\用户名\eclipse-workspace\FileProj

FileOp.java

package com.study.files;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class FileOp {
    private String fileName;

    public FileOp() {
        super();
        this.fileName = ".\\MyData.txt";
    }

    public FileOp(String fileName) {
        super();
        this.fileName = fileName;
    }

    public void writeFile() {
        String[] companies = { "Intel", "Microsoft", "IBM", "Oracle" };

        try {
            FileWriter fw = new FileWriter(this.fileName);
            BufferedWriter bw = new BufferedWriter(fw);

            for (String line : companies) {
                bw.write(line);
//                bw.write("\n");
                bw.newLine();
            }
            bw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void readFile() {
        ArrayList<String> companes = new ArrayList<String>();
        String line;

        try {
            FileReader fr = new FileReader(this.fileName);
            BufferedReader br = new BufferedReader(fr);

            while ((line = br.readLine()) != null) {
                companes.add(line);
                System.out.println(line);
            }
            br.close();
            fr.close();

//            for(String comp : companes) {
//                System.out.println(comp);
//            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Main.java

package com.study.files;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        FileOp obj = new FileOp();
        //下列函数中,`writerFile();` 为写文件函数,`readFile();`为读文件函数。
                //obj.writeFile(); 
                //obj.readFile();
        
    }

}

留言: