《Java语言程序设计(基础篇)》(第10版梁勇著)
第九章练习题答案
9.1
public class Exercise09_01 {
public static void main(String[] args) {
MyRectangle myRectangle = new MyRectangle(4, 40);
System.out.println("The area of a rectangle with width " +
myRectangle.width + " and height " +
myRectangle.height + " is " +
System.out.println("The perimeter of a rectangle is " +
MyRectangle yourRectangle = new MyRectangle(3.5, 35.9);
System.out.println("The area of a rectangle with width " +
yourRectangle.width + " and height " +
yourRectangle.height + " is " +
System.out.println("The perimeter of a rectangle is " +
}
}
class MyRectangle {
/
/ Data members
double width = 1, height = 1;
// Constructor
public MyRectangle() {
}
// Constructor
public MyRectangle(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}
9.2
public class Exercise09_02 {
public static void main(String[] args) {
Stock stock = new Stock("SUNW", "Sun MicroSystems Inc.");      stock.setPreviousClosingPrice(100);
// Set current price
stock.setCurrentPrice(90);
// Display stock info
System.out.println("Previous Closing Price: " +
System.out.println("Current Price: " +
System.out.println("Price Change: " +
}
}
class Stock {
String symbol;
String name;
double previousClosingPrice;
double currentPrice;
java程序设计基础视频public Stock() {
}
public Stock(String newSymbol, String newName) {
symbol = newSymbol;
name = newName;
}
public double getChangePercent() {
return (currentPrice - previousClosingPrice) /
previousClosingPrice;
}
public double getPreviousClosingPrice() {
return previousClosingPrice;
}
public double getCurrentPrice() {
return currentPrice;
}
public void setCurrentPrice(double newCurrentPrice) {
currentPrice = newCurrentPrice;
}
public void setPreviousClosingPrice(double newPreviousClosingPrice) {    previousClosingPrice = newPreviousClosingPrice;
}
}
9.3
public class Exercise09_03 {
public static void main(String[] args) {
Date date = new Date();
int count = 1;
long time = 10000;
while (count <= 8) {
date.setTime(time);
System.out.String());
count++;
time *= 10;
}
}
}
9.4
public class Exercise09_04 {
public static void main(String[] args) {
Random random = new Random(1000);
for (int i = 0; i < 50; i++)
System.out.Int(100) + " ");
}
9.5
public class Exercise09_05 {
public static void main(String[] args) {
GregorianCalendar calendar = new GregorianCalendar();
System.out.println("Year is " + (GregorianCalendar.YEAR));    System.out.println("Month is " + (GregorianCalendar.MONTH));    System.out.println("Date is " + (GregorianCalendar.DATE));
calendar.setTimeInMillis(1234567898765L);
System.out.println("Year is " + (GregorianCalendar.YEAR));    System.out.println("Month
is " + (GregorianCalendar.MONTH));    System.out.println("Date is " + (GregorianCalendar.DATE));  }
}
9.6
public class Exercise09_06 {
static String output = "";
/** Main method */
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter year
System.out.print("Enter full year (i.e. 2001): ");
int year = Int();
// Prompt the user to enter month
System.out.print("Enter month in number between 1 and 12: ");
int month = Int();
// Print calendar for the month of the year
printMonth(year, month);
System.out.println(output);
}
/** Print the calendar for a month in a year */
static void printMonth(int year, int month) {
// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);
/
/ Get number of days in the month
int numOfDaysInMonth = getNumOfDaysInMonth(year, month);
// Print headings
printMonthTitle(year, month);
// Print body
printMonthBody(startDay, numOfDaysInMonth);
}
/** Get the start day of the first day in a month */
static int getStartDay(int year, int month) {
// Get total number of days since 1/1/1800
int startDay1800 = 3;
long totalNumOfDays = getTotalNumOfDays(year, month);
// Return the start day
return (int)((totalNumOfDays + startDay1800) % 7);
}
/** Get the total number of days since Jan 1, 1800 */
static long getTotalNumOfDays(int year, int month) {
long total = 0;
// Get the total days from 1800 to year -1
for (int i = 1800; i < year; i++)
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;
// Add days from Jan to the month prior to the calendar month for (int i = 1; i < month; i++)
total = total + getNumOfDaysInMonth(year, i);
return total;
}
/** Get the number of days in a month */
static int getNumOfDaysInMonth(int year, int month) {
if (month == 1 || month==3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
return 31;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。