Cornelius Eanes's Programming Portal
Assignments (Methods)
[90] Picture Menu

GOAL: Learn how to call methods

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Picture Menu
/// File Name: PictureMenu.java
/// Date Finished: Feb 19, 2016

import java.util.Scanner;

public class PictureMenu
{
    public static void main( String[] args )
    {
        Scanner kb = new Scanner(System.in);
        int choice;

        System.out.println( "1. Butterfly " );
        System.out.println( "2. Elephant  " );
        System.out.println( "3. Teddy Bear" );
        System.out.println( "4. Snake     " );

        System.out.print( "\nWhich animal to draw? " );
        choice = kb.nextInt();
        System.out.println();

        if ( choice == 1 )
        {
            butterfly();
            butterfly();
            // the method is ran twice, rather, the code in that method is ran twice
        }
        else if ( choice == 2 )
        {
            elephant();
        }
        else if ( choice == 3 )
        {
            teddybear();
        }
        else if ( choice == 4 )
        {
            snake();
        }
        else
        {
            System.out.println( "Sorry, that wasn't one of the choices." );
        }

        System.out.println( "\nGoodbye!" );
    }

    public static void butterfly()
    {
        System.out.println("  .==-.                   .-==.     ");
        System.out.println("   \\()8`-._  `.   .'  _.-'8()/     ");
        System.out.println("   (88\"   ::.  \\./  .::   \"88)     ");
        System.out.println("    \\_.'`-::::.(#).::::-'`._/      ");
        System.out.println("      `._... .q(_)p. ..._.'         ");
        System.out.println("        \"\"-..-'|=|`-..-\"\"       ");
        System.out.println("        .\"\"' .'|=|`. `\"\".       ");
        System.out.println("      ,':8(o)./|=|\\.(o)8:`.        ");
        System.out.println("     (O :8 ::/ \\_/ \\:: 8: O)      ");
        System.out.println("      \\O `::/       \\::' O/       ");
        System.out.println("       \"\"--'         `--\"\"   hjw");
    }

    public static void elephant()
    {
        System.out.println("       _..--\"\"-.                  .-\"\"--.._ ");
        System.out.println("   _.-'         \\ __...----...__ /         '-._");
        System.out.println(" .'      .:::...,'              ',...:::.      '.");
        System.out.println("(     .'``'''::;                  ;::'''``'.     )");
        System.out.println(" \\             '-)              (-'             /");
        System.out.println("  \\             /                \\             /");
        System.out.println("   \\          .'.-.            .-.'.          /");
        System.out.println("    \\         | \\0|            |0/ |         /");
        System.out.println("    |          \\  |   .-==-.   |  /          |");
        System.out.println("     \\          `/`;          ;`\\`          /");
        System.out.println("      '.._      (_ |  .-==-.  | _)      _..'");
        System.out.println("          `\"`\"-`/ `/'        '\\` \\`-\"`\"`");
        System.out.println("               / /`;   .==.   ;`\\ \\");
        System.out.println("         .---./_/   \\  .==.  /   \\ \\");
        System.out.println("        / '.    `-.__)       |    `\"");
        System.out.println("       | =(`-.        '==.   ;");
        System.out.println(" jgs    \\  '. `-.           /");
        System.out.println("         \\_:_)   `\"--.....-'");
    }

    public static void teddybear()
    {
        System.out.println("            ___   .--. ");
        System.out.println("      .--.-\"   \"-' .- |");
        System.out.println("     / .-,`          .'");
        System.out.println("     \\   `           \\");
        System.out.println("      '.            ! \\");
        System.out.println("        |     !  .--.  |");
        System.out.println("        \\        '--'  /.____");
        System.out.println("       /`-.     \\__,'.'      `\\");
        System.out.println("    __/   \\`-.____.-' `\\      /");
        System.out.println("    | `---`'-'._/-`     \\----'    _");
        System.out.println("    |,-'`  /             |    _.-' `\\");
        System.out.println("   .'     /              |--'`     / |");
        System.out.println("  /      /\\              `         | |");
        System.out.println("  |   .\\/  \\      .--. __          \\ |");
        System.out.println("   '-'      '._       /  `\\         /");
        System.out.println("      jgs      `\\    '     |------'`");
        System.out.println("                 \\  |      |");
        System.out.println("                  \\        /");
        System.out.println("                   '._  _.'");
        System.out.println("                      ``");

    }

