//Scanner这样写?
Scanner input = new Scanner(System.in);
//不断获得下一个单词
names[i] = toTitleCase(input.next());
inheritance
• Constructors are inherited, but use super()
public class Parent { public Parent() {
do some code; }
}
public class Child extends Parent {
public Child() {
super(); // call Parent() do some more code;
} }
so in a main method:
Child c = new Child(); // Calls Parent(), too
Default constructor == no parameters
第72页,子类能加新方法吗?
//yuec2 yue cheng package exam1; //import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; public class ScoreBoard { String[] players; int[][] playerScores; Scanner input = new Scanner(System.in); //do not change this method public static void main(String[] args) { ScoreBoard scoreBoard = new ScoreBoard(); scoreBoard.readScores("GameScores.txt"); scoreBoard.showScores(scoreBoard.getMenuChoice()); } //do not change this method int getMenuChoice() { System.out.println("*** Score Board ***"); System.out.println("1. List all the scores of a player"); System.out.println("2. Find a player's average score"); System.out.println("3. Find all highest scores"); System.out.println("4. Exit"); int choice = input.nextInt(); input.nextLine(); return choice; } void readScores(String filename) { //write your code here try { int counts=0; Scanner sc = new Scanner((new FileReader("GameScores.txt"))); while(sc.hasNextLine()) { sc.nextLine(); counts++;} players = new String[counts]; playerScores = new int[counts][]; sc = new Scanner((new FileReader("GameScores.txt"))); for(int i=0; i< counts; i++) { String thisline= sc.nextLine(); players[i] = thisline.split(":")[0].trim(); String[] temp = thisline.split(":")[1].trim().split(","); int size = temp.length; playerScores[i] = new int[size]; for (int j = 0; j < size; j++) { playerScores[i][j] = Integer.valueOf(temp[j].trim()); } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } void showScores(int choice) { //write your code here if (choice == 1) { String name = input.nextLine(); findPlayerScores(name); }else if (choice == 2) { String name = input.nextLine(); findPlayerAverageScore(name); }else if (choice == 3) { findAllHighestScores(); }else if (choice == 4) { System.out.println("Good bye!"); } return ; } int[] findPlayerScores(String name) { //write your code here int index = -1; for (int i = 0; i < players.length; i++) { //System.out.println("players[i] = " + players[i]); if (players[i].toLowerCase().equals(name.toLowerCase())) index = i; } //corner case if (index == -1) return null; //System.out.println("index = " + index); //System.out.println(players[index] +"'s scores\n"); for (int j = 0; j < playerScores[index].length; j++) { System.out.println(playerScores[index][j]); } return playerScores[index]; } float findPlayerAverageScore(String name) { //write your code here int[] scores = findPlayerScores(name); float sum = 0; for(int i = 0; i < scores.length; i++) { sum += scores[i]; } System.out.printf("%s's average score is %.2f", name, sum / scores.length); return sum / scores.length; } int[] findAllHighestScores() { //write your code here int[] highestScores = new int[players.length]; int max = Integer.MIN_VALUE; for (int i = 0; i < players.length; i++) { int size = playerScores[i].length; for (int j = 0; j < size; j++) { max = Math.max(max, playerScores[i][j]); //System.out.println("max = " + max); } highestScores[i] = max; max = Integer.MIN_VALUE; System.out.println(highestScores[i]); } return highestScores; //return null; } }
//里面的参数竟然是FileReader
Scanner sc = new Scanner((new FileReader("GameScores.txt")));
/就改个这里就行了 .toLowerCase()
System.out.println //自动竖行分隔
//不能这样用,因为里面有打印,要再写一遍
//int[] scores = findPlayerScores(name);
System.out.printf("%s's average score is %.2f", name, sum / scores.length);
//打印和返回分开?不应该啊
return (float)sum / scores.length;
for (int i = 0; i < players.length; i++) {
//max的定义必须在循环里面啊
exam2
Caesar
//yuec2 Yue Cheng package exam2; public class Caesar extends Message{ Caesar(String text, String key) { super(text, key); // TODO Auto-generated constructor stub } @Override public String encrypt() { // TODO Auto-generated method stub //handle the return case int keyNumber = Integer.valueOf(key); StringBuilder encryptedSb = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char charToEncrypt = text.charAt(i); char encryptedChar = (char) (charToEncrypt + keyNumber); if (encryptedChar > 'Z') encryptedChar = (char) (encryptedChar + ('A' - 'Z' -1)); encryptedSb.append(encryptedChar); } return encryptedSb.toString(); } @Override public String decrypt() { // TODO Auto-generated method stub int keyNumber = Integer.valueOf(key); StringBuilder decryptedSb = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char charToDecrypt = text.charAt(i); System.out.println("charToDecrypt = " + charToDecrypt); char decryptedChar = (char) ((int)charToDecrypt - keyNumber); //wrong System.out.println("decryptedChar1 = " + decryptedChar); if (decryptedChar > 'Z') decryptedChar = (char) (decryptedChar + ('A' - 'Z' - 1)); decryptedSb.append(decryptedChar); } return decryptedSb.toString(); } @Override boolean validateInput() { // TODO Auto-generated method stub text = text.toUpperCase(); for (int i = 0; i < text.length(); i++) { if (text.charAt(i) < 'A' || text.charAt(i) > 'Z') return false; } //text = text.toLowerCase(); //get all digist in key to numbers //Integer number = Integer.valueOf(key); for (int i = 0; i < key.length(); i++) { int digit = Integer.valueOf(key.charAt(i)); if (digit < 0 || digit > 9) return false; } return true; } }
Keyword
//yuec2 Yue Cheng package exam2; public class Keyword extends Message{ Keyword(String text, String key) { super(text, key); // TODO Auto-generated constructor stub } @Override public String encrypt() { // TODO Auto-generated method stub char[] keyChars = key.toCharArray(); StringBuilder encryptedSb = new StringBuilder(); //handle the repeat case //correct //System.out.println("text.length()="+ text.length()); for (int i = 0; i < text.length(); i++) { //if (i == keyChars.length - 1) i = 0; char charToEncrypt = text.charAt(i); //System.out.println("charToEncrypt="+ charToEncrypt); char encryptedChar = (char) (charToEncrypt + (keyChars[i] % 'A' + 1)); if (encryptedChar > 'Z') encryptedChar = (char) (encryptedChar + ('A' - 'Z' -1)); //System.out.println("encryptedChar="+ encryptedChar); encryptedSb.append(encryptedChar); } return encryptedSb.toString(); } @Override public String decrypt() { // TODO Auto-generated method stub char[] keyChars = key.toCharArray(); StringBuilder decryptedSb = new StringBuilder(); //handle the repeat case for (int i = 0; i < text.length(); i++) { //if (i == keyChars.length - 1) i = 0; char charToDecrypt = text.charAt(i); char decryptedChar = (char) (charToDecrypt - (keyChars[i] % 'A' + 1)); //wrong System.out.println("decryptedChar1="+decryptedChar); if (decryptedChar > 'Z') decryptedChar = (char) (decryptedChar + ('A' - 'Z' -1)); System.out.println("decryptedChar2="+decryptedChar); decryptedSb.append(decryptedChar); } System.out.println("decryptedSb.toString()="+decryptedSb.toString()); return decryptedSb.toString(); } @Override boolean validateInput() { // TODO Auto-generated method stub text = text.toUpperCase(); for (int i = 0; i < text.length(); i++) { if (text.charAt(i) < 'A' || text.charAt(i) > 'Z') return false; } key = key.toUpperCase(); for (int i = 0; i < key.length(); i++) { if (key.charAt(i) < 'A' || key.charAt(i) > 'Z') return false; } return true; } }
//转格式要加到try catch中
int keyNumber; try{ keyNumber = Integer.valueOf(key); }catch(NumberFormatException ex){ // handle your exception keyNumber = 0; }
exam3:
关键字this 是用来指向当前对象或类实例的
抽象类和接口的对比
参数 | 抽象类 | 接口 |
默认的方法实现 | 它可以有默认的方法实现 | 接口完全是抽象的。它根本不存在方法的实现 |
实现 | 子类使用extends关键字来继承抽象类。如果子类不是抽象类的话,它需要提供抽象类中所有声明的方法的实现。 | 子类使用关键字implements来实现接口。它需要提供接口中所有声明的方法的实现 |
构造器 | 抽象类可以有构造器 | 接口不能有构造器 |
与正常Java类的区别 | 除了你不能实例化抽象类之外,它和普通Java类没有任何区别 | 接口是完全不同的类型 |
访问修饰符 | 抽象方法可以有public、protected和default这些修饰符 | 接口方法默认修饰符是public。你不可以使用其它修饰符。 |
main方法 | 抽象方法可以有main方法并且我们可以运行它 | 接口没有main方法,因此我们不能运行它。 |
多继承 | 抽象方法可以继承一个类和实现多个接口 | 接口只可以继承一个或多个其它接口 |
速度 | 它比接口速度要快 | 接口是稍微有点慢的,因为它需要时间去寻找在类中实现的方法。 |
添加新方法 | 如果你往抽象类中添加新的方法,你可以给它提供默认的实现。因此你不需要改变你现在的代码。 | 如果你往接口中添加方法,那么你必须改变实现该接口的类。 |
什么时候使用抽象类和接口
- 如果你拥有一些方法并且想让它们中的一些有默认实现,那么使用抽象类吧。
- 如果你想实现多重继承,那么你必须使用接口。由于Java不支持多继承,子类不能够继承多个类,但可以实现多个接口。因此你就可以使用接口来解决它。
- 如果基本功能在不断改变,那么就需要使用抽象类。如果不断改变基本功能并且使用接口,那么就需要改变所有实现了该接口的类。
EXAM3
//override the hashcode method
//按照name来对object进行哈希
public int hashCode(){
return Objects.hash(name);
}
//hash物体的equals必须这么写,只有比较的参数:name最重要
public boolean equals(Object o){
if (o == null) return false;
if (this == o) return true;// make sure they
//are not pointing to the same location
if (getClass() != o.getClass()) return false;
Member m = (Member) o;
//compare by name
if(name.equals(m.name)) return true;
else return false;
}
//用this关键字代表当前object
String name = this.members[i];
//有点忘了还能这么写
Collections.sort(memberList);
//排序一开始就建好了
Collections.sort(memberList);
TreeMap