Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

How do I into java

Name: Anonymous 2011-06-03 19:02

Any Javafags here?

So I'm trying too write a program that will print the letters of a string backwards on each line, but I can only get it too print the last letter of the string infinitely. Can anyone show me how I would get the result I want?

This is what I wrote so far:

String word = "anything";
        int length = word.length();
        char letter = word.charAt(length-1);
        while(length > 0)
        System.out.println(letter);

I'm trying to teach my self java, so any help is much appreciated.

Name: Anonymous 2011-06-03 23:15

>>14
#!/usr/bin/php -q
<?php

/**
 * Set of classes for handling advanced operations on strings,
 * as per application guidelines.
 *
 * @package AdvancedStringHandling
 * @author Anonymous
 */


interface ASH_IStringContainer
{
    public function __construct($string);
    public function __toString();
}

interface ASH_IFactory
{   
    public static function create($type);
}

class ASH_StandardStringFunctionAsMethodCallException extends InvalidArgumentException
{
    const UNSPECIFIED = 0;
    const UNRECOGNIZED = 1;
    const UNSUPPORTED = 2;
   
    public function __construct($code = self::UNSPECIFIED, $message = '')
    {
        $this->code = (int)$code;
        $this->message = (string)$message;
    }
}   

class ASH_FactoryException extends Exception { }
class ASH_StringContainerFactoryException extends ASH_FactoryException { }



/**
 * Base inheritance class for all string containers. Not to be used as a standard string container.
 *
 * @abstract
 * @see ASH_StandardString
 */
abstract class ASH_StringContainer implements Countable, ArrayAccess, ASH_IStringContainer
{
    protected $str = array();
   
    public function __construct($string = '')
    {
        $this->_set($string);
    }
   
    public function __toString()
    {
        return implode($this->str);
    }
   
    /**
     * Magic method for allowing chainable standard string function calls on the string container's internal string.
     *
     * @final
     * @param mixed ... Use whichever variables are appropriate for the function call you want to make
     * @return $this if the result of the call was null, or if the call modified the internal string/array, otherwise returns the value of the call
     */
    final public function __call($name, $arguments)
    {
        switch ($name)
        {
            case 'echo':
            case 'print':
                return call_user_func_array(array(__CLASS__, '_echo'), $arguments);
            break;
           
            default:
                try
                {
                    $rf = new ReflectionFunction($name);
                    $offset = -1;
                   
                    // determine where our stored string is supposed to go in the argument list
                   
                    foreach ($rf->getParameters() as $paramNum => $param)
                    {
                        switch ($param->getName())
                        {
                            case 'glue':
                            case 'subject':
                            case 'str':
                            case 'str1':
                            case 'string':
                            case 'hebrew_text':
                                $offset = $paramNum;
                            break 2;
                        }
                    }
                   
                    if ($offset === -1)
                    {
                        throw new ASH_StandardStringFunctionAsMethodCallException(
                            ASH_StandardStringFunctionAsMethodCallException::UNSUPPORTED,
                            "Function {$name} not a supported standard string function to use as a magic method with StringContainer."
                        );
                    }
                    else
                    {
                        // insert our stored string in the appropriate space in the argument list before we pass it to the function
                        array_splice($arguments, $offset, 0, (string)$this);
                                               
                        $retval = $rf->invokeArgs($arguments);
                       
                        // should we return a (chainable) reference to self, or keep the value given to use by the call?
                        if (is_string($retval) || is_array($retval))
                        {
                            // this function intends to modify the stored value
                            $this->_set($retval);
                            $retval =& $this;
                        }
                        elseif(is_null($retval))
                        {
                            $retval =& $this;
                        }
                        return $retval;
                    }
                }
                catch (ReflectionException $e)
                {
                    throw new ASH_StandardStringFunctionAsMethodCallException(
                        ASH_StandardStringFunctionAsMethodCallException::UNRECOGNIZED,
                        "Function {$name} is not a known function."
                    );
                }
            break;
        }
       
    }
   
   
    /**
     * Manages the intricacies of our internal "string array"
     * @return $this
     */
    private function _set($string)
    {
        $this->str = str_split(is_array($string) ? implode(' ', $string) : (string)$string);
    }
   
    /**
     * Chainable command for outputting current state of the string.
     * @return $this
     */
    private function _echo($prepend = '', $append = '')
    {
        print($prepend . (string)$this . $append);
        return $this;
    }
   
