ITI0011:praktikum 6 T8

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

Tagasi ITI0011 lehele.

Üldine

Siin on praktikumi, mis toimub teisipäeval, 7. oktoobril kell 8, info.

Praktikumis käsitletavad teemad:

  • git
  • veebist lugemine
  • Stringi parsimine
  • objekti loomine
  • (sortimine)

Kood tuleb üles laadida git'i. Kausta (projekti) nimi peaks olema "prax6".

Koodinäide

Activities.java

<source lang="java"> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; import java.util.StringTokenizer;

public class Activities {

public static class Data { public int a; public String b;

public void print() { System.out.println(b + a); }

@Override public String toString() { // TODO Auto-generated method stub return b + " " + a + " (" + super.toString() + ")"; } }

public static void main(String[] args) { try { URL url = new URL("http://www.neti.ee"); InputStream is = url.openStream();

//Reader r = new InputStreamReader(System.in); Reader r = new InputStreamReader(is); BufferedReader br = new BufferedReader(r); while (true) { String line = br.readLine(); if (line == null) break; System.out.println(line); }

URL url2 = new URL("http://www.neti.ee"); BufferedReader br2 = new BufferedReader(new InputStreamReader(url2.openStream()));

} catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }

String line = "123 123 123 tere "; int start = 0; int end = -1; for (int i = 0; i < line.length(); i++) { if (line.charAt(i) == ' ') { end = i; System.out.println(line.substring(start, end)); start = i + 1; } //System.out.println(line.charAt(i));

} System.out.println(line.substring(start, line.length()));

String[] ss = line.split(" "); for (String s : ss) { try { int a = Integer.parseInt(s); double d = Double.parseDouble(s); System.out.println(a + 1); } catch (NumberFormatException e) { System.out.println(s); }

}

StringTokenizer st = new StringTokenizer(line);

System.out.println(st.nextToken()); System.out.println(st.nextToken()); System.out.println(st.nextToken()); System.out.println(st.nextToken()); if (st.hasMoreTokens()) { System.out.println(st.nextToken()); }


Data d = new Data(); d.a = 12; d.b = "tere"; System.out.println(d); }

}

</source>

Tips

  • URL, URL.openStream()
  • BufferedReader
  • String, String.split
  • StringTokenizer, StringTokenizer.nextToken()
  • Integer.parseInt(String), Double.parseDouble(String)
  • Collections.sort(List, Comparator)
  • Object.toString()

Exercise

You have a database of sports activities. The database is stored in a text file. Now you want to write a program that reads the data from the file and converts it into Java objects. You will write different objects for different activities. But in order to store them in one collection, you need to have a common type (e.g. super class).

1) Create objects for different activities:

  • running. has fields:
    • date (when the activity took place)
    • distance (in kms)
    • duration (in minutes)
    • steps (how many steps were done)
    • maxSpeed (in km/h)
  • cycling. has fields:
    • date (when the activity took place)
    • distance (in kms)
    • duration (in minutes)
    • max speed (in km/h)
  • walking
    • date (when the activity took place)
    • distance (in kms)
    • duration (in minutes)
    • resting duration (how many minutes in total resting took place during the activity)

2) Read information from file: http://dijkstra.cs.ttu.ee/~ago/iti0011/praktikum-06-input.txt

The structure of the file (R, C, W indicates activity type: running, cycling, walking accordingly):

in case of running:

date R distance duration steps maxspeed

in case of cycling:

date C distance duration maxspeed

in case of walking:

date W distance duration restduration

For example the file:

2014-10-01  R 10 50 5660 10
2014-10-03 C 12 30 33
2014-10-05 W 4 60 20

would yield in 3 objects: one running object, one cycling object and one walking object.

3) (optional) try to sort the objects by date.

An example: http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property

4) print out activities.

Activities should be stored in a list or array (or something else similar). Print out all the information about the activity. In case of running, print out "running" + date, duration, maxspeed, steps, distance etc.