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();
}
}
CarGameProj
(更新时间:2020年10月14日 16:30)
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";
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 = "Rocky";
this.color = "red";
this.speed = 0.0;
this.duration = 100;
this.status = Tools.NONE;
}
// parameter constructor
public Car(String owner, String color) {
this.owner = owner;
this.color = color;
this.speed = 0.0;
this.duration = 100;
this.status = Tools.NONE;
}
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;
}
public void speedDown() {
Random obj = new Random();
this.speed -= obj.nextDouble() * 5;
if (this.speed <= 0.0) {
this.speed = 0.0;
}
}
public void useTool(Tools theTool) {
if (theTool == Tools.LIFEUP) {
this.duration = 100;
this.status = Tools.NONE;
} else if (theTool == Tools.IRONFIRM) {
this.status = Tools.IRONFIRM;
} else if (theTool == Tools.FLASK) {
this.status = Tools.FLASK;
}
}
public boolean crash() {
Random obj = new Random();
int damage = (1+obj.nextInt(10));
this.computeDamage(damage);
if (this.duration<=0) {
System.out.println(this.owner + " lost the car.");
return false;
} else {
return true;
}
}
public boolean crash(Tools theTool) {
if (theTool == Tools.HALFKILL) {
// check the status
this.computeDamage(50);
}
if (this.duration<=0) {
System.out.println(this.owner + " lost the car.");
return false;
} else {
return true;
}
}
protected void computeDamage(int damage) {
Random obj = new Random();
int rnd;
if (this.status == Tools.NONE) {
this.duration -= damage;
} else if (this.status == Tools.IRONFIRM) {
this.duration -= damage/2;
} else if (this.status == Tools.FLASK) {
rnd = obj.nextInt(100);
if (rnd >= 80) this.duration -= damage;
}
}
}
Game.java
package com.study.carGame;
import java.util.Scanner;
public class Game {
private ArmoredCar obj1;
private Car obj2;
public void showMenu() {
@SuppressWarnings("resource")
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(" 5 -- Half Kill!");
System.out.println(" 6 -- Show class information!");
System.out.println(" 0 -- Exit");
System.out.println("----------------------------------");
// get a choice
do {
System.out.print("Please input your choice: ");
try {
sel = obj.nextInt();
} catch (Exception e) {
sel = -1;
}
} while(sel < 0 || sel > 6);
// switch-case for operations
switch(sel) {
case 0:
exitGame();
break;
case 1:
obj1 = new ArmoredCar("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:
if (obj1.crash())
obj1.show();
else
exitGame();
if (obj2.crash())
obj2.show();
else
exitGame();
break;
case 5:
obj1.useTool(Tools.IRONFIRM);
obj2.useTool(Tools.FLASK);
if (obj1.crash(Tools.HALFKILL))
obj1.show();
else
exitGame();
if (obj2.crash(Tools.HALFKILL))
obj2.show();
else
exitGame();
break;
case 6:
System.out.println("Class Name: " + Car.getClassName());
System.out.println("Version: " + Car.getVersion());
break;
}
// system("pause");
System.out.println("Press Enter to continuee...");
obj.nextLine();
obj.nextLine();
}
}
private void exitGame() {
System.out.println("------------ Bye Bye ------------");
System.exit(0);
}
}
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();
}
}
Tools.java
package com.study.carGame;
public enum Tools {
NONE, LIFEUP, IRONFIRM, FLASK, HALFKILL;
}
Armored.java
package com.study.carGame;
import java.util.Random;
public class ArmoredCar extends Car {
protected int armorLevel;
private static String ClassName = "ArmoredCar class";
private static String Version = "1.0.0";
public static String getClassName() {
return ClassName;
}
public static String getVersion() {
return Version;
}
public ArmoredCar() {
super();
this.duration = 200;
this.armorLevel = 10;
// TODO Auto-generated constructor stub
}
public ArmoredCar(String owner, String color) {
super(owner, color);
// TODO Auto-generated constructor stub
this.duration = 200;
this.armorLevel = 10;
}
public void incressArmor() {
this.armorLevel += 10;
}
public boolean crash() {
Random obj = new Random();
int damage = (1+obj.nextInt(10));
damage = damage - this.armorLevel;
if (damage<0) damage = 0;
this.computeDamage(damage);
if (this.duration<=0) {
System.out.println(this.owner + " lost the car.");
return false;
} else {
return true;
}
}
public boolean crash(Tools theTool) {
int damage = 50;
if (theTool == Tools.HALFKILL) {
// check the status
this.computeDamage(50);
}
damage = damage - this.armorLevel;
if (damage<0) damage = 0;
this.computeDamage(damage);
if (this.duration<=0) {
System.out.println(this.owner + " lost the car.");
return false;
} else {
return true;
}
}
}
ShapesProj
Factory.java
package com.study.shapes;
import java.util.Scanner;
public class Factory {
private final int n = 5;
Shape[] shapes;
public Factory() {
shapes = new Shape[n];
}
public void operate() {
double area,totalArea = 0.0;
Scanner obj = new Scanner(System.in);
int sel;
for (int i = 0; i < n; i++) {
// 显示菜单
System.out.println("=========菜单=========");
System.out.println("1. 正方形");
System.out.println("2. 三角形");
System.out.println("=====================");
// 选择
do {
try {
System.out.println("你的选择是:");
sel = obj.nextInt();
} catch (Exception e) {
sel = -1;
}
} while (sel < 1 || sel > 2);
// 生成图形
switch(sel) {
case 1:
shapes[i] = new Square();
break;
case 2:
shapes[i] = new Triangle();
break;
}
// 求面积
area = shapes[i].area();
System.out.println("图形面积是: "+ area + "\n");
totalArea += area;
}
System.out.println(n + "个图形的总面积是: " + totalArea);
}
}
MainClass.java
package com.study.shapes;
public class MainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
Factory obj = new Factory();
obj.operate();
}
}
Shape.java
package com.study.shapes;
public abstract class Shape {
public abstract double area();
}
Square.java
package com.study.shapes;
import java.util.Scanner;
public class Square extends Shape {
private double side;
public Square() {
@SuppressWarnings("resource")
Scanner obj = new Scanner(System.in);
try {
System.out.print("请输入正方形的边长:");
this.side = obj.nextDouble();
}catch(Exception e) {
this.side = 1.0;
}
}
@Override
public double area() {
// TODO Auto-generated method stub
return side * side;
}
}
Triangle.java
package com.study.shapes;
import java.util.Scanner;
public class Triangle extends Shape {
private double bottom;
private double height;
public Triangle() {
@SuppressWarnings("resource")
Scanner obj = new Scanner(System.in);
try {
System.out.print("请输入三角形的底和高:");
this.bottom = obj.nextDouble();
this.height = obj.nextDouble();
}catch(Exception e) {
this.bottom = 2.0;
this.height = 1.0;
}
}
@Override
public double area() {
// TODO Auto-generated method stub
return 0.5 * this.bottom * this.height;
}
}
本文由 小但 创作
全文共:15551个字
采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载,均为作者原创,转载前请务必署名
最后编辑时间为: Oct 21, 2020 at 03:18 pm