new semester, new repo structure

This commit is contained in:
2023-01-10 11:59:05 -06:00
parent 0a76658f85
commit ba6de6e18e
725 changed files with 1199 additions and 103 deletions

View File

@@ -0,0 +1,215 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>MP4_CalebFontenot.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #000000; background-color: #ffffff; font-family: monospace}
pre {color: #000000; background-color: #ffffff; font-family: monospace}
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
.ST0 {color: #969696; font-family: monospace; font-weight: bold}
.ST1 {font-family: monospace; font-weight: bold}
.comment {color: #969696}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
.ST4 {font-family: monospace; font-style: italic}
.ST2 {color: #ce54b8; font-family: monospace; font-style: italic}
.string {color: #1e9347}
.literal {color: #336bdd}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * To change this license header, choose License Headers in Project Properties.</span>
<span class="comment"> * To change this template file, choose Tools | Templates</span>
<span class="comment"> * and open the template in the editor.</span>
<span class="comment"> */</span>
<span class="literal">package</span> mp4_calebfontenot;
<span class="literal">import</span> java.util.Scanner;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> <span class="ST1">MP4_CalebFontenot</span> {
<span class="literal">static</span> <span class="literal">boolean</span> <span class="ST2">debug</span> = <span class="literal">false</span>;
<span class="comment">// Yes, I know the debugger exists. This is just a bit easier imho</span>
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST0">If</span> <span class="ST0">the</span> <span class="ST0">result</span> <span class="ST0">from</span> <span class="ST0">Step</span><span class="ST0"> 4 </span><span class="ST0">is</span> <span class="ST0">divisible</span> <span class="ST0">by</span><span class="ST0"> 10, </span><span class="ST0">the</span> <span class="ST0">card</span> <span class="ST0">number</span> <span class="ST0">is</span> <span class="ST0">valid</span><span class="ST0">; </span><span class="ST0">otherwise</span><span class="ST0">, </span><span class="ST0">it</span> <span class="ST0">is</span> <span class="ST0">invalid</span><span class="ST0">.</span> <span class="comment">For</span> <span class="comment">example</span><span class="comment">, </span><span class="comment">the</span> <span class="comment">number</span><span class="comment"> 4388576018402626 </span><span class="comment">is</span> <span class="comment">invalid</span><span class="comment">, </span><span class="comment">but</span> <span class="comment">the</span> <span class="comment">number</span><span class="comment"> 4388576018410707 </span><span class="comment">is</span> <span class="comment">valid</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@param</span> CreditCard
<span class="comment"> * </span><span class="ST0">@return</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST3">isValid</span>(String CreditCard) {
<span class="literal">int</span> sumResult = MP4_CalebFontenot.<span class="ST4">sum</span>(MP4_CalebFontenot.<span class="ST4">doubleDigitsAndSumSingleDigits</span>(CreditCard), MP4_CalebFontenot.<span class="ST4">addOddNumbersFromRightToLeft</span>(CreditCard));
<span class="literal">int</span> divisionResult = sumResult % 10;
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(sumResult + <span class="string">&quot;</span><span class="string">, </span><span class="string">&quot;</span> + divisionResult);
}
<span class="literal">if</span> (divisionResult == 0) {
<span class="literal">return</span> <span class="literal">true</span>;
} <span class="literal">else</span> {
<span class="literal">return</span> <span class="literal">false</span>;
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST0">Sum</span> <span class="ST0">the</span> <span class="ST0">results</span> <span class="ST0">from</span> <span class="ST0">Steps</span><span class="ST0"> 1, 2 </span><span class="ST0">and</span> <span class="ST0">Step</span><span class="ST0"> 3</span><span class="ST0">.</span><span class="comment"> 37 + 38 = 75</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@param</span> step1andSep2 <span class="comment">result</span> <span class="comment">form</span> <span class="comment">step</span><span class="comment"> 1 </span><span class="comment">and</span> <span class="comment">step</span><span class="comment"> 2</span>
<span class="comment"> * </span><span class="ST0">@param</span> step3 <span class="comment">result</span> <span class="comment">form</span> <span class="comment">step</span><span class="comment"> 3</span>
<span class="comment"> * </span><span class="ST0">@return</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">sum</span>(<span class="literal">int</span> step1andStep2, <span class="literal">int</span> step3) {
<span class="literal">return</span> step1andStep2 + step3;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST0">Add</span> <span class="ST0">all</span> <span class="ST0">digits</span> <span class="ST0">in</span> <span class="ST0">the</span> <span class="ST0">odd</span> <span class="ST0">places</span> <span class="ST0">from</span> <span class="ST0">right</span> <span class="ST0">to</span> <span class="ST0">left</span> <span class="ST0">in</span> <span class="ST0">the</span> <span class="ST0">card</span> <span class="ST0">number</span><span class="ST0">.</span><span class="comment"> 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@param</span><span class="comment"> -</span>creditCard <span class="comment">the</span> <span class="comment">credit</span> <span class="comment">card</span> <span class="comment">number</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">processed</span><span class="comment">.</span>
<span class="comment"> * </span><span class="ST0">@return</span> <span class="comment">the</span> <span class="comment">sum</span> <span class="comment">of</span> <span class="comment">digits</span> <span class="comment">at</span> <span class="comment">odd</span> <span class="comment">position</span> <span class="comment">from</span> <span class="comment">right</span> <span class="comment">to</span> <span class="comment">left</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">addOddNumbersFromRightToLeft</span>(String creditCard) {
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Now executing addOddNumbersFromRightToLeft()</span><span class="string">&quot;</span>);
}
<span class="comment">// Iterate through string</span>
<span class="literal">int</span> creditCardLength = creditCard.length();
<span class="literal">int</span> currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
<span class="comment">//System.out.println(creditCard.length());</span>
<span class="literal">for</span> (<span class="literal">int</span> i = (creditCardLength - 1); i &gt;= 0; i--) {
currentDigit = Character.<span class="ST4">getNumericValue</span>(creditCard.charAt(i)); <span class="comment">// Parses current digit as an integer so we can do math on it</span>
<span class="literal">if</span> (currentDigit % 2 != 0) { <span class="comment">// Is this number an odd number?</span>
sum += currentDigit;
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(currentDigit);
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">sum: </span><span class="string">&quot;</span> + sum);
}
}
}
<span class="literal">return</span> sum;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST0">Double</span> <span class="ST0">every</span> <span class="ST0">second</span> <span class="ST0">digit</span> <span class="ST0">from</span> <span class="ST0">right</span> <span class="ST0">to</span> <span class="ST0">left</span><span class="ST0">.</span> <span class="comment">If</span> <span class="comment">doubling</span> <span class="comment">of</span> <span class="comment">a</span> <span class="comment">digit</span> <span class="comment">results</span> <span class="comment">in</span> <span class="comment">a</span> <span class="comment">two</span><span class="comment">-</span><span class="comment">digit</span> <span class="comment">number</span><span class="comment">, </span><span class="comment">add</span> <span class="comment">up</span> <span class="comment">the</span> <span class="comment">two</span> <span class="comment">digits</span> <span class="comment">to</span> <span class="comment">get</span> <span class="comment">a</span> <span class="comment">single</span><span class="comment">-</span><span class="comment">digit</span> <span class="comment">number</span><span class="comment">.</span><span class="comment"> 2 * 2 = 4 2 * 2 = 4 4 * 2 = 8 1 * 2 = 2 6 * 2 = 12 (1 + 2 = 3) 5 * 2 = 10 (1 + 0 = 1) 8 * 2 = 16 (1 + 6 = 7) 4 * 2 = 8 </span><span class="comment">Step</span><span class="comment"> 2 </span><span class="comment">Now</span> <span class="comment">add</span> <span class="comment">all</span> <span class="comment">single</span><span class="comment">-</span><span class="comment">digit</span> <span class="comment">numbers</span> <span class="comment">from</span> <span class="comment">Step</span><span class="comment"> 1</span><span class="comment">.</span><span class="comment"> 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@param</span><span class="comment"> -</span>creditCard <span class="comment">the</span> <span class="comment">credit</span> <span class="comment">card</span> <span class="comment">number</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">processed</span><span class="comment">.</span>
<span class="comment"> * </span><span class="ST0">@return</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">doubleDigitsAndSumSingleDigits</span>(String creditCard) {
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Now executing doubleDigitsAndSumSingleDigits()</span><span class="string">&quot;</span>);
}
<span class="comment">// Iterate through string</span>
<span class="literal">int</span> creditCardLength = creditCard.length();
<span class="literal">int</span> currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
<span class="comment">//System.out.println(creditCard.length());</span>
<span class="literal">for</span> (<span class="literal">int</span> i = (creditCardLength - 1); i &gt;= 0; i--) {
<span class="literal">if</span> (i % 2 == 0) {
currentDigit = Character.<span class="ST4">getNumericValue</span>(creditCard.charAt(i)); <span class="comment">// Parses current digit as an integer so we can do math on it</span>
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">current digit: </span><span class="string">&quot;</span> + currentDigit);
}
digitProduct = currentDigit * 2;
<span class="literal">if</span> (digitProduct &gt; 9) { <span class="comment">// Product is larger than 9. Typecast the integer back into a string so we can seperate the digits and add them together.</span>
String workingString = Integer.<span class="ST4">toString</span>(digitProduct);
tensDigit = Character.<span class="ST4">getNumericValue</span>(workingString.charAt(0));
onesDigit = Character.<span class="ST4">getNumericValue</span>(workingString.charAt(1));
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">tensDigit is </span><span class="string">&quot;</span> + tensDigit);
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">onesDigit is </span><span class="string">&quot;</span> + onesDigit);
}
digitProduct = (tensDigit + onesDigit);
}
sum += digitProduct;
}
}
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">The sum is: </span><span class="string">&quot;</span> + sum);
}
<span class="literal">return</span> sum;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST0">Displays</span> <span class="ST0">Q</span><span class="ST0">/</span><span class="ST0">q</span> <span class="ST0">or</span> <span class="ST0">C</span><span class="ST0">/</span><span class="ST0">c</span><span class="ST0">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">user</span> <span class="comment">enters</span> <span class="comment">C</span><span class="comment">, </span><span class="comment">it</span> <span class="comment">read</span> <span class="comment">the</span> <span class="comment">credit</span> <span class="comment">number</span> <span class="comment">and</span> <span class="comment">returns</span> <span class="comment">it</span> <span class="comment">as</span> <span class="comment">String</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">user</span> <span class="comment">types</span> <span class="comment">Q</span><span class="comment">/</span><span class="comment">q</span> <span class="comment">returns</span><span class="comment"> 0</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@return</span> <span class="comment">a</span> <span class="comment">string</span> <span class="comment">with</span> <span class="comment">credit</span> <span class="comment">card</span> <span class="comment">number</span> <span class="comment">or</span><span class="comment"> 0</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST3">menu</span>() {
String userInput = <span class="string">&quot;&quot;</span>;
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>);
<span class="literal">while</span> (!userInput.equals(<span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>))<span class="comment">//user has not entered Q/q</span>
{
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Press Q/q to quit</span><span class="string">&quot;</span>);
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Press C/c to enter a credit card</span><span class="string">&quot;</span>);
userInput = input.next();
<span class="literal">if</span> (userInput.equalsIgnoreCase(<span class="string">&quot;</span><span class="string">q</span><span class="string">&quot;</span>)) {
userInput = <span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>;
}
<span class="literal">if</span> (userInput.equalsIgnoreCase(<span class="string">&quot;</span><span class="string">c</span><span class="string">&quot;</span>)) {
System.<span class="ST2">out</span>.print(<span class="string">&quot;</span><span class="string">Please enter a credit card number: </span><span class="string">&quot;</span>);
userInput = input.next();
<span class="literal">try</span> { <span class="comment">//Test to see if user has given us a string that looks like a credit card number</span>
<span class="comment">//Does it sucessfully convert to an integer?</span>
Long.<span class="ST4">parseLong</span>(userInput);
} <span class="literal">catch</span>(Exception e) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Invalid input: String doesn&#39;t look like a credit card, it failed successfully convert into an integer.</span><span class="string">&quot;</span>);
<span class="comment">//break;</span>
}
<span class="literal">if</span> (userInput.length() != 16) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Invalid input: String doesn&#39;t look like a credit card, incorrect length.</span><span class="string">&quot;</span>);
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">length: </span><span class="string">&quot;</span> + userInput.length());
}
} <span class="literal">else</span> {
<span class="literal">break</span>;
}
}
}
<span class="literal">return</span> userInput;
}
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@param</span> args
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">main</span>(String[] args) {
String sampleCreditCard = <span class="string">&quot;</span><span class="string">4388576018410707</span><span class="string">&quot;</span>;
String userInput = <span class="ST4">menu</span>();
<span class="literal">if</span> (userInput == <span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>) { <span class="comment">// Stop program execution if credit card not entered</span>
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Credit card not entered!</span><span class="string">&quot;</span>);
<span class="comment">//System.exit(0);</span>
<span class="literal">return</span>;
}
<span class="literal">boolean</span> creditCardValidity = MP4_CalebFontenot.<span class="ST4">isValid</span>(userInput);
String validityString;
<span class="literal">if</span> (creditCardValidity) {
validityString = <span class="string">&quot;</span><span class="string">valid</span><span class="string">&quot;</span>;
} <span class="literal">else</span> {
validityString = <span class="string">&quot;</span><span class="string">invalid</span><span class="string">&quot;</span>;
}
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">The credit card </span><span class="string">&quot;</span> + userInput + <span class="string">&quot;</span><span class="string"> is </span><span class="string">&quot;</span> + validityString);
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,197 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>MP4_CalebFontenot.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #000000; background-color: #ffffff; font-family: monospace}
pre {color: #000000; background-color: #ffffff; font-family: monospace}
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
.ST0 {color: #969696; font-family: monospace; font-weight: bold}
.ST1 {font-family: monospace; font-weight: bold}
.comment {color: #969696}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
.ST4 {font-family: monospace; font-style: italic}
.ST2 {color: #ce54b8; font-family: monospace; font-style: italic}
.string {color: #1e9347}
.literal {color: #336bdd}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * To change this license header, choose License Headers in Project Properties.</span>
<span class="comment"> * To change this template file, choose Tools | Templates</span>
<span class="comment"> * and open the template in the editor.</span>
<span class="comment"> */</span>
<span class="literal">package</span> mp4_calebfontenot;
<span class="literal">import</span> java.util.Scanner;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> <span class="ST1">MP4_CalebFontenot</span> {
<span class="literal">static</span> <span class="literal">boolean</span> <span class="ST2">debug</span> = <span class="literal">true</span>;
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST0">If</span> <span class="ST0">the</span> <span class="ST0">result</span> <span class="ST0">from</span> <span class="ST0">Step</span><span class="ST0"> 4 </span><span class="ST0">is</span> <span class="ST0">divisible</span> <span class="ST0">by</span><span class="ST0"> 10, </span><span class="ST0">the</span> <span class="ST0">card</span> <span class="ST0">number</span> <span class="ST0">is</span> <span class="ST0">valid</span><span class="ST0">; </span><span class="ST0">otherwise</span><span class="ST0">, </span><span class="ST0">it</span> <span class="ST0">is</span> <span class="ST0">invalid</span><span class="ST0">.</span> <span class="comment">For</span> <span class="comment">example</span><span class="comment">, </span><span class="comment">the</span> <span class="comment">number</span><span class="comment"> 4388576018402626 </span><span class="comment">is</span> <span class="comment">invalid</span><span class="comment">, </span><span class="comment">but</span> <span class="comment">the</span> <span class="comment">number</span><span class="comment"> 4388576018410707 </span><span class="comment">is</span> <span class="comment">valid</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@param</span> CreditCard
<span class="comment"> * </span><span class="ST0">@return</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST3">isValid</span>(String CreditCard) {
<span class="literal">int</span> sumResult = MP4_CalebFontenot.<span class="ST4">sum</span>(MP4_CalebFontenot.<span class="ST4">doubleDigitsAndSumSingleDigits</span>(CreditCard), MP4_CalebFontenot.<span class="ST4">addOddNumbersFromRightToLeft</span>(CreditCard));
<span class="literal">int</span> divisionResult = sumResult % 10;
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(sumResult + <span class="string">&quot;</span><span class="string">, </span><span class="string">&quot;</span> + divisionResult);
}
<span class="literal">if</span> (divisionResult == 0) {
<span class="literal">return</span> <span class="literal">true</span>;
} <span class="literal">else</span> {
<span class="literal">return</span> <span class="literal">false</span>;
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST0">Sum</span> <span class="ST0">the</span> <span class="ST0">results</span> <span class="ST0">from</span> <span class="ST0">Steps</span><span class="ST0"> 1, 2 </span><span class="ST0">and</span> <span class="ST0">Step</span><span class="ST0"> 3</span><span class="ST0">.</span><span class="comment"> 37 + 38 = 75</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@param</span> step1andSep2 <span class="comment">result</span> <span class="comment">form</span> <span class="comment">step</span><span class="comment"> 1 </span><span class="comment">and</span> <span class="comment">step</span><span class="comment"> 2</span>
<span class="comment"> * </span><span class="ST0">@param</span> step3 <span class="comment">result</span> <span class="comment">form</span> <span class="comment">step</span><span class="comment"> 3</span>
<span class="comment"> * </span><span class="ST0">@return</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">sum</span>(<span class="literal">int</span> step1andStep2, <span class="literal">int</span> step3) {
<span class="literal">return</span> step1andStep2 + step3;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST0">Add</span> <span class="ST0">all</span> <span class="ST0">digits</span> <span class="ST0">in</span> <span class="ST0">the</span> <span class="ST0">odd</span> <span class="ST0">places</span> <span class="ST0">from</span> <span class="ST0">right</span> <span class="ST0">to</span> <span class="ST0">left</span> <span class="ST0">in</span> <span class="ST0">the</span> <span class="ST0">card</span> <span class="ST0">number</span><span class="ST0">.</span><span class="comment"> 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@param</span><span class="comment"> -</span>creditCard <span class="comment">the</span> <span class="comment">credit</span> <span class="comment">card</span> <span class="comment">number</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">processed</span><span class="comment">.</span>
<span class="comment"> * </span><span class="ST0">@return</span> <span class="comment">the</span> <span class="comment">sum</span> <span class="comment">of</span> <span class="comment">digits</span> <span class="comment">at</span> <span class="comment">odd</span> <span class="comment">position</span> <span class="comment">from</span> <span class="comment">right</span> <span class="comment">to</span> <span class="comment">left</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">addOddNumbersFromRightToLeft</span>(String creditCard) {
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Now executing addOddNumbersFromRightToLeft()</span><span class="string">&quot;</span>);
}
<span class="comment">// Iterate through string</span>
<span class="literal">int</span> creditCardLength = creditCard.length();
<span class="literal">int</span> currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
<span class="comment">//System.out.println(creditCard.length());</span>
<span class="literal">for</span> (<span class="literal">int</span> i = (creditCardLength - 1); i &gt;= 0; i--) {
currentDigit = Character.<span class="ST4">getNumericValue</span>(creditCard.charAt(i)); <span class="comment">// Parses current digit as an integer so we can do math on it</span>
<span class="literal">if</span> (currentDigit % 2 != 0) { <span class="comment">// Is this number an odd number?</span>
sum += currentDigit;
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(currentDigit);
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">sum: </span><span class="string">&quot;</span> + sum);
}
}
}
<span class="literal">return</span> sum;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST0">Double</span> <span class="ST0">every</span> <span class="ST0">second</span> <span class="ST0">digit</span> <span class="ST0">from</span> <span class="ST0">right</span> <span class="ST0">to</span> <span class="ST0">left</span><span class="ST0">.</span> <span class="comment">If</span> <span class="comment">doubling</span> <span class="comment">of</span> <span class="comment">a</span> <span class="comment">digit</span> <span class="comment">results</span> <span class="comment">in</span> <span class="comment">a</span> <span class="comment">two</span><span class="comment">-</span><span class="comment">digit</span> <span class="comment">number</span><span class="comment">, </span><span class="comment">add</span> <span class="comment">up</span> <span class="comment">the</span> <span class="comment">two</span> <span class="comment">digits</span> <span class="comment">to</span> <span class="comment">get</span> <span class="comment">a</span> <span class="comment">single</span><span class="comment">-</span><span class="comment">digit</span> <span class="comment">number</span><span class="comment">.</span><span class="comment"> 2 * 2 = 4 2 * 2 = 4 4 * 2 = 8 1 * 2 = 2 6 * 2 = 12 (1 + 2 = 3) 5 * 2 = 10 (1 + 0 = 1) 8 * 2 = 16 (1 + 6 = 7) 4 * 2 = 8 </span><span class="comment">Step</span><span class="comment"> 2 </span><span class="comment">Now</span> <span class="comment">add</span> <span class="comment">all</span> <span class="comment">single</span><span class="comment">-</span><span class="comment">digit</span> <span class="comment">numbers</span> <span class="comment">from</span> <span class="comment">Step</span><span class="comment"> 1</span><span class="comment">.</span><span class="comment"> 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@param</span><span class="comment"> -</span>creditCard <span class="comment">the</span> <span class="comment">credit</span> <span class="comment">card</span> <span class="comment">number</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">processed</span><span class="comment">.</span>
<span class="comment"> * </span><span class="ST0">@return</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">doubleDigitsAndSumSingleDigits</span>(String creditCard) {
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Now executing doubleDigitsAndSumSingleDigits()</span><span class="string">&quot;</span>);
}
<span class="comment">// Iterate through string</span>
<span class="literal">int</span> creditCardLength = creditCard.length();
<span class="literal">int</span> currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
<span class="comment">//System.out.println(creditCard.length());</span>
<span class="literal">for</span> (<span class="literal">int</span> i = (creditCardLength - 1); i &gt;= 0; i--) {
<span class="literal">if</span> (i % 2 == 0) {
currentDigit = Character.<span class="ST4">getNumericValue</span>(creditCard.charAt(i)); <span class="comment">// Parses current digit as an integer so we can do math on it</span>
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">current digit: </span><span class="string">&quot;</span> + currentDigit);
}
digitProduct = currentDigit * 2;
<span class="literal">if</span> (digitProduct &gt; 9) { <span class="comment">// Product is larger than 9. Typecast the integer back into a string so we can seperate the digits and add them together.</span>
String workingString = Integer.<span class="ST4">toString</span>(digitProduct);
tensDigit = Character.<span class="ST4">getNumericValue</span>(workingString.charAt(0));
onesDigit = Character.<span class="ST4">getNumericValue</span>(workingString.charAt(1));
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">tensDigit is </span><span class="string">&quot;</span> + tensDigit);
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">onesDigit is </span><span class="string">&quot;</span> + onesDigit);
}
digitProduct = (tensDigit + onesDigit);
}
sum += digitProduct;
}
}
<span class="literal">if</span> (<span class="ST2">debug</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">The sum is: </span><span class="string">&quot;</span> + sum);
}
<span class="literal">return</span> sum;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST0">Displays</span> <span class="ST0">Q</span><span class="ST0">/</span><span class="ST0">q</span> <span class="ST0">or</span> <span class="ST0">C</span><span class="ST0">/</span><span class="ST0">c</span><span class="ST0">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">user</span> <span class="comment">enters</span> <span class="comment">C</span><span class="comment">, </span><span class="comment">it</span> <span class="comment">read</span> <span class="comment">the</span> <span class="comment">credit</span> <span class="comment">number</span> <span class="comment">and</span> <span class="comment">returns</span> <span class="comment">it</span> <span class="comment">as</span> <span class="comment">String</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">user</span> <span class="comment">types</span> <span class="comment">Q</span><span class="comment">/</span><span class="comment">q</span> <span class="comment">returns</span><span class="comment"> 0</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@return</span> <span class="comment">a</span> <span class="comment">string</span> <span class="comment">with</span> <span class="comment">credit</span> <span class="comment">card</span> <span class="comment">number</span> <span class="comment">or</span><span class="comment"> 0</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST3">menu</span>() {
String userInput = <span class="string">&quot;&quot;</span>;
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>);
<span class="literal">while</span> (!userInput.equals(<span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>))<span class="comment">//user has not entered Q/q</span>
{
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Press Q/q to quit</span><span class="string">&quot;</span>);
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Press C/c to enter a credit card</span><span class="string">&quot;</span>);
userInput = input.next();
<span class="literal">if</span> (userInput.equalsIgnoreCase(<span class="string">&quot;</span><span class="string">q</span><span class="string">&quot;</span>)) {
userInput = <span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>;
}
<span class="literal">if</span> (userInput.equalsIgnoreCase(<span class="string">&quot;</span><span class="string">c</span><span class="string">&quot;</span>)) {
System.<span class="ST2">out</span>.print(<span class="string">&quot;</span><span class="string">Please enter a credit card number: </span><span class="string">&quot;</span>);
userInput = input.next();
<span class="literal">break</span>;
}
}
<span class="literal">return</span> userInput;
}
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@param</span> args
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">main</span>(String[] args) {
<span class="comment">//String sampleCreditCard = &quot;4388576018410707&quot;;</span>
String userInput = <span class="ST4">menu</span>();
<span class="literal">if</span> (userInput == <span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>) { <span class="comment">// Stop program execution if credit card not entered</span>
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Credit card not entered!</span><span class="string">&quot;</span>);
System.<span class="ST4">exit</span>(0);
}
<span class="literal">boolean</span> creditCardValidity = MP4_CalebFontenot.<span class="ST4">isValid</span>(userInput);
String validityString;
<span class="literal">if</span> (creditCardValidity) {
validityString = <span class="string">&quot;</span><span class="string">valid</span><span class="string">&quot;</span>;
} <span class="literal">else</span> {
validityString = <span class="string">&quot;</span><span class="string">invalid</span><span class="string">&quot;</span>;
}
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">The credit card </span><span class="string">&quot;</span> + userInput + <span class="string">&quot;</span><span class="string"> is </span><span class="string">&quot;</span> + validityString);
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="MP4_CalebFontenot" default="default" basedir=".">
<description>Builds, tests, and runs the project MP4_CalebFontenot.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="MP4_CalebFontenot-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
build.xml.data.CRC32=a89314ee
build.xml.script.CRC32=65aa903c
build.xml.stylesheet.CRC32=f85dc8f2@1.104.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=a89314ee
nbproject/build-impl.xml.script.CRC32=22fbeebc
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.104.0.48

