Java day08 Parameter & Overload & Constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.*;
public class SpringPractice01
{
public static void main(String[] args)
{
System.out.println(max(1,4));
System.out.println(max(1.3,4.2, 5.6));
}

public static int max(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);
// }
public static double max(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

Variadic method

  1. 可变参数必须在parameter list的最后位置

  2. 一个method只能有一个

  3. int[] 不能和 int…同时存在,在底层他们是一样的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.*;
public class SpringPractice01
{
public static void main(String[] args)
{
System.out.println(sum());
System.out.println(sum(1,2,3,4,5));
}
/*
public static int sum(int[] nums)
{
int sum = 0;
for(int i = 0; i < nums.length; i++)
{
sum += nums[i];
}
return sum;
}
*/

public static int sum(int... nums)
{
int sum = 0;
for(int i = 0; i < nums.length; i++)
{
sum += nums[i];
}
return sum;
}
}

The main difference with the commented one is int… nums can run both array and random size number, but int[] nums can only accept array.

Find max between 1 and any array of numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.*;
public class SpringPractice01
{
public static void main(String[] args)
{
//System.out.println(sum());不可以,其中有非可变参数,必须传值
System.out.println(max(1,2,3,4,5));
}
//Find max between 1 and any array of numbers
public static int max(int num1, int... nums)
{
int max = num1;
for(int i = 0; i < nums.length; i++)
{
if(nums[i] > max)
{
max = nums[i];
}
}
return max;
}


}

Overload

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.*;
public class SpringPractice01
{
public static void main(String[] args)
{
GraphicTools g1 = new GraphicTools();
System.out.println(g1.getTriangleArea(3,4));
System.out.println(g1.getTriangleArea(3,4,5));
}
}

class GraphicTools
{
double getTriangleArea(double base, double height)
{
return (base * height) / 2;
}
double getTriangleArea(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
{
return 0.0;
}
}
}

Variadic method Exercise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.*;
public class SpringPractice01
{
public static void main(String[] args)
{
GraphicTools g1 = new GraphicTools();
System.out.println(g1.stringCombination("3","3", "4"));
System.out.println(g1.multi(1,2,3));
}
}

class GraphicTools
{
String stringCombination(String...args)
{
// String concatenation from 0, so 1 parameter
// is enough
String combination = "";
for(int i = 0; i < args.length; i++)
{
combination += args[i];
}
return combination;
}
long multi(int a, int...n)
{
long num1 = a;
for(int i = 0; i < n.length; i++)
{
num1 = num1 * n[i];
}
return num1;
}
}

Membe Variables and Local variables

1
2
3
4
5
(1) 成员变量如果没有初始化有默认值,局部变量没有初始化会报错
(2)局部变量,methods被调用,就开辟一块内存给methods的parameter list,methods执行结束就结束
成员变量,随着对象的创建而分配,对象垃圾回收而结束
(3)成员变量有很多修饰符
局部变量只有final
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.*;
public class SpringPractice01
{
/*
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
}
{


*/
public static void main(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;
}
}

class Student
{
String name;// member
int age; // member

void set(String n, int a) // n & a is local variable
{
name = n;
age = a;
}
}

Object array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;

/*
array可以存基本数据类型,也可以存引用数据类型

String[] arr; 存了一组字符串对象
Student[] arr:存了一组学生对象
*/
public class SpringPractice01
{
public static void main(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());
}

}
}
class Circle
{
double radius;

double getArea()
{
return 3.14 * radius * radius;
}
void printInfo()
{
System.out.println("Radius is " + radius + " area "
+ getArea());
}
}

Object array exercise 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;

/*
array可以存基本数据类型,也可以存引用数据类型

创建一个对象数组,长度为3
输入名字和成绩
打印名字和成绩
用冒泡排序,从高到低打印成绩
*/
public class SpringPractice01
{
public static void main(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);
}

for(int i = 1; i < stu.length ; i++)
{
for(int j = 0; j < stu.length - i; j++)
{
if(stu[j].score < stu[j + 1].score)
{
double temp = stu[j].score;
stu[j].score = stu[j+1].score;
stu[j+1].score = temp;
}
}
}

for(int i = 0; i < stu.length; i++)
{
System.out.println(stu[i].name + stu[i].score);
}
}
}

class Student
{
String name;
double score;
}

Encasulated version for the above program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.*;
public class SpringPractice01
{
public static void main(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());
}
}
}

