From da55838a8ebb284f45f2e93814c0df9c2b2b593f Mon Sep 17 00:00:00 2001 From: KylerSmith Date: Fri, 7 Jul 2017 18:12:14 -0700 Subject: [PATCH 1/3] Count character algo added --- CountChar.java | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 CountChar.java diff --git a/CountChar.java b/CountChar.java new file mode 100644 index 000000000000..dfdc0684d201 --- /dev/null +++ b/CountChar.java @@ -0,0 +1,42 @@ +import java.util.Scanner; + + +/** + * @author Kyler Smith, 2017 + * + * Implementation of a character count. + * (Slow, could be improved upon, effectively O(n). + * */ + +public class CountChar { + + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter your text: "); + String str = input.nextLine(); + + System.out.println("There are " + CountCharacters(str) + " characters."); + } + + + + /** + * @param str: String to count the characters + * + * @return int: Number of characters in the passed string + * */ + + public static int CountCharacters(String str) { + + int count = 0; + + if(str.isEmpty() || str == null) + return -1; + + for(int i = 0; i < str.length(); i++) + if(!Character.isWhitespace(str.charAt(i))) + count++; + + return count; + } +} From 1af3806a881e20e6b84cb1d7dfd36603029bd154 Mon Sep 17 00:00:00 2001 From: KylerSmith Date: Fri, 7 Jul 2017 18:24:45 -0700 Subject: [PATCH 2/3] Clean up FloydTriangle (ft.java) --- .DS_Store | Bin 8196 -> 6148 bytes ft.java | 32 +++++++++++++++----------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/.DS_Store b/.DS_Store index 27875fa6e0825fbffd3779ceb1765c6599a40067..4510cdaa3c92eed37289c2ac12b58a82591e3985 100644 GIT binary patch delta 139 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{MGjUEV6q~50D9Q$s2aA<3lr!Wr6fvYQ6mKkC z&dA6%`Hn#S;7em$mUf>j1Thf{ugEy;f8D^won4rY@8i&YSaoB*UrWpmVRiIh;3#3z1!@P@G1m zU8KX|Vy{ga0VA+TKx+2}S{Pu&n!Q-RkI}RRghKgAn*@8HY5R=yagi9=>Op|$Ln0m+Y6?;oLsqL=4*t8EUY;ajCM$Qos~0$L;ur^b#eH)` zz0Y%(Ebdp0_Bcdqa)#$T-$G2dMU=AVWySl;SB&$NoPSX>HYLzhN^)A|q}-RgoV?rY z{R`aQ{|eNE5qNL{>psG+)c*(N-~S&x4+~%fjKJR`;A-uYc8l>{Ne)tLN9 Date: Sat, 8 Jul 2017 20:10:43 -0700 Subject: [PATCH 3/3] Create Matrix data structure. --- .DS_Store | Bin 6148 -> 8196 bytes data_structures/Matrix/Matrix.java | 197 +++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 data_structures/Matrix/Matrix.java diff --git a/.DS_Store b/.DS_Store index 4510cdaa3c92eed37289c2ac12b58a82591e3985..f28585d78e4c824a62c4e8ee30d752891efb7b22 100644 GIT binary patch literal 8196 zcmeHM-A)rh6h6a`Y(oSBCU9l;#>5LkAVdilQDFL7Yiu&g|~_&YAtr{Pb*zNR-;{8qpFFS*R@MrqTSS z@N=$1WlY~;umGOu4LNj7&Q1{3hP2HM!+>GHFkl!k3>XHk0s}a+xmY>pzItj}!+>Gn zzhr>-2OE`TQOmxPa_c}NQvk>b+?EA>#czu+q`N++w%8zP`R}Z`|uG+uhAgbnkDj^?H`IcxUb5&SCT9efLA}KYk6N4GGqYj_UVW_Nw}@&(#?{I5MGrFTdWIfjq&y3FHu-YYdE;ci z=@~H(tdDu@%C=6s(n6Ex(kTA}@45gR0eqjp%Dx^!NOj!vV50`0$m zNbaF!_&mbf`FKZORUUYV@vKP|8A0Z?o;Mm^o0&J-gN3ub+4~b4LmXN_c^FHK5jD`u zaLy;fK=db}&vPcpYBE=On%8ufNO6(b5kSS2D&uN2OPv32=YRjdQe!rQF$@?6E|&o@ zQLdCruzB`_Hfx@9Z5#Cvl?&_jm6Q-PG98DM={V&1ABO1LP-RR}%f6DhgYx%31Y}N2 QbN-V#|1UA~`$x|IpZ9>FN&o-= delta 206 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{MGjUEV6q~50D9Q$s2aA<3lr!Wr6fvYQ6mKkC z&dA6%`H-Li7vl|}d<9Su`{X}@rmR6gmd0cr5pzzUID-ZdvrhIAv1WAxvSv)aB%;Rg z9||VBi0be#0@X8u>}31@e`Dc7mc{HG9D>Y1J`f0S0|{4 Rows: " + m1.getRows() + " Columns: " + m1.getColumns()); + System.out.println("m2 --> Rows: " + m2.getRows() + " Columns: " + m2.getColumns()); + System.out.println("m3 --> Rows: " + m3.getRows() + " Columns: " + m3.getColumns()); + + //check for reference issues + System.out.println("m2 -->\n" + m2); + data2[1][1] = 101; + System.out.println("m2 -->\n" + m2); + + //test equals + System.out.println("m2==null: " + m2.equals(null)); //false + System.out.println("m3==\"MATRIX\": " + m2.equals("MATRIX")); //false + System.out.println("m2==m1: " + m2.equals(m1)); //false + System.out.println("m2==m2: " + m2.equals(m2)); //true + System.out.println("m2==m3: " + m2.equals(m3)); //false + + //test operations (valid) + System.out.println("2 * m2:\n" + m2.scale(2)); + System.out.println("m2 + m3:\n" + m2.plus(m3)); + System.out.println("m2 - m3:\n" + m2.minus(m3)); + } + + + /** + * Data needs to be a deep copy as not to change the original state. + */ + private int[][] data; + + /** + * Constructor for the matrix takes in a 2D array + * + * @param pData + */ + public Matrix(int[][] pData) { + + /** Make a deep copy of the data */ + if(pData.length != 0) { + int[][] newData = new int[pData.length][pData[0].length]; + + for(int i = 0; i < pData.length; i++) + for(int j = 0; j < pData[0].length; j++) + newData[i][j] = pData[i][j]; + + this.data = newData; + } else { + this.data = null; + } + } + + /** + * Returns the element specified by the given location + * + * @param x : x cooridinate + * @param y : y cooridinate + * @return int : value at location + */ + public int getElement(int x, int y) { + return data[x][y]; + } + + /** + * Returns the number of rows in the Matrix + * + * @return rows + */ + public int getRows() { + if(this.data == null) + return 0; + + return data.length; + } + + /** + * Returns the number of rows in the Matrix + * + * @return columns + */ + public int getColumns() { + if(this.data == null) + return 0; + return data[0].length; + } + + /** + * Returns this matrix scaled by a factor. That is, computes sA where s is a + * constant and A is a matrix (this object). + * + * @param scalar : value to scale by + * @return A new matrix scaled by the scalar value + */ + public Matrix scale(int scalar) { + + int[][] newData = new int[this.data.length][this.data[0].length]; + + for (int i = 0; i < this.getRows(); ++i) + for(int j = 0; j < this.getColumns(); ++j) + newData[i][j] = this.data[i][j] * scalar; + + return new Matrix(newData); + } + + /** + * Adds this matrix to another matrix. + * + * @param other : Matrix to be added + * @return addend + */ + public Matrix plus(Matrix other) throws RuntimeException { + + int[][] newData = new int[this.data.length][this.data[0].length]; + + if(this.getRows() != other.getRows() || this.getColumns() != other.getColumns()) + throw new RuntimeException("Not the same size matrix."); + + for (int i = 0; i < this.getRows(); ++i) + for(int j = 0; j < this.getColumns(); ++j) + newData[i][j] = this.data[i][j] + other.getElement(i, j); + + return new Matrix(newData); + } + + /** + * Subtracts this matrix from another matrix. + * + * @param other : Matrix to be subtracted + * @return difference + */ + public Matrix minus(Matrix other) throws RuntimeException { + + int[][] newData = new int[this.data.length][this.data[0].length]; + + if(this.getRows() != other.getRows() || this.getColumns() != other.getColumns()) + throw new RuntimeException("Not the same size matrix."); + + for (int i = 0; i < this.getRows(); ++i) + for(int j = 0; j < this.getColumns(); ++j) + newData[i][j] = this.data[i][j] - other.getElement(i, j); + + return new Matrix(newData); + } + + /** + * Checks if the matrix passed is equal to this matrix + * + * @param other : the other matrix + * @return boolean + */ + public boolean equals(Matrix other) { + return this == other; + } + + /** + * Returns the Matrix as a String in the following format + * + * [ a b c ] ... + * [ x y z ] ... + * [ i j k ] ... + * ... + * + * @return Matrix as String + * TODO: Work formatting for different digit sizes + */ + public String toString() { + String str = ""; + + for(int i = 0; i < this.data.length; i++) { + str += "[ "; + for(int j = 0; j < this.data[0].length; j++) { + str += data[i][j]; + str += " "; + } + str += "]"; + str += "\n"; + } + + return str; + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy