horsemanship世界

星期一, 4月 24, 2006

Lab8

package lab8;
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(date.equals(date2))
System.out.println(date1 + "equals"+date2);
else
Syetem.out.println(date1 + "does not equal"+date2);

date1.setDate(7,28,1750);

if(date1.precedes(date2))
Syetem.out.println(date1 + " comes befpre "+date2);
else
Syetem.out.println(date2 + "comes befpre or is equal to "+date1);
}
}

=========================================
package lab8;
import java.io.*;
public class DateFourthTry
{
private String month;
private int day, 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)) && (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));
}}

星期一, 4月 10, 2006

Lab7

import java.io.*;
public class DateSecondTry {
public static void main(String[] args) throws IOException
{ DemoOfDateSecondTry date = new DemoOfDateSecondTry();
date.readInput();
date.writeOutput();}}
===============================================
import java.io.*;
public class DemoOfDateSecondTry
{ public String month;
public int day,year;
public void writeOutput()
{System.out.println(month+" "+ day +", " + year);}
public void readInput() throws IOException
{ BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter month, day, and year.");
System.out.println("Don't use a comma.");
month = keyboard.readLine();
String days = keyboard.readLine();
day = Integer.parseInt(days);
String years = keyboard.readLine();
year = Integer.parseInt(years); }}

結果:
Enter month, day, and year.
Don't use a comma.
12
31
2006
12 31, 2006

Homework6

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 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();
System.out.println("New year:");
date1.makeItNewYear();
date1.writeoutput();date2.month="July";
date2.day=4;
date2.year=1776;
System.out.println("date2:");
date2.writeoutput();
System.out.println("New year:");
date2.makeItNewYear();
date2.writeoutput();}}
結果:
date1:December 31 2007
NewYear of date1 is:January 1 2007
date2:July 4 1776
NewYears of date2 is:January 1 1776