TU Wien:Einführung in die Programmierung 2 VU (Puntigam)/Test 1 2019S

Aus VoWi
Zur Navigation springen Zur Suche springen

Probetest[Bearbeiten]

package probetest;
// This class can be modified and is for testing your solution.
// Modifications will NOT be reviewed or assessed.
public class PraxisApplication {
    public static void main(String[] args) {
        Monom m = new Monom(3,2);
        System.out.println(m); // 3*x^2
        System.out.println(m.eval(-5)); // 75
        System.out.println(m.eval(0)); // 0

        System.out.println(m.combine(new Monom(-1,2))); // true
        System.out.println(m); // 2*x^2

        System.out.println(m.combine(new Monom(-1,3))); // false
        System.out.println(m); // 2*x^2

        Polynom p1 = new Polynom(new int[] {2,0,3,1});
        Polynom p2 = new Polynom(new int[] {0,0,3,-1});

        System.out.println(p1); // 2*x^0 + 3*x^2 + 1*x^3
        System.out.println(p1.eval(-5)); // -48
        System.out.println(p2); // 3*x^2 + -1*x^3

        p1.add(p2);

        System.out.println(p1); // 2*x^0 + 6*x^2 + 0*x^3

        p1 = new Polynom(new int[] {2,0,3,1});
        p2 = new Polynom(new int[] {0,0,3,1});

        p2.add(p1);

        System.out.println(p2); // 2*x^0 + 6*x^2 + 2*x^3
    }
}
package probetest;
// This class represents monomials of the form a*x^d ("a mal x hoch d"), where
// 'a' is the coefficient of the monomial and 'd' is the degree.
public class Monom {
    //TODO: declare variables

    // A constructor with the coefficient 'coeff' and the 'degree' of this monomial.
    public Monom(int coeff, int degree) {
        // TODO: implement this constructor
    }

    // Copy-constructor: uses 'coeff' and 'degree' of 'm' to initialize this monomial.
    public Monom(Monom m) {
        // TODO: implement this constructor
    }

    // Adds the monomial 'm' to this monomial, if both monomials have the same degree.
    // In this case this monomial's coefficient is replaced by the sum of this monomial's
    // coefficient and the coefficient of 'm'. In this case the method returns 'true'.
    // If 'm' has not the same degree as 'this', the method has no effect and
    // returns 'false'.
    public boolean combine(Monom m) {
        // TODO: implement this method
        return false;
    }

    // Returns 'true' if 'm' has a higher degree than 'this', and 'false' otherwise.
    public boolean lowerDegreeThan(Monom m) {
        // TODO: implement this method
        return false;
    }

    // Returns the value of the monomial for a specified value of 'x'
    public int eval(int x) {
        // TODO: implement this method
        return 0;
    }

    // Returns a representation in mathematical notation, e.g. of the form "5*x^2".
    public String toString() {
        // TODO: implement this method
        return "";
    }
}
package probetest;
// This class represents a polynomial. A polynomial is a sum of multiple monomials (class 'Monom'),
// such as 3*x^1 + -1*x^2 + 1*x^5.
// 'Polynom' uses a binary search tree to store its monomials. The degree of the monomial is the key.
// A specific degree exists at most once in the polynomial.
// TODO: define further classes for the implementation of the binary search tree, if needed

public class Polynom {

    //TODO: declare variables

    // Initializes this polynomial with multiple monomials. The coefficients of the monomials are
    // specified by 'coeffs', where coeffs[i] is the coefficient of the monomial of degree i.
    // Entries with value 0 are ignored, i.e. corresponding monomials are not stored in the polynomial.
    public Polynom(int[] coeffs) {
        // TODO: implement this constructor
    }

    // Adds the monomial specified by 'coeff' and 'degree' to this polynomial, if coeff != 0,
    // otherwise 'add' has no effect.
    // If this polynomial already has a monomial of the same degree, no new monomial is added, instead
    // the coefficient of the monomial is modified accordingly ('combine' is called).
    public void add(int coeff, int degree) {
        // TODO: implement this method
    }

    // Adds all monomials of 'p' to this polynomial.
    // (The rules of 'add(int,int)' apply for each monomial to be added.)
    public void add(Polynom p) {
        // TODO: implement this method
    }

