顯示具有 Lab 標籤的文章。 顯示所有文章
顯示具有 Lab 標籤的文章。 顯示所有文章

2008年6月22日 星期日

Lab Magic Parking Tower(補交)

sorry 老師~
我以為我已經上傳了
所以現在才補交...





















2008年6月15日 星期日

2008年6月2日 星期一

Lab Java Constructor

如果直接執行下列程式
Date birthday = new Date("Jan",1,2000);
birthday.Date("Feb",1,2000);
birthday.setDate("Feb",1,2000);
birthday=new Date("Mar",1,2000);
會有錯誤
原因為birthday.Date("Feb",1,2000);
建構子不可以這樣呼叫
他必須跟在new後面











如果刪除那一行則可以正常運作
如下






2008年5月26日 星期一

Lab Overloading

import java.util.Scanner;
public class LabOverloading
{
private String mouth;
private int day;
private int year;

public void setDate(int mouthInt,int day,int year)
{
if (dateOK(mouthInt,day,year))
{
this.mouth=mouthString(mouthInt);
this.day=day;
this.year=year;
}
else
{
System.out.println("Fatal Error");
System.exit(0);
}
}

public void setDate(String mouthString,int day,int year)
{
if (dateOK(mouthString,day,year))
{
this.mouth=mouthString;
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 mouthInt,int dayInt,int yearInt )
{
return ((mouthInt>=1)&&(mouthInt<=12)&&(dayInt>=1)&&(dayInt<=31)&&(yearInt>=1000)&&(yearInt<=9999));
}

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

private boolean mouthOK(String mouth)
{
return (mouth.equals("January")mouth.equals("February")mouth.equals("March")mouth.equals("April")mouth.equals("May")mouth.equals("June")mouth.equals("July")mouth.equals("August")mouth.equals("September")mouth.equals("October")mouth.equals("November")mouth.equals("Deceber"));
}

public void readInput()
{
boolean tryAgain=true;
Scanner keyboard=new Scanner(System.in);
while(tryAgain)
{
System.out.println("Enter mouth ,day and year.");
System.out.println("Do not use a comma.");
String mouthInput= keyboard.next();
int dayInput= keyboard.nextInt();
int yearInput= keyboard.nextInt();
if (dateOK(mouthInput,dayInput,yearInput))
{
setDate(mouthInput,dayInput,yearInput);
tryAgain=false;
}
else System.out.println("Illegal date. Reenter input");
}
}

public String mouthString(int mouthNumber)
{
switch(mouthNumber)
{
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 "Deceber";
default: System.out.println("Fatal Error");
System.exit(0); return "Error";
}
}

public String toString()

{
return year+"/"+mouth+"/"+day;
}


public static void main(String[] args)
{
LabOverloading date1=new LabOverloading(),
date2=new LabOverloading(),
date3=new LabOverloading();

date1.setDate(1,2,2008);
date2.setDate("February",2,2008);
date3.setDate(2008);

System.out.println(date1);
System.out.println(date2);
System.out.println(date3);
}
}



Lab ADT, accessor, mutator

public class Complex
{
private int real;
private int imaginary;

public void setComplex(int real,int imaginary)
{
this.real=real;
this.imaginary=imaginary;
}

public String getComplex()
{
return (real+"+"+imaginary+"i");
}

public Complex addComplex(Complex newComplex)
{
Complex result=new Complex();
result.real=real+newComplex.real;
result.imaginary=imaginary+newComplex.imaginary;
return result;
}

}


public class LabComplexClass
{
public static void main(String[] args)
{
Complex data1= new Complex(),
data2= new Complex();

data1.setComplex(2,3);
data2.setComplex(4,5);

System.out.println(data1.addComplex(data2).getComplex());

//Mutator
data1.setComplex(5, 6);
System.out.println(data1.addComplex(data2).getComplex());
}

}












2008年5月19日 星期一

lab Fraction equality test

public class Fraction
{
private int mom;
private int son;

public void setFraction(int son ,int mom)
{
this.mom=mom;
this.son=son;
}
public Fraction add (Fraction newFraction)
{
Fraction result=new Fraction();
result.mom=newFraction.mom*mom;
result.son=newFraction.mom*son+mom*newFraction.son;
return result;
}
public String equals (Fraction newFraction)
{
if((float)son/(float)mom==(float)newFraction.son/(float)newFraction.mom)
{
return "they are equal";
}
else
{
return "they are not equal";
}
}

public String toString()
{
return this.son+"/"+this.mom;
}

}

public class labFractionAddition
{
public static void main(String[] args)
{
Fraction f1=new Fraction(),
f2=new Fraction();

f1.setFraction(5,6);
f2.setFraction(6,7);
System.out.println(f1.equals (f2).toString());
}

}






















lab Fraction Addition

public class Fraction
{
private int mom;
private int son;

public void setFraction(int son ,int mom)
{
this.mom=mom;
this.son=son;
}
public Fraction add (Fraction newFraction)
{
Fraction result=new Fraction();
result.mom=newFraction.mom*mom;
result.son=newFraction.mom*son+mom*newFraction.son;
return result;
}
public String equals (Fraction newFraction)
{
if((float)son/(float)mom==(float)newFraction.son/(float)newFraction.mom)
{
return "they are equal";
}
else
{
return "they are not equal";
}
}

public String toString()
{
return this.son+"/"+this.mom;
}

}



public class labFractionAddition
{
public static void main(String[] args)
{
Fraction f1=new Fraction(),
f2=new Fraction();

f1.setFraction(1,2);
f2.setFraction(1,3);
System.out.println(f1.add (f2).toString());
}

}























2008年4月28日 星期一

Class Definition 3






















應該做的修正:

改成

this.month=month

this.day=day

this.year=year


Lab counter

public class Counter
{
private int number;

public void reset()
{
number=0;
}
public void inc()
{
number=number+1;
}
public void dec()
{
number=number-1;
}
public void output()
{
System.out.println(number);
}
}


public class CounterMain
{
public static void main(String[] args)
{
Counter count;
count = new Counter();
count.reset();
count.inc();
count.inc();
count.dec();
count.output();
}
}

結果:













2008年4月14日 星期一

Display4.1+ makeItNewYear

public class DateFirstTry
{
public String month;
public int day;
public int year;
public void writeOutput()
{
System.out.println(month+" "+day+","+year);
}
public void makeItNewYear()
{
month = "January";
day = 1 ;
}
}


public class DateFirstTryDemo
{
public static void main(String[] args)
{
DateFirstTry date1,date2;
date1 = new DateFirstTry();
date2 = new DateFirstTry();
date1.month = "December";
date1.day = 31;
date1.year = 2007;
System.out.println("date1:");
date1.writeOutput();

date1.makeItNewYear();
System.out.println("new date1:");
date1.writeOutput();

date2.month = "July";
date2.day = 4;
date2.year = 1776;
System.out.println("date2:");
date2.writeOutput();

date2.makeItNewYear();
System.out.println("new date2:");
date2.writeOutput();
}
}

結果:

Display4.1

public class DateFirstTry
{
public String month;
public int day;
public int year;
public void writeOutput()
{
System.out.println(month+" "+day+","+year);
}
}

public class DateFirstTryDemo
{
public static void main(String[] args)
{
DateFirstTry date1,date2;
date1 = new DateFirstTry();
date2 = new DateFirstTry();
date1.month = "December";
date1.day = 31;
date1.year = 2007;
System.out.println("date1:");
date1.writeOutput();

date2.month = "July";
date2.day = 4;
date2.year = 1776;
System.out.println("date2:");
date2.writeOutput();
}
}

結果

2008年4月7日 星期一

Lab 9*9

public class Lab99
{
public static void main(String[] args)
{
for(int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
System.out.print(i+"*"+j+"="+(i*j)+" ");
}
System.out.println();
}
}
}

