2006/01/04

Lab 1-02-2006 Recursion

package work;
import java.io.*;
public class recursive {
public static double recursive(double n) {
if(n==0)return 1;
else if(n==1 )
return 1;
else
return (n*recursive(n-1));
}

public static void main(String[] args) throws IOException{
double num;
BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a positive number.");
String nextString = keyboard.readLine();
double next = Double.parseDouble(nextString.trim());
num=recursive(next);
System.out.println(next+"!="+num);
}
}

2006/01/03

Lab 1-02-2006 modular sorting

package work;
public class modularsorting
{
public static double[] sortt(double[] tmp)
{
int i,j;
double temp;
for (j = 0; j <> tmp[i - 1])
{
temp = tmp[i - 1];
tmp[i - 1] = tmp[i];
tmp[i] = temp;
}
}
}
return tmp;
}
public static void out(double[] temp2)
{
for(int i=0 ; i <>
{
System.out.print(" " + temp2[i]);
}
}
public static void main(String[] args)
{
double[] num={45,558,78,6,8,0,35};
double[] num2;
num2=sortt(num);
out(num2);
}
}

2006/01/02

Lab 12-26-2005 (2)

package welcome;

public class someProgram
{
public static void main(String[] arg)
{
int i;
if( arg.length == 0 )
{
System.out.println("ERROR");
System.exit(0);
}

for( i=arg.length;i>0;i--)
{
System.out.println( arg[i] );}

System.out.println("The length of parameter is " + arg.length);
}
}

2005/12/27

Lab 12-26-2005 (1)

package s9226262;

public class someProgram
{
public static void main(String[] arg)
{
System.out.println( arg[0] + "" + arg[2] + "" + arg[1] );
System.out.println("The length of parameter is " + arg.length);
}
}

2005/12/26

Homework 12-19-2005 Lab Equal Arrays

package work;

public class equaryArray {

public static boolean equals(int a[],int b[])

{

if(a.length ! = b.length)

return false;

else

{

int i=0;

for(i=0; i <>
{

if(a[i] ! = b[i])

{

System.out.println("Not equal");

return false;

}

}

}

System.out.println("Equal");

return true;

}


public static void main(String[] args) {


int[] a={1,9,6,4,0,2,1,2};

int[] b={1,9,6,4,0,2,1,2};

equals(a,b);

}

}

Lab 12-19-2005 Sorting

package work;
public class sort {
public static void main(String[] args) {
int num[] = {4, 3, 6, 7, 5};
int i,j, temp;
for (j = 0; j < i =" 1;"> num[i - 1]) {
temp = num[i - 1];
num[i - 1] = num[i];
num[i] = temp;
}
}
}
for(i=0;i<5;i++)
System.out.println(""+num[i]);
}
}

Lab 12-19-2005 Class Parameter

package welcome;

public class Complex
{
double real=0,image=0;
public Complex(double real,double image)
{ setComplex(real,image); }
public void setComplex(double real,double image)
{
this.real = real;
this.image = image;
}

public void add(Complex R)
{
this.real = this.real + R.real;
this.image = this.image + R.image;
}
}


package welcome;
public class Complex_Demo
{
public static void main(String args[])
{
Complex R1 = new Complex(2,3);
Complex R2 = new Complex(4,5);
System.out.println("R1: " + R1.real + " + " + R1.image + "*i");
System.out.println("R2: " + R2.real + " + " + R2.image + "*i");
R1.add(R2);System.out.println("Therefore, R1+R2 = " + R1.real + " + " + R1.image + "*i");
}
}

2005/12/19

Homework 12-12-2005 Counter II

package s9226262;

