面向对象程序设计(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 {
   
	private String owner;
	private String color;
	private double speed;
	private int duration;

	// 默认构造函数 default constructor
	public Car() {
   
		this.owner = "Rocky";
		this.color = "red";
		this.speed = 0.0;
		this.duration = 100;
	}

	// 有参构造函数 parameter constructor
	public Car(String owner, String color) {
   
		this.owner = owner;
		this.color = color;
		this.speed = 0.0;
		this.duration = 100;
	}

	// 展示当前车的信息
	public void show() {
   
		System.out.println(" ********** car information **********");
		System.out.println("Owner: " + this.owner);
		System.out.println("Color: " + this.color);
		System.out.println("Speed: " + this.speed);
		System.out.println("Duration: " + this.duration);
		System.out.println(" *************************************");
	}

	public void speedUp() {
    // 加速函数
		Random obj = new Random(); // 随机函数
		this.speed += obj.nextDouble() * 10; // 随机生成一个double类的值
	}

	public void speedDown() {
    // 减速函数
		Random obj = new Random(); // 随机函数
		this.speed -= obj.nextDouble() * 10; // 随机生成一个double类的值
		if (this.speed <= 0.0) {
    // 速度是大于等于0的
			this.speed = 0.0;
		}
	}

	public void crash() {
    // 碰撞造成耐久损失
		Random obj = new Random();
		// this.duration -= (obj.nextInt(10) + 1);//随机生成整数0-指定的值,但不包括指定的值
		if (this.duration <= 0) {
   
			System.out.println(this.owner + " lost the car."); // 车报废,说明
		}
		if (this.speed < 5) {
    // 复杂一点的设计 ,这部分不是老师写的
			this.duration -= (obj.nextInt(10) + 1);
			this.speed--;
		} else if (this.speed > 5) {
   
			this.duration -= (obj.nextInt(8) + 1);
			this.speed -= 3;
		} else if (this.speed > 10) {
   
			this.duration -= (obj.nextInt(6) + 1);
			this.speed -= 8;
		} else if (this.speed > 15) {
   
			this.duration -= (obj.nextInt(4) + 1);
			this.speed -= 13;
		}
	}
// public void 
}

Game.java

package com.study.carGame;

import java.util.Scanner;

public class Game {
   
	private Car obj1;
	private Car obj2;

	public void showMenu() {
   
		Scanner obj = new Scanner(System.in);
		int sel;
		while(true) {
   
			// Show an operation menu
			System.out.println(" ---------- Car Game ----------");
			System.out.println(" 1 -- Get two new cars");
			System.out.println(" 2 -- Speed up");
			System.out.println(" 3 -- Speed down");
			System.out.println(" 4 -- Crash!");
			System.out.println(" 0 -- Exit");
			System.out.println(" -----------------------------");
			
			// get a choice
			do {
   	//do-while 先进行一次操作,再进行判断是否继续进行操作
				System.out.print(" Please input your choice:");
				try {
   
					sel = obj.nextInt();
				}catch(Exception e) {
   	//设置sel =-1 可以使得while循环继续进行
					sel = -1;
					System.out.print(" Please input 0 - 4");	//提示
				}
			}while(sel<0 || sel>4); //循环条件不在 0 - 4之间
			// switch-case for operations
			switch(sel) {
   	//对每一种操作进行操作
			case 0: 
				System.exit(0);//结束程序的函数
			case 1:	
				obj1 = new Car("Tom", "Grey");		obj1.show();
				obj2 = new Car("Jerry", "red");		obj2.show();
				break;
			case 2:
				obj1.speedUp();		obj1.show();
				obj2.speedUp();		obj2.show();
				break;
			case 3:
				obj1.speedDown();	obj1.show();
				obj2.speedDown();	obj2.show();
				break;
			case 4:
				obj1.crash();	obj1.show();
				obj2.crash();	obj2.show();
				break;
			}
			
		}	
	}
}

MainClass.java

package com.study.carGame;

public class MainClass {
   

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

}

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务