wiki:k2014/demot/demo9english
Last modified 3 years ago Last modified on 2015-02-13 11:10:45

Demot » Demo 9, 16.3.2015

Task *2. Longest rising

Write function longestRising that returns the highest amount of rising character in a string. So all the same or alphabetically latterly organized letters will increase the amount.

Example:
  "abajiuxac"   => 3  (iux is the longest rising)
  "kissa"       => 3  (iss is longest in this case) 
  1. First design the algorithm. You can divide the problem into smaller parts.
  2. Write the algorith in java. (T)

Hint: char-type variables c1 and c2 can be compared

   if ( c1 <= c2 ) ...

Task 3. Copying from a file

Write a program that reads a file that has words and numbers separated from each other with white spaces. The program will copy the lines into another file that start with a number bigger than 30. (For testing you need to describe how the testing should be done in english)

File:                    will create a new file:   
-----------------------------------------------------
33 hiljaa 1 hiipii       33 hiljaa 1 hiipii
hyvä 33 tulee            36 1 3 5 55      
36 1 3 5 55
nyt 33 riittää         
-----------------------------------------------------

Comment: Rather than adding all the rows in the file into a list, rewrite the acceptable rows straight into the new file. It's good to use try-with -clause from Java 7 (try-with is a concept, not an actual keyword).

Task 4. Nominee

Write class Nominee that can be used in the forecoming main. Write the whole class yourself(attributes, all the methods etc. and remember to test the class.

public static void main(String[] args)  {
    Nominee nom1 = new Nominee(100000.0, 0);
    Nominee nom2 = new Nominee(20000.0, 300);
    System.out.println(nom1); // Prints: Money 100000, votes 0
    System.out.println(nom2); // Prints: Money 20000, votes 300
    nom1.buy(200, 100.0); // Buys 200 votes, 100 dollars each
    System.out.println(nom1); // Prints: Money 80000, votes 200
    boolean succesful = nom2.buy(300, 100);
    if (!succesful) System.out.println("Not enough money :-)");
    System.out.println(nom2); // Prints: Money 20000, votes 300
    if (nom1.compareTo(nom2) > 0) System.out.println("Nominee 1 won!");
    if (nom1.compareTo(nom2) < 0) System.out.println("Nominee 2 won!");
    if (nom1.compareTo(nom2) == 0) System.out.println("It's a draw!");
    // Comparison is done based on votes.
    // This example prints: Nominee 2 won!
}

Task 5. Mutable Int-class

Integer -class of Java is immutable, meaning that after it has been created, it cannot be changed. Create your own class Int that has a mutable value. Change the example dynaaminen/ArrayListMalliGen.java so that the number in the datastructure can be changed something like this:

    Int number = luvut.get(1);
    number.set(4);
  // or
    luvut.get(1).set(4);

You must make enough methods into the class that the example ArrayListMalliGen.java works when all the Integers are changed into Int. Change Int in the place of int in the printing. Since autoboxing does not work for your own classes, must the adding be done like:

luvut.add(new Int(0));                 

Remember to write toString-method into your Int class. Also remember to do the tests!

Task *6. Remove from array

This task is good for your project! This mimics somewhat how your own datastructure will work!

Create the method

    int remove(int array[],int amount,int n)

The remove method "kind of" removes all the numbers n from the array. Since the array can't really be changed without assigning a new array the remove will just move the n numbers into the end of the array and return the number of useable items in the array. In the next example even after the remove, there will be 6 items in the array, but the amt variable stores the amount of how many of them can be used. Remember to write the tests for this.

   int t[]={4,7,9,3,9,2};
   int amt=6;

   amt = remove(t,amt,9);  /* => t = {4,7,3,2,?,?}, amt= 4  */
   amt = remove(t,amt,2);  /* => t = {4,7,3,?,?,?}, amt= 3  */
   amt = remove(t,amt,4);  /* => t = {7,3,?,?,?,?}, amt= 2  */

Task 7. Clone

Implement public Taulukko clone()-method for the class dynaaminen/Taulukko.java.

...
public static void main(String args[]) {
  Taulukko luvut = new Taulukko(7);
  luvut.lisaa(0); luvut.lisaa(2); //lisaa means the same as add
  System.out.println(luvut); // Prints" 0 2" 
  Taulukko taul = luvut.clone();
  luvut.lisaa(77);   
  System.out.println(taul);  // Will print " 0 2"
}

Task 8. Remove for Taulukko-class

Write a remove that is similar to the task 6 for the class Taulukko. What parameters does this method need? Remember to do the tests!