結果:


Lab Fibonacci numbers

import java.util.Scanner;

public class Homework0407
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter the limit number:");

float first,second,temp,max,per;
first=1;
second=1;
max=keyboard.nextInt();

while(second<=max)
{
per=second/first;
System.out.println(second+"/"+first+"="+per);
temp=first+second;
first=second;
second=temp;
}
}
}

結果:

2008年3月24日 星期一

Lab: Tax Calculation

import java.util.Scanner;

public class TaxCalculation
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter net income:");

double netIncome = keyboard.nextInt();
double tax;

if (netIncome<=370000)
tax=netIncome*0.06;
else if((netIncome>370000)&&(netIncome<=990000))
tax=netIncome*0.13-25900;
else if ((netIncome>990000)&&(netIncome<=1980000))
tax=netIncome*0.21-105100;
else if ((netIncome>1980000)&&(netIncome<=3720000))
tax=netIncome*0.30-283300;
else if (netIncome>3720000)
tax=netIncome*0.40-655300;
else
tax=0.0;

System.out.print("總共需要");
System.out.printf("%10.2f",tax);
System.out.println("元");
}
}

結果:
1000000的稅




2000000的稅

Lab Finding the max of three numbers

import java.util.Scanner;

public class MaxOfThreeNumbers
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter three number:");

