面向对象程序设计(java)课堂代码

若有错误,可以指出

HelloProj1

HelloWorld.java

package com.study.hello;

public class HelloWorld1 {
   

	public static void main(String[] args) {
   
		// TODO Auto-generated method stub
		System.out.println("Hello, Java World!\n"+
				"tql"
				);	//换行输出+字符串连接‘+’
	} 

}

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("Please 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;
	private boolean flag = false; //用来判断是否使用input()
	public void input() {
   
		flag = true;
		Scanner obj = new Scanner(System.in);
		try {
   
			System.out.print("Please input your name: ");
			aName = obj.nextLine();
		} catch (Exception e) {
   
			aName = "nobody";
		} finally {
   
			obj.close();
		}
	}

	public void output() {
   
		if(flag) //如果使用了,即aName有值,输出即可
		System.out.println("Hello, " + aName);
		else 
			System.out.println("Hello, nobody");//没有值输出nobody
	
	}
}

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

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 <= 100 && pScore >= 90) {
   
			grade = "A";
		} else if (pScore >= 80 && pScore < 90) {
   
			grade = "B";
		} else if (pScore >= 70 && pScore < 80) {
   
			grade = "C";
		} else if (pScore >= 60 && pScore < 70) {
   
			grade = "D";
		} else if (pScore >= 0 && pScore < 60) {
   
			grade = "E";
		} else {
   
			grade = "Error";
		}
		System.out.println("The grade is " + grade);
	}

}

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");
		
	}

}

SavingMoney

Saver.java

package com.study.savingMoney;

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

	// constructor
	public Saver() {
    		//构造函数
		this.initial = 100;
		this.increase = 50;
		this.months = 12;
		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();
	}

	private 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(boolean withMonthSave) {
   
		double total = 0;
		int i = 0;
		while (i < this.saved.length) {
   		//数组的长度
			if (withMonthSave) {
   
				System.out.println("Saved: " + this.saved[i]);
			}
			total += this.saved[i];
			i++;
		}
		System.out.println("Total saved: " + total);
	}

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

MainClass.java

package com.study.savingMoney;

public class MainClass {
   

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

}

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;
import java.util.Scanner;

public class FileOp {
   
	private String fileName;

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

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

	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();
			}
			System.out.println("File saved.");
			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); // 将每一行添加进companes
			//	System.out.println(line); // 输出文件中每一行
			}
			
			for(String comp : companes) {
   		//从ArrayList中读取每一行
				System.out.println(comp);
			}
			
//			for (int i = 0; i < 4; i++) {		//只有四行,具体情况具体分析
				br.readLine();
//				System.out.println(br.readLine());		//输出文件中每一行
//			}
//			System.out.println("File	--   saved."); // 区分读取和写入
//			for (int i = 0; i < companes.size(); i++)  {		//	 从ArrayList中读取每一行,老师的说上面的while和for each
//				System.out.println(companes.get(i));
//			}
			
			br.close();
			fr.close();

		} catch (IOException e) {
    // 异常
			e.printStackTrace();
		}

	}
}

MainClass.java

package com.study.files;

public class MainClass {
   

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

}

CarGameProj

Car.java

package com.study.carGame;

import java.util.Random;

public class Car {
   
	protected String owner;
	protected String color;
	protected double speed;
	protected int duration;
	protected Tools status;

	private static String className = "CarClass";// static 静止的,初始化不能放在构造函数里
	private static String version = "1.0.0";

	public static String getClassName() {
   
		return className;
	}

	public static String getVersion() {
   
		return version;
	}

	// 默认构造函数 default constructor
	public Car() {
   
		this.owner = "
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值