Code Examples

Several examples of my code can be found on my Github. I am fairly new to using GitHub repositories; however, I have been trying to add some of my code for reference as of late.

The following code comes from a project from a C# class I have taken. It uses Windows Forms and this class to create an ammortization schedule.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Project2

{

    /// <summary>

    /// Amortization is a custom class with private fields principalAmount, interestRate, duration, monthlyPayment, and remainingBalance.

    /// Each variable has its own respective property with get/set functionality. The purpose of the class is to have all of the information for each

    /// amortization schedule in one place, with accompanying methods to make any necessary calculations.

    /// </summary>

 

    public class Amortization

    {

        private double principalAmount;

        private double interestRate;

        private int duration;

        private double monthlyPayment;

        private double remainingBalance;

        // principalAmount property with get/set

        public double PrincipalAmount

        {

            get

            {

                return principalAmount;

            }

            set

            {

                principalAmount = value;

            }

        }

        // interestRate property with get/set

        public double InterestRate

        {

            get

            {

                return interestRate;

            }

            set

            {

                interestRate = value;

            }

        }

        //duration property with get/set

        public int Duration

        {

            get

            {

                return duration;

            }

            set

            {

                duration = value;

            }

        }

        // monthlyPayment property with get/set

        public double MonthlyPayment

        {

            get

            {

                return monthlyPayment;

            }

            set

            {

                monthlyPayment = value;

            }

        }

        // remainingBalance property with get/set

        public double RemainingBalance

        {

            get

            {

                return remainingBalance;

            }

            set

            {

                remainingBalance = value;

            }

        }

        // Amortization constructor - use yearly interest rate and yearly term when class is instantiated

        public Amortization(double principal, double interest, int term)

        {

            principalAmount = principal;

            interestRate = interest/12;

            duration = term * 12;

            remainingBalance = principal;

        }

        // Calculate monthly payment using formula, gives this value to the monthlyPayment variable and returns this value

        public double CalcMonthlyPayment()

        {

            double rate, principal, term;

            double fixedPayment = 0;

            rate = InterestRate;

            term = Duration;

            principal = PrincipalAmount;

            double baseNum = 1 + rate; // Amount to be raised to "term" exponent in formula

            double expNum = Math.Pow(baseNum, term); // Exponentiated result

            MonthlyPayment = principal * ((rate * expNum) / (expNum - 1)); // Set monthlyPayment

            fixedPayment = MonthlyPayment;

            return fixedPayment;

        }

        // Calculate monthly interest paid using the remaining balance (which is initialized as the principal amount and updated each month) and the interest rate

        public double CalcMonthlyInterestPaid()

        {

            double result;

            result = RemainingBalance * InterestRate;

            return result;

        }

        // Calculate amount of principal paid per payment by subracting the interest paid that month from the fixed monthly payment

        public double CalcPrincipalPaid()

        {

            double interestPaid;

            double mPayment = MonthlyPayment;

            double result;

            interestPaid = CalcMonthlyInterestPaid();

            result = mPayment - interestPaid;

            return result;

        }

        // Calculate remaining principal by subracting result of CalcPrincipalPaid() from the total remaining principal

        public double CalcRemainingPrincipal()

        {

            double principalPaid = CalcPrincipalPaid();

            double result = RemainingBalance - principalPaid;

            return result;

        }

        // Update the remaining principal to keep track of this variable distinct from the original principal amount

        public void UpdateRemainingPrincipal()

        {

            RemainingBalance = CalcRemainingPrincipal();

        }

    }

}

The following code comes from a simple matching game I am working on in Java. It has a preset solution that the user tries to guess by matching letters in pairs.

Board Class


public class Board {

final int NUM_SPACES = 16;


public GamePiece[][] createGame() {
GamePiece[][] boardList = new GamePiece[4][4];

GamePiece A1 = new GamePiece('A');
boardList[0][0] = A1;
GamePiece A2 = new GamePiece('A');
boardList[0][3] = A2;
GamePiece B1 = new GamePiece('B');
boardList[2][1] = B1;
GamePiece B2 = new GamePiece('B');
boardList[0][1] = B2;
GamePiece C1 = new GamePiece('C');
boardList[2][2] = C1;
GamePiece C2 = new GamePiece('C');
boardList[1][0] = C2;
GamePiece D1 = new GamePiece('D');
boardList[3][1] = D1;
GamePiece D2 = new GamePiece('D');
boardList[3][3] = D2;
GamePiece E1 = new GamePiece('E');
boardList[1][3] = E1;
GamePiece E2 = new GamePiece('E');
boardList[3][2] = E2;
GamePiece F1 = new GamePiece('F');
boardList[1][2] = F1;
GamePiece F2 = new GamePiece('F');
boardList[3][0] = F2;
GamePiece G1 = new GamePiece('G');
boardList[1][1] = G1;
GamePiece G2 = new GamePiece('G');
boardList[2][3] = G2;
GamePiece H1 = new GamePiece('H');
boardList[0][2] = H1;
GamePiece H2 = new GamePiece('H');
boardList[2][0] = H2;



return boardList;
}

public void printBoard(GamePiece[][] temp) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j< 4; j++) {
System.out.print(temp[i][j].getId() + " ");
}
System.out.println();
}
}

