Overview

The purpose of this assignment is to gain experience writing in Java.

Details

For this lab, you will be implementing a few short methods in CodingBat, then copying your code into a full Java program. Be sure to follow your instructor's instructions. Here is a brief introduction on how to use CodingBat: Video.

Negate

Given a number, return its negative.

How Many Pennies

Given a number representing dollars and cents, e.g. 2.18, return the number of pennies corresponding to that amount.

Last Half

Given a non-empty string with an even number of characters, return the last half. E.g., Given "this" return "is".

Make Initialization

Make a program that writes a line of Java code. Given the name of the variable and its value, return an initialization of an int with that name and value. E.g., given the name "x" and the value "5", return "int x = 5;"

Fraction

Given a fraction expressed as a numerator and denominator, return the fraction as a floating point number.

Java Program

Once you have successfully completed the CodingBat problems, open your WSL terminal and do the following:

/*
* TODO: Complete your prologue (file header) with your own information
*/
package TODO; // replace TODO after package with your MSOE username

/**
* A collection of CodingBat exercises for students to get practice with
*/
public class Lab2 {
    public static int NUM_PENNIES = 100;

    public static void main(String[] args) {
        int[] negateTests = {2, 0,-3, 7, -299182, 47, 9, 13, -13};
        double[] penniesTests = {2.18, 58.72, 0.772, 13.1, 0.0};
        String[] lastHalfTests = {"this", "answer", "apples", "sing", "question", "we", "food",
            "applesauce", "Virginia", "This is fun!", "foodmaster", "pillow"};
        String[] initTestString = {"x", "y", "z", "foo", "apple", "barley", "quiz", "a", "b", "c",
            "d", "e"};
        int[] initTestInts = {5, 10, 13, -1, 12, 100, 63, 14, 11, 12, 29, 17};
        int[] fractionTest = {3, 3, 1, 2, 2, 4, 1, 4, 6, 14};

        System.out.println("Testing code...\n");
        for(int i = 0 ; i < negateTests.length; ++i) {
            System.out.println(negateTests[i] + " negated is " +
            negate(negateTests[i]));
        }
        System.out.println();

        for(int i = 0; i < penniesTests.length; ++i) {
            System.out.println("$" + penniesTests[i] + " has " + howManyPennies(penniesTests[i])
                + " pennies.");
        }
        System.out.println();

        for(int i = 0; i < lastHalfTests.length; ++i) {
            System.out.println("""
              The last half of "%s" is "%s" """.formatted(lastHalfTests[i],
              lastHalf(lastHalfTests[i])));
        }
        System.out.println();

        for(int i = 0; i < initTestString.length; ++i) {
            System.out.println("Initialization " + (i + 1) + " is " +
            makeInitialization(initTestString[i], initTestInts[i]));
        }
        System.out.println();

        for(int i = 0; i < fractionTest.length; ++i) {
            int num = fractionTest[i];
            int den = fractionTest[++i];
            System.out.println(num + "/" + den + " is " + fraction(num, den));
        }
        System.out.println("Tests completed.");
    }

    private static int negate(int value) {
        // TODO: PASTE YOUR NEGATE CODE HERE
    }

    private static int howManyPennies(double dollars) {
        // TODO: PASTE YOUR HOWMANYPENNIES CODE HERE
    }

    private static String lastHalf(String str) {
        // TODO: PASTE YOUR LASTHALF CODE HERE
    }

    private static String makeInitialization(String name, int value) {
        // TODO: PASTE YOUR MAKEINITIALIZATION CODE HERE
    }

    private static double fraction(int numerator, int denominator) {
        // TODO: PASTE YOUR FRACTION CODE HERE
    }
}

Once you have pasted your code for the CodingBat questions into the program and completed all of the TODO comments, save the program and exit nano. Then change your directory so you are "up" one level, into the directory you started in by using the cd .. shortcut. Now we will compile and run the program:

The output should look like this

Testing code...

2 negated is -2
0 negated is 0
-3 negated is 3
7 negated is -7
-299182 negated is 299182
47 negated is -47
9 negated is -9
13 negated is -13
-13 negated is 13

$2.18 has 218 pennies.
$58.72 has 5872 pennies.
$0.772 has 77 pennies.
$13.1 has 1310 pennies.
$0.0 has 0 pennies.

The last half of "this" is "is"
The last half of "answer" is "wer"
The last half of "apples" is "les"
The last half of "sing" is "ng"
The last half of "question" is "tion"
The last half of "we" is "e"
The last half of "food" is "od"
The last half of "applesauce" is "sauce"
The last half of "Virginia" is "inia"
The last half of "This is fun!" is "s fun!"
The last half of "foodmaster" is "aster"
The last half of "pillow" is "low"

Initialization 1 is int x = 5;
Initialization 2 is int y = 10;
Initialization 3 is int z = 13;
Initialization 4 is int foo = -1;
Initialization 5 is int apple = 12;
Initialization 6 is int barley = 100;
Initialization 7 is int quiz = 63;
Initialization 8 is int a = 14;
Initialization 9 is int b = 11;
Initialization 10 is int c = 12;
Initialization 11 is int d = 29;
Initialization 12 is int e = 17;

3/3 is 1.0
1/2 is 0.5
2/4 is 0.5
1/4 is 0.25
6/14 is 0.42857142857142855
Tests completed.

Acknowledgement

This laboratory assignment was developed by Dr. Chris Taylor and Dr. Josiah Yoder.

See your professor's instructions for details on submission guidelines and due dates.