Hi Guys,
Please post your queries here. The "introduction" thread has grown too big so please avoid posting in there. Happy to help the beginners. Post your simple program requirements here. Expect queries/programs req. in c,cpp,c# also.
Monday, July 27, 2009
Monday, June 8, 2009
Sample computer merchandise program for blue
import java.util.*;
public class ComputerMerchandise {
public static void main(String[] args) {
java.util.Hashtable<Integer, Product> htProducts = new java.util.Hashtable<Integer, Product>();
for(Product product:Products.GetAllAvailableProduct())
{
htProducts.put(product.getProductId(),product);
System.out.println(product.toString());
}
Hashtable<Integer,Integer> orders = new Hashtable<Integer,Integer>();
java.util.Scanner scanner = new Scanner(System.in);
while(true)
{
System.out.print("To Order new item. Press Y to leave press any other key..");
String input = scanner.next();
if(!input.equalsIgnoreCase("Y")) break;
System.out.print("Enter the product id:");
int productId = scanner.nextInt();
Product selectedProduct = htProducts.get(new Integer(productId));
if(selectedProduct==null)
System.out.println("InValid Product.");
else
{
System.out.print("Enter nos:");
int quantity = scanner.nextInt();
Integer intOrdered = orders.get(new Integer(productId));
int alreadyOrdered = intOrdered==null?0:intOrdered.intValue();
if(alreadyOrdered<=0)
{
orders.put(new Integer(productId), new Integer(quantity));
}else
{
orders.put(new Integer(productId), new Integer(quantity+alreadyOrdered));
}
}
}
if(!orders.isEmpty())
{
double total = 0.0d;
Enumeration<Integer> enu = orders.keys();
while(enu.hasMoreElements())
{
Integer key = enu.nextElement();
int nos = orders.get(key).intValue();
Product soldProduct = htProducts.get(key);
double price = soldProduct.getCost()*nos;
total+=price;
String display = "Product Id:"+soldProduct.getProductId()+",Item:"+soldProduct.getName()+",Quantity:"+nos+",Cost="+price;
System.out.println(display);
}
if(total>0)
System.out.println("Total="+total);
}
}
}
class Products
{
/*
* Returns a collection of avilable products
* */
static Product[] GetAllAvailableProduct()
{
Product[] products = {
new Product("iBallMouse",600,1234),
new Product("Samsung CRT Monitor",5000,3457),
new Product("LG Keyboard",1500,200)
};
return products;
}
}
class Product
{
private double cost;
private String name;
private Integer productId;
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
Product(String name,double cost,int productId)
{
this.name = name;
this.cost = cost;
this.productId = new Integer(productId);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public String toString()
{
return String.format("ProductId:%d,ProductName:%s,Price:%s", this.productId,this.name,this.cost);
}
}
Friday, June 27, 2008
For Kajiri
/* Description: Program to get Withdrawl amount and then the denominations. please find the * the sepcifications and reques from kajiri at the introdution thread
*/
import java.io.*;
import java.util.*;
public class forKajiri{
double wamount = 0.0;
String[] alldenoms = new String[100];
int alldenomsindex=0;
forKajiri() { while (true) {
System.out.print("Enter the Amount to be withdrawn: ");
Scanner scan = new Scanner(System.in);
wamount=Double.parseDouble(scan.nextLine());
double tdamount = 0.0; alldenomsindex = 0;
while (wamount != tdamount) {
double wamount2=wamount;
System.out.println("Enter the denominations like : 100 X 2 , type Exit to leave ");
String temp = scan.nextLine();
if (temp.toLowerCase().indexOf("exit") > -1) break;
alldenoms[alldenomsindex] = temp;
double dtemp=CalculateAmount(temp.toLowerCase());
wamount2=wamount-tdamount;
tdamount += dtemp;
double dt=wamount2-dtemp;
alldenoms[alldenomsindex] += "=" + dtemp;
System.out.println("Balance WDRL will be: " + wamount2 + " - " + dtemp + " = " + dt);
alldenomsindex++;
}
System.out.println("--------------------");
System.out.println("WDRL Amount: " + wamount);
System.out.println("Denominations Total: " + tdamount);
for (int i = 0; i < alldenomsindex; i++)
System.out.println(alldenoms[i]);
System.out.println("--------------------");
System.out.println("To continue type Y or type anyother");
if (!scan.nextLine().toLowerCase().trim().equals("y")) break; }
}
public double CalculateAmount(String samount) {
if(samount.indexOf("*")>-1) samount=samount.replace('*','x');
return Double.parseDouble(samount.trim().split("x")[0])*Double.parseDouble(samount.trim().split("x")[1]);
}
public static void main(String args[]){ new forKajiri(); }
}
Thursday, November 1, 2007
Now in C# also...
Hi Friends,
Not only in Java if you need simple sample programs in C#..post here your requirements... we are ready to help you in C# also...
Not only in Java if you need simple sample programs in C#..post here your requirements... we are ready to help you in C# also...
Wednesday, October 24, 2007
FOr Fizkiz
There you go fizkiz. To find the average, smallest and largest and the total sum of the text file.
package cruft;
import java.io.*;
import java.util.*;
public class NumberSorter
{
public static void main(String[] args)
{
String fileName;
int count=1;
int sum=0;
int value;
int largest =0;
int smallest = 0;
double average =0;
System.out.println("I will sort through the numbers and give you the ");
System.out.println("smallest, largest, and the average of all the numbers.");
System.out.println("Enter the file name:");
Scanner s = new Scanner(System.in);
try
{
System.out.print("File Name: ");
fileName = s.next().trim();
Scanner scan = new Scanner(new File(fileName));
while(count<=50) {
value = scan.nextInt();
if(value > largest)
largest = value;
if(value < smallest)
smallest = value;
sum += value;
count++;
}
average = sum/50;
}
catch(FileNotFoundException e)
{
System.out.println("File not found.");
}
catch(IOException e)
{
System.out.println("Error reading from file");
}
System.out.println("Sum: " + sum);
System.out.println("Highest: " + largest);
System.out.println("Smallest: " + smallest);
System.out.println("Average: " + average);
}
}
package cruft;
import java.io.*;
import java.util.*;
public class NumberSorter
{
public static void main(String[] args)
{
String fileName;
int count=1;
int sum=0;
int value;
int largest =0;
int smallest = 0;
double average =0;
System.out.println("I will sort through the numbers and give you the ");
System.out.println("smallest, largest, and the average of all the numbers.");
System.out.println("Enter the file name:");
Scanner s = new Scanner(System.in);
try
{
System.out.print("File Name: ");
fileName = s.next().trim();
Scanner scan = new Scanner(new File(fileName));
while(count<=50) {
value = scan.nextInt();
if(value > largest)
largest = value;
if(value < smallest)
smallest = value;
sum += value;
count++;
}
average = sum/50;
}
catch(FileNotFoundException e)
{
System.out.println("File not found.");
}
catch(IOException e)
{
System.out.println("Error reading from file");
}
System.out.println("Sum: " + sum);
System.out.println("Highest: " + largest);
System.out.println("Smallest: " + smallest);
System.out.println("Average: " + average);
}
}
Saturday, October 13, 2007
Inheritance,Overloading,Overriding
This Arrow Plotting program is created for the request from fizkiz
Find fizkiz's request/specifications at:
https://www.blogger.com/comment.g?blogID=5133484865049964130&postID=7557509313208084077
Leave your comments..
FizKiz i dinot check all the test cases [don't have enough time.. busy have many other works to do..] so if you find any error..plz report...
Find fizkiz's request/specifications at:
https://www.blogger.com/comment.g?blogID=5133484865049964130&postID=7557509313208084077
/*
The size of the arrow is determined by two numbers,
one for the length of the "tail," which is 12 and for the width, which is 7.
The width of the base cannot be an even number, and your constructors
and mutator methods should check to make sure that it is alway odd.
Write a test program for each class that tests all the methods in the class.
You can assume that the base of the arrowhead is 3 or more.
Here is an example of the arrow to be drawn.
*
* *
* *
************ *
* *
* *
*/
/*Class Name 'Arrow' will fit better than this 'Figure' */
class Figure{
protected int ArrowLength=0,ArrowWidth=0;
public Figure(){
this.ArrowLength=12;
this.ArrowWidth=7;
}
public Figure(int ArrowLength,int ArrowWidth){
this.ArrowLength=ArrowLength;
this.ArrowWidth=checkArrowWidth(ArrowLength,ArrowWidth);
}
public void printArrowWidthConstraints(){
System.out.println("Arrow Width should not be an even value..\n It should be an Odd Value...\n Also Arrow Width should be lesser than Arrow Length..\n Minimum value of Arrow Width is 3..");
}
public int getNewArrowWidth(String s){
System.out.println(s);
try{
java.util.Scanner scan=new java.util.Scanner(System.in);
return Integer.parseInt(scan.nextLine());
}catch(Exception e){
System.out.println("InValid Input..Taking Default Value..7");
e.printStackTrace();
}
return 7;
}
public int checkArrowWidth(int ArrowLength,int ArrowWidth){
if(ArrowWidth<3){
printArrowWidthConstraints();
return checkArrowWidth(ArrowLength,getNewArrowWidth("Please Enter an a value Greater than 2...:"));
}
if(ArrowWidth%2==0){
printArrowWidthConstraints();
return checkArrowWidth(ArrowLength,getNewArrowWidth("Please Enter an Odd Value...:"));
}
if(ArrowWidth>=ArrowLength){
printArrowWidthConstraints();
return checkArrowWidth(ArrowLength,getNewArrowWidth("Please Enter a new valid Arrow Width...:"));
}
return ArrowWidth;
}
public void setValues(int ArrowLength,int ArrowWidth){
this.ArrowLength=ArrowLength;
this.ArrowWidth=checkArrowWidth(ArrowLength,ArrowWidth);
}
public int getArrowLength(){
return this.ArrowLength;
}
public int getArrowWidth(){
return this.ArrowWidth;
}
public void drawStars(int space,int noStars){
String s="";
if(space>0){
for(int i=0;i<space;i++)
s+=" ";
}
for(int i=0;i<noStars;i++)
s+="*";
System.out.println(s);
}
public void drawStars(int noStars){
String s="";
for(int i=0;i<noStars;i++)
s+="*";
System.out.println(s);
}
}
//-------------------End of Figure Class--------------------------
class RightArrow extends Figure{
RightArrow(){
super();
}
RightArrow(int ArrowLength,int ArrowWidth){
super(ArrowLength,ArrowWidth);
}
public void draw(int ArrowLength,int ArrowWidth){
setValues(ArrowLength,ArrowWidth);
draw();
}
public void draw(){
int middleLine=(getArrowWidth()/2)+1;
int space=getArrowLength()-(getArrowWidth()/2)-1,noStars=0;
for(int i=1;i<=getArrowWidth();i++){
if(i==middleLine)
drawStars(getArrowLength());
else{
if(i<middleLine)
drawStars(space,++noStars);
if(i>middleLine)
drawStars(space,noStars--);
}
}
}
}
//------------------End of RightArrow Class--------------------
class LeftArrow extends Figure{
LeftArrow(){
super();
}
LeftArrow(int ArrowLength,int ArrowWidth){
super(ArrowLength,ArrowWidth);
}
public void draw(int ArrowLength,int ArrowWidth){
setValues(ArrowLength,ArrowWidth);
draw();
}
public void draw(){
int middleLine=getArrowWidth()/2+1;
int space=getArrowWidth()/2+1,noStars=0;
for(int i=1;i<=getArrowWidth();i++){
if(i==middleLine)
drawStars(getArrowLength());
else{
if(i<middleLine)
drawStars(--space,++noStars);
if(i>middleLine)
drawStars(space++,noStars--);
}
}
}
}
//------------------End of LeftArrow Class--------------------
public class Demo{
public static void main(String args[]){
java.util.Scanner sc;
while(true){
RightArrow ra=null;
LeftArrow la=null;
System.out.println("Enter 1 to draw Right Arrow"); System.out.println("Enter 2 to draw Left Arrow"); System.out.println("Any other entry to Exit");
try{
sc=new java.util.Scanner(System.in);
int ArrowType=Integer.parseInt(sc.nextLine());
if(ArrowType<1 || ArrowType>2){
System.out.println("Terminating..");
System.exit(0);
}
System.out.println("Enter 3 to use Default Arrow Length and Default Arrow Width --default Arrow Length is 12, default Arrow Width is 7"); System.out.println("Enter 4 if you want to enter new Arrow Length and Arrow Width");
sc=new java.util.Scanner(System.in);
int paramType=Integer.parseInt(sc.nextLine());
if(paramType<3 || ArrowType>4){
System.out.println("InValid Input Terminating..");
System.exit(0);
}
if(paramType==3){
if(ArrowType==1){
ra=new RightArrow();
ra.draw();
}else if(ArrowType==2){
la=new LeftArrow();
la.draw();
}
}
if(paramType==4){
System.out.println("Enter Arrow Length..[should be greater than Arrow Width..]..:");
sc=new java.util.Scanner(System.in);
int ArrowLength=Integer.parseInt(sc.nextLine());
System.out.println("Enter Arrow Width...:");
sc=new java.util.Scanner(System.in);
int ArrowWidth=Integer.parseInt(sc.nextLine());
if(ArrowType==1){
ra=new RightArrow(ArrowLength,ArrowWidth);
ra.draw();
}else if(ArrowType==2){
la=new LeftArrow(ArrowLength,ArrowWidth);
la.draw();
}
}
System.out.println("Enter 5 if you want to Change Arrow Length and Arrow Width");
sc=new java.util.Scanner(System.in);
int changeType=Integer.parseInt(sc.nextLine());
if(changeType==5){
System.out.println("Enter new Arrow Length.:");
sc=new java.util.Scanner(System.in);
int ArrowLength=Integer.parseInt(sc.nextLine());
System.out.println("Enter new Arrow Width...:");
sc=new java.util.Scanner(System.in);
int ArrowWidth=Integer.parseInt(sc.nextLine());
if(ArrowType==1){
if(ra!=null)
ra.draw(ArrowLength,ArrowWidth);
}else if(ArrowType==2){
if(la!=null)
la.draw(ArrowLength,ArrowWidth);
}
}
}catch(Exception e){
System.out.println("InValid Entry Terminating..");
System.exit(0);
}
}
}
}
Leave your comments..
FizKiz i dinot check all the test cases [don't have enough time.. busy have many other works to do..] so if you find any error..plz report...
Monday, October 1, 2007
Temperature Conversion Program
This Temperature Conversion program is created for the request from fizkiz
Find fizkiz's request/specifications at:
https://www.blogger.com/comment.g?blogID=5133484865049964130&postID=7557509313208084077
Find fizkiz's request/specifications at:
https://www.blogger.com/comment.g?blogID=5133484865049964130&postID=7557509313208084077
/* >>and round to the nearest tenth of a degree
* ????what does it mean... give example...
*/
class Temperature{
float temperature_value;
boolean isFarenheit=false;
/*
From Fahrenheit to Celsius:--
subtract 32
Divide by 9
multiply by 5.
From Celsius to Fahrenheit:--
multiply by 9
Divide by 5
add 32.
*/
/*Default temperature_value is 0.0 and degree type is 'Celsius' */
Temperature(float temperature_reading,char scale){
temperature_value=temperature_reading;
if(scale=='c' || scale=='C')
isFarenheit=false;
else if(scale=='F' || scale=='f')
isFarenheit=true;
else{
System.out.println("Invalid Temperature scale type ..\n Exiting..");
System.exit(0);
}
}
Temperature(char scale){
temperature_value=0;
if(scale=='c' || scale=='C')
isFarenheit=false;
else if(scale=='F' || scale=='f')
isFarenheit=true;
else{
System.out.println("Invalid Temperature scale type ..\n Exiting..");
System.exit(0);
}
}
Temperature(float temperature_reading){
temperature_value=temperature_reading;
isFarenheit=false;
}
Temperature(){
temperature_value=0;
isFarenheit=false;
}
public float getTemperatureInCelsius(){
if(isFarenheit){
return (toCelsius(temperature_value));
}
return temperature_value;
}
public float getTemperatureInFarenheit(){
if(!isFarenheit){
return (toFarenheit(temperature_value));
}
return temperature_value;
}
public float toCelsius(float t_val){
return ((t_val-32)/9*5);
}
public float toFarenheit(float t_val){
return (t_val*9/5+32);
}
public boolean isEqual(Temperature tobject){
if(this.getTemperatureInCelsius()==tobject.getTemperatureInCelsius())
return true;
return false;
}
public boolean isGreater(Temperature tobject){
if(this.getTemperatureInCelsius() > tobject.getTemperatureInCelsius())
return true;
return false;
}
public boolean isLesser(Temperature tobject){
if(this.getTemperatureInCelsius() < tobject.getTemperatureInCelsius())
return true;
return false;
}
}
public class TemperatureTest{
java.util.Scanner sc;
int readTemperature(){
try{
sc=new java.util.Scanner(System.in);
System.out.println("Enter the temperature value:");
return Integer.parseInt(sc.nextLine());
}catch(Exception e){
e.printStackTrace();
System.out.println("Invalid input..");
terminate();
}
return 0;
}
char readScale(){
try{
sc=new java.util.Scanner(System.in);
System.out.println("Enter the Scale type, Enter C for celsius, F for Farenheit:");
return sc.nextLine().charAt(0);
}catch(Exception e){
e.printStackTrace();
System.out.println("Invalid input..");
terminate();;
}
return 'C';
}
Temperature createNewInstance(){
Temperature tempobject=new Temperature();
System.out.println("Enter 1 to use constructor with temperature and scale type");
System.out.println("Enter 2 to use constructor with temperature --default scale type is celsius");
System.out.println("Enter 3 to use constructor with scale type --default temperature is 0");
System.out.println("Enter 4 to use empty constructor --default temperature is 0 and default scale is celsius");
System.out.println("Any other entry to Exit");
try{
sc=new java.util.Scanner(System.in);
int option=Integer.parseInt(sc.nextLine());
switch (option){
case 1:
tempobject=new Temperature(readTemperature(),readScale());
break;
case 2:
tempobject=new Temperature(readTemperature());
break;
case 3:
tempobject=new Temperature(readScale());
break;
case 4:
tempobject=new Temperature();
break;
default:
terminate();
break;
}
}catch(Exception e){ terminate(); }
return tempobject;
}
TemperatureTest(){
Temperature t1,t2;
while(true){
try{
t1=createNewInstance();
System.out.println("Enter 6 to read temperature in Celsius");
System.out.println("Enter 7 to read temperature in Farenheit");
System.out.println("Enter 8 to create new Temperature object so you can compare" +
"\n their temperatures..");
System.out.println("Any other entry to Exit");
sc=new java.util.Scanner(System.in);
int option=Integer.parseInt(sc.nextLine());
switch (option){
case 6:
System.out.println(" Temperature in Celsius is:"+t1.getTemperatureInCelsius());
break;
case 7:
System.out.println(" Temperature in Farenheit is:"+t1.getTemperatureInFarenheit());
break;
case 8:
t2=createNewInstance();
compareTwoObjects(t1,t2);
break;
default:
terminate();
break;
}
System.out.println("-------------------------------------------------------");
}catch(Exception e){ terminate(); }
}
}
void compareTwoObjects(Temperature t1,Temperature t2){
//Equal comparision
if(t1.isEqual(t2))System.out.println("Both are Equal..");
//Lesser than comparision
if(t1.isLesser(t2))System.out.println("First Temperature is Lesser than second Temperature");
//Greater than comparision
if(t1.isGreater(t2))System.out.println("First Temperature is Greater than second Temperature");
}
void terminate(){
System.out.println("Terminating..");
System.exit(0);
}
public static void main(String args[]){
new TemperatureTest();
}
}
Subscribe to:
Posts (Atom)