星期一, 12月 26, 2005

lab 12-26(1)

Homework 12-19





















Sorting

import java.io.*;

public class Sorting {
public static void main(String[] args) throws IOException {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));
double[] a = new double[5];
double temp;
int i, j;
System.out.println("Enter 5 number:");
for (i = 0; i <>
a[i] = Double.parseDouble(keyboard.readLine());
}
for (i = 0; i <>
for (j = 0; j <>
if (a[i] <>
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("The answer:");
for (i = 0; i <>
System.out.println(a[i]);
}
}
}

星期四, 12月 22, 2005

complex

package complex1;
public class complextest
{
private double x,y;
public complex(double real, double imaginary)
{
this.x=real;
this.y=imaginary;
}
public double real()
{
return x;
}
public double imaginaty()
{
return y;
}
public void print1()
{
if(y>=0)
System.out.println(x+"+"+y+"i");
else
System.out.println(x+""+y+"i");
}
public static complex add(complex a,complex b)
{
complex test= new complex(0,0);
test.x= a.x + b.x;
test.y= a.y + b.y;
return test;
}
}
-----------------------------------------------------------------------------------
package complex1;
public class complextest2
{
public static void main(String[] args)
{
complex c = new complex(5,6);
complex d = new complex(2,3);
complex.add(c,d).print1();
}
}

測試結果

星期一, 12月 19, 2005

Static class

public class Fibonacci {
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 <>
Fibonacci.next();
System.out.println("F(" + (i + 2) + ") = " + sum);
}
}
}

Homework 12-12

package javacode2;

public class Counter
{
public Counter()
{
count = 0;
}

public void increase()
{
count++;
}

public void decrease()
{
if(count >=1)
count--;
else
count = 0;
}

public int getVasue()
{
return count;
}

public void printcount()
{
System.out.println("counter count is " + count);
}

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

public void resetToZero()
{
count = 0;
}

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

private int count;
}

-----------------------------------------------------------------
package javacode2;

class CounterTest
{
public static void main(String[] args)
{
Counter counter1 = new Counter();
Counter counter2 = new Counter();

counter1.increase();
counter2.increase();

if (counter1.equals(counter2))
{
System.out.println( counter1 + " is equal to " + counter2);
}
else
{
System.out.println( counter1 + " is not equal to " + counter2);
}

counter2.increase();
counter1.decrease();
if (counter1.equals(counter2))
{
System.out.println( counter1 + " is equal to " + counter2);
}
else
{
System.out.println( counter1 + " is not equal to " + counter2);
}
}
}


Homework 12-5-2005

package ccc;
public class temperature
{
private double degree;
private char scale;

public temperature()
{
this.degree=0;
this.scale='c';
}
public temperature(double degree)
{
this.degree=degree;
this.scale='c';
}
public temperature(char scale)
{
this.degree=0;
if(scale=='f'||scale=='F')
{
this.scale='F';
}
else if(scale=='c'||scale=='C')
{
this.scale='C';
}
else
{
System.out.println("error scale and please scale set C");
this.scale='C';
}
}
public temperature(double degree,char scale)
{
this.degree=degree;
if(scale=='f'||scale=='F')
{
this.scale='F';
}
else if(scale=='c'||scale=='C')
{
this.scale='C';
}
else
{
System.out.println("error scale and please scale set C");
this.scale='C';
}
}
public double get_temperatureC()
{
double degreesC=0;
if(scale=='C')
{
degreesC=degree;
}
else if(scale=='F')
{
degreesC = (degree -32 )*5/9;
}
return degreesC;
}
public double get_temperatureF()
{
double degreesF=0;
if(scale=='F')
{
degreesF=degree;
}
else if(scale=='C')
{
degreesF = (degree *9/5 )+32;
}
return degreesF;
}
public void setdegree(double degree)
{
this.degree=degree;
}
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 scale and scale no change");
}
}
public void setScale(double degree,char scale)
{
if(scale=='f'||scale=='f')
{
this.scale='F';
}
else if(scale=='c'||scale=='C')
{
this.scale='C';
}
else
{
System.out.println("Error scale and scale no change");
}
this.degree=degree;
}
public boolean isequals(temperature othertemp)
{
if (this.scale == 'C' && this.degree == othertemp.get_temperatureC())
{
return true;
}
else if (this.scale == 'F' && this.degree == othertemp.get_temperatureF())
{
return true;
}
else
{
return false;
}
}
public boolean isgreaterthan(temperature otherTemp)
{
if (this.scale == 'C' && this.degree >= otherTemp.get_temperatureC())
{
return true;
}
else if (this.scale == 'F' && this.degree >= otherTemp.get_temperatureF())
{
return true;
}
else
{
return false;
}
}
public boolean islessthan(temperature otherTemp)
{
if (this.scale == 'C' && this.degree <= otherTemp.get_temperatureC())
{
return true;
}
else if (this.scale == 'F' && this.degree <= otherTemp.get_temperatureF())
{
return true;
}
else
{
return false;
}
}