import javax.swing.JOptionPane;
public class Counter
{
int count;
public void setToZero()
{ count=0; }

public void increase()
{ count+=1; }

public void decrease()
{
count-=1;
if ( count<0>
{ System.out.println("Error"); }
}

public int accessor()
{ return count; }

public void screen()
{ System.out.println("The Count2 = "+count); }

public boolean equals(Counter Count)
{ return this.count == Count.count; }

public String toString()
{ return Integer.toString(count); }
}




package
s9226262;

public class CounterDemo
{
public static void main(String[] args)
{
Counter count1 = new Counter();
Counter count2 = new Counter();
count1.setToZero();
count2.setToZero();
count1.increase();
count2.increase();
int Count1 = count1.accessor();
System.out.println("Return Count1 =" + Count1);
count2.decrease();
count2.screen();
System.out.println("Is Count1 = Count2 ?? is "+ count1.equals(count2));
System.out.println("");
System.out.println("ToString Coun1 & Count2:");
System.out.println("Count1: "+ count1.toString());
System.out.println("Count2: "+ count2.toString());
}
}

Lab 12-12-2005 (2) Static Class

package welcome;

import javax.swing.JOptionPane;
public class Lab_Fibonacci
{
public static double f1=1,f2=1,f3=0;
public static void main(String[] args)
{
String num_str = JOptionPane.showInputDialog("Enter the items number n :");
System.out.println( Lab_Fibonacci.next(num_str) );
System.exit(0);
}

public static double next(String num_str)
{
int n = Integer.parseInt(num_str);
for ( double i=1;i<=n;i++ )
{
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
return f3;
}

}

Lab 12-12-2005 (1) Constructor

package welcome;

import java.io.*;
public class Date
{
private String month;
private int day;
private int year;

public Date( )
{
month = "January";
day = 1;
year = 1000;
}

public Date(int monthInt, int day, int year)
{ setDate(monthInt, day, year); }

public Date(String monthString, int day, int year)
{ setDate(monthString, day, year); }

public Date(int year)
{ setDate(1, 1, year); }

public Date(Date aDate)
{
if (aDate == null)
{
System.out.println("Fatal Error.");
System.exit(0);
}
month = aDate.month;
day = aDate.day;
year = aDate.year;
}

public void setDate(int monthInt, int day, int year)
{
if (dateOK(monthInt, day, year))
{
this.month = monthString(monthInt);
this.day = day;
this.year = year;
}
else
{
System.out.println("Fatal Error");
System.exit(0);
}
}

public void setDate(String monthString, int day, int year)
{
if (dateOK(monthString, day, year))
{
this.month = monthString;
this.day = day;
this.year = year;
}
else
{
System.out.println("Fatal Error");
System.exit(0);
}
}

public void setDate(int year)
{ setDate(1, 1, year); }

public void setYear(int year)
{
if ( (year <> 9999) )
{
System.out.println("Fatal Error");
System.exit(0);
}
else
this.year = year;
}

public void setMonth(int monthNumber)
{
if ((monthNumber <= 0) || (monthNumber > 12))
{
System.out.println("Fatal Error");
System.exit(0);
}
else
month = monthString(monthNumber);
}

public void setDay(int day)
{
if ((day <= 0) || (day > 31))
{
System.out.println("Fatal Error");
System.exit(0);
}
else
this.day = day;
}

public int getMonth( )
{
if (month.equals("January"))
return 1;
else if (month.equals("February"))
return 2;
else if (month.equalsIgnoreCase("March"))
return 3;
else if (month.equalsIgnoreCase("April"))
return 4;
else if (month.equalsIgnoreCase("May"))
return 5;
else if (month.equals("June"))
return 6;
else if (month.equalsIgnoreCase("July"))
return 7;
else if (month.equalsIgnoreCase("August"))
return 8;
else if (month.equalsIgnoreCase("September"))
return 9;
else if (month.equalsIgnoreCase("October"))
return 10;
else if (month.equals("November"))
return 11;
else if (month.equals("December"))
return 12;
else
{
System.out.println("Fatal Error");
System.exit(0);
return 0;
}
}

public int getDay( )
{ return day; }

public int getYear( )
{ return year; }

public String toString( )
{ return (month + " " + day + ", " + year); }

public boolean equals(Date otherDate)
{
return ( (month.equals(otherDate.month)) &&amp;amp; (day == otherDate.day) && (year == otherDate.year) );
}

public boolean precedes(Date otherDate)
{
return ( (year < year ="=">
|| (year == otherDate.year && month.equals(otherDate.month)&& day <>
}

public void readInput( ) throws IOException
{
boolean tryAgain = true;
BufferedReader keyboard= new BufferedReader(new
InputStreamReader(System.in));

while (tryAgain)
{
System.out.println("Enter month, day, and year.");
System.out.println("Do not use a comma.");
String monthInput = keyboard.readLine( );
int dayInput = keyboard.read( );
int yearInput = keyboard.read( );
if (dateOK(monthInput, dayInput, yearInput) )
{
setDate(monthInput, dayInput, yearInput);
tryAgain = false;
}
else
System.out.println("Illegal date. Reenter input.");
}
}

private boolean dateOK(int monthInt, int dayInt, int yearInt)
{
return ( (monthInt >= 1) &&amp;amp; (monthInt <= 12) && (dayInt >= 1) && (dayInt <= 31) &&
(yearInt >= 1000) &&amp;amp; (yearInt <= 9999) );
}

private boolean dateOK(String monthString, int dayInt, int yearInt)
{
return ( monthOK(monthString) &&amp;amp; (dayInt >= 1) && (dayInt <= 31) && (yearInt >= 1000) &&amp; (yearInt <= 9999) );
}

private boolean monthOK(String month)
{
return (month.equals("January") || month.equals("February") ||
month.equals("March") || month.equals("April") ||
month.equals("May") || month.equals("June") ||
month.equals("July") || month.equals("August") ||
month.equals("September") || month.equals("October") ||
month.equals("November") || month.equals("December") );
}

private String monthString(int monthNumber)
{
switch (monthNumber)
{
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default:
System.out.println("Fatal Error");
System.exit(0);
return "Error";
}
}


}

package welcome;

public class ConstructorsDemo
{
public static void main(String[] args)
{
Date date1 = new Date("December", 16, 1770),
date2 = new Date(12, 27, 1756),
date3 = new Date(1882),
date4 = new Date();

System.out.println("Whose birthday is " + date1 + "?");
System.out.println("Whose birthday is " + date2 + "?");
System.out.println("Whose birthday is " + date3 + "?");
System.out.println("The default date is " + date4 + ".");
}
}

Homework 12-5-2005

package welcome;

public class Temp
{


private double value;
private char scale;

public void writeout()
{
System.out.println(value + " " + scale);
}


public Temp()
{
this.value = 0;
this.scale = 'C';
}

public Temp(double value)
{
this.value = value;
this.scale = 'C';
}

public Temp(char scale)
{
this.value = 0;
if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{
System.out.println("Error ");
this.scale = 'C';
}
}

public Temp(double value, char scale)
{
this.value = value;
if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{
System.out.println("Error ");
this.scale = 'C';
}
}



public double getC()
{
double Tc = 0;
if (scale == 'C')
{ Tc = value; }
else if (scale == 'F')
{ Tc = (value - 32) * 5 / 9; }
return Tc;
}

public double getF() {
double Tf = 0;
if (scale == 'F')
{ Tf = value; }
else if (scale == 'C')
{ Tf = (value * 9 / 5) + 32; }
return Tf;
}

public void setValue(double value)
{ this.value = value; }

public void setScale(char scale)
{

if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{ System.out.println("Error "); }

}

public void setTemp(double value, char scale)
{
if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{ System.out.println("Error "); }
this.value = value;
}

public boolean equals(Temp otherTemp)
{
if ( this.scale == 'C' && this.value == otherTemp.getC() )
{ return true; }
else if ( this.scale == 'F' && this.value == otherTemp.getF() )
{ return true; }
else
{ return false; }
}

public boolean greater(Temp otherTemp)
{
if ( this.scale == 'C' && this.value >= otherTemp.getC() )
{ return true; }
else if ( this.scale == 'F' && this.value >= otherTemp.getF() )
{ return true; }
else
{ return false; }
}

public boolean smaller(Temp otherTemp)
{
if ( this.scale == 'C' && this.value <= otherTemp.getC() )
{ return true; }
else if ( this.scale == 'F' && this.value <= otherTemp.getF() )
{ return true; }
else
{ return false; }
}


}







package welcome;


public class TempDisplay
{

public static void main(String[] args)
{

Temp Tc_mp = new Temp(0.0, 'C');
Temp Tf_mp = new Temp(32.0, 'F');

Temp Tc_bp = new Temp(100.0);
Tc_bp.setScale('C');

Temp Tf_bp = new Temp(212.0);
Tf_bp.setScale('F');

Temp Tc_cold = new Temp();
Tc_cold.setTemp( -40.0, 'C');

Temp Tf_cold = new Temp();
Tf_cold.setScale('F');
Tf_cold.setValue( -40.0);

System.out.print("The Tc of melt point = ");
Tc_mp.writeout();
System.out.print("The Tf of melt point = ");
Tf_mp.writeout();
System.out.print("The Tc of boil point = ");
Tc_bp.writeout();
System.out.print("The Tf of boil point = ");
Tf_bp.writeout();
System.out.print("The Tc of cold = ");
Tc_cold.writeout();
System.out.print("The Tf of cold = ");
Tc_cold.writeout();

System.out.print("\nTest equals.\n");
System.out.println("Tf_bp" + " = " + "Tc_bp" + " ? " + Tf_bp.equals(Tc_bp));
System.out.println("Tc_mp" + " = " + "Tf_mp" + " ? " + Tc_mp.equals(Tf_mp));
System.out.println("Tc_cold" + " = " + "Tc_cold" + " ? " + Tf_cold.equals(Tc_cold));
System.out.println("Tf_mp" + " = " + "Tc_mp" + " ? " + Tf_mp.equals(Tc_mp));

System.out.print("\nTest smaller.\n");
System.out.println("Tc_mp" + " <= " + "Tc_cold" + " ? " + Tc_mp.smaller(Tc_cold));
System.out.println("Tc_mp" + " <= " + "Tf_bp" + " ? " + Tc_mp.smaller(Tf_bp));
System.out.println("Tf_mp" + " <= " + "Tc_cold" + " ? " + Tf_mp.smaller(Tc_cold));
System.out.println("Tf_mp" + " <= " + "Tc_bp" + " ? " + Tf_mp.smaller(Tc_bp));

System.out.print("\nTest geater\n");
System.out.println("Tc_mp" + " >= " + "Tf_bp" + " ? " + Tc_mp.greater(Tc_bp));
System.out.println("Tc_mp" + " >= " + "Tf_cold" + " ? " + Tc_mp.greater(Tf_cold));
System.out.println("Tf_mp" + " >= " + "Tf_bp" + " ? " + Tf_mp.greater(Tf_bp));
System.out.println("Tf_mp" + " >= " + "Tf_cold" + " ? " + Tf_mp.greater(Tf_cold));
}

}


2005/12/12

Lab 12-5-2005 (2) Overloading

package welcome;

import javax.swing.*;

public class DateSixthTry
{
private String month;
private int day;
private int year;

public void setDate(int month, int day, int year)
{
if ( dateOK(month, day, year) )
{
this.month = monthString(month);
this.day = day;
this.year = year;
}
else
{
System.out.println("Fatal Error");
System.exit(0);
}
}

public void setDate(String month, int day, int year)
{
if ( dateOK(month, day, year) )
{
this.month = month;
this.day = day;
this.year = year;
}
else
{
System.out.println("Fatal Error");
System.exit(0);
}
}

public void setDate(int year)
{
setDate(1, 1, year);
}


private boolean dateOK(int month, int day, int year)
{
return ( (month >= 1) && (month <= 12) && (day >= 1) && (day <= 31) &&
(year >= 1000) && (year <= 9999) );
}

private boolean dateOK(String month, int day, int year)
{
return ( monthOK(month) && (day >= 1) && (day <= 31) && (year >= 1000) && (year <= 9999) );
}

private boolean monthOK(String month)
{
return ( month.equals("Jan") || month.equals("Feb") ||
month.equals("Mar") || month.equals("Apr") ||
month.equals("May") || month.equals("Jun") ||
month.equals("Jul") || month.equals("Aug") ||
month.equals("Sep") || month.equals("Oct") ||
month.equals("Nov") || month.equals("Dec") );
}


public void readInput( )
{
boolean tryAgain = true;
String keyboard = JOptionPane.showInputDialog("Enter keybord");

while ( tryAgain )
{
System.out.println("Enter month, day, and year.");
System.out.println("Do not use a comma.");
String month = keyboard;
int day = Integer.parseInt(keyboard);
int year = Integer.parseInt(keyboard);
if ( dateOK(month, day, year) )
{
setDate(month, day, year);
tryAgain = false;
}
else
System.out.println("Illegal date. Reenter input.");
}
}


public void writeOutput( )
{
System.out.println(month + " " + day + ", " + year);
}

public void setMonth(int month)
{
if ( (month <= 0) || (month > 12))
{
System.out.println("Fatal Error");
System.exit(0);
}
else
this.month = monthString(month);
}

public void setDay(int day)
{
if ( (day <= 0) || (day > 31) )
{
System.out.println("Fatal Error");
System.exit(0);
}
else
this.day = day;
}

public void setYear(int year)
{
if ( (year <> 9999) )
{
System.out.println("Fatal Error");
System.exit(0);
}
else
this.year = year;
}

public boolean equals(DateSixthTry otherDate)
{
return ( (month.equalsIgnoreCase(otherDate.month))
&& (day == otherDate.day) && (year == otherDate.year) );
}

public boolean precedes(DateSixthTry otherDate)
{
return ( (year <>
(year == otherDate.year && getMonth( ) <>
(year == otherDate.year && month.equals(otherDate.month) && day <>
}

public String toString( )
{
return (month + " " + day + ", " + year);
}

public int getDay( )
{
return day;
}

public int getYear( )
{
return year;
}

public int getMonth( )
{
if (month.equalsIgnoreCase("Jan"))
return 1;
else if (month.equalsIgnoreCase("Feb"))
return 2;
else if (month.equalsIgnoreCase("Mar"))
return 3;
else if (month.equalsIgnoreCase("Apr"))
return 4;
else if (month.equalsIgnoreCase("May"))
return 5;
else if (month.equals("Jun"))
return 6;
else if (month.equalsIgnoreCase("Jul"))
return 7;
else if (month.equalsIgnoreCase("Aug"))
return 8;
else if (month.equalsIgnoreCase("Sep"))
return 9;
else if (month.equalsIgnoreCase("Oct"))
return 10;
else if (month.equalsIgnoreCase("Nov"))
return 11;
else if (month.equalsIgnoreCase("Dec"))
return 12;
else
{
System.out.println("Fatal Error");
System.exit(0);
return 0;
}
}


private String monthString(int month)
{
switch (month)
{
case 1: return "Jan";
case 2: return "Feb";
case 3: return "Mar";
case 4: return "Apr";
case 5: return "May";
case 6: return "Jun";
case 7: return "Jul";
case 8: return "Aug";
case 9: return "Sep";
case 10: return "Oct";
case 11: return "Nov";
case 12: return "Dec";
default: System.out.println("Fatal Error");
System.exit(0);
return "Error";
}
}

public void setMonth(String month)
{ int monthInt;
if (month=="Jan")
{ monthInt = 1;}
else if (month=="Feb")
{ monthInt= 2;}
else if (month=="Mar")
{ monthInt= 3;}
else if (month=="Apr")
{ monthInt= 4;}
else if (month=="May")
{monthInt= 5;}
else if(month=="Jun")
{ monthInt= 6;}
else if (month=="Jul")
{ monthInt= 7;}
else if (month=="Aug")
{ monthInt= 8;}
else if (month=="Sep")
{ monthInt= 9;}
else if (month=="Oct")
{ monthInt= 10;}
else if (month=="Nov")
{ monthInt= 11;}
else if (month=="Dec")
{ monthInt= 12;}
else
{
System.out.println("Fatal Error");
System.exit(0);
{ monthInt= 0;}
}
this.setMonth(monthInt);

}



package welcome;

public class overloadingDisplay {
public static void main(String[] args)
{
DateSixthTry date1= new DateSixthTry();
DateSixthTry date2= new DateSixthTry();
DateSixthTry date3= new DateSixthTry();

date1.setDate(1,2,2007);
date1.setMonth(3);
date2.setDate("Feb",2,2007);
date2.setMonth("Apr");
date3.setDate(2007);

System.out.println(date1);
System.out.println(date2);
System.out.println(date3);
}
}
}