publicstaticintmax(int a, int b) { return Math.max(a, b); } //Java doesn't care return value //As long as parameter list not equal, that is overload // public static int max(double a, double b, double c) // { // return Math.max(Math.max(a, b), c); // } publicstaticdoublemax(double a, double b, double c) { return Math.max(Math.max(a, b), c); } }
Java doesn’t care return value(public static double…) (public static int…) As long as parameter list not equal, that is overload
import java.util.*; publicclassSpringPractice01 { publicstaticvoidmain(String[] args) { //System.out.println(sum());不可以,其中有非可变参数,必须传值 System.out.println(max(1,2,3,4,5)); } //Find max between 1 and any array of numbers publicstaticintmax(int num1, int... nums) { int max = num1; for(int i = 0; i < nums.length; i++) { if(nums[i] > max) { max = nums[i]; } } return max; }
classGraphicTools { doublegetTriangleArea(double base, double height) { return (base * height) / 2; } doublegetTriangleArea(double a, double b, double c) { if(a + b > c && a + c > b && b + c > a) { double s = (a + b + c) / 2; return Math.sqrt(s * (s - a) * (s - b) * (s - c)); } else { return0.0; } } }
import java.util.*; publicclassSpringPractice01 { /* Member variables: in class, out of methods, stored in head class { int foo;//member variable void foobar() { } } Local variables: in methods, stored in stack - parameter are local variables class { void foobar() { int foo; // local variable } { */ publicstaticvoidmain(String[] args)//args is local variable { String name = "Bryce"; // name is local variable int age = 23;// age is local variable
Student s1 = new Student(); // s1 is local s1.name = name; s1.age = age; } }
classStudent { String name;// member int age; // member
voidset(String n, int a)// n & a is local variable { name = n; age = a; } }
/* array可以存基本数据类型,也可以存引用数据类型 String[] arr; 存了一组字符串对象 Student[] arr:存了一组学生对象 */ publicclassSpringPractice01 { publicstaticvoidmain(String[] args)//args is local variable { //use array to store five radius object, radius from //1 - 5
// 1. declare object array Circle[] arr = new Circle[5];// "new" is not creating circle object, it is creating array object
// 2. traverse and assign values for(int i = 0; i < arr.length; i++) { //arr[i].radius = i + 1;//NullPointerException //要在遍历赋值的时候创建对象 arr[i] = new Circle(); arr[i].radius = i + 1; }
// 3. print for(int i = 0; i < arr.length; i++) { arr[i].printInfo(); //System.out.println("Radius is " + arr[i].radius + " area " //+ arr[i].getArea()); }
/* array可以存基本数据类型,也可以存引用数据类型 创建一个对象数组,长度为3 输入名字和成绩 打印名字和成绩 用冒泡排序,从高到低打印成绩 */ publicclassSpringPractice01 { publicstaticvoidmain(String[] args)//args is local variable { Scanner scan = new Scanner(System.in); Student[] stu = new Student[3]; for(int i = 0; i < stu.length; i++) { stu[i] = new Student(); System.out.println("Please enter name for " + (i+1)); stu[i].name = scan.next(); System.out.println("Please enter score for " + (i+1)); stu[i].score = scan.nextDouble(); }
for(int i = 0; i < stu.length ; i++) { System.out.println(stu[i].name + " " + stu[i].score); }
import java.util.*; publicclassSpringPractice01 { publicstaticvoidmain(String[] args)//args is local variable { Scanner scan = new Scanner(System.in); Student[] stu = new Student[3]; for(int i = 0; i < stu.length; i++) { stu[i] = new Student(); System.out.println("Please enter name for " + (i+1)); stu[i].setName(scan.next()); System.out.println("Please enter score for " + (i+1)); stu[i].setScore(scan.nextDouble()); }
for(int i = 0; i < stu.length ; i++) { System.out.println(stu[i].getName() + " " + stu[i].getScore()); }
for(int i = 1; i < stu.length ; i++) { for(int j = 0; j < stu.length - i; j++) { if(stu[j].getScore() < stu[j + 1].getScore()) { /* Need to note that we need to set score, instead of get one */ double temp = stu[j].getScore(); stu[j].setScore(stu[j+1].getScore()); stu[j+1].setScore(temp); } } }
for(int i = 0; i < stu.length; i++) { System.out.println(stu[i].getName() + stu[i].getScore()); } } }
1, Declare student class 2. Attributes: Name, Age, Score and use private to encapsulate 3. Provide getInfo() to return info of Student class 4. Use set() to assign values, use getInfo to get student info
publicclassSpringPractice01 { publicstaticvoidmain(String[] args)//args is local variable { double[] list = {3.2,2.3,3.4,5.6}; double[] new_list = {3.26, 7.8}; Circle c = new Circle(list, 3); c.setRadius(new_list); c.printInfo(); } }