ITI0011:praktikum 25

Allikas: Kursused
Redaktsioon seisuga 28. aprill 2015, kell 06:07 kasutajalt Ago (arutelu | kaastöö) (Uus lehekülg: '== Koodinäide == Rec.java <source lang="java"> public class Rec { public static void main(String[] args) { p3(); p(10); System.out.println("---"); System.out.println...')
(erin) ←Vanem redaktsioon | Viimane redaktsiooni (erin) | Uuem redaktsioon→ (erin)
Mine navigeerimisribale Mine otsikasti

Koodinäide

Rec.java <source lang="java">

public class Rec {

public static void main(String[] args) { p3(); p(10); System.out.println("---"); System.out.println(countX("xxterexx")); System.out.println(countXX("xxterexx")); }

public static void p3() { System.out.println(3); p2(); } public static void p2() { System.out.println(2); p1(); } public static void p1() { System.out.println(1); }

public static void p(int n) { if (n < 1) return; System.out.println(n); p(n - 1); }

public static int countX(String s) { /* int count = 0; for (int i = 0; i < s.length(); i++) { // kui on praegune sümbol on X, siis suurenda counti if (s.charAt(i) == 'x') { count++; } } return count; */ if (s.length() == 0) { return 0; } // minu info + järgmise inimese info() int my = 0; if (s.startsWith("x")) { my = 1; } else { my = 0; } int friend = countX(s.substring(1)); return my + friend; }

public static int countXX(String s) { if (s == null) return 0; if (s.length() < 2) { return 0; } int my = 0; if (s.startsWith("xx")) { my = 1; } int friend = 0; if (my == 1) { friend = countXX(s.substring(2)); } else { friend = countXX(s.substring(1)); } return my + friend; } } </source>

RecTest.java <source lang="java">

import static org.junit.Assert.*;

import org.junit.Test;

public class RecTest {


@Test public void testName() { assertEquals(0, Rec.countXX("")); assertEquals(0, Rec.countXX("tere")); assertEquals(1, Rec.countXX("xx")); assertEquals(2, Rec.countXX("xxterexx")); assertEquals(1, Rec.countXX("xxterex")); assertEquals(1, Rec.countXX("axxterex")); assertEquals(0, Rec.countXX(null));

}

public void blah() { System.out.println("head aega"); }

}

</source>