wiki:k2014/demot/demo4english
Last modified 4 years ago Last modified on 2014-06-13 12:17:50

Demot » Demo 4, 27.1.2014

Task 1*. User interface

Write a java-GUI-program that will work in the following way:

I will tell you what your age is this year!
Your year of birth > [1978]
You will be 32 years this year.

Task 2.* Triangle

Write a function that returns the hypotenuse of a right-angled triangle when the sides are given as parameters. Write a fucntion for counting the area of the same triangle using the sides as parameters. Write tests for both of the functions. Remember that functions won't print anything.

Task 3. Precision

Write function and tests: int positivityOfDouble(double x, double eps), which will return

-1  if x<-eps,   eps is always >= 0
 0  if -eps<=x<=eps
 1  if x>eps

Task 4. Demo points

Write function demo points that takes the sum of demo points done by the student and the maximum amount of demo tasks. Function will return compensation points according to Ohjelmointi 2-kurssin taulukon (You can hardcode the values). Remember tests.

Task 5*. Table test

Do a "table test" on the following code to define what are the printed out lines after the program has finished. You do not need to take functions i and set into concern when doing the table test. What will the program print out?

You can look for examples from luentomoniste and luentovideot

/**
 * What will the program print?? 
 * @author  Vesa Lappalainen
 * @version 1.0, 19.01.2003
 */
public class Alisotk2 {

  private static int i(StringBuffer s) {
    return Integer.parseInt(s.toString());
  }
  private static void set(StringBuffer s,int i) {
    s.delete(0, s.length()).append(""+i);
  }

/* 01 */ private static StringBuffer b; private static int c;
/* 02 */
/* 03 */ private static void s_1(StringBuffer a, int b)
/* 04 */ {
/* 05 */   int d;
/* 06 */   d  = i(a);
/* 07 */   c  = b + 3;
/* 08 */   b  = d - 1;
/* 09 */   set(a,c - 5);
/* 10 */ }
/* 11 */
/* 12 */ private static void a_2(int a, StringBuffer b)
/* 13 */ {
/* 14 */   c  = a + i(b);
/* 15 */   { int c; c = i(b);
/* 16 */   a = 8 * c; }
/* 17 */   set(b,175);
/* 18 */ }
/* 19 */
/* 20 */ public static void main(String[] args) {
/* 21 */   StringBuffer a = new StringBuffer("4"); int d=9;
/* 22 */   System.out.println("" + a + " " + b + " " + c + " " + d);
/* 23 */   b=new StringBuffer("3"); c=2; d=1;
/* 24 */   s_1(b,c);
/* 25 */   a_2(d,a);
/* 26 */   s_1(a,3+d);
/* 27 */   System.out.println("" + a + " " + b + " " + c + " " + d);
/* 28 */ }
}

Task 6. redefining if-statements

What are the values of the variables at the end of the program? Rewrite these if statements according to good coding habbits (You only have to add and remove empty spaces and rows)! There are 4 different pieces of code under.

/* 1 */ a=4; b=2; c=0;     |    /* 2 */ a=4; b=2; c=0;
if ( a-b != 0 ) c++; a=1;  |    if ( c != 0 ) 
b=3;                       |      c--; else b=1;
                           |    a = b-2;

/* 3 */ a=4; b=2; c=0;     |    /* 4 */ a=4; b=2; c=0;
if ( a > 0 ) if ( b > 1 )  |    if ( c++ != 0 ) if ( a-- != 0);
  c=9;                     |    else b-=3; else c+=8;
else if ( c == 1 ) ;       |
else b = 0;                |
a = 3;                     |

Task 7-8*. Person-class

In the example class Aika all the variables are of type int. But in Kissa/ Elain- there are also string-type attributes stored.

Create class named Person which will store the first and last name of the person and his birthyear. Make the class work with the main under:

public static void main(String[] args) {
    Person donald = new Person("Donald", "Duck", 1934);
    System.out.println(donald .getName());  // prints Donald Duck
    System.out.println(donald .getBirthyear());  // prints 1934
    System.out.println(donald .toString()); // prints Donald|Duck|1934
}