JAVA

Sunday, January 15, 2006

河內塔

Thursday, January 05, 2006

01/02/06 recursion

import javax.swing.JOptionPane;
public class recursion {
public static void main(String[] arg){
String PString=JOptionPane.showInputDialog("請輸入一非負整數:");
int i=Integer.parseInt(PString);
System.out.println(i+"! = "+recursion.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;}
}

圖檔

01/02/06 modular sorting

import java.io.*;
public class lab0102{
public static void main(String args[])throws IOException{
double[] number= new double[3];
int x,y;
double z;
for(x=0;x<3;x++){
BufferedReader keyin= new BufferedReader(new InputStreamReader(System.in));
number[x]=Integer.parseInt(keyin.readLine());
}
System.out.println("after sorting:");
for(x=0;x<3;x++){
for(y=0;y<3;y++){
if (number[x]>number[y]){
z=number[x];
number[x]=number[y];
number[y]=z;
}
}
}
for(x=0;x<3;x++){
System.out.print(""+number[x]+" ");
}
System.out.println("");
System.exit(0);
}
}


圖檔

Monday, January 02, 2006

12/26 lab(2)

public class lab2{
public static void main(String[] args){
if(args.length==0)
System.out.println("no parameters");
else{
for(int j=args.length-1;0<=j;j--)
System.out.print(args[j]+" ");
}
}
}
圖檔

Monday, December 26, 2005

12/19homework

public class Equal{
public static void main(String[] args) {
int i;
int[] a={1,9,6,4,0,2,1,2};
int[] b={1,9,6,4,0,2,1,2};
System.out.print("a[]=");
for(i=0;i {

System.out.print(" "+a[i]);
}
System.out.println("");
System.out.print("b[]=");

for(i=0;i {

System.out.print(" "+b[i]);
}
System.out.println("");
if (equalArrays(a, b))
System.out.println("a and b are equal .");

else
System.out.println("a and b are not equal .");


}


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


http://home.pchome.com.tw/club/savvygirl/home.JPG

Lab12/19

import java.io.*;

public class Sorting {
public Sorting() {
}

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

if (score[a] > score[b]) {
y = score[a];
score[a] = score[b];
score[b] = y;
}
}
}
for (i=0;i< 5;i++) {
System.out.println(score[i]);
}
}
}















http://home.pchome.com.tw/club/savvygirl/me.JPG

Monday, December 19, 2005

12/19lab 1

class Number{

public double real;
public double complex;

public Number(double real,double complex )
{this.real=real;
this.complex=complex;
}

public void Add(Number q3)
{

this.real=this.real+q3.real;
this.complex=this.complex+q3.complex;

}

}

class Compare {
public static void main(String[] args)
{



Number num1=new Number(2,3);
Number num2=new Number(4,5);
System.out.println("Number1 real:"+num1.real);
System.out.println("Number1 complex:"+num1.complex+"i");
System.out.println("Number2 real:"+num2.real);
System.out.println("Number2 complex:"+num2.complex+"i");
num1.Add(num2);
System.out.println("Number complex:"+num1.real);

System.out.println("Number complex:"+num1.complex+"i");

}
}

12/12lab(2)

public class fib {
public static double f0=1,f1=1,f2=0;
public static double next(){
f2=f0+f1;
f0 = f1;
f1 = f2;
return f2;
}
public static void main(String[] args) {
int i;
for(i=0;i<=100;++i){
System.out.println(i +"="+ f2);
fib.next();

}

}
}

Lab 12-12-2005 (1) Constructor

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

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 < 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("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 < 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 on three lines.");
System.out.println(
"Enter month, day, and year as three integers.");

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

public class aa {
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 + ".");
}
}
http://home.pchome.com.tw/club/savvygirl/me.JPG

12/12homework

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

class DateFourthTry
{
private String month;

public String toString()
{
return(month);
}

public void writeOutput()
{
System.out.println(month);
}



public boolean equals(DateFourthTry date)
{
return((this.month.equals(date.month)));
}

public void setDate(int month){

this.month = monthString(month);

}

public String monthString(int monthNmmber){



switch (monthNmmber){
case -1:
return "ERROR!";
case 0:
return "0";
case 1:
return "1";
case 2:
return "2";
case 3:
return "3";
case 4:
return "4";
case 5:
return "5";
case 6:
return "6";
case 7:
return "7";
case 8:
return "8";
case 9:
return "9";
case 10:
return "10";
default:
System.out.println("Error");
System.exit(0);
return "Error";
}
}
}

public class a
{
public static void main(String[] args)
{
DateFourthTry date1=new DateFourthTry();
DateFourthTry date2=new DateFourthTry();

date1.setDate(6);
date2.setDate(7);

if(date1.equals(date2))
System.out.println(date1+" equals"+date2);
else
System.out.println(date1+" does not equals"+date2);


System.out.println("兩者皆增一比比看:");
date1.setDate(7);
date2.setDate(8);

if(date1.equals(date2))
System.out.println(date1+" equals"+date2);
else
System.out.println(date1+" does not equals"+date2);


System.out.println("一增一減比比看:");
date1.setDate(8);
date2.setDate(7);


if(date1.equals(date2))
System.out.println(date1+" equals"+date2);
else
System.out.println(date1+" does not equals"+date2);

}
}

Monday, December 12, 2005

12/5homework

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 Scale,and scale set C");
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 Scale,and scale set C");
this.scale = 'C';
}
}

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

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

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 Scale,and scale no change");
}
}

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 Scale,and scale no change");
}
this.value = value;
}

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

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

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

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

}


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.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 + " , " + 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 LessThan ? \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 GreaterThan ? \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));


}

}


http://home.pchome.com.tw/club/savvygirl/ya.JPG

Monday, December 05, 2005

Homework 11-28-2005

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

class Counter
{
private int count;

public Counter(int r)
{if(r<0)
{
System.out.println("error!");
r=0;}
else{
count=r;
}
}
public void countIncrease()
{
count=count+1;
}

public void countDecrease()
{
count=count-1;
}

public void show1()
{
System.out.println("count1:"+count);

}
public void show2()
{
System.out.println("count2:"+count);

}
}




public class ananfish2
{
public static void main(String args[])
{

Counter count1=new Counter(-4);
count1.show1();
Counter count2=new Counter(3);
count2.show2();
System.out.println("兩者皆增一比比看:");
count1.countIncrease();
count2.countIncrease();
count1.show1();
count2.show2();

System.out.println("一增一減比比看:");
count1.countIncrease();
count2.countDecrease();
count1.show1();
count2.show2();
if(count1 ==count2)
{System.out.println("count1 is equal to count2");
} else
{System.out.println("count1 does not equal to count2");

}
}




}
完成圖

Lab11/28

OK