    // Returns the value of the polynomial for a specified value of 'x'
    public int eval(int x) {
        // TODO: implement this method
        return 0;
    }

    // Returns a polynomial representation in mathematical notation such as
    // "2*x^0 + 6*x^2 + -2*x^3", where monomials are ordered ascending according to
    // their degree. Note that a positive sign of the leftmost coefficient is
    // not shown.
    // Returns the string "0" if the polynomial has no monomials (is empty).
    public String toString() {
        // TODO: implement this method
        return "";
    }
}

Binärer Suchbaum[Bearbeiten]

package btree;
// This class can be modified and is for testing your solution.
// Modifications will NOT be reviewed or assessed.
//
public class PraxisApplication {

    public static void main(String[] args) {

        Rational r = Rational.create(1,2);
        System.out.println(r); // 1/2
        System.out.println(r.value()); // 0.5

        Rational s = Rational.create(2,0);
        System.out.println(s); // NaN
        System.out.println(s.value()); // NaN

        System.out.println(s.mult(r)); // NaN
        System.out.println(r.mult(s)); // NaN
        System.out.println(r.mult(r)); // 1/4
        System.out.println(s.mult(s)); // NaN

        RationalCollection m1 = new RationalCollection();

        m1.add(r);
        m1.add(Rational.create(2,-3));
        m1.add(Rational.create(-3,-4));
        m1.add(Rational.create(1,-3));

        System.out.println(m1); // [-2/3, -1/3, 1/2, 3/4]

        System.out.println(m1.product(-1)); // 6/72
        System.out.println(m1.product(0.5)); // 3/8
        System.out.println(m1.product(1)); // 1/1

    }
}
package btree;
// This class represents a sorted collection of elements of type 'Rational'.
// The elements are sorted with respect to their value.
// 'RationalCollection' uses a binary search tree for storing elements.
// For simplicity it is assumed that rounding errors do not matter; don't worry about them.
// Duplicate elements (entries) can occur in the collection.
// TODO: define further classes for the implementation of the binary search tree, if needed (either in this file
//  or in separate files)
//
public class RationalCollection {
    // TODO: declare variables

    // Adds the specified Rational 'r' to this set.
    // Preconditions: r != null and r is not NaN (need not be checked).
    public void add(Rational r) {
        // TODO: implement constructor
    }

    // Returns the product of all elements of this collection with values
    // greater than or equal to 'lower'.
    // Returns 1/1 if there are no such elements in the collection.
    public Rational product(double lower) {
        // TODO: implement this method
        return null;
    }

    // Returns a string representation of this collection. Elements are in
    // ascending order according to their values. Example: "[-2/3, -1/3, 1/2, 3/4]"
    public String toString() {
        // TODO: implement this method
        return "";
    }
}
package btree;
// Rational represents a fraction of two integers such as
// 1/2 (with numerator 1, denominator 2 and value 0.5)
// or -3/4 (with numerator -3, denominator 4 and value -0.75).
// Rational can also represent "not a number" (NaN).
//
public class Rational {
    // TODO: declare variables

    // Returns a new instance of 'Rational' with numerator and denominator.
    // If the denominator is 0, the object returned is an instance of Rational
    // corresponding to NaN (not a number).
    public static Rational create(int numerator, int denominator) {
        // TODO: implement constructor
        return null;
    }

    // TODO: define a constructor (if needed) that is called by 'Rational.create' only.

    // TODO: further definitions, if needed (depends on your solution)

    // Returns the product of 'this' and 'r'.
    // For example, the product of 2/3 and 4/5 is 8/15.
    // If either 'this' or 'r' is NaN, the method returns NaN.
    public Rational mult(Rational r) {
        // TODO: implement this method
        return null;
    }

    // Returns the value of 'this'.
    // If 'this' represents NaN, the method returns Double.NaN.
    public double value() {
        // TODO: implement this method
        return 0d;
    }

    // Returns a representation with numerator and denominator such as "-1/2".
    // If the value is negative, the sign is the first character, i.e., the denominator
    // in the resulting string is always a positive number
    // (making use of the equivalence between x/-y and -x/y).
    // If 'this' is NaN, the method returns "NaN".
    public String toString() {
        // TODO: implement this method
        return "";
    }
}

Ringliste[Bearbeiten]

package ringliste;
// This class can be modified and is for testing your solution.
// Modifications will NOT be reviewed or assessed.
//
public class PraxisApplication {
    public static void main(String[] args) {

        Fighter fighter1 = new Fighter("The Viper", 9);
        Fighter fighter2 = new Fighter("The Mountain", 10);
        fighter1.fights(fighter2);
        System.out.println(fighter1.isDead()); // true
        System.out.println(fighter2.isDead()); // false

        Fighter biter = new Fighter("Freddie",2);

        Team undead = new Team(new Fighter[]{biter,
                            new Fighter("Creeper",2),
                            new Fighter("Dan",2),
                            new Fighter("Abey",2)});

        Team living = new Team(new Fighter[]{new Fighter("Andrea",5),
            new Fighter("Bernd",4)});

        System.out.println(living + " : " + undead);
        // [ *Andrea(5)* Bernd(4) ] : [ *Freddie(2)* Creeper(2) Dan(2) Abey(2) ]

        while(!(undead.isEmpty() || living.isEmpty()) ) {
            living.fight(undead);
            System.out.println(living + " : " + undead);
        }
        /*
        [ *Andrea(5)* Bernd(4) ] : [ *Freddie(2)* Creeper(2) Dan(2) Abey(2) ]
        [ Andrea(3) *Bernd(4)* ] : [ *Creeper(2)* Dan(2) Abey(2) ]
        [ *Andrea(3)* Bernd(2) ] : [ *Dan(2)* Abey(2) ]
        [ Andrea(1) *Bernd(2)* ] : [ *Abey(2)* ]
        [ *Andrea(1)* ] : [ ]
         */
    }
}
package ringliste;
// This class represents a fighting character in a role playing game. Every
// fighter has a name and a number of hitpoints.
//
public class Fighter {
    // TODO: declare variables

    // A constructor with the 'name' of the fighter and his 'hitpoints'.
    public Fighter(String name, int hitpoints) {
        // TODO: implement this constructor
    }

    // Simulates a fight-to-the-death between 'this' and 'opponent'.
    // The fighter with more 'hitpoints' wins. His hitpoints are reduced
    // by the hitpoints of the losing fighter. The losing fighter is dead
    // after the fight (see method 'isDead').
    // In the case of equal hitpoints, both fighters are dead after the fight.
    // Precondition: opponent != null (needs not be checked).
    public void fights(Fighter opponent) {
        // TODO: implement this method
    }

    // Returns 'false' in the case of positive hitpoints, otherwise
    // it returns 'true'.
    public boolean isDead() {
        // TODO: implement this method
        return false;
    }

    // Returns a representation of this player with his name and his hitpoints in
    // parentheses. If this fighter is dead, hitpoints are 0.
    public String toString() {
        // TODO: implement this method
        return "";
    }
}
package ringliste;
// A team consists of multiple fighters stored in a ring list (all nodes have a successor node != null).
// One fighter of the team is active and engages a one-on-one fight with the active fighter
// of the opponent team. After that, the next fighter becomes active in each team.
// TODO: define further classes for the implementation of the ring list, if needed (either in this file
//  or in separate files)
//
public class Team {
    // TODO: declare variables

    // Initializes this team with the fighters from 'fighters' and sets the first
    // fighter in the list active.
    // Preconditions: fighters != null and has no entries equal to 'null' (need not be checked).
    public Team(Fighter[] fighters) {
        // TODO: implement this constructor
    }

    // Simulates a one-on-one fight: the active fighter of this team fights
    // the active fighter of team 'opponent' by calling the method 'fight' of class 'Fighter'.
    // Dead fighters are removed from their team (from the ring list) after
    // the one-on-one fight. After a fight, the next fighter becomes active in each team.
    // If 'this' or 'opponent' is empty, calling this method has no effect.
    // Precondition: opponent != null (needs not be checked).
    public void fight(Team opponent) {
        // TODO: implement this method
    }

    // Returns 'true' if there is no fighter in the team.
    public boolean isEmpty() {
        // TODO: implement this method
        return false;
    }

    // Returns a representation of this team with all its (remaining) fighters in
    // brackets in order of their insertion. The active fighter is marked by '*'.
    // Returns "[]" if the team is empty.
    public String toString() {
        // TODO: implement this method
        return "";
    }
}