    public static void snake()
    {
        System.out.println("         ,,'6''-,.");
        System.out.println("        <====,.;;--.");
        System.out.println("        _`---===. \"\"\"==__");
        System.out.println("      //\"\"@@-\\===\\@@@@ \"\"\\\\");
        System.out.println("     |( @@@  |===|  @@@  ||");
        System.out.println("      \\\\ @@   |===|  @@  //");
        System.out.println("        \\\\ @@ |===|@@@ //");
        System.out.println("         \\\\  |===|  //");
        System.out.println("___________\\\\|===| //_____,----\"\"\"\"\"\"\"\"\"\"-----,_");
        System.out.println("  \"\"\"\"---,__`\\===`/ _________,---------,____    `,");
        System.out.println("             |==||                           `\\   \\");
        System.out.println("            |==| |          pb                 )   |");
        System.out.println("           |==| |       _____         ______,--'   '");
        System.out.println("           |=|  `----\"\"\"     `\"\"\"\"\"\"\"\"         _,-'");
        System.out.println("            `=\\     __,---\"\"\"-------------\"\"\"''");
        System.out.println("                \"\"\"\"		");
    }
}

Output

[91] Flicker Phase

GOAL: Learn what methods do.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: PROGRAM_NAME
/// File Name: FlickerPhrase.java
/// Date Finished: Feb 19, 2016

import java.util.Random;

public class FlickerPhrase
{
    public static void main( String[] args ) throws Exception
    {
        Random rng = new Random();
        int r;

        for ( int i=0; i<100000; i++ )
        {
            r = 1 + rng.nextInt(5) + 1;
            // Write five if statements here.
            // If r is 1, then call the method named 'first'.
            // If r is 2, then call the method named 'second', and so on.
            if (r == 1) {
                first();
            } else if (r == 2) {
                second();
            } else if (r == 3) {
                third();
            } else if (r == 4) {
                fourth();
            } else if (r == 5) {
                fifth();
            }

            //Thread.sleep(1000);
            // Optional: after the if statements are over, add in a slight delay.
        }

        System.out.println("I pledge allegiance to the flag.");

    }

    public static void first()
    {
        System.out.print("I                               \r");
    }

    public static void second()
    {
        System.out.print("  pledge                        \r");
    }

    public static void third()
    {
        System.out.print("         allegiance             \r");
    }

    public static void fourth()
    {
        System.out.print("                    to the      \r");
    }

    public static void fifth()
    {
        System.out.print("                           flag.\r");
    }
}

Output

[92] Heron's Formula

GOAL: Use a method that has a return value.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Heron's Formula
/// File Name: HeronsForumula.java
/// Date Finished: Feb 19, 2016

public class HeronsFormula
{
    public static void main( String[] args )
    {
        double a;

        a = triangleArea(3, 3, 3);
        System.out.println("A triangle with sides 3,3,3 has an area of " + a );

        a = triangleArea(3, 4, 5);
        System.out.println("A triangle with sides 3,4,5 has an area of " + a );

        a = triangleArea(7, 8, 9);
        System.out.println("A triangle with sides 7,8,9 has an area of " + a );

        System.out.println("A triangle with sides 5,12,13 has an area of " + triangleArea(5, 12, 13) );
        System.out.println("A triangle with sides 10,9,11 has an area of " + triangleArea(10, 9, 11) );
        System.out.println("A triangle with sides 8,15,17 has an area of " + triangleArea(8, 15, 17) );

        System.out.println("A triangle with sides 9,9,9 has an area of " + triangleArea(9, 9, 9));
    }

    public static double triangleArea( int a, int b, int c )
    {
        // the code in this method computes the area of a triangle whose sides have lengths a, b, and c
        double s, A;

        s = (a+b+c) / 2.0;
        A = Math.sqrt( s*(s-a)*(s-b)*(s-c) );

        return A;
        // ^ after computing the area, "return" it
    }

    /*

     * Both code snippets return the same results.
     * The class with the extra method is 23 lines long (with whitespace and comments removed).
     * The class without the extra method is 44 lines long (with whitespace and comments removed).
     * It was easier to fix the file with the method, as I only had to change one value.
     * It was easier to add to the class with the method, as I only needed one line of code.

     */

}

Output

[93] Distance Formula

GOAL: Use a method to easily calculate the distance formula.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Distance Formula
/// File Name: DistanceFormula.java
/// Date Finished: Feb 19, 2016

public class DistanceFormula
{
    public static void main( String[] args )
    {
        // test the formula a bit
        double d1 = distance(-2,1 , 1,5);
        System.out.println(" (-2,1) to (1,5) => " + d1 );

        double d2 = distance(-2,-3 , -4,4);
        System.out.println(" (-2,-3) to (-4,4) => " + d2 );

        System.out.println(" (2,-3) to (-1,-2) => " + distance(2,-3,-1,-2) );

        System.out.println(" (4,5) to (4,5) => " + distance(4,5,4,5) );
    }