public String toString()
{
return Double.toString(degree) + scale;
}
}


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

package ccc;
public class temperature_a
{
public static void main(String[] args)
{

temperature cloudC = new temperature(0.0, 'C');
temperature cloudF = new temperature(32.0, 'F');
temperature hotC = new temperature(100.0);
hotC.setScale('C');
temperature hotF = new temperature(212.0);
hotF.setScale('F');

temperature degreeC = new temperature();
degreeC.setScale(-40.0, 'C');
temperature degreeF = new temperature();
degreeF.setScale('F');
degreeF.setdegree( -40.0);

System.out.println("cloudC = " + cloudC );
System.out.println("cloudF = " + cloudF );
System.out.println("hotC = " + hotC );
System.out.println("hotF = " + hotF );
System.out.println("degreeC = " + degreeC + " , " + degreeC.get_temperatureF() + "F");
System.out.println("degreeF = " + degreeF + " , " + degreeF.get_temperatureC() + "C");

System.out.print("Test isequals");
System.out.println(cloudC + " = " + degreeC + " ? " + cloudC.isequals(degreeC));
System.out.println(cloudC + " = " + cloudF + " ? " + cloudC.isequals(cloudF));
System.out.println(cloudF + " = " + degreeC + " ? " + cloudF.isequals(degreeC));
System.out.println(cloudF + " = " + cloudC + " ? " + cloudF.isequals(cloudC));

System.out.print("Test islessthan");
System.out.println(cloudC + " <= " + degreeC + " ? " + cloudC.islessthan(degreeC));
System.out.println(cloudC + " <= " + hotF + " ? " + cloudC.islessthan(hotF));
System.out.println(cloudF + " <= " + degreeC + " ? " + cloudF.islessthan(degreeC));
System.out.println(cloudF + " <= " + hotC + " ? " + cloudF.islessthan(hotC));

System.out.print("Test isgreaterthan");
System.out.println(cloudC + " >= " + hotC + " ? " +cloudC.isgreaterthan(hotC));
System.out.println(cloudC + " >= " + degreeF + " ? " + cloudC.isgreaterthan(degreeF));
System.out.println(cloudF + " >= " + hotF + " ? " + cloudF.isgreaterthan(hotF));
System.out.println(cloudF + " >= " + degreeF + " ? " + cloudF.isgreaterthan(degreeF));
}
}




星期一, 12月 12, 2005

Constructors

package aaa;
import java.io.*;
public class Date {
private String month;
private int day;
private int year;
public Date() {
month = "Jan";
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("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;
}
}

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 == otherDate.year && getMonth() <>
(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 ues comma.");
int monthInput = Integer.parseInt(keyboard.readLine());
int dayInput = Integer.parseInt(keyboard.readLine());
int yearInput = Integer.parseInt(keyboard.readLine());
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("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";
}
}
}

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

package aaa;
class ConstructorsDemo {
public static void main(String[] args) {
Date date1 = new Date("Dec", 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 + ".");
}
}

Overloading














































































星期一, 12月 05, 2005

Lab 12-5(1)


month = monthString(newmonth);
day = newday;
year = newyear;
改成
this.month = monthString(month);
this.day = day;
this.year = year;
結果









Homework 11-28






























Lab 11-28

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

public class DateFourthTry {
public 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) &&amp; day == (otherDate.day) &&
year == (otherDate.year));
}

public boolean precedes(DateFourthTry otherDate) {
return (year <>
(year == otherDate.year && getMonth() <>
(year == otherDate.year && month.equals(otherDate.month)
&& 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("Fateal Error");
System.exit(0);
return 0;
}
}

public String monthString(int monthNmmber) {
switch (monthNmmber) {
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("Fateal Error");
System.exit(0);
return "Error";
}

}

public void setDate(int newMonth, int newDay, int newYear) {
month = monthString(newMonth);
day = newDay;
year = newYear;
}
}

---------------------------------------------------------------------------------
public class EqualsAndToStringDemo
{
public static void main(String[] args)
{
DateFourthTry date1 = new DateFourthTry();
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);
}
}
}