Skip to content

Instantly share code, notes, and snippets.

View tgruber5150's full-sized avatar

Tony Gruber tgruber5150

View GitHub Profile
@tgruber5150
tgruber5150 / FindTheNumberClosestToZero
Created December 6, 2012 01:30
This program finds the number in an array that's closest to zero.
import org.junit.Test;
public class BetterProgrammer05122012Test extends junit.framework.TestCase {
public static int[] numbers = new int[] {5, 10, -5, 0, 25, 35};
public static int[] numbers2 = new int[] {5, 10, -5, -2, 0, 25, 35};
public static int[] noNegativeNumbers = new int[] {5, 10, 15, 25, 35};
@Test
public void testBetterProgrammer05122012 () {
@tgruber5150
tgruber5150 / Total a string of numbers
Created December 5, 2012 02:36
This program pulls numbers out of a string and totals them
import org.junit.Test;
public class BetterProgrammingTaskTest extends junit.framework.TestCase {
public static String numbers = "5 25 35 5" ;
@Test
public void testGetSumOfNumbers() throws Exception {
assertEquals(70, BetterProgrammingTask.getSumOfNumbers(numbers));
}
}
@tgruber5150
tgruber5150 / MultiDimArray.java
Created September 14, 2012 20:17
This exercise demonstrates the nature of 3 dimensional arrays. It might be helpful to think of them as an indexed box, rather than 3 lists.
import java.util.Random;
import java.util.Scanner;
public class MultiDimArray {
static Random rand = new Random();
static int count = 0;
public static void main(String[] args) {
// 3-D array with fixed length:
@tgruber5150
tgruber5150 / SalesTax.java
Created September 11, 2012 18:15
Sales Tax Calculator: For either Point-of-Sale or After-the-Sale methods.
import java.text.DecimalFormat;
import java.util.Scanner;
public class SalesTaxInput {
public static void main(String[] args){
System.out.print("What are your taxable sales? ");
double sales = getInput();
sales = SalesTax.roundTwoDecimals(sales);
System.out.print("What is your sales tax rate? 1% = .01: ");
@tgruber5150
tgruber5150 / PickANumber.java
Created September 10, 2012 23:43
Pick a number: You get 5 changes to pick the right number, then you're kicked out of the program!
import java.util.Scanner;
public class PickANumber {
private static int a = (int) (Math.random() * 10 + 1);
public static void main (String[] args) {
for (int i = 0; i < 5; ++i) {
System.out.print("Pick a number from 1 to 10: ");
Scanner in = new Scanner (System.in);
int b = Integer.parseInt(in.next());
@tgruber5150
tgruber5150 / alphabetInNumericOrder.java
Created May 7, 2012 19:43
I refactored the alphabet program so a separate method is easy to copy for other applications. Feel free to use this program, or the separate method; labled 'addOrderAppendage'.
import java.util.Scanner;
class Alphabet {
static String[] fields = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z"};
public static void main(String[] args) {
String letter = null;
letter = pickALetter(letter);