    public static double distance( int x1, int y1, int x2, int y2 )
    {
        return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
    }
}

Output

[94] Month Name

GOAL: Using a method, figure out what month it is.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: PROGRAM_NAME
/// File Name: MonthName.java
/// Date Finished: Feb 19, 2016

public class MonthName
{
    public static String monthName( int month )
    {
        String result;
        // Your code goes in here.
        if (month == 1) {
            result = "January";
        } else if (month == 2) {
            result = "February";
        } else if (month == 3) {
            result = "March";
        } else if (month == 4) {
            result = "April";
        } else if (month == 5) {
            result = "May";
        } else if (month == 6) {
            result = "June";
        } else if (month == 7) {
            result = "July";
        } else if (month == 8) {
            result = "August";
        } else if (month == 9) {
            result = "September";
        } else if (month == 10) {
            result = "October";
        } else if (month == 11) {
            result = "November";
        } else if (month == 12) {
            result = "December";
        } else {
            result = "error";
        }
        return result;
    }


    public static void main( String[] args )
    {
        System.out.println( "Month 1: " + monthName(1) );
        System.out.println( "Month 2: " + monthName(2) );
        System.out.println( "Month 3: " + monthName(3) );
        System.out.println( "Month 4: " + monthName(4) );
        System.out.println( "Month 5: " + monthName(5) );
        System.out.println( "Month 6: " + monthName(6) );
        System.out.println( "Month 7: " + monthName(7) );
        System.out.println( "Month 8: " + monthName(8) );
        System.out.println( "Month 9: " + monthName(9) );
        System.out.println( "Month 10: " + monthName(10) );
        System.out.println( "Month 11: " + monthName(11) );
        System.out.println( "Month 12: " + monthName(12) );
        System.out.println( "Month 43: " + monthName(43) );
    }
}

Output

[95] Month Offset

GOAL: Using a method, figure out a month's offset when calculating the weekday.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: PROGRAM_NAME
/// File Name: MonthOffset.java
/// Date Finished: Feb 19, 2016

public class MonthOffset
{
    public static int monthOffset( int m )
    {
        int result;
        // Your code goes in here.
        if (m == 1) {
            result = 1;
        } else if (m == 2) {
            result = 4;
        } else if (m == 3) {
            result = 4;
        } else if (m == 4) {
            result = 0;
        } else if (m == 5) {
            result = 2;
        } else if (m == 6) {
            result = 5;
        } else if (m == 7) {
            result = 0;
        } else if (m == 8) {
            result = 3;
        } else if (m == 9) {
            result = 6;
        } else if (m == 10) {
            result = 1;
        } else if (m == 11) {
            result = 4;
        } else if (m == 12) {
            result = 6;
        } else {
            result = -1;
        }
        return result;
    }

    public static void main( String[] args )
    {
        System.out.println( "Offset for month 1: " + monthOffset(1) );
        System.out.println( "Offset for month 2: " + monthOffset(2) );
        System.out.println( "Offset for month 3: " + monthOffset(3) );
        System.out.println( "Offset for month 4: " + monthOffset(4) );
        System.out.println( "Offset for month 5: " + monthOffset(5) );
        System.out.println( "Offset for month 6: " + monthOffset(6) );
        System.out.println( "Offset for month 7: " + monthOffset(7) );
        System.out.println( "Offset for month 8: " + monthOffset(8) );
        System.out.println( "Offset for month 9: " + monthOffset(9) );
        System.out.println( "Offset for month 10: " + monthOffset(10) );
        System.out.println( "Offset for month 11: " + monthOffset(11) );
        System.out.println( "Offset for month 12: " + monthOffset(12) );
        System.out.println( "Offset for month 43: " + monthOffset(43) );
    }

}

Output

[96] Weekday Calculator

GOAL: Using previous methods, calculate what weekday you were born on.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Weekday Calculator
/// File Name: WeekdayCalculator.java
/// Date Finished: Feb 25, 2016

import java.util.Scanner;

public class WeekdayCalculator {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("What's your birthday? (MM DD YYYY)");
        System.out.print("> ");

        int month = input.nextInt();
        int day = input.nextInt();
        int year = input.nextInt();

        String result = getWeekdayString(day, month, year);