    function prepend($string)
    {
        $this->_set($string . (string)$this);
        return $this;
    }
   
    function append($string)
    {
        $this->_set((string)$this . $string);
        return $this;
    }
   
    function set($string)
    {
        $this->_set($string);
    }
   
/// Countable implementation
 
    public function count()
    {
        return sizeof($this->str);
    }

/// ArrayAccess implementation
   
    public function offsetSet($offset, $value)
    {
        $this->str[$offset] = (string)$value;
    }
   
    public function offsetExists($offset)
    {
        return isset($this->str[$offset]);
    }
   
    public function offsetUnset($offset)
    {
        $this->str = array_splice($this->str, $offset, 1);
    }
   
    public function offsetGet($offset)
    {
        return $this->str[$offset];
    }   
}

/**
 * Standard string container, default created by factory
 */
class ASH_StringContainer_Standard extends ASH_StringContainer { }

/**
 * Standard string container, default created by factory
 */
class ASH_StringContainer_Reverse extends ASH_StringContainer
{
    public function __construct($string)
    {
        parent::__construct($string);
        $this->str = array_reverse($this->str);
    }
}


abstract class ASH_Factory implements ASH_IFactory
{
    /**
     * used to cache classname bases (expensive string operations)
     * @internal
     */
    private static $classnameBaseLookup = array();
   
    /**
     * Private constructor prevents inherited classes from being able to instantiate
     * @private
     */
    private function __construct()
    {
    }
   
    /**
     * Default implementation of factory creation.
     * If the class to be created does not follow naming conventions,
     * or requires arguments in the constructor, you must override $create.
     *
     * @return object classname decided by naming convention
     * @see getClassName
     * @see getClassNameBase
     */   
    public static function create($type)
    {
        $className = self::getClassName($type);
        if (class_exists($className))
        {
            return new $className();
        }
        else
        {
            throw new ASH_FactoryException("Unknown class type {$containerType} for factory " . get_called_class());
        }
    }
   
    /**
     * @return string Full class name being called by the Factory (by convention)
     */   
    protected static function getClassName($type)
    {
        $classSubname = preg_replace('/[\s]+/', ' ', ucwords(preg_replace('/[^a-zA-Z ]/', '', (string)$type)));
        return self::getClassNameBase() . "_{$classSubname}";
    }
   
    /**
     * @return string Base class name prefix for which the inherited factory is associated (by convention)
     */
    protected static function getClassNameBase()
    {
        $classname = get_called_class();
        if (!isset(self::$classnameBaseLookup[$classname]))
        {
            $parts = explode('_', $classname);
            if ('Factory' === end($parts))
            {
                array_pop($parts);
            }
            self::$classnameBaseLookup[$classname] = implode('_', $parts);
        }
        return self::$classnameBaseLookup[$classname];
    }
}

abstract class ASH_StringContainer_Factory extends ASH_Factory
{
    const DEFAULT_CONTAINER_TYPE = 'Standard';
   
    public static function create($type = self::DEFAULT_CONTAINER_TYPE)
    {
        return self::createString('', $type);
    }
   
    public static function createString($string, $type = self::DEFAULT_CONTAINER_TYPE)
    {
        $className = self::getClassName($type);
        if (class_exists($className) && is_subclass_of($className, self::getClassNameBase()))
        {
            return new $className($string);
        }
        else
        {
            throw new ASH_StringContainerFactoryException("Unknown StringContainer type {$type} (expected class {$className})");
        }
    }
}


/**
 * Inline Unit Test (IUT)
 * @internal
 */
if (isset($argc) && strstr($argv[0], basename(__FILE__)))
{
    // environment setup
    $argumentString = implode(' ', array_slice($argv, 1));
    $argumentString = $argumentString ? $argumentString : 'Default Unit Test String';
   
    // test
    ASH_StringContainer_Factory::createString($argumentString, 'Reverse')
        ->print()
        ->strrev()
        ->print("\n")
        ->strtoupper()
        ->append(' (I am typing in all caps to indicate that I am shouting)')
        ->print("\n");
}


$ php -f "AdvancedStringHandling_Main.php"
gnirtS tseT tinU tluafeD
Default Unit Test String
DEFAULT UNIT TEST STRING (I am typing in all caps to indicate that I am shouting)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List