JAVA

2006/01/04

Lab 1-02-2006 Recursion

package hk;

import java.io.*;

public class sh {
public static double recursive(double number) {
if (number == 0)
return 0;
else if (number == 1) {
return 1;
}
else return (number * recursive(number - 1));
}

public static void main(String[] args) throws IOException {
double n;
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.
in));
System.out.println("please type positive number");
String input = keyboard.readLine();
double temp = Double.parseDouble(input.trim());
n = recursive(temp);
System.out.println(temp + "!=" + n);
}
}

2006/01/03

Lab 12-26-2005 (2)


2006/01/02

Lab 1-02-2006 modular sorting

package slelectionsort;
public class QuickSort {
public static void sort(double[] a, int left, int right) {
int i, j;
double s;

if (left < right) {
s = a[left];
i = left;
j = right + 1;

while (true) {
while (i + 1 < a.length && a[++i] < s);
while (j - 1 > -1 && a[--j] > s);
if (i >= j)
break;
double temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}

a[left] = a[j];
a[j] = s;

sort(a, left, j - 1); // 對左邊進行遞迴
sort(a, j + 1, right); // 對右邊進行遞迴
}
}
}

--------------------------------------------------------
package slelectionsort;

public class SelectionSort {
public static void sort(double[] a, int numberUsed)
{
int index, inedxOfNextSmallest;
for(index = 0 ; index < numberUsed - 1 ; index++ )
{
inedxOfNextSmallest = inedxOfSmallest(index, a, numberUsed);
interchange(index, inedxOfNextSmallest, a);
}
}

private static int inedxOfSmallest(int startIndex, double[] a, int numberUsed)
{
double min = a[startIndex];
int indexOfMin = startIndex;
int index;
for(index = startIndex + 1 ; index < numberUsed ; index++ )
if(a[index] < min)
{
min = a[index];
indexOfMin = index;
}
return indexOfMin;
}

private static void interchange(int i, int j, double[] a)
{
double temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

-------------------------------------------------------
package slelectionsort;

public class SelectionSortDemo {
public static void main(String[] args)
{
double[] b = {7.7 , 8 , 5.5 , 77 , 11 , 3 , 100.5 , 4.4 , 20 , 47 , 49};
double[] a = {83.5 , 84.4 , 5.5 , 10 , 0 , 3 , 983.1 , 88.555 , 32.44 , 47.6 , 49};

System.out.println("Array(b) contents before sorting:");
for(int i = 0; i < b.length ; i++)
System.out.print(b[i] + " ");
System.out.println();

System.out.println("Array(a) contents before sorting:");
for(int i = 0; i < b.length ; i++)
System.out.print(a[i] + " ");
System.out.println();

SelectionSort.sort(b, b.length );
QuickSort.sort(a, 0, a.length-1 );

System.out.println("Sorted array(b) values:");
for(int i = 0; i < b.length ; i++ )
System.out.print(b[i] + " ");
System.out.println();

System.out.println("Sorted array(a) values:");
for(int i = 0; i < a.length ; i++ )
System.out.print(a[i] + " ");
System.out.println();
}
}

2006/01/01

Lab 12-26-2005 (1)

package lab1;

public class sh {
public static void main(String[] a)
{
System.out.println("input is: "+a[0]+" "+a[1]+" "+a[2]);
System.out.println("output is: "+a[0]+" "+a[2]+" "+a[1]);
System.out.println("the length of parameter is: "+a.length);
}
}




Homework 12-19-2005 Lab Equal Arrays

package hw;

public class equal {
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};
System.out.println("a=1,9,6,4,0,2,1,2");
System.out.println("b=1,9,6,4,0,2,1,2");
if (equal(a, b))
System.out.println("a and b are equal ");
else
System.out.println("a and b are not equal ");
}

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





2005/12/26

Lab 12-19-2005 Sorting

package untitled14;

import java.io.*;

public class add {
private static Object keyboard;
public static void main(String[] args) throws IOException,
NumberFormatException {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.
in));

double tem;
double[] number = new double[5];
System.out.println("輸入5個數:");

for (int i = 0; i < 5; i++) {
number[i] = Double.parseDouble(keyboard.readLine());
}

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (number[i] < number[j]) {
tem = number[i];
number[i] = number[j];
number[j] = tem;
}
}
}

System.out.println();
System.out.println("結果");

for (int i = 0; i < 5; i++){
System.out.println(number[i]);
}
}
}

Lab 12-19-2005 Class Parameter

package untitled14;

public class add {
public static void main(String[] args) {
complex x = new complex(2, 3);
complex y = new complex(4, 5);
x.add(y);
System.out.println("ans:" + x.a + "+" + y.b + "i");

}
}

---------------------------------------------------------------
package untitled14;

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

2005/12/19

Homework 12-12-2005 Counter II

package hw;

public class hw {
private int value = 0;
public boolean equals(hw otherCounter) {
return this.value == otherCounter.value;
}

public void resetToZero() {
value = 0;
}

public void increment() {
value++;
}

public void decrement() {
if (value <= 0)
value = 0;
else
value--;
}

public int getValue() {
return value;
}

public void output() {
System.out.println(" Counter value is " + value);
}

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

-------------------------------------------------------------------------
package hw;

public class hw1 {
public static void main(String[] args) {
hw counter1 = new hw();
hw counter2 = new hw();
counter1.resetToZero();
counter1.increment();
counter2.resetToZero();
counter2.increment();
if (counter1.equals(counter2)) {
System.out.println( counter1 + " is equal to " + counter2);
} else {
System.out.println( counter1 + " is not equal to " + counter2);
}
counter2.increment();

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

Lab 12-12-2005 (2) Static Class

package lab;

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

Homework 12-5-2005

package hw;

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

public String less(Temperature coldC) {
return "";
}
}


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

2005/12/12

Lab 12-5-2005 (2) Overloading


package lab1;

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 < 1000 ) || (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 lab1;

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-12-2005 (1) Constructor


package lab1;

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 < 1000 ) || (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 lab1;

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