View File

@@ -0,0 +1,95 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.modulepath=\
${run.modulepath}
debug.test.classpath=\
${run.test.classpath}
debug.test.modulepath=\
${run.test.modulepath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/MP4_CalebFontenot.jar
dist.javadoc.dir=${dist.dir}/javadoc
dist.jlink.dir=${dist.dir}/jlink
dist.jlink.output=${dist.jlink.dir}/MP4_CalebFontenot
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.source=1.8
javac.target=1.8
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.modulepath=\
${javac.modulepath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.html5=false
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
# The jlink additional root modules to resolve
jlink.additionalmodules=
# The jlink additional command line parameters
jlink.additionalparam=
jlink.launcher=true
jlink.launcher.name=MP4_CalebFontenot
main.class=mp4_calebfontenot.MP4_CalebFontenot
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.modulepath=\
${javac.modulepath}
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
run.test.modulepath=\
${javac.test.modulepath}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>MP4_CalebFontenot</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>

View File

@@ -0,0 +1,188 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mp4_calebfontenot;
import java.util.Scanner;
/**
*
* @author caleb
*/
public class MP4_CalebFontenot {
static boolean debug = false;
// Yes, I know the debugger exists. This is just a bit easier imho
/**
* If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid
*
* @param CreditCard
* @return
*/
public static boolean isValid(String CreditCard) {
int sumResult = MP4_CalebFontenot.sum(MP4_CalebFontenot.doubleDigitsAndSumSingleDigits(CreditCard), MP4_CalebFontenot.addOddNumbersFromRightToLeft(CreditCard));
int divisionResult = sumResult % 10;
if (debug) {
System.out.println(sumResult + ", " + divisionResult);
}
if (divisionResult == 0) {
return true;
} else {
return false;
}
}
/**
* Sum the results from Steps 1, 2 and Step 3. 37 + 38 = 75
*
* @param step1andSep2 result form step 1 and step 2
* @param step3 result form step 3
* @return
*/
public static int sum(int step1andStep2, int step3) {
return step1andStep2 + step3;
}
/**
* Add all digits in the odd places from right to left in the card number. 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
*
* @param -creditCard the credit card number to be processed.
* @return the sum of digits at odd position from right to left.
*/
public static int addOddNumbersFromRightToLeft(String creditCard) {
if (debug) {
System.out.println("Now executing addOddNumbersFromRightToLeft()");
}
// Iterate through string
int creditCardLength = creditCard.length();
int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
//System.out.println(creditCard.length());
for (int i = (creditCardLength - 1); i >= 0; i--) {
currentDigit = Character.getNumericValue(creditCard.charAt(i)); // Parses current digit as an integer so we can do math on it
if (currentDigit % 2 != 0) { // Is this number an odd number?
sum += currentDigit;
if (debug) {
System.out.println(currentDigit);
System.out.println("sum: " + sum);
}
}
}
return sum;
}
/**
* Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number. 2 * 2 = 4 2 * 2 = 4 4 * 2 = 8 1 * 2 = 2 6 * 2 = 12 (1 + 2 = 3) 5 * 2 = 10 (1 + 0 = 1) 8 * 2 = 16 (1 + 6 = 7) 4 * 2 = 8 Step 2 Now add all single-digit numbers from Step 1. 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
*
* @param -creditCard the credit card number to be processed.
* @return
*/
public static int doubleDigitsAndSumSingleDigits(String creditCard) {
if (debug) {
System.out.println("Now executing doubleDigitsAndSumSingleDigits()");
}
// Iterate through string
int creditCardLength = creditCard.length();
int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
//System.out.println(creditCard.length());
for (int i = (creditCardLength - 1); i >= 0; i--) {
if (i % 2 == 0) {
currentDigit = Character.getNumericValue(creditCard.charAt(i)); // Parses current digit as an integer so we can do math on it
if (debug) {
System.out.println("current digit: " + currentDigit);
}
digitProduct = currentDigit * 2;
if (digitProduct > 9) { // Product is larger than 9. Typecast the integer back into a string so we can seperate the digits and add them together.
String workingString = Integer.toString(digitProduct);
tensDigit = Character.getNumericValue(workingString.charAt(0));
onesDigit = Character.getNumericValue(workingString.charAt(1));
if (debug) {
System.out.println("tensDigit is " + tensDigit);
System.out.println("onesDigit is " + onesDigit);
}
digitProduct = (tensDigit + onesDigit);
}
sum += digitProduct;
}
}
if (debug) {
System.out.println("The sum is: " + sum);
}
return sum;
}
/**
* Displays Q/q or C/c. If the user enters C, it read the credit number and returns it as String. If the user types Q/q returns 0
*
* @return a string with credit card number or 0.
*/
public static String menu() {
String userInput = "";
Scanner input = new Scanner(System.in);
while (!userInput.equals("0"))//user has not entered Q/q
{
System.out.println("Press Q/q to quit");
System.out.println("Press C/c to enter a credit card");
userInput = input.next();
if (userInput.equalsIgnoreCase("q")) {
userInput = "0";
}
if (userInput.equalsIgnoreCase("c")) {
System.out.print("Please enter a credit card number: ");
userInput = input.next();
try { //Test to see if user has given us a string that looks like a credit card number
//Does it sucessfully convert to an integer?
Long.parseLong(userInput);
} catch(Exception e) {
System.out.println("Invalid input: String doesn't look like a credit card, it failed successfully convert into an integer.");
//break;
}
if (userInput.length() != 16) {
System.out.println("Invalid input: String doesn't look like a credit card, incorrect length.");
if (debug) {
System.out.println("length: " + userInput.length());
}
} else {
break;
}
}
}
return userInput;
}
/**
*
* @param args
*/
public static void main(String[] args) {
//String sampleCreditCard = "4388576018410707";
String userInput = menu();
if (userInput == "0") { // Stop program execution if credit card not entered
System.out.println("Credit card not entered!");
//System.exit(0);
return;
}
boolean creditCardValidity = MP4_CalebFontenot.isValid(userInput);
String validityString;
if (creditCardValidity) {
validityString = "valid";
} else {
validityString = "invalid";
}
System.out.println("The credit card " + userInput + " is " + validityString);
}
}