Könnte ich das so abgeben?

klauskarambulut

Bekanntes Mitglied
Also wenn ich am KIT studieren würde, was ich nicht tue?
Und es um dieses Aufgabenblatt ginge https://sdqweb.ipd.kit.edu/lehre/WS1516-Programmieren/assignment03.pdf
Checkstyle - https://sdqweb.ipd.kit.edu/lehre/WS1516-Programmieren/checkstyle-assignment03-all.xml
Wieviel Punkte würde ich dafür wohl bekommen?
Soviel ich sehen kann scheint das weitestgehend korrekt zu sein unter den vereinfachten Bedingungen der Aufgabenstellung.
Teil A
package-info.java
Java:
/**
* Implementation of a TicTacToe-Game.
*/
package edu.kit.informatik;

TicTacToe.java
Java:
package edu.kit.informatik;

/**
* Implementation of the Game TicTacToe.
*
* @author klauskarambulut
*/
public final class TicTacToe {

    /**
     * Size of the Game.
     */
    private static final int GAME_SIZE = 9;

    /**
     * Indices of possiblities to win.
     */
    private static final int[][] WINNING_CONDITIONS = new int[][]{
        {0, 1, 2}, {3, 4, 5}, {6, 7, 8},
        {0, 3, 6}, {1, 4, 7}, {2, 5, 8},
        {0, 4, 7}, {2, 4, 6}
    };

    /**
     * The field for the TicTacToe-Game.
     */
    private final int[] field = new int[GAME_SIZE];

    /**
     * The player on turn.
     */
    private int player = 1;

    /**
     *
     * Creates a new TicTacToe-Game.
     */
    public TicTacToe() {
        // Default Constructor
    }

    /**
     * Makes a move and returns the winner or 0 if draw.
     *
     * @param position where to make the move
     * @return the number of the winner or 0 if nobody won!
     */
    public int move(final int position) {
        field[position] = player;
        int winner = getWinner();
        swapPlayer();
        return winner;
    }

    /**
     * Swaps the player from 1 to 2 and vice versa.
     */
    private void swapPlayer() {
        if (player == 1) {
            player = 2;
        } else {
            player = 1;
        }
    }

    /**
     * Checks if the current Player won.
     *
     * @return the player number if the current player won or 0 on draw
     */
    private int getWinner() {
        for (int[] wC : WINNING_CONDITIONS) {
            if (field[wC[0]] == player
                    && field[wC[1]] == player
                    && field[wC[2]] == player) {
                return player;
            }
        }
        return 0;
    }

    @Override
    public String toString() {
        String fieldString = "[";
        for (int i = 0; i < field.length; i++) {
            fieldString += (field[i] + ",");
        }

        return "TicTacToe{" + "field=" + fieldString
                + "], player=" + player + '}';
    }

    /**
     * Plays a fine Party of TicTacToe.
     *
     * @param args array of length 9 with the moves for the Game
     *
     */
    public static void main(final String[] args) {

        TicTacToe game = new TicTacToe();
        for (int turn = 0; turn < GAME_SIZE; turn++) {
            int position = Integer.parseInt(args[turn]);
            int winner = game.move(position);
            if (winner != 0) {
                System.out.println("P" + winner + " wins " + (turn + 1));
                return;
            }
        }
        System.out.println("draw");

    }

}

Teil B
package-info.java
Java:
/**
* A Banking implementation.
*/
package edu.kit.informatik;
Bank.java
Java:
package edu.kit.informatik;

/**
* A Bank.
*
* @author klauskarambulut
*/
public final class Bank {

    /**
     * Initial Account Capacity.
     */
    private static final int INITIAL_ACCOUNT_CAPACITY = 4;
    /**
     * The bankcode.
     */
    private final int bankCode;
    /**
     * Storage for accounts.
     */
    private Account[] accounts = new Account[INITIAL_ACCOUNT_CAPACITY];
    /**
     * Number of accounts in storage.
     */
    private int size = 0;

    /**
     * Creates a new bank.
     *
     * @param newBankCode of the bank.
     */
    public Bank(final int newBankCode) {
        this.bankCode = newBankCode;
    }

    /**
     * Creates a new Account.
     *
     * @param accountNumber for the new Account
     * @return storage position
     */
    public int createAccount(final int accountNumber) {
        Account account = new Account(bankCode, accountNumber);
        int index = generateFreeAccountIndex();
        accounts[index] = account;
        size++;
        return index;
    }

