wiki:k2014/demot/demo10english
Last modified 3 years ago Last modified on 2015-03-20 13:53:12

Demot » Demo 10, 23.3.2015

Task 1. Adding number of line to a file

Write a java program that prints a file into the console, but with a line number added to the front of the line with two number precision(01,02, etc.) and each line should only print maximum of 40 characters. Remember to write tests! For example example.txt

123456789012345678901234567890123456789012345678901234567890
Cat sits in a tree
and wonders
what is going on in the world

prints:

/* 01 */ 1234567890123456789012345678901234567890
/* 02 */ Cat sits in a tree
/* 03 */ and wonders
/* 04 */ what is going on in the world

Task 2. Printing defined rows from a file

Write a program that asks the name of the file and prints all the rows from the file that start with **. Remember to write tests!

Task 3*. Count the amount of alphabets in the file

Write a program that reads a file using datastreams. After the file has been read, write how many times each alphabet was in the file. Ignore capitalletters ('A' = 'a').

Task 4*. Dynamically increasing the size of the array

Add to the example file dynaaminen/TaulukkoGen.java automatic increasing of the size of the array when the size of the original array is too small. Tests are important for this one too!

Task 5. Clone for TaulukkoGen?

Add method clone() to the class dynaaminen/TaulukkoGen.java. This time the clone makes a deep clone of the original object. For testing, add the necessary poista-method from demo9 answers. You also need to replace line:

taulukko[luku] != n

with

!alkiot[luku].equals(n) 

since the array contains objects. Deep cloning means that the new copy of the object is a copy of the original object but they have no common references. Write ComTest-tests for clone -method (use Int-class):

   * <pre name="test">
   * #THROWS CloneNotSupportedException 
   * #import demo.d9.Int;
   *  TaulukkoGen<Int> luvut = new TaulukkoGen<Int>();
   *  luvut.lisaa(new Int(0)); luvut.lisaa(new Int(2));
   *  luvut.lisaa(new Int(99));
   *  TaulukkoGen<Int> taul = luvut.clone();
   *  luvut.toString() === " 0 2 99";
   *  taul.toString()  === " 0 2 99";
   *  luvut.get(1).set(3);
   *  luvut.toString() === " 0 3 99";
   *  taul.toString()  === " 0 2 99";
   *  luvut.lisaa(new Int(2)); luvut.lisaa(new Int(5)); 
   *  luvut.lisaa(new Int(2)); luvut.lisaa(new Int(6));
   *  luvut.toString() === " 0 3 99 2 5 2 6";
   *  taul.toString()  === " 0 2 99";
   *  taul.get(3).intValue() === 2; #THROWS IndexOutOfBoundsException
   *  luvut.poista(new Int(2)); 
   *  luvut.toString() === " 0 3 99 5 6";
   *  taul.toString()  === " 0 2 99";
   * </pre>

Task 6. Picture of a linked list

Draw a picture of the datastructure according to the next class. Draw the class the way it looks like on the row "DRAW PICTURE".

package demo;
import fi.jyu.mit.ohj2.*;
/**
 * Example of a linked list,
 * @author Vesa Lappalainen
 * @version 1.0, 15.03.2003
 */
public class School{

  public static class Student {
    private String name;
    private double average;
    private Student next;

    public Student(String name, double average) {
      this.name = name; this.average = average;
    }

    public String toString() {
      return Mjonot.fmt(name,-22) + " average: " +
                  Mjonot.fmt(average,5,2);
    }
  }

  private String schoolClass;
  private int students;
  private Student first;
  private Student last;

  public School(String schoolClass) { this.schoolClass = schoolClass; }

  public void add(Student student) {
  }

  public void print(OutputStream os)  {
    PrintStream out = new PrintStream(os);
  }

  public void removeAll() {
  }

  public static void main(String[] args)  {
    School schoolClass = new School("1b");

    schoolClass.add(new Student("Duck Donald",5.0));
    schoolClass.add(new Student("Duck Hewey",7.0));
    schoolClass.add(new Student("Mouse Mickey",9.0));

    schoolClass.print(System.out);  // DRAW PICTURE
    schoolClass.removeAll();
    schoolClass.print(System.out);
  }
}

Continue the next tasks from the class given in task 6

Task 7*. Adding to the linked list

Complete the method add. Do the tests for add method too.

Task 8*. Going through a linked list

Complete method print. The method should print out the details of the class and the details of all the students, the print should use student.toString()-method. Remember tests!