View SourceHide Source/**
* This program is written to save the input parameter of
* a program for future use.
* It uses the library provided by http://www.braju.com/ for printf()
*/
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
/*
* hb16.zip from http://www.braju.com/
* contains set of classes to provide printf() function
*/
import com.braju.format.Format;
import com.braju.format.Parameters;
public class HelloWorld {
static String quote = "Hello World!";
static String code =
"import java.io.PrintStream;" +
"import java.io.FileOutputStream;" +
"import java.io.OutputStream;" +
"import com.braju.format.Format;" +
"import com.braju.format.Parameters;" +
"public class HelloWorld{" +
"static String quote=*%s*;" +
"static String code=*%s*;" +
"public static void main(String args[]) throws Exception {" +
"if(args.length==1)quote = args[0];System.out.println(quote);" +
"OutputStream os = new FileOutputStream(*HelloWorld.java*);" +
"PrintStream ps = new PrintStream(os);" +
"ps.println("+
"Format.sprintf(code.replace('%c', (char)34), "+
"new Parameters(quote).add(code).add((char)42)));" +
"ps.close();" +
"Runtime.getRuntime().exec(*javac -classpath hb16.zip HelloWorld.java*);" +
"}}";
public static void main(String args[]) throws Exception {
if (args.length == 1) quote = args[0];
System.out.println(quote);
OutputStream os = new FileOutputStream("HelloWorld.java");
PrintStream ps = new PrintStream(os);
ps.println(Format.sprintf(code.replace('*', (char) 34),
new Parameters(quote).add(code).add((char) 42)));
ps.close();
Runtime.getRuntime().exec("javac -classpath hb16.zip HelloWorld.java");
}
}
Compile: javac -classpath hb16.zip HelloWorld.java
Execute:
(For Linux) java -cp .:hb16.zip HelloWorld
(For Windows) java -cp .;hb16.zip HelloWorld
Sample:
$ java -cp .:hb16.zip HelloWorld
Hello World!
$ java -cp .:hb16.zip HelloWorld Parameter
Parameter
$ java -cp .:hb16.zip HelloWorld
Parameter
/* Parameter which was the previous input to the program has been saved. */
With a excellent introduction to python language I started working on python and today I thought I will dedicate the first python program of mine to
Sidharth Kuruvila LJUserInfoThough this program is not essentially any worth(my opinion), I am still happy because it is my first program :)
People really interested can go ahead to have look at the source and comment on it.
View Python sourceHide Python sourceI created a small program Evaluate.py:
def functionCreator(choice):
"""Create the function and return it depending on choice."""
if(choice==1):
print "Create function for evaluating x*y+z ..."
func = lambda x,y,z: x*y+z
elif(choice==2):
print "Create function for evaluating x*y-z ..."
func = lambda x,y,z: x*y-z
elif(choice==3):
print "Create function for evaluating x*y*z ..."
func = lambda x,y,z: x*y*z
elif(choice==4):
print "Create function for evaluating x*y**z ..."
func = lambda x,y,z: x*y**z
# return the generated function
return func
# A class which defines a default function definition to evaulate its variables
# It also allows user to change the function definition at runtime so the class
# behaviour to evaulate its variables changes.
class Evaluate(object):
""" Allow evaluation of 3 class variables with custom function at runtime."""
def __init__(self,x,y,z):
"""Initializer which takes 3 variables x,y,z"""
self.x = x #self.__dict__["x"] = x
self.y = y #self.__dict__["x"] = y
self.z = z #self.__dict__["x"] = z
def evaluate(self,x,y,z):
""" A default function definition to evaluate the variables.
This function definition is configurable at runtime."""
return x+y+z
def func(self):
""" Call this function to evaluate the variables of the class
This will delegate the work to evaluate function."""
print "%s: %f" % ("Result",self.evaluate(self.x,self.y,self.z))
def change(self):
""" This function will allow user to configure the function definition
of evaluate."""
print "Available function generation for evaluating expression..."
print "1. x*y+z"
print "2. x*y-z"
print "3. x*y*z"
print "4. x*y**z"
choice = int(raw_input("Please enter your choice(1,2,3,4): "))
# Change the evaluate function defintion
self.__dict__["evaluate"] = functionCreator(choice)
# Inform user that the function definition was changed and can view the change
print "Please try object.func() to see the change in evaluation."
# Testing for this module
if __name__ == "__main__":
print "Evaluate Module loaded..."
I executed the script using IDLE and here were some of the actions I proceeded with some actions and got the results:
View Python outputHide Python output
>>> exeval=Evaluate(10,20,30)
>>> exeval.func()
Result: 60.000000
>>> exeval.change()
Available function generation for evaluating expression...
1. x*y+z
2. x*y-z
3. x*y*z
4. x*y**z
Please enter your choice(1,2,3,4): 1
Create function for evaluating x*y+z ...
Please try object.func() to see the change in evaluation.
>>> exeval.func()
Result: 230.000000
>>> exeval.change()
Available function generation for evaluating expression...
1. x*y+z
2. x*y-z
3. x*y*z
4. x*y**z
Please enter your choice(1,2,3,4): 2
Create function for evaluating x*y-z ...
Please try object.func() to see the change in evaluation.
>>> exeval.func()
Result: 170.000000
>>> exeval.change()
Available function generation for evaluating expression...
1. x*y+z
2. x*y-z
3. x*y*z
4. x*y**z
Please enter your choice(1,2,3,4): 3
Create function for evaluating x*y*z ...
Please try object.func() to see the change in evaluation.
>>> exeval.func()
Result: 6000.000000
>>> exeval.change()
Available function generation for evaluating expression...
1. x*y+z
2. x*y-z
3. x*y*z
4. x*y**z
Please enter your choice(1,2,3,4): 4
Create function for evaluating x*y**z ...
Please try object.func() to see the change in evaluation.
>>> exeval.func()
Result: 10737418239999999609046623630827404132352.000000
>>>