        System.out.println("You were born on " + result + "!");

    }

    public static int getMonthOffset(int m) {
        int result;
        if (m == 1) {
            result = 1;
        } else if (m == 2) {
            result = 4;
        } else if (m == 3) {
            result = 4;
        } else if (m == 4) {
            result = 0;
        } else if (m == 5) {
            result = 2;
        } else if (m == 6) {
            result = 5;
        } else if (m == 7) {
            result = 0;
        } else if (m == 8) {
            result = 3;
        } else if (m == 9) {
            result = 6;
        } else if (m == 10) {
            result = 1;
        } else if (m == 11) {
            result = 4;
        } else if (m == 12) {
            result = 6;
        } else {
            result = -1;
        }
        return result;
    }

    public static boolean isLeapYear(int year) {
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    }

    public static String getWeekdayName(int weekday) {
        String result;
        if (weekday == 1) {
            result = "Sunday";
        } else if (weekday == 2) {
            result = "Monday";
        } else if (weekday == 3) {
            result = "Tuesday";
        } else if (weekday == 4) {
            result = "Wednesday";
        } else if (weekday == 5) {
            result = "Thursday";
        } else if (weekday == 6) {
            result = "Friday";
        } else if (weekday == 7) {
            result = "Saturday";
        } else {
            result = "error";
        }
        return result;
    }

    public static String getMonthName(int month) {
        String result;
        if (month == 1) {
            result = "January";
        } else if (month == 2) {
            result = "February";
        } else if (month == 3) {
            result = "March";
        } else if (month == 4) {
            result = "April";
        } else if (month == 5) {
            result = "May";
        } else if (month == 6) {
            result = "June";
        } else if (month == 7) {
            result = "July";
        } else if (month == 8) {
            result = "August";
        } else if (month == 9) {
            result = "September";
        } else if (month == 10) {
            result = "October";
        } else if (month == 11) {
            result = "November";
        } else if (month == 12) {
            result = "December";
        } else {
            result = "error";
        }
        return result;
    }

    public static String getWeekdayString(int day, int month, int year) {
        int yy = year - 1900;
        int total = yy / 4;
        total += yy;
        total += day;
        total += getMonthOffset(month);
        if (isLeapYear(year) || month == 1 || month == 2) {
            total--;
        }
        int r = total % 7;
        String weekdayStr = getWeekdayName(r);
        String monthStr = getMonthName(month);
        return weekdayStr + ", " + monthStr + " " + day + ", " + year;
    }

}

Output

[97] Area Calculator

GOAL: Calculate the areas of shapes with methods.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Area Calculator
/// File Name: AreaCalculator.java
/// Date Finished: Feb 25, 2016

import java.util.Scanner;

public class AreaCalculator {

    public static double getAreaCircle(double radius) {
        return Math.PI * (radius * radius);
    }

    public static double getAreaRectangle(double length, double width) {
        return length * width;
    }

    public static double getAreaTriangle(double base, double height) {
        return 0.5 * base * height;
    }

    public static double getAreaSquare(double length) {
        return length * length;
    }

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Shape Area Calculator\n");

        int choice = -1;
        while (choice != 5) {
            System.out.println("1) Square\n2) Rectangle\n3) Triangle\n4) Circle\n5) Quit");
            System.out.print("> ");
            choice = input.nextInt();
            System.out.println();
            if (choice == 1) {
                System.out.print("Length: ");
                double length = input.nextDouble();
                System.out.println("\nThe area is " + getAreaSquare(length) + ".\n");
            } else if (choice == 2) {
                System.out.print("Length: ");
                double length = input.nextDouble();
                System.out.print("Width: ");
                double width = input.nextDouble();
                System.out.println("\nThe area is " + getAreaRectangle(length, width) + ".\n");
            } else if (choice == 3) {
                System.out.print("Base: ");
                double base = input.nextDouble();
                System.out.print("Height: ");
                double height = input.nextDouble();
                System.out.println("\nThe area is " + getAreaTriangle(base, height) + ".\n");
            } else if (choice == 4) {
                System.out.print("Radius: ");
                double radius = input.nextDouble();
                System.out.println("\nThe area is " + getAreaCircle(radius) + ".\n");
            }
        }

    }

}

Output

[98] Function Call Alphabet

GOAL: Correctly print a lot of stuff to the screen.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Function Call Alphabet
/// File Name: MethodCallAlphabet.java
/// Date Finished: Feb 26, 2016

// Method Call Alphabet - Yet another program to practice methods, but this time it's method calls only.

