星期三, 12月 28, 2005

Lab 12-26-2005 (1)

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


輸入 hi ! there

結果 hi there !

12-19-2005 Lab Equal Arrays

public class untitledc {
public untitledc() {
}

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};
if (a == b)
System.out.println("a and b are equal by ==.");
else
System.out.println("a and b are not equal by ==.");

System.out.println("== only tests memory addresses.");

if (equalArrays(a, b))
System.out.println(
"a and b are equal by the equalArrays method.");
else
System.out.println(
"a and b are not equal by the equalArrays method.");

System.out.println(
"An equalArrays method is usually a more useful test.");

}

public static boolean equalArrays(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;
}
}

Lab 12-19-2005 Sorting

import java.io.*;

public class untitled {
public untitled() {
}

public static void main(String[] args) throws IOException {
double[] score = new double[5];
int index, a, b;
double x;
BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("Enter 5 numbers :");
for (index = 0; index < 5; index++) {
score[index] = Double.parseDouble(keyboard.readLine());
}
System.out.println("and the new score =");
for (a = 0; a < 5;a++) {
for(b=a+1;b<5;b++){
if (score[a] > score[b]) {
x = score[a];
score[a] = score[b];
score[b] = x;
}
}
}
for (index = 0; index < 5; index++) {
System.out.println(score[index]);
}
}
}

星期一, 12月 19, 2005

12.12homework

package q1;
public class counter1 {
public static void main(String[] args) {
counter count1 = new counter(),
count2 = new counter();
count1.increment();
count1.decrement();
count2.increment();
count2.decrement();
if (count1.equals(count2))
System.out.println("The are equal");
else
System.out.println("The are not equal");
count2.increment();
if (count1.equals(count2)) {
System.out.println("The are equal");
} else {
System.out.println("The are not equal");

}
}
}
////////////////////////////////////////////////
package q1;
public class counter {
private int count = 0;
public void resetToZero() {
count = 0;
}

public void increment() {
count++;
}

public void decrement() {
if (count <= 0)
count = 0;
else
count--;
}
public int getValue()
{
return count;
}
public void output() {
System.out.println(" The counter is " + count);
}
public boolean equals(counter otherCounter)
{
return this.count == otherCounter.count;
}
public String toString() {
return Integer.toString(count);
}
}

11.28HOMEWORK

package untitled4;

public class a1 {
public static void main(String[] args) {
Counter count1 = new Counter(),count2=new Counter();
count1.setZero();
System.out.print("count1=");
count1.out();
System.out.print("count2=");
count2.setZero();count2.out();
if(count1 ==count2)System.out.println("They are equal ");
else
System.out.println("They are not equal ");
count2.countInc();
count2.accessor();}}
////////////////////////////////////////////////////
package untitled4;

public class Counter {
private int count;
public Counter() {}
public void setZero(){count=0;
}
public void countInc(){
count++;
}public void countDec(){
if((count-1)>=0)
count--;
else
if((count-1)<0)
System.out.println("Decrease counnt is negative");
}public int accessor(){return count;
}public void out(){
System.out.println(""+count);
}
public static void main(String[] args) {}}

//////////////////////////////////////////////////

Lab 12-19-2005 Class Parameter

package untitled5;

public class complex
{double a=0,b=0;
public complex(double a,double b){
setComplex(a,b);
}
public void setComplex(double a,double b) {
this.a=a;
this.b=b;
}
public void add(complex C){
this.a=this.a+C.a;
this.b=this.b+C.b;
}
}
////////////////////////////////////////
package untitled5;




public class complexDemo {
public static void main(String args[]) {
complex Q1= new complex(2,3);
complex Q2= new complex(4,5);
Q1.add(Q2);
System.out.println(Q1.a+"+"+Q1.b+"*i");
}
}

//Object要大寫(C.Q1.Q2)

Homework 12-5-2005

import java.io.*;

