Java作業一

星期二, 1月 03, 2006

Lab 1-02-2006 Recursion

package xxx;

import java.io.*;

public class Recursive
{
public static void main(String[] arg)
{
String MyString=JOptionPane.showInputDialog("幾階?");
int i=Integer.parseInt(MyString);
System.out.println(i+" 階 = "+Recursive.orders(i));
System.exit(0);
}
public static int orders(int i)
{
int n=1;
if (i==0)
{
return 1;
}
else if(i>0)
{
return(orders(i-1)*i);
}
else return 0;
}
}

Lab 1-02-2006 modular sorting

package xxx;

import java.io.*;

public class SortingDemo
{
public static void main(String args[])throws IOException
{
double[] number= new double[5];
int x,y;
double z;
for(x=0;x<5;x++)
{
BufferedReader keyin= new BufferedReader(new InputStreamReader(System.in));
number[x]=Integer.parseInt(keyin.readLine());
}
System.out.println("-------------------------------");
System.out.println("由大到小排列");
for(x=0;x<5;x++)
{
for(y=0;y<5;y++)
{
if (number[x]>number[y])
{
z=number[x];
number[x]=number[y];
number[y]=z;
}
}
}
for(x=0;x<5;x++)
{
System.out.print(""+number[x]+" ");
}
System.out.println("");
System.exit(0);
}
}
---------------------------------------------------------------------------------------------------

星期一, 1月 02, 2006

Lab 12-26-2005 (2)

public class myProgram
{
public static void main(String[] arg)
{
int i;
if(arg.length==0)
{
System.out.println("EORR");
System.exit(0);
}
for(i=arg.length-1;i>=0;i--)
{
System.out.println(arg[i]);
}
System.out.println("The length of parameter is "+arg.length);
}
}
---------------------------------------------------------------------------------------------------

星期一, 12月 26, 2005

"Lab 12-26-2005 (1)"

package bbb;

public class test
{
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);
}
}
------------------------------------------------------------------------------------------------

"Homework 12-19-2005 Lab Equal Arrays"

public class array
{
public static boolean equal(int a[],int b[])
{
if (a.length != b.length)
{
return false;
}
else
{
int i=0;
while (i < a.length)
{
if (a[i] != b[i])
{
return false;
}
i++;
}
}
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};
int k;
System.out.print("a={ ");
for(k=0; k
System.out.print(a[k]+" ");
System.out.println("}");
System.out.print("b={ ");
for(k=0; k
System.out.print(b[k]+" ");
System.out.println("}");
if(equal(a,b))
{
System.out.println("a and b are same arrarys.");
}
else
{
System.out.println("a and b are different arrarys.");
}
}
}

"Lab 12-19-2005 Sorting"

package Ch5;

import javax.swing.JOptionPane;

public class Sorting
{
public static void main(String args[])
{
int[] m= new int[5];
int i,j,h;
for(i=0;i<5;i++)
{
String PString=JOptionPane.showInputDialog("Enter the m["+i+"]"); m[i]=Integer.parseInt(PString);
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if (m[i]>m[j])
{
h=m[i];
m[i]=m[j];
m[j]=h;
}
}
}
for(i=0;i<5;i++)
{
System.out.println(m[i]);
}
System.exit(0);
}
}

"Lab 12-19-2005 Class Parameter"

package Ch5;

public class add
{
public static void main(String[] args)
{
complex a1=new complex(2,3);
complex a2=new complex(4,5);
a1.add(a2);
System.out.println(a1.a+"+"+a1.b+"i");
}
}
------------------------------------------------------------------------------------------------
package Ch5;

public class complex
{
int a=0,b=0;
public complex(int a,int b)
{
setComplex(a,b);
}
public void setComplex(int a,int b)
{
this.a=a; this.b=b;
}
public void add(complex Q1)
{
this.a=this.a+Q1.a;
this.b=this.b+Q1.b;
}
}

星期一, 12月 19, 2005

"Lab 12-12-2005 (2) Static Class"

package ch5;