public class MethodCallAlphabet
{
    public static void main( String[] args )
    {
        // ???? a( ???? );    // displays a word beginning with A
        // ???? b( ???? );    // returns the word to be displayed
        // ???? c( ???? );    // pass it 'true' and it will display a word beginning with C
        // ???? d( ???? );    // displays a word beginning with D
        // ???? e( ???? );    // pass it the number of letters to display (9)
        // ???? f( ???? );    // displays the word you pass it beginning with "F"
        // ???? g( ???? );    // returns the word to be displayed
        // ???? h( ???? );    // tell it how many times to display the word (1)
        a();
        System.out.print(b());
        c(true);
        d();
        e(9);
        f("Frog");
        System.out.print(g());
        h(1);

        // ???? i( ???? );    // pass it any integer and it will display a word beginning with I
        // ???? j( ???? );    // returns a different word depending on what you pass it (1-3)
        // ???? k( ???? );    // displays a word beginning with K
        // ???? l( ???? );    // displays a different word depending on the two booleans you pass it
        // ???? m( ???? );    // displays a different word depending on the two booleans you pass it
        // ???? n( ???? );    // displays the word you pass it beginning with "N"
        // ???? o( ???? );    // displays a word beginning with O and returns a useless value
        // ???? p( ???? );    // returns the word to be displayed
        // ???? q( ???? );    // displays the word
        System.out.println();
        i(Integer.MAX_VALUE);
        j(1);
        j(2);
        j(3);
        k();
        l(true, true);
        l(true, false);
        m(true, false);
        m(false, false);
        n("Noodles");
        o();
        System.out.print(p());
        q();

        // ???? r( ???? );    // returns a different word depending on if you pass it 'true' or 'false'
        // ???? s( ???? );    // pass it the number of letters to display (6)
        // ???? t( ???? );    // displays the word you pass it beginning with "T"
        // ???? u( ???? );    // returns the word to be displayed
        // ???? v( ???? );    // tell it how many times to display the word (1)
        // ???? w( ???? );    // pass it any integer and it will display a word beginning with W
        // ???? x( ???? );    // returns a different word depending on what you pass it (1-2)
        // ???? y( ???? );    // displays a word beginning with Y
        // ???? z( ???? );    // returns a different word depending on which two boolean values you pass it
        System.out.println();
        r(true);
        r(false);
        s(6);
        t("Tough");
        System.out.print(u());
        v(1);
        w(0xff000000);
        x(1);
        x(2);
        y();
        z(true, true);
        z(false, false);
        System.out.println();
    }

    /**************************************
     * Don't change anything below here!! *
     *************************************/
     
     // More code down here...

Output

[99] Fill-In Functions

GOAL: Fix the code so it compiles and works.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Fill-In Functions
/// File Name: FillInMethods.java
/// Date Finished: Feb 26, 2016

// Fill-In Methods - Fix the broken methods and method calls.

public class FillInMethods
{

    public static void main( String[] args )
    {
        // Fill in the method calls where appropriate.

        System.out.println("Watch as we demonstrate methods.");

        System.out.println();
        System.out.println("I'm going to get a random character from A-Z");
        char c;
        c = randChar();
        System.out.println("The character is: " + c );

        System.out.println();
        System.out.println("Now let's count from -10 to 10");
        int begin, end;
        begin = -10;
        end = 10;
        counter(begin, end);
        System.out.println("How was that?");

        System.out.println();
        System.out.println("Now we take the absolute value of a number.");
        int x, y;
        x = -10;
        y = abso(x);
        System.out.println("|" + x + "| = " + y );

        System.out.println();
        System.out.println("That's all.  This program has been brought to you by:");
        credits();
    }

	public static void credits()
	// No parameters.
	{
		// displays some boilerplate text saying who wrote this program, etc.

		System.out.println();
		System.out.println("programmed by Graham Mitchell");
		System.out.println("modified by Cornelius Eanes");
		System.out.print("This code is distributed under the terms of the standard ");
		System.out.println("BSD license.  Do with it as you wish.");

	}

	public static char randChar()
	// No parameters.
	{
		// chooses a random character in the range "A" to "Z"

		int numval;
		char charval;

		// pick a random number from 0 to 25
		numval = (int)(Math.random()*26);
		// now add that offset to the value of the letter 'A'
		charval = (char) ('A' + numval);

		return charval;
	}

	public static int counter(int start, int stop)
	// Parameters are:
	//     int start;
	//     int stop;
	{
		// counts from start to stop by ones
		int ctr;

		ctr = start;
		while ( ctr <= stop )
		{
			System.out.print(ctr + " ");
			ctr = ctr+1;
		}

		return ctr;
	}

