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);
}
}

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


/*
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

/* >>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();
}
}