Friday, December 9, 2011

Challenge 035’s Solution

To complete Challenge 35, the following code must be pasted into BlueJ™:

/**
 * This class creates and deletes “Task” and “TaskManager” objects
 *
 * @author Phoenix Jones
 * @version 1.0
 */
public class TaskRunner
{
    // creates three “Task” objects and one “TaskManager” object
    Task task1 = new Task("Task One", "Low");
    Task task2 = new Task("Task Two", "Medium");
    Task task3 = new Task("Task Three", "High");
    TaskManager taskManager1 = new TaskManager();
    public void main()
    {
        // adds all three tasks to the “TaskManager” object
        taskManager1.addTask(task1);
        taskManager1.addTask(task2);
        taskManager1.addTask(task3);
        // prints all three tasks to the console window
        taskManager1.showTask(0);
        System.out.println("\n");
        taskManager1.showTask(1);
        System.out.println("\n");
        taskManager1.showTask(2);
        // deletes one of the tasks from the “TaskManager” object
        taskManager1.removeTask(0);
    }
}
 
Pasting this code into BlueJ™ will produce the same scenario as in your original blog post.
 
−Phoenix

Thursday, December 8, 2011

Challenge 033’s Solution

To complete Challenge 34, the following code must be pasted into BlueJ™:

import java.util.Scanner;
/**
 * Challenge 033 (400 XPs) : Aurora Scores a New Transit System
 */
public class TicketMachine {
    // instance variables
    private int price;
    private int balance;
    private int total;
    /**
     * Constructor for objects of class “TicketMachine”
     */
    public TicketMachine(int price)
    {
        // initializes instance variables
        this.price = price;
        this.balance = balance;
        this.total = total;
    }
    /**
     * Displays the price of a ticket
     */
    public void getPrice()
    {
        System.out.println("Price: " + price + "¢");
    }
    /**
     * Displays one’s current balance
     */
    public void getBalance()
    {
        System.out.println("Current Balance: " + balance + "¢");
    }
    /**
     * Allows one to insert coins to add to one’s balance
     */
    public void insertMoney()
    {
        Scanner input = new Scanner(System.in);
        while(balance < price)
        {
            System.out.println("Please insert change: ");
            int i = input.nextInt();
            System.out.print("\n");
            if((i == 1)||(i == 5)||(i == 10)||(i == 25)||(i == 50)||(i == 100))
            {
                balance = balance + i;
                System.out.println("Your current balance is: " + balance + "¢.");
            }
            else
            {
                System.out.println("ERROR: Not a real payable amount!");
            }
        }
        System.out.println("You’ve matched the price of the ticket.");
    }
    /**
     * Prints one’s ticket if their balance is high enough
     */
    public int printTicket()
    {
        if(balance >= price)
        {
            System.out.println("  ****************************  ");
            System.out.println("**   Greenman Transit Lines   **");
            System.out.println("**         Price: " + price + "¢         **");
            System.out.println("  ****************************  ");
            total = price - balance;
        }
        else
            System.out.println("Please insert " + (price - balance) + "¢");
        return total;
    }
    /**
     * Refunds one’s change once they’ve printed their ticket
     */
    public void refundBalance()
    {
        if (total <= 0)
        {
            System.out.println("Thank you for using Greenman Transit Lines!\nYour change is " + total + "¢.");
        }
        else
            System.out.println("You have been fully refunded " + balance + "¢.");
    }
}
 
Pasting this code into BlueJ™ will produce the same scenario as in your original blog post.
 
−Phoenix
 
Note 1: I completed this challenge with the aid of Zach J, Itay P, and Brian W.
Note 2: Because of the use of int values for balance, one can insert all types of U.S. coins except for the half−cent.

Monday, December 5, 2011

Challenge 034’s Solution

To complete Challenge 34, the following code must be pasted into BlueJ™’s TaskManager class: 

    /**
     * Checks for invalid index values
     *
     * @param n  integer of index where task resides in ArrayList
     */
    public boolean checkValidIndex(int n)
    {
        if (n >= 0)
            return true;
        else
            return false;
    }
 
Pasting this code into BlueJ™ will produce the same scenario as in your original blog post.
 
−Phoenix