	public static int abso(int value)
	// Parameters are:
	//     int value;
	{
		// finds the absolute value of the parameter
		int absval;

		if ( value < 0 )
			absval = -value;
		else
			absval = value;

		return absval;
	}

}

Output

[100] More Fill-In Methods

GOAL: Fix some more code so it compiles and runs.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: More Fill-In Methods
/// File Name: MoreFillInMethods.java
/// Date Finished: Feb 26, 2016

// More Fill-In Methods - Fix the broken methods and method calls (again).

import java.util.Scanner;

public class MoreFillInMethods
{
    public static void main( String[] args )
    {
        // Fill in the method calls where indicated.
        System.out.println("Here we go.");

        System.out.println();
        System.out.println("Some random numbers, if you please: ");
        int lo, hi, val1, val2;
        lo = 1;
        hi = 10;
        val1 = superRand(lo, hi);
        System.out.println("First: " + val1 );
        val2 = superRand(hi, lo);  // this time, put hi first
        System.out.println("Second: " + val2 );
        if ( val1 == val2 )
            System.out.println("Hey!  They were the same!");
        else
            System.out.println("They were not the same.");

        System.out.println();
        System.out.print("More counting, but this time not by ones: ");
        // count from 2 to 8 by 2s
        stepCount(2, 8, 2);
        // count from 10 to 2 by 2s
        stepCount(10, 2, 2);

        System.out.println();
        System.out.println("Let's figure a project grade.");
        int a = 4, b = 3, c = 4, d = 5, e = 2, f = 1, result;
        result = projectGrade(a, b, c, d, e, f);
        System.out.println("434521 -> " + result );

        System.out.println();
        System.out.print("Finally, some easy ones.");

        String namae;
        namae = getName();
        System.out.println("Hi, " + namae );

        System.out.println();
        System.out.println("Do you feel lucky, punk?");
        crash();
        System.out.println();
    }



	public static int superRand(int a, int b)
	// Parameters are:
	//	int a;
	//	int b;
	{
		// picks a number between a and b, no matter which is larger
		int c;

		if ( a < b )	// b is larger
			c = a + (int)(Math.random()*(b-a+1));
		else if ( a > b )	// a is larger
			c = b + (int)(Math.random()*(a-b+1));
		else
			c = a;	// or c = b; doesn't matter since they're equal

		return c;
	}

 	public static int stepCount(int first, int last, int step)
	// Parameters are:
	//     int first;
	//     int last;
	//     int step;
	{
		// counts from 'first' to 'last' by 'step'
		// handles forward and backward
		int x;

		if ( first < last )
		{
			x = first;
			while ( x <= last )
			{
				System.out.print(x + " ");
				x = x + step;
			}
		}
		else
		{
			x = first;
			while ( x >= last )
			{
				System.out.print(x + " ");
				x = x - step;
			}
		}

		return x;
	}

	public static int projectGrade(int p1, int p2, int p3, int p4, int p5, int p6)
	// Parameters are:
	//     int p1, p2, p3, p4, p5, p6;
	{
		// given six integers representing scores out of five in each of the
		// six categories for the first six weeks project: tells you your
		// overall project grade out of 100

		int overall_grade;

		overall_grade = p1 * 6;    // six points per point for the first category
		overall_grade = overall_grade + ( p2 * 6 );   // six points per point
		overall_grade = overall_grade + ( p3 * 4 );   // four points per point
		overall_grade = overall_grade + ( p4 * 2 );   // two points per point
		overall_grade = overall_grade + ( p5 * 1 );   // one point per point
		overall_grade = overall_grade + ( p6 * 1 );   // one point per point

		return overall_grade;
	}

	public static String getName()
	// No parameters.
	{
		// finds out the user's name
		Scanner keyboard = new Scanner(System.in);

		String name;
		System.out.print("Please enter your name: ");
		name = keyboard.next();

		return name;
	}

	public static String crash()
	// No parameters.
	{
		// displays "you win" or "you lose".  You lose 90% of the time.
		String magic_word;

		if ( (int)(Math.random()*10) == 0 )
		{
			// What do you know?  We won!
			magic_word = "win";
		}
		else
		{
			// No big suprise; we lost.
			magic_word = "lose";
		}

		System.out.print("you " + magic_word);

		return magic_word;
	}

}

Output

[101] Keychains for Sale

GOAL: Write a skeleton of a keychain-buying program.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Keychains for Sale
/// File Name: KeychainsForSale.java
/// Date Finished: Feb 26, 2016

import java.util.Scanner;

public class KeychainsForSale {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        boolean run = true;

        System.out.println("Ye Olde Keychains Shoppe");
        System.out.println();