public class Temperature {

private double degreesF;
private char degreesC;

public Temperature() {
this.degreesF = 0;
this.degreesC = 'C';
}

public Temperature(double value) {
this.degreesF = degreesF;
this.degreesC = 'C';
}

public Temperature(char scale) {
this.degreesF = 0;
if (scale == 'f' || scale == 'F') {
this.degreesC = 'F';
}
else if (scale == 'c' || scale == 'C') {
this.degreesC = 'C';
}
else {
System.out.println("Error degreesC,and scale set C");
this.degreesC = 'C';
}
}

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

public double getTempC() {
double temp = 0;
if (degreesC == 'C')
{
temp = degreesF;
}
else if (degreesC == 'F') {
temp = (degreesF - 32) * 5 / 9;
}
return temp;
}

public double getTempF() {
double temp = 0;
if (degreesC == 'F') {
temp = degreesF;
}
else if (degreesC == 'C') {
temp = (degreesF * 9 / 5) + 32;
}
return temp;
}

public void setdegreesF(double degreesF) {
this.degreesF = degreesF;
}

public void setScale(char scale) {
if (scale == 'f' || scale == 'F') {
this.degreesC = 'F';
}
else if (scale == 'c' || scale == 'C') {
this.degreesC = 'C';
}
else {
System.out.println("Error degreesC,and scale no change");
}
}

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

public boolean Equals(Temperature otherTemp) {
if (this.degreesC == 'C' && this.degreesF == otherTemp.getTempC()) {
return true;
}
else if (this.degreesC == 'F' && this.degreesF == otherTemp.getTempF()) {
return true;
}
else {
return false;
}
}

public boolean GreaterThan(Temperature otherTemp) {
if (this.degreesC == 'C' && this.degreesF >= otherTemp.getTempC()) {
return true;
}
else if (this.degreesC == 'F' && this.degreesF >= otherTemp.getTempF()) {
return true;
}
else {
return false;
}
}

public boolean LessThan(Temperature otherTemp) {
if (this.degreesC == 'C' && this.degreesF <= otherTemp.getTempC()) {
return true;
}
else if (this.degreesC == 'F' && this.degreesF <= otherTemp.getTempF()) {
return true;
}
else {
return false;
}
}

public String toString() {
return Double.toString(degreesF) + degreesC;
}

}


class TemperatureTest {

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.setTemp( -40.0, 'C');
Temperature coldF = new Temperature();
coldF.setScale('F');
coldF.setdegreesF( -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 + " , " + coldC.getTempF() + "F");
System.out.println("coldF = " + coldF + " , " + coldF.getTempC() + "C");

System.out.print("\nTest Equals ? \n");
System.out.println(iceC + " = " + coldC + " ? " + iceC.Equals(coldC));
System.out.println(iceC + " = " + iceF + " ? " + iceC.Equals(iceF));
System.out.println(iceF + " = " + coldC + " ? " + iceF.Equals(coldC));
System.out.println(iceF + " = " + iceC + " ? " + iceF.Equals(iceC));

System.out.print("\nTest Less Than ? \n");
System.out.println(iceC + " <= " + coldC + " ? " + iceC.LessThan(coldC));
System.out.println(iceC + " <= " + fireF + " ? " + iceC.LessThan(fireF));
System.out.println(iceF + " <= " + coldC + " ? " + iceF.LessThan(coldC));
System.out.println(iceF + " <= " + fireC + " ? " + iceF.LessThan(fireC));

System.out.print("\nTest Greater Than ? \n");
System.out.println(iceC + " >= " + fireC + " ? " + iceC.GreaterThan(fireC));
System.out.println(iceC + " >= " + coldF + " ? " + iceC.GreaterThan(coldF));
System.out.println(iceF + " >= " + fireF + " ? " + iceF.GreaterThan(fireF));
System.out.println(iceF + " >= " + coldF + " ? " + iceF.GreaterThan(coldF));
}}