public class F
{
public static double num0 = 1, num1 = 1, sum = 0;
public static double next()
{
sum = num0 + num1;
num0 = num1;
num1 = sum;
return sum;
}
public static void main(String[] args)
{
for (int i = 0; i < 100; i++)
{
F.next();
System.out.println("F" + (i + 2) + " = " + sum);
}
}
}

------------------------------------------------------------------------------------------------

星期五, 12月 16, 2005

"Homework 12-5-2005"

package Ch4;
public class Temperature
{
private double value;
private char scale;
public Temperature()
{
this.value = 0;
this.scale = 'C';
}
public Temperature(double value)
{
this.value = value;
this.scale = 'C';
}
public Temperature(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 Temperature(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 rtn = 0;
if (scale == 'C')
{
rtn = value;
}
else if (scale == 'F')
{
rtn = (value - 32) * 5 / 9;
}
return rtn;
}
public double getF()
{
double rtn = 0;
if (scale == 'F')
{
rtn = value;
}
else if (scale == 'C')
{
rtn = (value * 9 / 5) + 32;
}
return rtn;
}
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 setTemperature(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(Temperature 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(Temperature 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 Ch4;
class TemperatureDemo
{
public static void main(String[] args)
{
Temperature iceC = new Temperature(0.0, 'C');
Temperature iceF = new Temperature(32.0, 'F');
Temperature fireC = new Temperature(100.0);
fireC.setScale('C');
Temperature fireF = new Temperature(212.0);
fireF.setScale('F');
Temperature coldC = new Temperature();
coldC.setTemperature( -40.0, 'C');
Temperature coldF = new Temperature();
coldF.setScale('F');
coldF.setValue( -40.0);
System.out.println("iceC = " + iceC );
System.out.println("iceF = " + iceF );
System.out.println("fireC = " + fireC );
System.out.println("fireF = " + fireF );
System.out.println("coldC = " + coldC );
System.out.println("coldF = " + coldF );
System.out.print("\nTest equals.\n");
System.out.println(fireF + " = " + fireC + " ? " + fireF.equals(fireC));
System.out.println(iceC + " = " + iceF + " ? " + iceC.equals(iceF));
System.out.println(coldF+ " = " + coldC + " ? " + coldF.equals(coldC));
System.out.println(iceF + " = " + iceC + " ? " + iceF.equals(iceC));
System.out.print("\nTest less.\n");
System.out.println(iceC + " <= " + coldC + " ? " + iceC.less(coldC));
System.out.println(iceC + " <= " + fireF + " ? " + iceC.less(fireF));
System.out.println(iceF + " <= " + coldC + " ? " + iceF.less(coldC));
System.out.println(iceF + " <= " + fireC + " ? " + iceF.less(fireC));
System.out.print("\nTest geater\n");
System.out.println(iceC + " >= " + fireC + " ? " +iceC.greater(fireC));
System.out.println(iceC + " >= " + coldF + " ? " + iceC.greater(coldF));
System.out.println(iceF + " >= " + fireF + " ? " + iceF.greater(fireF));
System.out.println(iceF + " >= " + coldF + " ? " + iceF.greater(coldF));
}
}

星期一, 12月 12, 2005

"Lab 12-12-2005 (1) Constructor"

package ch4;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

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))
&& (day == otherDate.day) && (year == otherDate.year) );
}

public boolean precedes(Date otherDate)
{
return ( (year < otherDate.year) ||
(year == otherDate.year && getMonth( ) < otherDate.getMonth( )) ||
(year == otherDate.year && month.equals(otherDate.month)
&& day < otherDate.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) && (monthInt <= 12) &&
(dayInt >= 1) && (dayInt <= 31) &&
(yearInt >= 1000) && (yearInt <= 9999) );
}

private boolean dateOK(String monthString, int dayInt, int yearInt)
{
return ( monthOK(monthString) &&
(dayInt >= 1) && (dayInt <= 31) &&
(yearInt >= 1000) && (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 ch4;
public class ConstructorsDemo
{
public static void main(String[] args)
{
Date date1 = new Date("December", 16, 1770),
date2 = new Date(1, 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 + ".");
}
} ------------------------------------------------------------------------------------------------

星期一, 12月 05, 2005

Lab 12-5-2005 (2) Overloading

package ch4;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class DateSixthTry
{
private String month;
private int day;
private int 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);
}

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

private boolean dateOK(String monthString, int dayInt, int yearInt)
{
return ( monthOK(monthString) &&
(dayInt >= 1) && (dayInt <= 31) &&
(yearInt >= 1000) && (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") );
}


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


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 setMonth(String month)
{
this.month=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 < otherDate.year) ||
(year == otherDate.year && getMonth( ) < otherDate.getMonth( )) ||
(year == otherDate.year && month.equals(otherDate.month)
&& day < otherDate.day) );
}

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

public int getDay( )
{
return day;
}

public int getYear( )
{
return year;
}

public int getMonth( )
{
if (month.equalsIgnoreCase("January"))
return 1;
else if (month.equalsIgnoreCase("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.equalsIgnoreCase("November"))
return 11;
else if (month.equalsIgnoreCase("December"))
return 12;
else
{
System.out.println("Fatal Error");
System.exit(0);
return 0;
}
}


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 ch4;

public class OverloadedingDemo
{
public static void main(String[] args)
{
DateSixthTry date1=new DateSixthTry(),
date2=new DateSixthTry(),
date3=new DateSixthTry();
date1.setDate(1,2,2007);
date1.setMonth(3);
date2.setDate("February",2,2007);
date2.setMonth("April");
date3.setDate(2007);
System.out.println(date1);
System.out.println(date2);
System.out.println(date3);
}
} ------------------------------------------------------------------------------------------------

Lab 12-5-2005 (1) Using "this"

package ch4;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class DateFourthTry
{
private String month;
private int day;
private int year;
public String toString( )
{
return (month + " " + day + ", " + year);
}
public void writeOutput( )
{
System.out.println(month + " " + day + ", " + year);
}
public boolean equals(DateFourthTry otherDate)
{
return ( (month.equals(otherDate.month))
&& (day == otherDate.day) && (year == otherDate.year) );
}
public boolean precedes(DateFourthTry otherDate)
{
return ( (year < otherDate.year) ||
(year == otherDate.year && getMonth( ) < otherDate.getMonth( )) ||
(year == otherDate.year && month.equals(otherDate.month)
&& day < otherDate.day) );
}
public void setDate(int Month, int day, int year)
{
this.month = monthString(Month);
this.day = day;
this.year = year;
}
public String monthString(int month)
{
switch (month)
{
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";
}
}
public void readInput( )throws IOException
{
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter month, day, and year.");
System.out.println("Do not use a comma.");
month = keyboard.readLine( );
day = keyboard.read( );
year = keyboard.read( );
}
public int getDay( )
{
return day;
}
public int getYear( )
{
return year;
}
public int getMonth( )
{
if (month.equalsIgnoreCase("January"))
return 1;
else if (month.equalsIgnoreCase("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.equalsIgnoreCase("November"))
return 11;
else if (month.equalsIgnoreCase("December"))
return 12;
else
{
System.out.println("Fatal Error");
System.exit(0);
return 0;
}
}
}
------------------------------------------------------------------------------------------------
package ch4;

public class EqualsAndToStringDemo
{
public static void main(String[] args)
{
DateFourthTry date1 = new DateFourthTry( ),
date2 = new DateFourthTry( );
date1.setDate(6, 17, 1882);
date2.setDate(6, 17, 1882);
if (date1.equals(date2))
System.out.println(date1 + " equals " + date2);
else
System.out.println(date1 + " does not equal " + date2);
date1.setDate(7, 28, 1750);
if (date1.precedes(date2))
System.out.println(date1 + " comes before " + date2);
else
System.out.println(date2 + " comes before or is equal to "
+ date1);
}
}
------------------------------------------------------------------------------------------------

Homework 11-28-2005

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("Eorr");
}
}
public int accessor()
{
return count;
}
public void screen()
{
System.out.println("The count2 ="+count);
}
}






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 newcount1 = count1.accessor();
System.out.println("return count1="+newcount1);
count2.decrease();
count2.screen();
if(count1.equals(count2))
{
System.out.println("count1=count2");
}
else System.out.println("count1 =/= count2");
}
}