        while (run) {
            System.out.println("1. Add keychains to order");
            System.out.println("2. Remove keychains from order");
            System.out.println("3. View current order");
            System.out.println("4. Checkout");
            System.out.println();
            System.out.print("> ");

            int choice = input.nextInt();
            if (choice == 1) {
                addKeychains();
            } else if (choice == 2) {
                removeKeychains();
            } else if (choice == 3) {
                viewOrder();
            } else if (choice == 4) {
                checkout();
                run = false;
            }
        }

    }

    public static void addKeychains() {
        System.out.println("// TODO: Add keychains");
    }

    public static void removeKeychains() {
        System.out.println("// TODO: Remove keychains");
    }

    public static void viewOrder() {
        System.out.println("// TODO: View order");
    }

    public static void checkout() {
        System.out.println("// TODO: Checkout order");
    }

}

Output

[102] Keychains for Sale, for real this time

GOAL: Complete the keychain assignment.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Keychains for Sale, For Real This Time
/// File Name: KeychainsForSale2.java
/// Date Finished: Feb 26, 2016

import java.util.Scanner;

public class KeychainsForSale2 {

    private static Scanner input;
    private static int keychainCount;
    private static double cost;

    public static void main(String[] args) {

        input = new Scanner(System.in);
        keychainCount = 0;
        cost = 10.00;
        boolean run = true;

        System.out.println("Ye Olde Keychains Shoppe");
        System.out.println();

        while (run) {
            System.out.println("1. Add keychains to order");
            System.out.println("2. Remove keychains from order");
            System.out.println("3. View current order");
            System.out.println("4. Checkout");
            System.out.println();
            System.out.print("> ");

            int choice = input.nextInt();
            System.out.println();
            if (choice == 1) {
                addKeychains();
            } else if (choice == 2) {
                removeKeychains();
            } else if (choice == 3) {
                viewOrder();
            } else if (choice == 4) {
                checkout();
                run = false;
            }
            System.out.println();
        }

    }

    public static void addKeychains() {
        System.out.println("You have " + keychainCount + " keychain(s).");
        System.out.println("How many keychains do you want to add?");
        System.out.print("> ");
        int amount = input.nextInt();
        keychainCount += amount;
        System.out.println("You now have " + keychainCount + " keychain(s).");
    }

    public static void removeKeychains() {
        System.out.println("You have " + keychainCount + " keychain(s).");
        System.out.println("How many keychains do you want to remove?");
        System.out.print("> ");
        int amount = input.nextInt();
        keychainCount -= amount;
        System.out.println("You now have " + keychainCount + " keychain(s).");
    }

    public static void viewOrder() {
        System.out.println("Number of keychains: " + keychainCount);
        System.out.println("Cost per keychain: $" + zeroPad(cost));
        System.out.println("Total: $" + zeroPad(calculateTotal()));
    }

    public static void checkout() {
        System.out.print("Please enter your name: ");
        String name = input.next();
        viewOrder();
        System.out.println("Thanks for your order, " + name + "!");
    }

    public static String zeroPad(double d) {
        return String.format("%01.02f", d);
    }

    public static double calculateTotal() {
        return cost * keychainCount;
    }

}

Output

[103] Keychains for Sale, real ultimate power

GOAL: Modify the completed keychain assignment to include sales tax and shipping fees and error checking.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Keychains for Sale: Ultimate Power
/// File Name: KeychainsForSale3.java
/// Date Finished: Feb 29, 2016

import java.util.Scanner;

public class KeychainsForSale3 {

    private static Scanner input;
    private static int keychainCount;
    private static double cost, tax, shippingInitial, shippingMarginal;

    public static void main(String[] args) {

        input = new Scanner(System.in);
        keychainCount = 0;
        cost = 10.00;
        tax = 0.0825;
        shippingInitial = 5.00;
        shippingMarginal = 1.00;
        boolean run = true;

        System.out.println("Ye Olde Keychains Shoppe");
        System.out.println();

        while (run) {
            System.out.println("1. Add keychains to order");
            System.out.println("2. Remove keychains from order");
            System.out.println("3. View current order");
            System.out.println("4. Checkout");
            System.out.println();
            System.out.print("> ");

            int choice = input.nextInt();
            System.out.println();
            if (choice == 1) {
                addKeychains();
            } else if (choice == 2) {
                removeKeychains();
            } else if (choice == 3) {
                viewOrder();
            } else if (choice == 4) {
                checkout();
                run = false;
            }
            System.out.println();
        }

    }

    public static void addKeychains() {
        boolean next = false;
        int amount = -1;
        while (!next) {
            System.out.println("You have " + keychainCount + " keychain(s).");
            System.out.println("How many keychains do you want to add?");
            System.out.print("> ");
            amount = input.nextInt();
            if (!checkKeychains(amount)) {
                System.out.println("ERROR: Cannot have a negative keychain count.\n");
            } else {
                next = true;
            }
        }
        keychainCount += amount;
        System.out.println("You now have " + keychainCount + " keychain(s).");
    }

