Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
Homework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Siyu Lin
Homework
Commits
fa3a1d1e
Commit
fa3a1d1e
authored
10 years ago
by
Siyu Lin
Browse files
Options
Downloads
Patches
Plain Diff
lab11 Recursion
parent
314c7aed
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
project11/.classpath
+6
-0
6 additions, 0 deletions
project11/.classpath
project11/src/lab11/lab11.java
+151
-0
151 additions, 0 deletions
project11/src/lab11/lab11.java
with
157 additions
and
0 deletions
project11/.classpath
0 → 100644
+
6
−
0
View file @
fa3a1d1e
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry
kind=
"src"
path=
"src"
/>
<classpathentry
kind=
"con"
path=
"org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"
/>
<classpathentry
kind=
"output"
path=
"bin"
/>
</classpath>
This diff is collapsed.
Click to expand it.
project11/src/lab11/lab11.java
0 → 100644
+
151
−
0
View file @
fa3a1d1e
package
lab11
;
import
java.io.File
;
import
java.util.ArrayList
;
public
class
lab11
{
public
static
void
main
(
String
[]
args
)
{
// TODO Auto-generated method stub
System
.
out
.
println
(
power
(
5
));
System
.
out
.
println
(
getPyramidCount
(
7
));
int
[]
test
=
{
3
,
4
,
5
,
7
,
2
,
3
,
2
};
// sum should be 20
int
result
=
arrayMax
(
test
);
System
.
out
.
println
(
result
);
File
testFile
=
new
File
(
"../project10/src/lab10"
);
System
.
out
.
println
(
sizeOfDir
(
testFile
));
ArrayList
<
String
>
javaNames
=
javaFilesFinder
(
testFile
);
for
(
int
i
=
0
;
i
<
javaNames
.
size
();
i
++){
System
.
out
.
println
(
javaNames
.
get
(
i
));
}
}
public
static
int
power
(
int
p
)
{
if
(
p
==
0
)
{
return
1
;
}
else
{
power
(
p
-
1
);
return
power
(
p
-
1
)
*
2
;
}
}
public
static
int
getPyramidCount
(
int
levels
)
{
if
(
levels
==
0
)
{
return
0
;
}
else
{
getPyramidCount
(
levels
-
1
);
return
getPyramidCount
(
levels
-
1
)
+
levels
*
levels
;
}
}
public
static
int
arrayMax
(
int
[]
arr
)
{
int
result
=
arrayMaxVerbose
(
arr
,
0
,
arr
.
length
-
1
,
0
);
return
result
;
}
private
static
String
subArrayToString
(
int
[]
arr
,
int
start
,
int
end
)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"["
);
if
(
start
<=
end
)
{
sb
.
append
(
arr
[
start
]);
}
for
(
int
i
=
start
+
1
;
i
<=
end
;
++
i
)
{
sb
.
append
(
", "
+
arr
[
i
]);
}
sb
.
append
(
"]"
);
return
sb
.
toString
();
}
private
static
String
makeSpaces
(
int
n
)
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
sb
.
append
(
" "
);
}
return
sb
.
toString
();
}
private
static
int
arrayMaxVerbose
(
int
[]
arr
,
int
start
,
int
end
,
int
depth
)
{
if
(
start
==
end
)
{
return
arr
[
start
];
}
else
{
// show how it's split
System
.
out
.
print
(
makeSpaces
(
depth
));
int
mid
=
(
start
+
end
)
/
2
;
String
leftString
=
subArrayToString
(
arr
,
start
,
mid
);
// recursively find max for left subarray
int
leftMax
=
arrayMaxVerbose
(
arr
,
start
,
mid
,
depth
);
// print left subarray again followed by result
System
.
out
.
print
(
makeSpaces
(
depth
));
// recursively find max for right subarray, line up output
int
rightDepth
=
depth
+
leftString
.
length
()
+
1
;
int
rightMax
=
arrayMaxVerbose
(
arr
,
mid
+
1
,
end
,
rightDepth
);
//Compare left and right array and the get the new array
if
(
leftMax
>
rightMax
){
return
leftMax
;
}
else
{
return
rightMax
;
}
}
}
public
static
long
sizeOfDir
(
File
file
){
long
result
=
0
;
if
(
file
.
isFile
()){
result
=
result
+
file
.
length
();
}
else
{
//If it is not file
//Find
File
[]
files
=
file
.
listFiles
();
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++){
result
=
result
+
sizeOfDir
(
files
[
i
]);
}
}
return
result
;
}
public
static
void
javaFilesFinderVerbose
(
File
file
,
ArrayList
<
String
>
names
){
if
(
file
.
isFile
()
&&
isJavaFile
(
file
.
getName
())){
names
.
add
(
file
.
getName
());
}
else
if
(
file
.
isDirectory
())
{
File
[]
files
=
file
.
listFiles
();
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++){
javaFilesFinderVerbose
(
files
[
i
],
names
);
}
}
else
{
return
;
}
}
private
static
boolean
isJavaFile
(
String
name
){
if
(
name
.
contains
(
".java"
)){
return
true
;
}
else
{
return
false
;
}
}
public
static
ArrayList
<
String
>
javaFilesFinder
(
File
file
){
ArrayList
<
String
>
results
=
new
ArrayList
<
String
>();
javaFilesFinderVerbose
(
file
,
results
);
return
results
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment