Browse Source

added OOP TP 1

master
Georges-Emile Bihr 1 year ago
parent
commit
2e2b1ba044
  1. 30
      .gitignore
  2. 112
      unistra/l2_s1a_2021/poo/tp1/part1/q2/Main.java

30
.gitignore

@ -1,3 +1,7 @@
# ---> custom
.vimrc_local
# ---> C
# Prerequisites
*.d
@ -226,3 +230,29 @@ dmypy.json
# Cython debug symbols
cython_debug/
# --> Java
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

112
unistra/l2_s1a_2021/poo/tp1/part1/q2/Main.java

@ -0,0 +1,112 @@
import java.util.Arrays;
class MainContent {
private String string_from = "Hello, my name is Darkgeem.";
private String[] utf_useful = {
new String(new byte[] {(byte)0xc3, (byte)0xa0}), // 0: à
new String(new byte[] {(byte)0xc3, (byte)0xa2}), // 1: â ^
new String(new byte[] {(byte)0xc3, (byte)0xa6}), // 2: æ
new String(new byte[] {(byte)0xc3, (byte)0xa7}), // 3: ç
new String(new byte[] {(byte)0xc3, (byte)0xa8}), // 4: è
new String(new byte[] {(byte)0xc3, (byte)0xa9}), // 5: é
new String(new byte[] {(byte)0xc3, (byte)0xaa}), // 6: ê ^
new String(new byte[] {(byte)0xc3, (byte)0xab}), // 7: ë ¨
new String(new byte[] {(byte)0xc3, (byte)0xae}), // 8: î ^
new String(new byte[] {(byte)0xc3, (byte)0xaf}), // 9: ï ¨
new String(new byte[] {(byte)0xc3, (byte)0xb4}), // 10: ô ^
new String(new byte[] {(byte)0xc3, (byte)0xb5}), // 11: ö ¨
new String(new byte[] {(byte)0xc3, (byte)0xb9}), // 12: ù
new String(new byte[] {(byte)0xc3, (byte)0xbb}), // 13: û ^
new String(new byte[] {(byte)0xc3, (byte)0xbc}), // 14: ü ¨
};
private void showChar(int pos, boolean is_stdout) {
java.io.PrintStream ps = (is_stdout ? System.out : System.err);
for (int i = 0; i < pos; i++) {
ps.print(' ');
}
ps.print('^');
}
private void showChar(int pos) {
showChar(pos, true);
}
private void q1() {
// String est non-mutable
// StringBuffer est mutable
}
private void q2a() {
// string_from.indexOf(String string_search) -> retourne l'index
// du premier caractère de la sous-chaîne string_search
// dans la chaîne string_from
int where = string_from.indexOf("Darkgeem");
System.out.println(string_from);
if (where >= 0) {
showChar(where);
System.out.printf(": \"Darkgeem\" found at position %d\n", where);
}
}
private void q2b() {
// string_from.indexOf(int c_search, int startpos) -> retourne
// l'index de la première occurence du caractère c_search
// en partant de la position startpos
int where1 = string_from.indexOf('m', 0);
int where2 = string_from.indexOf('m', where1+1);
System.out.println(string_from);
showChar(where1);
System.out.printf(": 'm' found at position %d\n\n", where1);
System.out.println(string_from);
showChar(where2);
System.out.printf(": second 'm' found at position %d\n", where2);
}
private void q2c() {
// string_from.split(String regex) -> retourne un array de String
// coupant la chaîne à chaque occurence de regex. attention,
// regex n'est pas une simple chaîne de caractères, mais une
// expression régulière.
String[] currentStrArr = string_from.split(" ");
for (int i = 0; i < currentStrArr.length; i++) {
System.out.printf("currentStrArr[%d] = \"%s\"\n", i, currentStrArr[i]);
}
}
private void q3() {
// Arrays -> java.util.Array (voir ligne(s) import)
// la fonction Arrays.sort(int[] a) trie le tableau a en place
// dans l'ordre croissant
int[] a = {2, 3, 9, 5, 4, 6, 1, 7, 8, 0}; // tableau 0 -> 9 non trié
System.out.println("Tableau non tri"+utf_useful[4]+":");
System.out.println(Arrays.toString(a)); // Arrays.toString(a) -> "[2, 3, 9, 5, 4, 6, 1, 7, 8, 0]"
Arrays.sort(a); // trie a
System.out.println("Tableau tri"+utf_useful[4]+":");
System.out.println(Arrays.toString(a));
}
private void main() {
System.out.printf("===== 1.2.a =====\n\n");
q2a();
System.out.printf("\n\n===== 1.2.b =====\n\n");
q2b();
System.out.printf("\n\n===== 1.2.c =====\n\n");
q2c();
System.out.printf("\n\n===== 1.3 =====\n\n");
q3();
System.out.printf("\n\n===== FIN =====\n");
System.console().readLine();
}
public MainContent() {
main();
}
}
public class Main {
public static void main(String[] args) {
MainContent mc = new MainContent();
}
}
Loading…
Cancel
Save