Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
G2_MSAYIF_BASSEM_G2_LE_HAIDANG TP1-TP2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
MSAYIF Bassem
G2_MSAYIF_BASSEM_G2_LE_HAIDANG TP1-TP2
Commits
03bf060e
Commit
03bf060e
authored
4 years ago
by
MSAYIF Bassem
Browse files
Options
Downloads
Patches
Plain Diff
Updated Crossword Class (Task 2) + Updated Main Class
-Implémentation de la recherche d’un mot dans le tableau Crossword
parent
450f277d
Branches
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
tp2/Crossword.java
+139
-96
139 additions, 96 deletions
tp2/Crossword.java
tp2/Main.java
+21
-12
21 additions, 12 deletions
tp2/Main.java
with
160 additions
and
108 deletions
tp2/Crossword.java
+
139
−
96
View file @
03bf060e
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Scanner
;
import
java.io.*
;
...
...
@@ -6,104 +7,146 @@ import java.io.*;
* Class Crossword : Creates a 2D array of characters read from the input file
*/
public
class
Crossword
{
private
char
array
[][];
// Tableaux 2D contenant les caractères
private
int
rows
;
// Nombres de lignes de array
private
int
columns
;
// Nombres de colonnes de array
private
int
WordCount
;
// Nombre d'occurences d'un mot
private
int
PositionX
;
// Colonne de début du mot
private
int
PositionY
;
// Ligne de début du mot
private
int
EndX
;
// Colonne de fin du mot
private
int
EndY
;
// Ligne de fin du mot
/*** Constructor: Reads each line from File <file> and writes to a new row in the array.
** Updates <rows> and <columns> to height and width of the array. */
public
Crossword
(
File
file
)
throws
IOException
{
if
(!
file
.
exists
()){
public
class
Crossword
{
private
char
array
[][];
// Tableaux 2D contenant les caractères
private
int
rows
;
// Nombres de lignes de array
private
int
columns
;
// Nombres de colonnes de array
private
int
WordCount
;
// Nombre d'occurences d'un mot
private
int
PositionX
;
// Colonne de début du mot
private
int
PositionY
;
// Ligne de début du mot
private
int
EndX
;
// Colonne de fin du mot
private
int
EndY
;
// Ligne de fin du mot
/*** Constructor: Reads each line from File <file> and writes to a new row in the array.
** Updates <rows> and <columns> to height and width of the array. */
public
Crossword
(
File
file
)
throws
IOException
{
if
(!
file
.
exists
())
{
System
.
out
.
println
(
"le fichier n'existe pas"
);
return
;
}
// Code pour calculer la taille du tableau et initialiser les variables d'instance "rows" et "columns"
Scanner
scan
=
new
Scanner
(
file
);
columns
=
scan
.
next
().
length
();
while
(
scan
.
hasNextLine
())
{
rows
++;
scan
.
nextLine
();
}
// Code pour créer le tableau "array"
char
[][]
array
=
new
char
[
rows
][
columns
];
// Code pour remplir "array" avec les caractères du Fichier "file"
Scanner
scanner
=
new
Scanner
(
file
);
for
(
int
i
=
0
;
i
<
rows
;
i
++)
{
String
fileLine
=
scanner
.
nextLine
();
for
(
int
j
=
0
;
j
<
columns
;
j
++)
{
char
nextChar
=
fileLine
.
charAt
(
j
);
array
[
i
][
j
]
=
nextChar
;
}
}
this
.
array
=
array
;
}
/*** Methode Search(String) : Trouver le premier occurence du mot <word> dans le Tableau
*** Si touver, mettre à jour les valeurs (PositionY, PositionX, EndY, EndX)*/
public
boolean
search
(
String
word
)
{
// Verifier que le taile du mot "word" est supérieure à zero. Sinon rien à faire.
// Chercher le premier caractère du mot "word" dans le tableau array.
// Si array[i][j] contient ce caractère, alors le mot peut apparaître dans le même ligne,
// ou dans le même colonne. Utiliser les methodes SearchRow() ou searchColumn() selon le cas.
return
false
;
// mot pas trouvé
}
/* Methode Interne SearchRow(int,int, String) : Cherche une ligne du tableaux pour le mot <word> à partir de array[y][x] */
private
boolean
searchRow
(
int
y
,
int
x
,
String
word
)
{
// Ecrire code ici ...
return
false
;
}
/* Methode Interne SearchRow(int,int, String) : Cherche une colonne du tableaux pour le mot <word> à partir de array[y][x] */
private
boolean
searchColumn
(
int
y
,
int
x
,
String
word
)
{
// Ecrire code ici ...
return
false
;
}
/*** Methode pour visualiser le tableau. (Déjà fourni) */
public
void
display
()
{
if
(
rows
>
0
&&
columns
>
0
)
CrosswordGUI
.
display
(
array
);
else
System
.
out
.
println
(
"Error: Array is Empty."
);
}
/*** Methode pour visualiser le tableau avec le mot en surbrillance. (Déjà fourni) */
public
void
displayWord
()
{
if
((
PositionX
<
0
)
||
(
PositionX
>
EndX
)
||
(
EndX
>=
columns
))
{
System
.
out
.
println
(
"Error: Incorrect x-coordinates for Word"
);
return
;
}
if
((
PositionY
<
0
)
||
(
PositionY
>
EndY
)
||
(
EndY
>=
rows
))
{
System
.
out
.
println
(
"Error: Incorrect y-coordinates for Word"
);
return
;
}
CrosswordGUI
.
display
(
array
,
PositionY
,
PositionX
,
EndY
,
EndX
);
}
// Code pour calculer la taille du tableau et initialiser les variables d'instance "rows" et "columns"
Scanner
scan
=
new
Scanner
(
file
);
columns
=
scan
.
next
().
length
();
while
(
scan
.
hasNextLine
())
{
rows
++;
scan
.
nextLine
();
}
// Code pour créer le tableau "array"
char
[][]
array
=
new
char
[
rows
][
columns
];
// Code pour remplir "array" avec les caractères du Fichier "file"
Scanner
scanner
=
new
Scanner
(
file
);
for
(
int
i
=
0
;
i
<
rows
;
i
++)
{
String
fileLine
=
scanner
.
nextLine
();
for
(
int
j
=
0
;
j
<
columns
;
j
++)
{
char
nextChar
=
fileLine
.
charAt
(
j
);
array
[
i
][
j
]
=
nextChar
;
}
}
this
.
array
=
array
;
}
/*** Methode Search(String) : Trouver le premier occurence du mot <word> dans le Tableau
*** Si touver, mettre à jour les valeurs (PositionY, PositionX, EndY, EndX)*/
public
boolean
search
(
String
word
)
{
// Verifier que le taile du mot "word" est supérieure à zero. Sinon rien à faire.
if
(
word
.
length
()
<=
0
)
{
return
false
;
}
for
(
int
j
=
0
;
j
<
columns
;
++
j
)
{
for
(
int
i
=
0
;
i
<
rows
;
++
i
)
{
if
(
searchRow
(
j
,
i
,
word
))
{
PositionX
=
j
;
EndX
=
PositionX
+
word
.
length
()
-
1
;
PositionY
=
i
;
EndY
=
PositionY
;
//System.out.println(PositionX + " " + EndX + " " + PositionY + " " + EndY);
return
true
;
}
else
if
(
searchColumn
(
j
,
i
,
word
))
{
PositionX
=
j
;
EndX
=
PositionX
;
PositionY
=
i
;
EndY
=
PositionY
+
word
.
length
()
-
1
;
//System.out.println(PositionX + " " + EndX + " " + PositionY + " " + EndY);
return
true
;
}
}
}
return
false
;
// mot pas trouvé
}
/* Methode Interne SearchRow(int,int, String) : Cherche une ligne du tableaux pour le mot <word> à partir de array[y][x] */
private
boolean
searchRow
(
int
y
,
int
x
,
String
word
)
{
int
length
=
word
.
length
();
String
wordfound
=
""
;
for
(
int
z
=
0
;
z
<
length
;
++
z
)
{
if
(
y
<
this
.
columns
)
{
if
(
array
[
x
][
y
]
==
word
.
charAt
(
z
))
{
wordfound
=
wordfound
+
word
.
charAt
(
z
);
++
y
;
}
}
}
return
(
wordfound
.
equals
(
word
));
}
/* Methode Interne SearchRow(int,int, String) : Cherche une colonne du tableaux pour le mot <word> à partir de array[y][x] */
private
boolean
searchColumn
(
int
y
,
int
x
,
String
word
)
{
int
length
=
word
.
length
();
String
wordfound
=
""
;
for
(
int
z
=
0
;
z
<
length
;
++
z
)
{
if
(
x
<
this
.
rows
)
{
if
(
array
[
x
][
y
]
==
word
.
charAt
(
z
))
{
wordfound
=
wordfound
+
word
.
charAt
(
z
);
++
x
;
}
}
}
return
(
wordfound
.
equals
(
word
));
}
/*** Methode pour visualiser le tableau. (Déjà fourni) */
public
void
display
()
{
if
(
rows
>
0
&&
columns
>
0
)
CrosswordGUI
.
display
(
array
);
else
System
.
out
.
println
(
"Error: Array is Empty."
);
}
/*** Methode pour visualiser le tableau avec le mot en surbrillance. (Déjà fourni) */
public
void
displayWord
()
{
if
((
PositionX
<
0
)
||
(
PositionX
>
EndX
)
||
(
EndX
>=
columns
))
{
System
.
out
.
println
(
"Error: Incorrect x-coordinates for Word"
);
return
;
}
if
((
PositionY
<
0
)
||
(
PositionY
>
EndY
)
||
(
EndY
>=
rows
))
{
System
.
out
.
println
(
"Error: Incorrect y-coordinates for Word"
);
return
;
}
CrosswordGUI
.
display
(
array
,
PositionY
,
PositionX
,
EndY
,
EndX
);
}
public
void
printArr
()
{
for
(
int
j
=
0
;
j
<
columns
;
j
++)
{
for
(
int
i
=
0
;
i
<
rows
;
i
++)
System
.
out
.
print
(
array
[
i
][
j
]
+
""
);
}
System
.
out
.
println
();
}
}
This diff is collapsed.
Click to expand it.
tp2/Main.java
+
21
−
12
View file @
03bf060e
import
java.io.*
;
import
java.lang.reflect.Array
;
import
java.util.Scanner
;
/*** Methode Main : Lire le fichier donné et créer un tableau 2D qui contint le characters du fichier.
*** Demander un mot d'utilisateur et le chercher dans le tableau
*** Visualiser the tableau avec le mot trouver ou informer l'utilisatuer si le mot n'est pas present */
*** Visualiser the tableau avec le mot trouver ou informer l'utilisatuer si le mot n'est pas present */
public
class
Main
{
public
static
void
main
(
String
args
[])
throws
IOException
{
String
filename
;
// Demander le nom de Fichier à l'utisateur
// Constriure une instance de la classe "Crossword"
// Crossword tableau = ...
//tableau.display();
String
filename
;
Scanner
scan
=
new
Scanner
(
System
.
in
);
// Demander le nom de Fichier à l'utisateur
filename
=
scan
.
next
();
File
file
=
new
File
(
filename
);
// Construire une instance de la classe "Crossword"
Crossword
tableau
=
new
Crossword
(
file
);
tableau
.
display
();
// Demander un mot à chercher ...
while
(
true
)
{
String
word
=
scan
.
next
();
// Utiliser la méthode Crossword.search() pour chercher le mot
tableau
.
search
(
word
);
// Ecrire un message sur le terminal pour informer l'utilisateur du resultat.
if
(
tableau
.
search
(
word
))
{
System
.
out
.
println
(
"Trouvé!"
);
}
else
System
.
out
.
println
(
"Pas trouvé..."
);
}
// Demander un mot à chercher ...
// Utiliser la méthode Crossword.search() pour chercher le mot
// Ecrire un message sur le terminal pour informer l'utilisateur du resultat.
}
}
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