"Lab 12-12-2005 (2) Static Class" (請病假)補po

//副程式
public class Fibonacci {
public static double f0=1,f1=1,f2=0;
public static double next()
{
f2=f0+f1;
f0=f1;
f1=f2;
return f2;
}
}

//主程式
public class main {
public static void main(String[] args)
{
for(int i=0;i<100;i++)
{
Fibonacci.next();
System.out.println("F(" + (i + 2) + ") = " + f2);
}
}
}

Lab 12-12-2005 (1) Constructor

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

public DateSixthTry() {
month = "Jan";
day = 1;
year = 1000;
}

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

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

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

public DateSixthTry(DateSixthTry 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("Jan")) {
return 1;
} else if (month.equals("Feb")) {
return 2;
} else if (month.equals("Mar")) {
return 3;
} else if (month.equals("Apr")) {
return 4;
} else if (month.equals("May")) {
return 5;
} else if (month.equals("Jun")) {
return 6;
} else if (month.equals("Jul")) {
return 7;
} else if (month.equals("Aug")) {
return 8;
} else if (month.equals("Sep")) {
return 9;
} else if (month.equals("Oct")) {
return 10;
} else if (month.equals("Nov")) {
return 11;
} else if (month.equals("Dec")) {
return 12;
} else {
System.out.println("Fatal Error");
System.exit(0);
return 0; //Needed to keep the compiler happy
}
}

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 < year ="="" year ="="" tryagain =" true;" keyboard =" new" monthinput =" Integer.parseInt(keyboard.readLine());" dayinput =" Integer.parseInt(keyboard.readLine());" yearinput =" Integer.parseInt(keyboard.readLine());" tryagain =" false;">= 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("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"));
}

private String monthString(int monthNumber) {
switch (monthNumber) {
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";
}
}
}



////////////////////////////////////////////////////////////////////////////
class ConstructorsDemo {
public static void main(String[] args) {
DateSixthTry date1 = new DateSixthTry("Dec", 16, 1770),
date2 = new DateSixthTry(1, 27, 1756),
date3 = new DateSixthTry(1882),
date4 = new DateSixthTry();

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

Lab 12-5-2005 (2) Overloading

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);
}
}
//////////////////////////////////////////////////////////
import java.io.*;
public class DateSixthTry {
private String month;
private int day;
private int year; //a four digit number.
public void setDate(int monthInt, int day, int year) {
if (dateOK(monthInt, day, year)) {
this.month = month(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) &&amp; (monthInt <= 12) &&(dayInt >= 1) && (dayInt <= 31) &&(yearInt >= 1000) && (yearInt <= 9999) ); } private boolean dateOK(String monthString, int dayInt, int yearInt) { return ( monthOK(monthString) &&amp; (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(newInputStreamReader(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 = month(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)) &&amp; (day == otherDate.day) && (year == otherDate.year)); }

public boolean precedes(DateSixthTry otherDate) {
return ( (year < otherDate.year) (year == otherDate.year &&amp; getMonth( ) < otherDate.getMonth()) (year == otherDate.year && month.equals(otherDate.month) &&amp; 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 month(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";
}
}
}

星期一, 12月 05, 2005

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

package zxc;

import java.io.*;
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.day=day;
this.year=year;
this.month=getmonth(month);}//修改的地方
public String getmonth(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.exit(0);
return "Eorror";}}
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.equalsIgnoreCase("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;}}}

"Lab 11-28-2005"

import java.io.*;
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 newMonth ,int newDay,int newYear){
day=newDay;
year=newYear;
month=getmonth(newMonth);}
public String getmonth(int getmonth){switch(getmonth){
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.exit(0);
return "Eorror";}}
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.equalsIgnoreCase("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;}}}
///////////////////////////////////////////////////////////////////
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);}}
////////////////////////////////////////////////////////////////////