All pastes #1867139 Raw Edit

pitcher.java

public java v1 · immutable
#1867139 ·published 2010-04-19 02:15 UTC
rendered paste body
/* Pitcher.c * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */import java.io.*;  // I need all thisimport java.util.*; // Java sucksclass Pitcher {	public static void main(String[] useless) {		/* This useless main() function is a tribute to the suckiness of Java. */		Pitcher Pedro = new Pitcher();		if (Pedro.pitch() != 0)			System.out.println("Terminated without a solution.");		else			System.out.println("Program terminated.");		return;	}	/*********************************	***** Recursive Thingamabob *****/	kitchenCabinet recursionweirdness(int i, int goal, kitchenCabinet constant, kitchenCabinet variable) {		int tested = 0;		int j = 0;		while (j < constant.cabinetSize()) {				tested = tested + (constant.retrievePitcher(j) * variable.retrievePitcher(j));				j++;		}		if (tested == goal) { // If we got the goal!!			return variable;		} else { // If the goal was not reached.			if (i + 1 < constant.cabinetSize()) { // First it will try to go down a level.				variable.setANonPitcher(i, variable.retrievePitcher(i) + 1);				return recursionweirdness(i + 1, goal, constant, variable);			} else if (variable.retrievePitcher(i) < 7) { // If it can't do that, it increments by 1.				variable.setANonPitcher(i, variable.retrievePitcher(i) + 1);				return recursionweirdness(i, goal, constant, variable);			} else if (i > 0) {				variable.setANonPitcher(i, -7); // If it can't do that, it goes up a level.				return recursionweirdness(i - 1, goal, constant, variable);			} else {				return null;			}		}	}	/************** Print out answer ****************/	int start_the_parade(kitchenCabinet completed, kitchenCabinet constant) {		System.out.println("Here is the prize-winning solution:");		int j = 0;		while (j < completed.cabinetSize()) {				if (completed.retrievePitcher(j) < 0) {					System.out.println ("Fill pitcher " + constant.retrievePitcher(j) + ": " + (completed.retrievePitcher(j) * -1) + " times from the other pitchers.");				} else {					System.out.println ("Fill pitcher " + constant.retrievePitcher(j) + ": " + completed.retrievePitcher(j) + " times from the river.");				}				j++;		}		return 0;	} 	/**************** Main ***************/	int pitch() {	try {		BufferedReader input = null;		kitchenCabinet byARiver = new kitchenCabinet();		System.out.println("What are the sizes of your pitchers?");		if ( input == null )		{			InputStreamReader rd = new InputStreamReader(System.in);			input = new BufferedReader( rd );		}		String s = input.readLine(); // get input		StringTokenizer u = new StringTokenizer(s, ", ");		while (u.hasMoreTokens()) { /* Java can't detect how many numbers I put in		because it is dumb like dog turds. Let's use this stupid while loop instead		and then double-check it manually. */		/* WHERE IS SCANF()? Here is the same thing in C:		           while(ch != EOL) scanf("%d", byARiver[i++]);*/			byARiver.placeANewPitcher(Integer.parseInt (u.nextToken()));		}		System.out.println("What is the goal size?");		if ( input == null )				{					InputStreamReader rd = new InputStreamReader(System.in);					input = new BufferedReader( rd );				}		s = input.readLine(); // get input. woah, deja vu		u = new StringTokenizer(s, " ");		int goal = Integer.parseInt(u.nextToken());		double tested = 1;		int i = 0;		while (i < byARiver.cabinetSize()) {			if (goal <= byARiver.retrievePitcher(i)) {				tested = 0;			}			i++;		}		if (tested == 1) {			System.out.println("This problem is impossible. Next time, try a goal that isn't larger than the pitchers.");			return 1;		}		i = 0;		tested = 0;		kitchenCabinet settable = new kitchenCabinet();		while (i++ < byARiver.cabinetSize()) {			settable.placeANewPitcher(-8);		}		i = 0;		kitchenCabinet completed = recursionweirdness(0, goal, byARiver, settable);		if (completed == null) {			System.out.println("This is an impossible configuration of Pitcherspace.");			return 1;		} else {			start_the_parade(completed, byARiver);			return 0;		}	} catch ( NumberFormatException e) { System.out.println("Put in NUMBERS!"); return 5;	} catch ( IOException e ) { System.err.println( "Cryptic program error: " + e ); return 6; } }}/****** This is like an array. *******/class kitchenCabinet extends Vector {	Vector javasucks = new Vector();	int retrievePitcher (int position) {		try { Integer jd = (Integer) javasucks.get(position);		return jd.intValue();}	catch (ArrayIndexOutOfBoundsException e) { System.err.println( "Cryptic error retrieving: " + e );return -1; }	}	/*int retrieveTheSumOfAllPitchers() {		int i = -1;		int finalc = 0;		while (javasucks.get(++i)) {			finalc = finalc + javasucks.get(i);		}		return finalc;	}Disabled because Java sucks */	void placeANewPitcher(int canyoubelievejavahasaspecialclassForObjectifyingInts) {		Integer i = new Integer (canyoubelievejavahasaspecialclassForObjectifyingInts);		javasucks.add(i);		return;	}	int setANonPitcher(int index, int tobeset) {		try { Integer sunmicrosystemsshouldbemauledbyrabidwolves = new Integer(tobeset);		javasucks.set(index, sunmicrosystemsshouldbemauledbyrabidwolves);		return 0; }	     catch (ArrayIndexOutOfBoundsException e) { System.err.println( "Cryptic error setting: " + e );return -1; }	 }	int cabinetSize() {		return javasucks.size();	}}