7.1(矩形类Rectangle)编写名为Rectangle的类表示矩形,这个类包括: ? 两个double类型的数据域width和height表示矩形的宽和高,它们的默认值都为1;
? String类型的数据域color表示矩形的颜色,进一步假设所有矩形的颜色都是相同的,默认颜色为白色; ? 无参构造方法创建默认矩形;
? 一个构造方法创建指定width和height的矩形; ? 所有三个数据域的访问器和修改器方法; ? getArea()方法返回该矩形的面积; ? getPerimeter()方法返回它的周长;
编写一个测试程序,创建两个Rectangle对象,设置第一个对象的宽为4,高为40,第二个对象的宽为3.5,高为35.9,所有Rectangle对象的颜色为红色。显示两个对象的属性并求它们的面积和周长。 7.2(风扇类Fan)设计一个名为Fan的类模拟风扇。这个类包括: ? 三个常量SLOW,MEDIUM,和FAST,其值分别为1,2,3,表示风扇的速度;
? int类型的数据域speed表示风扇的速度(默认值为SLOW); ? boolean类型的数据域on表示风扇是否打开(默认值为false); ? double类型的数据域radius表示风扇的半径(默认值为5); ? String类型的数据域color表示风扇的颜色(默认值为blue); ? 无参构造方法创建默认风扇; ? 四个参数的访问器和修改器方法;
? toString()方法返回描述风扇的字符串。如果风扇打开,该方法用一个组合的字符串返回风扇的速度、颜色和半径;否则,用一个组合的字符串和“fan is off”一起返回风扇的颜色和半径。 编写一个测试程序,创建两个Fan对象,将第一个对象设置为最大速度,半径为10,颜色为yellow,打开状态;第二个对象为中等速度,半径为5,颜色为blue,关闭状态。通过调用toString方法显示该对象。
7.3(账户类Account)设计一个名为Account的类模拟账户,它包括: ? int类型数据域id表示帐号(默认值为0);
? double类型数据域balance表示账户余额(默认值为0); ? double类型数据域annualInterestRate存储当前年利率(默认值为0);
? Date型数据域dateCreated存储账户开户的日期; ? 无参构造方法创建一个默认的账户;
? id,balance和annualInterestRate的访问器和修改器; ? dateCreated的访问器;
? getMonthlyInterestRate()方法返回月利率; ? withDraw方法从账户提取特定数额的款; ? deposite方法向账户存特定数额的款;
编写一个测试程序,创建一个帐号为1122、余额为20000、年利率为4.5%的Account对象。使用withDraw方法提款2500美元,使用deposite方法存款3000美元,并打印余额和月利率,以及该账户的开
户日期。
7.4(股票类Stock)设计名为Stock的类模拟股票,该类包括: ? String 类型数据域symbol表示股票代号; ? String类型数据域name表示股票的名称;
? double型数据域previousClosingPrice存储前一天的股票价格; ? double型数据域currentPrice存储当前时间的股票价格; ? 一个构造方法根据指定的股票代号和名称创建股票; ? 所有数据域的访问器方法;
? previousClosingPrice和currentPrice的修改器方法;
? changePercent()方法返回从previousClosingPrice变为currentPrice的百分比;
? 编写一个测试程序创建Stock对象,其股票代号为SUNW、名称为SunMicrosystem Inc.、上期收盘价为100。设置新的当前价为90,显示价格变化百分比。
7.5(使用GregorianCalendar类)Java API在包java.util中有一个GregorianCalendar类,使用它可以得到日期的年月日。它的无参构造方法构建一个当前日期的实例,get(GregorianCalendar.YEAR), get(GregorianCalendar.MONTH) ,get(GregorianCalendar.DAY_OF_MONTH)方法返回年、月和日。编写程序测试这个类,显示当前日期的年月日。
7.6(显示日历)重写类PrintCalendar,在信息对话框中显示日历。因为输出是由该类中多个静态方法产生的,所以,应该定义一个静态String
型变量output存储输出结果,并在信息对话框中显示它。
public class PrintCalendar { /** Main method */
public static void main(String[] args) { // Prompt the user to enter year
String yearString = JOptionPane.showInputDialog( \);
// Convert string into integer
int year = Integer.parseInt(yearString);
// Prompt the user to enter month
String monthString = JOptionPane.showInputDialog( \);
// Convert string into integer
int month = Integer.parseInt(monthString);
// Print calendar for the month of the year printMonth(year, month); }
/** A stub for printMonth may look like this */
public static void printMonth(int year, int month) { System.out.print(month + \ + year); }
/** A stub for printMonthTitle may look like this */
public static void printMonthTitle(int year, int month) { }
/** A stub for getMonthName may look like this */ public static String getMonthName(int month) { return \; // a dummy value }
/** A stub for getMonthNmae may look like this */ public static int getStartDay(int year, int month) { return 1; // a dummy value }
/** A stub for getNumberOfDaysInMonth may look like this */
public static int getNumberOfDaysInMonth(int year, int month) {
return 31; // a dummy value }
/** A stub for getTotalNumberOfDays may look like this */
public static int getTotalNumberOfDays(int year, int month) { return 10000; // a dummy value }
/** A stub for getTotalNumberOfDays may look like this */ public static boolean isLeapYear(int year) { return true; // a dummy value } }
7.7(时间类Time)设计名为Time的类。该类包含: ? 数据域hour、minute和second表示时间;
? 无参构造方法为当前时间创建Time对象;(数据域的值表示当前的时间)
? 一个构造方法在指定以毫秒表示的、从1970年1月1日午夜开始已逝去的时间时,创建一个Time对象;(数据域的值表示该时间) ? 三个get方法分别获得数据域hour,minute,和second;
编写测试程序创建两个Time对象(使用new Tme()和new Time(5555500000)),并显示它们的小时、分钟和秒。
提示 无参构造方法可以使用当前时间,当前时间可以通过System.currentTimeMillis()获得。另外一个构造方法为指定逝去的时间设置小时、分钟和秒的值。例如,如果逝去的时间为5555500000毫秒,那么小时为10,分钟为19,秒为10; 7.8(课程类Course)在程序清单
1 public class Course { 2 private String name;
3 private String[] students = new String[100]