FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Java 3/HW1Lesson.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Java 3
/
HW1Lesson.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
124 lines (100 loc) · 3.09 KB
Breadcrumbs
JavaExercises
/
Java 3
/
HW1Lesson.java
Copy path
File metadata and controls
124 lines (100 loc) · 3.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Java. Level 3. Lesson 1. Homework
*
* 1. Write a method which changes the places of the two elements of the array.
* (array can be of any reference type)
* 2. Write method which converts an array in ArrayList
* 3. "Big task" about fruits (apples, oranges) and baskets (box)
*
* @author Sergey Iryupin
* @version Feb 03, 2018
*/
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
public
class
HW1Lesson
{
public
static
void
main
(
String
[]
args
) {
HW1Lesson
hw
=
new
HW1Lesson
();
// tasks 1, 2
String
[]
strings
= {
"First"
,
"Second"
,
"Third"
,
"Fourth"
,
"Five"
};
System
.
out
.
println
(
hw
.
convertArrayToList
(
strings
));
// task 2
hw
.
swapElements
(
0
,
2
,
strings
);
System
.
out
.
println
(
Arrays
.
toString
(
strings
));
Integer
[]
numbers
= {
1
,
2
,
3
,
4
,
5
,
6
};
System
.
out
.
println
(
hw
.
convertArrayToList
(
numbers
));
// task 2
hw
.
swapElements
(
0
,
2
,
numbers
);
System
.
out
.
println
(
Arrays
.
toString
(
numbers
));
// task 3
Box
<
Apple
>
appleBox
=
new
Box
<>();
Box
<
Orange
>
orangeBox
=
new
Box
<>();
for
(
int
i
=
0
;
i
<
5
;
i
++) {
appleBox
.
add
(
new
Apple
());
orangeBox
.
add
(
new
Orange
());
}
System
.
out
.
println
(
appleBox
.
getFruits
());
System
.
out
.
println
(
orangeBox
.
getFruits
());
Box
<
Apple
>
appleBox1
=
new
Box
<>();
appleBox1
.
add
(
new
Apple
());
appleBox
.
moveAllToNextBox
(
appleBox1
);
System
.
out
.
println
(
appleBox
.
getFruits
());
System
.
out
.
println
(
appleBox1
.
getFruits
());
}
private
<
T
>
void
swapElements
(
int
idx1
,
int
idx2
,
T
[]
array
) {
T
tmp
=
array
[
idx1
];
array
[
idx1
] =
array
[
idx2
];
array
[
idx2
] =
tmp
;
}
private
<
T
>
ArrayList
<
T
>
convertArrayToList
(
T
[]
array
) {
return
new
ArrayList
<>(
Arrays
.
asList
(
array
));
}
}
class
Box
<
T
extends
Fruit
> {
private
ArrayList
<
T
>
fruits
;
public
Box
() {
fruits
=
new
ArrayList
<>();
}
public
void
add
(
T
fruit
) {
fruits
.
add
(
fruit
);
}
public
void
addFruits
(
ArrayList
<
T
>
fruits
) {
this
.
fruits
.
addAll
(
fruits
);
}
public
ArrayList
<
T
>
getFruits
() {
return
fruits
;
}
public
float
getWeight
() {
float
totalWeight
=
0.0f
;
for
(
T
fruit
:
fruits
)
totalWeight
+=
fruit
.
getWeight
();
return
(
float
)
Math
.
ceil
(
totalWeight
*
100
)/
100
;
}
public
boolean
compare
(
Box
<?>
box
) {
return
Float
.
compare
(
getWeight
(),
box
.
getWeight
()) ==
0
;
}
public
void
moveAllToNextBox
(
Box
<
T
>
box
) {
box
.
addFruits
(
getFruits
());
fruits
.
clear
();
}
}
class
Apple
extends
Fruit
{
public
Apple
() {
super
(
0.7f
+ (
float
)
Math
.
random
()/
3
);
}
}
class
Orange
extends
Fruit
{
public
Orange
() {
super
(
1.2f
+ (
float
)
Math
.
random
()/
3
);
}
}
abstract
class
Fruit
{
protected
float
weight
;
public
Fruit
(
float
weight
) {
this
.
weight
=
weight
;
}
public
float
getWeight
() {
return
weight
;
}
@
Override
public
String
toString
() {
return
this
.
getClass
().
getName
() +
"="
+
Math
.
ceil
(
weight
*
100
)/
100
;
}
}
Back
|
FazBrowse Home
|
New Git URL