public class House{
private int street_number;
private int year=2000;
private int tax_band=0;
private String street_name;
private String town;
public House(){
}
public House(String town,String street_name,int street_number)
{set_town(town);
set_street_name(street_name);
set_street_number(street_number);
}
public House(String town,String street_name,int street_number,int year,int tax_band){
set_town(town);
set_street_name(street_name);
set_street_number(street_number);
set_year(year);
set_tax_band(tax_band);
} public void printDetails(){
System.out.println("the details:");
System.out.print(town+" ");
System.out.print(street_name+" ");
System.out.println(street_number);
//Str1= toString(year);
//Str2= toString(tax_band);
//System.out.println(Str1);
//System.out.println(Str2);
} public void set_street_number(int street_number)
{this.street_number=street_number;}
public int get_street_number()
{return street_number;} public void set_year(int year)
{this.year=year;} public void set_tax_band(int tax_band)
{this.tax_band=tax_band;} public void set_street_name(String street_name)
{this.street_name=street_name;}
public String get_street_name()
{return street_name;} public void set_town(String town)
{this.town=town;}
public String get_town()
{return town;}
}public class Home extends House
{
private String family;
private OurDate movedIn=new OurDate();
//house即超类的street_number,street即超类的street_name
public Home(String town,String street,int house,String family,OurDate movedIn){
super(town,street,house);
setFamily(family);
setDateMovedIn(movedIn);
}
public Home(String town,String street,int house,int yearConstructed,int taxBand,String Family,OurDate movedIn){
super(town,street,house,yearConstructed,taxBand); //父类构造器的用法!!
setFamily(Family);
setDateMovedIn(movedIn);
}
public Home(){
}
public void setFamily(String family)
{this.family=family;}
public String getFamily()
{return family;} public void setDateMovedIn(OurDate movedIn)
{this.movedIn=movedIn;}
public String getDateMovedIn()
{return movedIn.toString();}
public void printDetails(){
System.out.println("the details:");
System.out.print(get_town()+" "+get_street_name()+
" "+get_street_number()+" ");
System.out.println("has been occupied by "+getFamily()+" since "+
getDateMovedIn());
}
}import java.util.*;
public class test{
public static void main(String[] args) throws Exception
{
House testhouse = new House("宁波","江北",209,2000,123);
Home testhome = new Home("宁波","江北",209,"me",new OurDate(2002,9,1));
testhouse.printDetails();
testhome.printDetails();
System.out.println("以上是测试一,下面的是测试二:");
///////////////////////////////////////////////////////////////////////////////////
House[] collection = new House[100];
String string="";
for(int i=0;i<100;i++){
System.out.println("输入城市,街道,门牌号,如果知道还可以包括住户名和入住时间(以年 月 日的形式输入),之间用空格隔开,结束输入请键入end");
string = BasicIo.readString();
StringTokenizer tokens=new StringTokenizer(string);
if(string.equals("end"))break;
String str[] =new String[7];
for(int j=0;j<7;j++){
while(tokens.hasMoreTokens()){
str[j]=tokens.nextToken();
break;
}
}
System.out.println(str[3]);
int temp = Integer.parseInt(str[2]);
if(tokens.countTokens()<4){
collection[i].set_town(str[0]);
collection[i].set_street_name(str[1]);
collection[i].set_street_number(temp);
}else{
int y,m,d;
y=Integer.parseInt(str[4]);
m=Integer.parseInt(str[5]);
d=Integer.parseInt(str[6]);
OurDate datemovedin;
datemovedin=new OurDate(y,m,d);
testhome.setFamily(str[3]);
testhome.setDateMovedIn(datemovedin);
collection[i] = (House)testhome;// zilei shi chaolei
collection[i].set_street_name(str[1]);
collection[i].set_town(str[0]);
collection[i].set_street_number(temp);
}
}
for(int i=0;i<100;i++){
if(collection[i]==null) break;
collection[i].printDetails();//duo tai xing
}
}
}
帮忙看看哪错了 怎么调?
小弟先谢过了
private int street_number;
private int year=2000;
private int tax_band=0;
private String street_name;
private String town;
public House(){
}
public House(String town,String street_name,int street_number)
{set_town(town);
set_street_name(street_name);
set_street_number(street_number);
}
public House(String town,String street_name,int street_number,int year,int tax_band){
set_town(town);
set_street_name(street_name);
set_street_number(street_number);
set_year(year);
set_tax_band(tax_band);
} public void printDetails(){
System.out.println("the details:");
System.out.print(town+" ");
System.out.print(street_name+" ");
System.out.println(street_number);
//Str1= toString(year);
//Str2= toString(tax_band);
//System.out.println(Str1);
//System.out.println(Str2);
} public void set_street_number(int street_number)
{this.street_number=street_number;}
public int get_street_number()
{return street_number;} public void set_year(int year)
{this.year=year;} public void set_tax_band(int tax_band)
{this.tax_band=tax_band;} public void set_street_name(String street_name)
{this.street_name=street_name;}
public String get_street_name()
{return street_name;} public void set_town(String town)
{this.town=town;}
public String get_town()
{return town;}
}public class Home extends House
{
private String family;
private OurDate movedIn=new OurDate();
//house即超类的street_number,street即超类的street_name
public Home(String town,String street,int house,String family,OurDate movedIn){
super(town,street,house);
setFamily(family);
setDateMovedIn(movedIn);
}
public Home(String town,String street,int house,int yearConstructed,int taxBand,String Family,OurDate movedIn){
super(town,street,house,yearConstructed,taxBand); //父类构造器的用法!!
setFamily(Family);
setDateMovedIn(movedIn);
}
public Home(){
}
public void setFamily(String family)
{this.family=family;}
public String getFamily()
{return family;} public void setDateMovedIn(OurDate movedIn)
{this.movedIn=movedIn;}
public String getDateMovedIn()
{return movedIn.toString();}
public void printDetails(){
System.out.println("the details:");
System.out.print(get_town()+" "+get_street_name()+
" "+get_street_number()+" ");
System.out.println("has been occupied by "+getFamily()+" since "+
getDateMovedIn());
}
}import java.util.*;
public class test{
public static void main(String[] args) throws Exception
{
House testhouse = new House("宁波","江北",209,2000,123);
Home testhome = new Home("宁波","江北",209,"me",new OurDate(2002,9,1));
testhouse.printDetails();
testhome.printDetails();
System.out.println("以上是测试一,下面的是测试二:");
///////////////////////////////////////////////////////////////////////////////////
House[] collection = new House[100];
String string="";
for(int i=0;i<100;i++){
System.out.println("输入城市,街道,门牌号,如果知道还可以包括住户名和入住时间(以年 月 日的形式输入),之间用空格隔开,结束输入请键入end");
string = BasicIo.readString();
StringTokenizer tokens=new StringTokenizer(string);
if(string.equals("end"))break;
String str[] =new String[7];
for(int j=0;j<7;j++){
while(tokens.hasMoreTokens()){
str[j]=tokens.nextToken();
break;
}
}
System.out.println(str[3]);
int temp = Integer.parseInt(str[2]);
if(tokens.countTokens()<4){
collection[i].set_town(str[0]);
collection[i].set_street_name(str[1]);
collection[i].set_street_number(temp);
}else{
int y,m,d;
y=Integer.parseInt(str[4]);
m=Integer.parseInt(str[5]);
d=Integer.parseInt(str[6]);
OurDate datemovedin;
datemovedin=new OurDate(y,m,d);
testhome.setFamily(str[3]);
testhome.setDateMovedIn(datemovedin);
collection[i] = (House)testhome;// zilei shi chaolei
collection[i].set_street_name(str[1]);
collection[i].set_town(str[0]);
collection[i].set_street_number(temp);
}
}
for(int i=0;i<100;i++){
if(collection[i]==null) break;
collection[i].printDetails();//duo tai xing
}
}
}
帮忙看看哪错了 怎么调?
小弟先谢过了
BasicIo.java
import java.io.* ;/**
* simple methods for basic input/output (Java 1.1)
* @author Roger Garside
* @version Last Rewritten: 16/Sept/97
*/public class BasicIo
{
/**
* output a prompt message to the screen
* @param prompt message
*/
public static void prompt(String s)
{
System.err.print(s) ;
System.err.flush() ;
} // end of method prompt /**
* read the next line from the keyboard as a String
* @return the line as a String
*/
public static String readString() throws IOException
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)) ; return in.readLine() ;
} // end of method readString /**
* read a character, the first on the line
* @return the first character on the line
*/
public static char readCharacter() throws IOException
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)) ; String s = in.readLine() ;
if (s.length() == 0)
return ' ' ;
else
return s.charAt(0) ;
} // end of method readCharacter /**
* read the next line from the keyboard as an integer
* @return the line as an integer
*/
public static int readInteger() throws IOException
{
String line ; BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)) ; try
{
line = in.readLine() ;
int i = Integer.parseInt(line.trim()) ;
return i ;
}
catch (Exception e)
{
throw new IOException("invalid integer") ;
}
} // end of method readInteger /**
* read the next line from the keyboard as a float
* @return the line as a float
*/
public static float readFloat() throws IOException
{
String line ; BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)) ; try
{
line = in.readLine() ;
return Float.valueOf(line.trim()).floatValue() ;
}
catch (Exception e)
{
throw new IOException("invalid float") ;
}
} // end of method readFloat /**
* read the next line from the keyboard as a double
* @return the line as a double
*/
public static double readDouble() throws IOException
{
String line ; BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)) ; try
{
line = in.readLine() ;
return Double.valueOf(line.trim()).doubleValue() ;
}
catch (Exception e)
{
throw new IOException("invalid double") ;
}
} // end of method readDouble /**
* output an integer in a field of a specified width
* @param n the integer to be output
* @param w the width of the field
*/
public static void writeInteger(int n, int w)
{
Integer n1 = new Integer(n) ;
String t = n1.toString() ; if (t.length() > w)
System.out.print(t) ;
else
{
for (int i = 0 ; i < w - t.length() ; i++)
System.out.print(' ') ;
System.out.print(t) ;
}
} // end of method writeInteger /**
* output a float in a field of a specified width
* @param n the float to be output
* @param w the width of the field
* @param d the number of decimal places
*/
public static void writeFloat(float n, int w, int d) throws IOException
{
Float n1 = new Float(n) ;
String t = n1.toString() ;
//System.err.println("string is **" + t + "**") ; if (t.indexOf('e') != -1)
throw new IOException("out of range") ;
if (d < 1)
throw new IOException("invalid 3rd argument") ;
boolean negative = false ;
if (t.charAt(0) == '-')
{
negative = true ;
t = t.substring(1) ;
}
int index = t.indexOf('.') ;
int decimals ;
if (index == -1)
decimals = 0 ;
else
{
decimals = t.length() - index - 1 ;
if (decimals <= 0)
throw new IOException("funny format") ;
}
if (decimals < d)
{
if (decimals == 0)
t += '.' ;
for (int i = 0 ; i < d - decimals ; i++)
t += '0' ;
}
else if (decimals > d)
{
int offset = t.length() - decimals + d ;
if (t.charAt(offset) < '5')
t = t.substring(0, t.length() - decimals + d) ;
else
{
StringBuffer sb = new StringBuffer(t) ;
offset-- ;
while ((offset >= 0) &&
((sb.charAt(offset) == '9') ||
(sb.charAt(offset) == '.')))
{
if (sb.charAt(offset) == '9')
sb.setCharAt(offset, '0') ;
offset-- ;
}
if (offset >= 0)
{
sb.setCharAt(offset,
Character.forDigit(Character.digit(sb.charAt(offset), 10) + 1, 10)) ;
t = sb.toString().substring(0, t.length() - decimals + d) ;
}
else
t = '1' + sb.toString().substring(0, t.length() - decimals + d) ;
}
}
if (negative)
t = '-' + t ; if (t.length() > w)
System.out.print(t) ;
else
{
for (int i = 0 ; i < w - t.length() ; i++)
System.out.print(' ') ;
System.out.print(t) ;
}
} // end of method writeFloat /**
* output a double in a field of a specified width
* @param n the double to be output
* @param w the width of the field
* @param d the number of decimal places
*/
public static void writeDouble(double n, int w, int d) throws IOException
{
Double n1 = new Double(n) ;
String t = n1.toString() ;
//System.err.println("string is **" + t + "**") ; if (t.indexOf('e') != -1)
throw new IOException("out of range") ;
if (d < 1)
throw new IOException("invalid 3rd argument") ;
boolean negative = false ;
if (t.charAt(0) == '-')
{
negative = true ;
t = t.substring(1) ;
}
int index = t.indexOf('.') ;
int decimals ;
if (index == -1)
decimals = 0 ;
else
{
decimals = t.length() - index - 1 ;
if (decimals <= 0)
throw new IOException("funny format") ;
}
if (decimals < d)
{
if (decimals == 0)
t += '.' ;
for (int i = 0 ; i < d - decimals ; i++)
t += '0' ;
}
else if (decimals > d)
{
int offset = t.length() - decimals + d ;
if (t.charAt(offset) < '5')
t = t.substring(0, t.length() - decimals + d) ;
else
{
StringBuffer sb = new StringBuffer(t) ;
offset-- ;
while ((offset >= 0) &&
((sb.charAt(offset) == '9') ||
(sb.charAt(offset) == '.')))
{
if (sb.charAt(offset) == '9')
sb.setCharAt(offset, '0') ;
offset-- ;
}
if (offset >= 0)
{
sb.setCharAt(offset,
Character.forDigit(Character.digit(sb.charAt(offset), 10) + 1, 10)) ;
t = sb.toString().substring(0, t.length() - decimals + d) ;
}
else
t = '1' + sb.toString().substring(0, t.length() - decimals + d) ;
}
}
if (negative)
t = '-' + t ; if (t.length() > w)
System.out.print(t) ;
else
{
for (int i = 0 ; i < w - t.length() ; i++)
System.out.print(' ') ;
System.out.print(t) ;
}
} // end of method writeDouble } // end of class BasicIo
import java.util.*;
public class OurDate
{
// OurDate Class Constants
/**
* Constant - Month January
*/
public static final int JAN = 1 ;
/**
* Constant - Month February
*/
public static final int FEB = 2 ;
/**
* Constant - Month March
*/
public static final int MAR = 3 ;
/**
* Constant - Month April
*/
public static final int APR = 4 ;
/**
* Constant - Month May
*/
public static final int MAY = 5 ;
/**
* Constant - Month June
*/
public static final int JUN = 6 ;
/**
* Constant - Month July
*/
public static final int JUL = 7 ;
/**
* Constant - Month August
*/
public static final int AUG = 8 ;
/**
* Constant - Month September
*/
public static final int SEP = 9 ;
/**
* Constant - Month October
*/
public static final int OCT = 10 ;
/**
* Constant - Month November
*/
public static final int NOV = 11 ;
/**
* Constant - Month December
*/
public static final int DEC = 12 ; // OurDate Instance Variables private int dayOfMonth, month, year; // OurDate Constructor Methods
/**
* Creates an instance of the OurDate class with default values
* (day, month and year zero)
*/
public OurDate()
{
dayOfMonth = 0;
month = 0;
year = 0;
} // end of constructor method /**
* Creates an instance of the OurDate class with specified attribute
* values
* @param y the year number
* @param m the month number (1 to 12)
* @param d the day number within the month (1 to 31)
*/
public OurDate(int y, int m, int d)
{
dayOfMonth = d;
month = m;
year = y;
} // end of constructor method // OurDate Selector Methods /**
* returns the day within the month attribute of the date
* @return the day within the month attribute of the date
*/
public int getDayOfMonth()
{
return dayOfMonth ;
} // end of method getDayOfMonth /**
* returns the month attribute of the date
* @return the month attribute of the date
*/
public int getMonth()
{
return month ;
} // end of method getMonth /**
* returns the year attribute of the date
* @return the year attribute of the date
*/
public int getYear()
{
return year ;
} // end of method getYear // OurDate Mutator Methods /**
* set the day within the month attribute of the date
* @param d the day within the month attribute of the date
*/
public void setDayOfMonth(int d)
{
dayOfMonth = d;
} // end of method setDayOfMonth /**
* set the month attribute of the date
* @param m the month attribute of the date
*/
public void setMonth(int m)
{
month = m;
} // end of method setMonth /**
* set the year attribute of the date
* @param y the year attribute of the date
*/
public void setYear(int y)
{
year = y;
} // end of method setYear // Other OurDate Methods /**
* find the number of days between two dates
* @param low the earlier date
* @param high the later date
* @return the number of days difference
*/
public static int subtract(OurDate low, OurDate high)
{
/*
given two "OurDates" (where 'low' < 'high') this subtracts (high - low)
and returns the result as an integer (of days)
*/ int lowdoty, highdoty, lowyear, highyear, i, days, daysleft ; days = 0 ;
lowdoty = low.dayOfTheYear() ;
highdoty = high.dayOfTheYear() ;
if (low.year == high.year)
{
days = highdoty - lowdoty ;
return(days) ;
}
lowyear = low.year+1 ;
highyear = high.year-1 ;
for (i = lowyear ; i <= highyear ; i++)
{
days = days + 365 ;
if (leapyear(i) == true) days++ ;
}
daysleft = 365 - lowdoty ;
if (leapyear(low.year) == true) daysleft++ ;
days = days + highdoty + daysleft ;
return(days) ;
} // end of method subtract /**
* return a copy of the date
* @return a copy of the OurDate instance
*/
public OurDate copy()
{
OurDate t ;
t = new OurDate() ;
t.dayOfMonth = dayOfMonth ;
t.month = month ;
t.year = year ;
return t ;
} // end of method copy /**
* return a string representing the date
* @return a string representing the date "day/month/year"
*/
public String toString()
{
return dayOfMonth + "/" + month + "/" + year ;
} // end of method toString /**
* calculate the day number within the year
* @return the day number within the year
*/
private int dayOfTheYear()
{
int days = 0 ;
for (int i = 1 ; i < month ; i++)
switch (i)
{
case SEP : case APR : case JUN : case NOV :
days += 30 ;
break ;
case FEB :
days += 28 ;
if (leapyear(year))
days++ ;
break ;
case JAN : case MAR : case MAY : case JUL :
case AUG : case OCT : case DEC :
days += 31 ;
break ;
}
days += dayOfMonth ;
return days ;
} // end of method dayOfTheYear /**
* find out if the year is a leapyear
* @param y the year in question
* @return true if it is a leapyear, false if not
*/
private static boolean leapyear(int y)
{
return ((y % 400 == 0) ||
((y % 4 == 0) && (y % 100 != 0))) ;
} // end of method leapyear
} // end of class OurDate
这是前面3个程序用到了 两个 类