    public static void removeKeychains() {
        boolean next = false;
        int amount = -1;
        while (!next) {
            System.out.println("You have " + keychainCount + " keychain(s).");
            System.out.println("How many keychains do you want to remove?");
            System.out.print("> ");
            amount = input.nextInt();
            if (!checkKeychains(-amount)) {
                System.out.println("ERROR: Cannot have a negative keychain count.\n");
            } else {
                next = true;
            }
        }
        keychainCount -= amount;
        System.out.println("You now have " + keychainCount + " keychain(s).");
    }

    public static void viewOrder() {
        System.out.println("Number of keychains: " + keychainCount);
        System.out.println("Cost per keychain: $" + zeroPad(cost));
        System.out.println("Shipping charges ($" + zeroPad(shippingInitial) + " + $" + zeroPad(shippingMarginal) +
                " for each keychain): $" + zeroPad(calculateShippingCharges()));
        System.out.println("Subtotal: $" + zeroPad(calculateSubtotal()));
        System.out.println("Tax Charge (" + String.format("%01.02f", tax * 100) + "%): $" + zeroPad(calculateTaxCharge()));
        System.out.println("Total: $" + zeroPad(calculateTotal()));
    }

    public static void checkout() {
        System.out.print("Please enter your name: ");
        String name = input.next();
        viewOrder();
        System.out.println("Thanks for your order, " + name + "!");
    }

    public static double calculateShippingCharges() {
        return shippingInitial + shippingMarginal * keychainCount;
    }

    public static double calculateSubtotal() {
        return keychainCount * cost + calculateShippingCharges();
    }

    public static double calculateTotal() {
        return calculateSubtotal() * (1.0 + tax);
    }

    public static double calculateTaxCharge() {
        return calculateSubtotal() * tax;
    }

    public static String zeroPad(double d) {
        return String.format("%01.02f", d);
    }

    public static boolean checkKeychains(int amount) {
        return keychainCount + amount >= 0;
    }

}

Output

[104] Calling Methods from Other Files

GOAL: Learn how to call methods from other files.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Calling Methods from Other Files
/// File Name: MethodsFromOtherFiles.java
/// Date Finished: Feb 29, 2016

// Don't worry about the package naming
import ifstatements._36.WeekdayName;
import methods._94.MonthName;
import methods._95.MonthOffset;
import methods._96.WeekdayCalculator;

import java.util.Scanner;

public class MethodsFromOtherFiles {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("What's your birthday? (MM DD YYYY)");
        System.out.print("> ");

        int month = input.nextInt();
        int day = input.nextInt();
        int year = input.nextInt();

        String result = getWeekdayString(day, month, year);

        System.out.println("You were born on " + result + "!");

    }

    public static String getWeekdayString(int day, int month, int year) {
        int yy = year - 1900;
        int total = yy / 4;
        total += yy;
        total += day;
        total += MonthOffset.monthOffset(month);
        if (WeekdayCalculator.isLeapYear(year) || month == 1 || month == 2) {
            total--;
        }
        int r = total % 7;
        String weekdayStr = WeekdayName.getWeekdayName(r);
        String monthStr = MonthName.monthName(month);
        return weekdayStr + ", " + monthStr + " " + day + ", " + year;
    }

}

Output

[105] Evenness Method

GOAL: Using methods, figure out what numbers are even or divisible by 3.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Evenness Method
/// File Name: Evenness.java
/// Date Finished: Feb 29, 2016

public class Evenness {

    public static boolean isEven(int i) {
        return i % 2 == 0;
    }

    public static boolean isDivisibleBy3(int i) {
        return i % 3 == 0;
    }

    public static void main(String[] args) {

        for (int i = 1; i <= 20; i++) {
            System.out.print(i + " ");
            if (isEven(i)) {
                System.out.print("<");
            }
            if (isDivisibleBy3(i)) {
                System.out.print("=");
            }
            System.out.println();
        }

    }

}

Output

[106] Finding Prime Numbers

GOAL: Using a method, find as many prime numbers as possible.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Finding Prime Numbers
/// File Name: PrimeNumbers.java
/// Date Finished: Feb 29, 2016

public class PrimeNumbers {

    public static boolean isPrime(int n) {
        for (int i = n - 1; i > 1; i--) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        for (int i = 2; i <= 100; i++) {
            if (isPrime(i)) {
                System.out.println(i);
            }
        }
    }

}

Output