Question One
函数应用
import java.util.Arrays;public class Main{public static void main(String[] args){String s1 = "you are a student";String s2 = "you are a ";// 判断前缀if(s1.startsWith(s2)){System.out.println("\"" + s1 + "\"" + " starts with " + "\"" + s2 + "\"");}char[] c = s1.toCharArray();System.out.println(Arrays.toString(c));int[] num = new int[] {1, 2, 3, 4, 5};System.out.println(Arrays.toString(num));}
}
Question Two
编写一个Java程序,完成以下功能:
(1)声明一个名为 name 的String 对象,内容是“My name is Networkcrazy”;
(2)打印字符串的长度;
(3)打印字符串中的第一个字符;
(4)打印字符串中的最后一个字符;
(5)打印字符串中的第一个单词;
(6)打印字符串中crazy的位置。
public class Main{public static void main(String[] args) {String name = new String("My name is NetworkCrazy");System.out.println(name.length());System.out.println(name.charAt(0));System.out.println(name.charAt(name.length() - 1));System.out.println(name.substring(0, name.indexOf(" ")));System.out.println("[" + name.indexOf("Crazy") + "," + (name.indexOf("Crazy") + 4) + "]");}
}
Question Three
文件流
import java.io.FileInputStream;public class Main{public static void main(String[] args) {try{FileInputStream fis = new FileInputStream("C:\\Users\\95432\\Desktop\\Test.txt");int b;while((b = fis.read()) != -1){System.out.print((char)b);}}catch (Exception e){System.out.println("出错了, 原因是: " + e.getMessage());}}
}
FileInputStream fis = new FileInputStream("pos");
int x = fis.read();
if(x != -1){that means we read a byte of data ;if we want to translate it into a char, just use (char)(x) ;
}
Question Four
用类的对象初始化类
class Point{public int x;public int y;public Point(int x, int y){this.x = x;this.y = y;}
}class Ellipse{public Point center;public int a, b;public Ellipse(Point center, int a, int b){this.center = center;this.a = a;this.b = b;}public double getArea(){return Math.PI * a * b;}
}public class Main{public static void main(String[] args) {Point p1 = new Point(10, 20);Ellipse e1 = new Ellipse(p1, 25, 15);System.out.println(e1.getArea());}
}
Question Five
Exception Catch - NullPointerException
class Point{public int x;public int y;public Point(int x, int y){this.x = x;this.y = y;}
}class Ellipse{public Point center;public int a, b;public Ellipse(Point center, int a, int b){this.center = center;this.a = a;this.b = b;}public double getArea(){return Math.PI * a * b;}
}class Test{public static void test(){String s = null;try{if((s != null) & (s.length() > 0)){}}catch(Exception e){System.out.println(e.toString());
// System.out.println(e.toString() + " " + e.getMessage());}try{if((s != null) && (s.length() > 0)){}}catch(Exception e){System.out.println(e.toString());
// System.out.println(e.toString() + " " + e.getMessage());}try{if((s == null) | (s.length() == 0)){}}catch(Exception e){System.out.println(e.toString());
// System.out.println(e.toString() + " " + e.getMessage());}try{if((s == null) || (s.length() == 0)){}}catch(Exception e){System.out.println(e.toString());
// System.out.println(e.toString() + " " + e.getMessage());}}
}public class Main{public static void main(String[] args) {Point p1 = new Point(10, 20);Ellipse e1 = new Ellipse(p1, 25, 15);System.out.println(e1.getArea());Test.test();}
}
- output res
java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null
Question Six
异常捕获类型演示
public class Main{public static void main(String[] args) {for(int i = 0; i < 4; i ++){int k;try{switch(i){case 0:int zero = 0;k = 911 / zero;break;case 1:int b[] = null;k = b[0];break;case 2:int c[] = new int[2];k = c[9];break;case 3:char ch = "abc".charAt(99);break;}}catch (Exception e){System.out.println(e.toString());}}}
}
从上述例子可以看出,如果需要捕获多种异常,放到一个 try-catch 语句块里面即可
出现以下四种类型的异常 :
ArithmeticException : / by zero
NullPointerException : null
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
Question Seven
split the string into an array of integers by space and sort it
public class Main{public static void main(String[] args) {String str = "20 79 -1 0 0 0 -2";String[] numsOfStr = str.split(" ");int[] nums = new int[numsOfStr.length];for (int i = 0; i < numsOfStr.length; i++) {nums[i] = Integer.parseInt(numsOfStr[i]);}System.out.println(java.util.Arrays.toString(nums));java.util.Arrays.sort(nums);System.out.println(java.util.Arrays.toString(nums));}
}