int firstNumber = keyboard.nextInt();
int secondNumber = keyboard.nextInt();
int thirdNumber = keyboard.nextInt();
int temp=firstNumber;

if(secondNumber>=temp)
{temp=secondNumber;}
if(thirdNumber>=temp)
{temp=thirdNumber;}

System.out.println("the max of three number is : "+temp);
}
}

結果:

2008年3月17日 星期一

Lab Keyboard input

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

public class LabKeyboardinput
{
public static void main(String[] args)throws IOException
{
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the number of pods followed by");
System.out.println("the number of peas in the pod:");
String numberOfPods = keyboard.readLine();
String peasPerPod = keyboard.readLine();

double totalNumberOfPeas = Double.parseDouble (numberOfPods)*Double.parseDouble (peasPerPod);

System.out.print(numberOfPods+"pods and");
System.out.println(peasPerPod+"peas per pod.");
System.out.println("The total number of peas ="+totalNumberOfPeas);
}
}

結果:

Lab Scanner

import java.util.Scanner;

public class ScannerDemo
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter the number of pods followed by");
System.out.println("the number of peas in the pod:");
int numberOfPods = keyboard.nextInt();
int peasPerPod = keyboard.nextInt();
int totalNumberOfPeas = numberOfPods*peasPerPod;

System.out.print(numberOfPods+"pods and");
System.out.println(peasPerPod+"peas per pod.");
System.out.println("The total number of peas ="+totalNumberOfPeas);
}
}

2008年3月10日 星期一

Lab: Simple Calculation

public class SimpleCalculation
{
public static void main(String[] args)
{
double result6;
result6=6000/5280.0;
System.out.print("6000 feet 為");
System.out.printf("%10.2f",result6);
System.out.println("mile");
System.out.print("6000 feet 總共需要");
System.out.printf("%10.2f",5000*result6);
System.out.println("元");

double result4;
result4=4000/5280.0;
System.out.print("4000 feet 為");
System.out.printf("%10.2f",result4);
System.out.println("mile");
System.out.print("4000 feet 總共需要");
System.out.printf("%10.2f",5000*result4);
System.out.println("元");
}
}


2008年3月9日 星期日

Lab 2 Java for Scientific Computation

阿斯巴甜的每日安全劑量:每公斤體重50毫克。
小白鼠平均體重:20g








參考資料:
http://tw.search.yahoo.com/
http://tw.knowledge.yahoo.com/question/?qid=1405102414901

2008年3月3日 星期一

Lab Get familiar with JBuilder

Do Display 1.1

1.開啟新的專案
2.開啟新的類別
3.執行java應用程式
4.結果如下: