2022年09月23日Java上机

示例3-2

JBT3202

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class A_JBT3202 {
int x, y;
float z;

double add(double f1, double f2) {
return f1 + f2;
}

int add(int i1, int i2) {
return i1 + i2;
}
}

public class JBT3202 {
public static void main(String args[]) {
A_JBT3202 a = new A_JBT3202();
System.out.println("浮点数相加=" + a.add(1.1, 2.2));
System.out.println("整数相加=" + a.add(1, 2));
}
}

示例3-3

JBT3301

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
class A_JBT3301 {
int x;
int y;

void set(int x, int y) {
this.x = x;
this.y = y;
}

int getx() {
return x;
}

int gety() {
return y;
}

int add() {
return (x + y);
}
}

public class JBT3301 {
public static void main(String args[]) {
//创建对象方式1
A_JBT3301 a1;
a1 = new A_JBT3301();
a1.set(1, 2);
System.out.println(a1.x + "+" + a1.y + "=" + a1.add());
System.out.println(a1.getx() + "+" + a1.gety() + "=" + a1.add());
//创建对象方式2
A_JBT3301 a2 = new A_JBT3301();
a2.set(3, 4);
System.out.println(a2.x + "+" + a2.y + "=" + a2.add());
System.out.println(a2.getx() + "+" + a2.gety() + "=" + a2.add());
a1 = null;
a2 = null;
}
}

示例3-7

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
class A {
static int x;
int y;

void setx(int x0) {
x = x0;
}

void sety(int y0) {
y = y0;
}

void showx() {
System.out.println("x=" + x);
}

void showy() {
System.out.println("y=" + y);
}
}

public class JBT3404 {
public static void main(String args[]) {
A a1 = new A();
A a2 = new A();
//start1
System.out.println("通过a1来设置x和y的值");
a1.setx(1);
a1.showx();
a2.showx();
a1.sety(2);
a1.showy();
a2.showy();
//start2
System.out.println("通过a2来设置x和y的值");
a2.setx(3);
a1.showx();
a2.showx();
a2.sety(4);
a1.showy();
a2.showy();
}
}

示例3-15

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Point {
int x, y;

void setXY(int x1, int y1) {
x = x1;
y = y1;
}

void squarePoint(Point p) {
p.x = p.x * p.x;
p.y = p.y * p.y;
}
}

public class JBT3505 {
public static void main(String args[]) {
Point p0 = new Point();
p0.setXY(10, 20);
System.out.println("x=" + p0.x + "...y=" + p0.y);
p0.squarePoint(p0);
System.out.println("x=" + p0.x + "...y=" + p0.y);
}
}

示例3-16

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class JBT3506 {
public static void main(String args[]) {
int a[] = new int[6];
for (int i = 0; i < a.length; i++)
a[i] = i;
for (int i = 0; i < a.length; i++)
System.out.println("a[" + i + "]=" + a[i]);
addN(a, 10);
for (int i = 0; i < a.length; i++)
System.out.println("a[" + i + "]=" + a[i]);
}

static void addN(int b[], int N) {
for (int i = 0; i < b.length; i++)
b[i] = b[i] + N;
}
}

课后4-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
public class Main {
public static void main(String args[]) {
myBox obj = new myBox();
int x = 10, y = 20;
obj.setvalue(x, y);
System.out.println("width=" + obj.width + " length=" + obj.length);
System.out.println("周长=" + obj.circle());
System.out.println("面积=" + obj.area());
}
}

class myBox {
int width, length;

void setvalue(int width, int length) {
this.width = width;
this.length = length;
}

int circle() {
return 2 * (width + length);
}

int area() {
return width * length;
}
}

2022年09月23日Java上机
https://www.eldpepar.com/coding/46902/
作者
EldPepar
发布于
2022年9月22日
许可协议