public void printTempBoard(GamePiece[][] temp, int x1, int y1, int x2, int y2) {
temp[x1][y1].setId(temp[x1][y1].getDisplay());
temp[x2][y2].setId(temp[x2][y2].getDisplay());

for (int i = 0; i < 4; i++) {
for (int j = 0; j< 4; j++) {
System.out.print(temp[i][j].getId() + " ");
}
System.out.println();
}
}

public void updateBoard(GamePiece[][] temp, int x1, int y1, int x2, int y2) {
temp[x1][y1].setId(temp[x1][y1].getDisplay());
temp[x2][y2].setId(temp[x2][y2].getDisplay());
}

public void reverseBoard(GamePiece[][] temp, int x1, int y1, int x2, int y2) {
temp[x1][y1].setId('O');
temp[x2][y2].setId('O');
}

public void printSpaces() {
for (int i = 0; i < 5; i++) {
System.out.println();
}
}
}

Main Method

import java.util.Scanner;

public class Match {

public static void main (String[] args) {
Board test = new Board();
int run = 0;
char cont;
Scanner input = new Scanner(System.in);

GamePiece[][] pieces = test.createGame();


System.out.println("Enter coordinates to turn over two objects.");
while (run < 8) {
test.printBoard(pieces);

System.out.print("X1: ");
int x1 = input.nextInt();

System.out.print("Y1: ");
int y1 = input.nextInt();

System.out.print("X2: ");
int x2 = input.nextInt();

System.out.print("Y2: ");
int y2 = input.nextInt();
System.out.println();

test.printSpaces();
if (pieces[x1][y1].getMatched() || pieces[x2][y2].getMatched()) {
test.printSpaces();
System.out.println("YOU ALREADY MATCHED THOSE.");
}
else if (pieces[x1][y1].getDisplay() == pieces[x2][y2].getDisplay()) {
test.printSpaces();
System.out.println("MATCH. PLEASE CONTINUE.");
run++;
pieces[x1][y1].match();
pieces[x2][y2].match();
test.updateBoard(pieces, x1, y1, x2, y2);
test.printBoard(pieces);
cont = input.next().charAt(0);
test.printSpaces();
}

else {
System.out.println("NO MATCH. PLEASE CONTINUE");
test.printTempBoard(pieces, x1, y1, x2, y2);
cont = input.next().charAt(0);
test.reverseBoard(pieces, x1, y1, x2, y2);
test.printSpaces();
}


}
for (int i = 0; i < 10; i++) {
System.out.println();
}
System.out.println("YOU WIN!!");


input.close();
}
}

This code is from an assignment where I was tasked with implementing a Doubly Linked List in C#. The following code contains a Node class in addition to a DoublyLinkedList class.

Doubly Linked List Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project3
{
class DoublyLinkedList
{
private Node head = null;

// Insert node at the beginning of the list
public void InsertFirst(DoublyLinkedList list, double input)
{
Node insertedNode = new Node(input);
insertedNode.next = list.head;
list.head = insertedNode;
insertedNode.previous = null;
}

// Insert node at the end of the list or beginning if the list is empty
public void InsertLast(DoublyLinkedList list, double input)
{
Node insertedNode = new Node(input);

if (list.head == null)
{
list.head = insertedNode;
insertedNode.next = null;
insertedNode.previous = null;
return;
}

Node lastNode = GetLastNode(list);
lastNode.next = insertedNode;
insertedNode.next = null;
insertedNode.previous = lastNode;
}

// Insert node after given node if the list isn't empty
public void InsertAfter(Node previousNode, double newData)
{
if (previousNode == null)
{
return;
}

Node insertedNode = new Node(newData);
Node temp = previousNode.next;
insertedNode.next = previousNode.next;
previousNode.next = insertedNode;
insertedNode.previous = previousNode;
temp.previous = insertedNode;
}

// Return head node
public Node GetFirstNode(DoublyLinkedList input) // Returns null if list is empty
{
Node temp = input.head;

if (temp == null)
{
return null;
}

return temp;
}

// Return tail node
public Node GetLastNode(DoublyLinkedList input)
{
Node temp = input.head;

while (temp.next != null)
{
temp = temp.next;
}

return temp;
}

// Return number of nodes in list
public int GetNumNodes(DoublyLinkedList input)
{
int count = 0;
Node temp = input.head;

if (temp == null)
{
return count;
}

while (temp != null)
{
temp = temp.next;
count++;
}
return count;
}
}

}


Node Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project3
{
class Node
{
public double data;
public Node next;
public Node previous;

// Constructor
public Node(double d)
{
data = d;
next = null;
previous = null;
}
}
}