class Student
{
private String name;
private double score;

public String getName()
{
return name;
}

public void setName(String n)
{
name = n;
}

public double getScore()
{
return score;
}

public void setScore(Double s)
{
score = s;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;

public class SpringPractice01
{
public static void main(String[] args)//args is local variable
{
int[] arr = {3, 4, 5, 6};
int target = 5;
System.out.println(binarySearch(arr, target));

}

public static int binarySearch(int[] arr, int target)
{
int start = 0, end = arr.length - 1;
while (start <= end)
{
int mid = (start + end) / 2;
if (target == arr[mid])
{
return mid;
} else if (target > arr[mid])
{
start = mid + 1;
} else
{
end = mid - 1;
}
}
return -1;
}
}

Encapsulation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(1) 封装的实现靠权限修饰符来控制可见的范围
本类 本包 其他包子类 任意位置
private: Y N N N
(default): Y Y N N
protected: Y Y Y N
public Y Y Y Y

(2)哪些可以加权限修饰
class, attribute, methods, constructor, 内部类

(3) 分别可以加什么权限修饰符
class: default / public, 如果class前面有public, 那么必须与源文件名相同
attribute: All
methods: All

(4) 大多数情况下属性的封装都是private, 提供get/set methods
(5) 大多数情况下methods都是public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class SpringPractice01
{
public static void main(String[] args)//args is local variable
{
Circle c1 = new Circle();
//c1.radius = 1.0
c1.setRadius(3.0);
System.out.println(c1.getRadius());//不set就是默认值,因为radius是member variable
}
}

class Circle
{
private double radius;//属性封装
public void setRadius(double r)
{
if(r > 0)//control assignment
{
radius = r;
}
}

public double getRadius()
{
return radius;
}
}

Encapsulation exercise

1
2
3
4
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
public class SpringPractice01
{
public static void main(String[] args)//args is local variable
{
Scanner scan = new Scanner(System.in);
Student s1 = new Student();
s1.setName(scan.next());
s1.setAge(scan.nextInt());
s1.setScore(scan.nextDouble());
s1.getInfo();
}
}

class Student
{
private String name;
private int age;
private double score;

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

public double getScore()
{
return score;
}

public void setScore(double score)
{
this.score = score;
}

void getInfo()
{
System.out.println("Name is " + getName() + " age is " +
getAge() + " score is " + getScore());
}
}

Constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1. constructor: create object
和new一起使用,每次调用它就是创建新的对象

constructor 长得像方法,编译后是一个实例初始化方法

2. 所以class都有constructor, 如果一个class没有显式constructor, 编译器
会生成默认的无参数constructor
如果declare constructor,编译器就不会生成默认的

3.语法结构
public Circle()
{
}

或者

public Circle(double d)
{
}

4. Constructor没有返回值类型,也不写void
5. constructor可以重载

Constructor with array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class SpringPractice01
{
public static void main(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();
}
}

class Circle
{
private double[] radius;
private int permeter;
public Circle(double[] r, int p)
{
radius = r;
permeter = p;
}
public void printInfo()
{
for(int i = 0; i < radius.length; i++)
{
System.out.println("Radius is " + radius[i] + " " + permeter);
}
}

public void setRadius(double[] radius)
{
this.radius = radius;
}
}

Constructor exercise

  1. Declare Student class

    • private attributes: name, age, score
    • Provide parameter constructor and non parameter constructor
    • Provide public get/set
    • Provide getInfo() methods, return info of Student class
  2. In main

    • Use both constructor to create variables
    • set attribute
    • call getinfo()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class SpringPractice01
{
public static void main(String[] args)//args is local variable
{
Student s1 = new Student();
s1.setName("ALEX");
s1.setAge(20);
s1.setScore(88.2);

Student s2 = new Student("Bryce", 23, 98);

System.out.println(s1.getInfo());
System.out.println(s2.getInfo());
}
}

class Student
{
private String name;
private int age;
private double score;

public Student() {}

public Student(String name, int age, double score)
{
this.name = name;
this.age = age;
this.score = score;
}

public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}

public void setAge(int age)
{
this.age = age;
}

public int getAge()
{
return age;
}

public void setScore(double score)
{
this.score = score;
}

public double getScore()
{
return score;
}

public String getInfo()
{
return "Name " + name + " Age " + age + " Score " + score;
}
}