    /**
     * Removes an Account.
     *
     * @param accountNumber of account.
     * @return true on success.
     */
    public boolean removeAccount(final int accountNumber) {
        boolean accountRemoved = false;
        for (int i = 0; i < accounts.length; i++) {
            if (accountRemoved) {
                accounts[i - 1] = accounts[i];
            } else {
                Account account = accounts[i];
                if (account != null
                        && account.getAccountNumber() == accountNumber) {
                    accounts[i] = null;
                    accountRemoved = true;
                    size--;
                }
            }
        }
        resizeAcconts();
        return accountRemoved;
    }

    /**
     * Checks if Account with number exists.
     *
     * @param accountNumber to check.
     * @return true if account exists.
     */
    public boolean containsAccount(final int accountNumber) {
        for (Account account : accounts) {
            if (account != null
                    && account.getAccountNumber() == accountNumber) {
                return true;
            }
        }
        return false;
    }

    /**
     * Transfers money from one Account to another.
     *
     * @param fromAccountNumber as sender
     * @param toAccountNumber as receiver
     * @param amount to send.
     * @return true on success.
     */
    public boolean internalBankTransfer(final int fromAccountNumber,
            final int toAccountNumber, final int amount) {
        Account fromAccount = findAccountByNumber(fromAccountNumber);
        Account toAccount = findAccountByNumber(toAccountNumber);
        if (fromAccount != null && toAccount != null) {
            if (fromAccount.withdraw(amount)) {
                toAccount.deposit(amount);
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * Storage size.
     *
     * @return the storage size.
     */
    public int length() {
        return accounts.length;
    }

    /**
     * The number of Accounts.
     *
     * @return number of accounts.
     */
    public int size() {
        return size;
    }

    /**
     * Returns the Account with the index.
     *
     * @param index of storage.
     * @return Account if available otherwise null.
     */
    public Account getAccount(final int index) {
        if (index < accounts.length) {
            return accounts[index];
        } else {
            return null;
        }
    }

    /**
     * Generates a new index for storing accounts.
     *
     * @return a free index for storing an account.
     */
    private int generateFreeAccountIndex() {
        while (size >= accounts.length) {
            doubleAccountArray();
        }
        return size;
    }

    /**
     * Doubles the Size of the accountstorage.
     */
    private void doubleAccountArray() {
        Account[] newAccountsArray = new Account[accounts.length * 2];
        for (int i = 0; i < accounts.length; i++) {
            newAccountsArray[i] = accounts[i];
        }
        accounts = newAccountsArray;
    }

    /**
     * Reduces the accountstorage if needed.
     */
    private void resizeAcconts() {
        if (size < (accounts.length / INITIAL_ACCOUNT_CAPACITY)
                && accounts.length > INITIAL_ACCOUNT_CAPACITY) {
            Account[] newAccountsArray = new Account[accounts.length / 2];
            for (int i = 0; i < newAccountsArray.length; i++) {
                newAccountsArray[i] = accounts[i];
            }
            accounts = newAccountsArray;
        }
    }

    /**
     * Returns an account by accountnumber.
     *
     * @param accountNumber of the account.
     * @return the searched account or null.
     */
    private Account findAccountByNumber(final int accountNumber) {
        for (int i = 0; i < accounts.length; i++) {
            Account account = accounts[i];
            if (account != null
                    && account.getAccountNumber() == accountNumber) {
                return account;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        String result = "Bank{" + "bankCode=" + bankCode
                + ", size=" + size + ", accounts ";
        for (int i = 0; i < accounts.length; i++) {
            result += "\n\t" + i + ": " + accounts[i];
        }

        result += "}'";
        return result;
    }

}

Account.java
Java:
package edu.kit.informatik;

/**
* A Bank Account.
*
* @author klauskarambulut
*
*/
public final class Account {
    /**
     * The accountnumber.
     */
    private final int fAccountNumber;
    /**
     * The bankcode.
     */
    private final int fBankCode;
    /**
     * The Balance.
     */
    private int balance;

    /**
     * Creates a new Account.
     *
     * @param bankCode of Account.
     * @param accountNumber of Account.
     */
    public Account(final int bankCode, final int accountNumber) {
        this.fAccountNumber = accountNumber;
        this.fBankCode = bankCode;
        this.balance = 0;
    }

    /**
     * Withdraw money if possible.
     *
     * @param amount to withdraw
     * @return true on success otherwise false, and keeps balance.
     */
    public boolean withdraw(final int amount) {
        if (amount > balance) {
            return false;
        } else {
            balance = balance - amount;
            return true;
        }
    }

    /**
     * Deposit money to the account.
     *
     * @param amount to deposit.
     */
    public void deposit(final int amount) {
        balance += amount;
    }

    /**
     * Returns the Account Number.
     *
     * @return accountnumber.
     */
    public int getAccountNumber() {
        return fAccountNumber;
    }

    /**
     * Returns the Bankcode.
     *
     * @return the bankcode.
     */
    public int getBankCode() {
        return fBankCode;
    }

    /**
     * Returns the balance.
     *
     * @return the balance.
     */
    public int getBalance() {
        return balance;
    }

    /**
     * Sets the balance.
     *
     * @param newBalance the balance to set.
     */
    public void setBalance(final int newBalance) {
        this.balance = newBalance;
    }

    @Override
    public String toString() {
        return "Account{"
                + "accountNumber=" + fAccountNumber
                + ", bankCode=" + fBankCode
                + ", balance=" + balance + '}';
    }

}

Main.java
Java:
package edu.kit.informatik;

/**
* Test of the Bank.
*
* @author klauskarambulut
*/
public final class Main {

    /**
     * Private Constructor Utility Class.
     */
    private Main() {
        //Do nothing
    }
    /**
     * Test bank code.
     */
    private static final int TEST_BANK_CODE = 110;

    /**
     * Number of accounts to create.
     */
    private static final int ACCOUNTS_TO_CREATE = 10;

    /**
     * Tests the Banking Application.
     *
     * @param args ignored.
     */
    public static void main(final String[] args) {

        Bank bank = new Bank(TEST_BANK_CODE);

        for (int accountID = 0; accountID < ACCOUNTS_TO_CREATE; accountID++) {
            testCreateAccount(bank, accountID);
        }

        System.out.println(bank);

        while (bank.size() > 2) {
            Account toRemove = bank.getAccount(bank.size() - 1);
            bank.removeAccount(toRemove.getAccountNumber());
        }

        System.out.println(bank);

        Account a = bank.getAccount(0);
        a.setBalance(INITIAL_BALANCE);

        Account b = bank.getAccount(1);

        boolean success = bank.internalBankTransfer(a.getAccountNumber(),
                b.getAccountNumber(), 1);

        System.out.println("Transfer successful: " + success);

        boolean failure = bank.internalBankTransfer(b.getAccountNumber(),
                a.getAccountNumber(), 2);

        System.out.println("Faulty transfer: " + failure);

        System.out.println(bank);

    }
    /**
     * Initial balance.
     */
    private static final int INITIAL_BALANCE = 50;

    /**
     * Creates some accounts.
     *
     * @param bank the bank.
     * @param accountNumber the accountnumber.
     */
    private static void testCreateAccount(final Bank bank,
            final int accountNumber) {
        int index = bank.createAccount(accountNumber);
        System.out.println("Account created with index " + index);
    }

}

Teil C
package-info.java
Java:
/**
* A Routing Application.
*/
package edu.kit.informatik;

Routes.java
Java:
package edu.kit.informatik;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
* The Routing Application.
*
* @author klauskarambulut
*/
public final class Routes {

    /**
     * Usage String.
     */
    private static final String USAGE = "Routes filename from to withSteps";
    /**
     * The Error Message.
     */
    private static final String ERROR_MESSAGE = "Error Reading the file";
    /**
     * The index of the Steps argument.
     */
    private static final int STEPS_PARAMETER = 3;
    /**
     * The number of parameters.
     */
    private static final int NUMBER_OF_PARAMETERS = 4;
    /**
     * Initial Size of the number of Edges.
     */
    private static final int INITIAL_EDGE_SIZE = 4;
    /**
     * Array of edges.
     */
    private Edge[] edges = new Edge[INITIAL_EDGE_SIZE];
    /**
     * Number of Edges.
     */
    private int size;
    /**
     * Highest id for Citys.
     */
    private int highestCityIndex = -1;

    /**
     * Creates a new Routes-Graph.
     */
    public Routes() {
        // Do nothing
    }

    /**
     * Adding a new Edge to the Routing-Graph.
     *
     * @param from identifier.
     * @param to identifier.
     */
    public void addEdge(final int from, final int to) {
        Edge edge = new Edge(from, to);
        if (this.highestCityIndex < from) {
            this.highestCityIndex = from;
        } else if (this.highestCityIndex < to) {
            this.highestCityIndex = to;
        }
        while (size >= edges.length) {
            Edge[] newEdges = new Edge[edges.length * 2];
            for (int i = 0; i < edges.length; i++) {
                newEdges[i] = edges[i];
            }
            edges = newEdges;
        }
        edges[size] = edge;
        size++;
    }

    /**
     * Counts the routes from from to to with number of steps steps.
     *
     * @param from identifier
     * @param to identifier
     * @param steps number of steps to get from from to to.
     * @return number of routes to get from from to to.
     */
    public int countRoutes(final int from, final int to, final int steps) {
        int[][] adjacentMatrix = getAdjacentMatrix();
        int[][] current = adjacentMatrix;
        for (int i = 1; i < steps; i++) {
            current = multiplyMatrix(current, adjacentMatrix);
        }
        return current[from][to];
    }

    /**
     * Returns the Adjacent Matrix.
     *
     * @return the Adjacent Matrix.
     */
    public int[][] getAdjacentMatrix() {
        int adjMatrixSize = highestCityIndex + 1;
        int[][] adjacentMatrix = new int[adjMatrixSize][adjMatrixSize];
        for (Edge edge : edges) {
            if (edge != null) {
                adjacentMatrix[edge.getFrom()][edge.getTo()] = 1;
            }
        }
        return adjacentMatrix;
    }

    /**
     * Multiplies to Matrices.
     *
     * @param a a Matrix.
     * @param b another Matrix.
     * @return product of a multiplied b.
     */
    public static int[][] multiplyMatrix(final int[][] a, final int[][] b) {
        int[][] result = new int[a.length][b.length];
        for (int x = 0; x < a.length; x++) {
            for (int y = 0; y < b.length; y++) {
                for (int z = 0; z < a[x].length; z++) {
                    result[x][y] += a[x][z] * b[z][y];
                }
            }
        }
        return result;
    }

    /**
     * Routing Application calculating number of routes.
     *
     * @param args filename from to steps.
     */
    public static void main(final String[] args) {
        if (args.length != NUMBER_OF_PARAMETERS) {
            System.out.println(USAGE);
            System.exit(1);
        }
        FileReader in = null;
        try {
            in = new FileReader(args[0]);
        } catch (FileNotFoundException e) {
            System.out.println(ERROR_MESSAGE);
            System.exit(1);
        }
        BufferedReader reader = new BufferedReader(in);
        Routes routes = new Routes();
        try {
            String line = reader.readLine();
            while (line != null) {
                String[] parts = line.split(" ");
                int from = Integer.parseInt(parts[0]);
                int to = Integer.parseInt(parts[1]);
                routes.addEdge(from, to);
                line = reader.readLine();
            }
        } catch (IOException e) {
            System.out.println(ERROR_MESSAGE);
            System.exit(1);
        }

        int fromCity = Integer.parseInt(args[1]);
        int toCity = Integer.parseInt(args[2]);
        int steps = Integer.parseInt(args[STEPS_PARAMETER]);
        System.out.println(routes.countRoutes(fromCity, toCity, steps));

    }

}

Edge.java
Java:
package edu.kit.informatik;

/**
* A Graph-Edge going from from to to.
*
* @author klauskarambulut
*/
public final class Edge {

    /**
     * Id for from.
     */
    private final int from;
    /**
     * Id for to.
     */
    private final int to;

    /**
     * Creates a new Edge from from to to.
     *
     * @param begin identifier.
     * @param end identifier.
     */
    public Edge(final int begin, final int end) {
        this.from = begin;
        this.to = end;
    }

    /**
     * Returns the from id.
     *
     * @return from id.
     */
    public int getFrom() {
        return from;
    }

    /**
     * Returns the to id.
     *
     * @return to id.
     */
    public int getTo() {
        return to;
    }

}
 

Neue Themen


Oben