Java语言程序设计-基础篇-第八版-编程练习题-第八章

时间:2022-07-01 01:55:56
package 编程练习题chapter8;

import java.util.GregorianCalendar;

public class Exercise8_5 {
    public static void main(String[] args) {
        GregorianCalendar calendar = new GregorianCalendar();
        System.out.println("Year is " + calendar.get(GregorianCalendar.YEAR));
        System.out.println("Month is " + calendar.get(GregorianCalendar.MONTH));
        System.out.println("Date is " + calendar.get(GregorianCalendar.DATE));
        
        calendar.setTimeInMillis(1234567898765L);
        System.out.println("Year is " + calendar.get(GregorianCalendar.YEAR));
        System.out.println("Month is " + calendar.get(GregorianCalendar.MONTH));
        System.out.println("Date is " + calendar.get(GregorianCalendar.DATE));
    }
}
package 编程练习题chapter8;

import javax.swing.JOptionPane;

public class Exercise8_6 {
    static String output = "";

    public static void main(String[] args) {
        String yearString = JOptionPane.showInputDialog(null,
                "Enter full year (i.e. 2001):", "Enter Year",
                JOptionPane.QUESTION_MESSAGE);

        int year = Integer.parseInt(yearString);

        String monthString = JOptionPane.showInputDialog(null,
                "Enter month in number between 1 and 12:", "Enter Month",
                JOptionPane.QUESTION_MESSAGE);

        int month = Integer.parseInt(monthString);

        printMonth(year, month);

        JOptionPane.showMessageDialog(null, output);
    }

    static void printMonth(int year, int month) {

        int startDay = getStartDay(year, month);

        int numOfDaysInMonth = getNumOfDaysInMonth(year, month);

        printMonthTitle(year, month);

        printMonthBody(startDay, numOfDaysInMonth);
    }

    static int getStartDay(int year, int month) {
        int startDay1800 = 3;
        long totalNumOfDays = getTotalNumOfDays(year, month);

        return (int) ((totalNumOfDays + startDay1800) % 7);
    }

    static long getTotalNumOfDays(int year, int month) {
        long total = 0;
        for (int i = 1800; i < year; i++)
            if (isLeapYear(i))
                total = total + 366;
            else
                total = total + 365;

        for (int i = 1; i < month; i++)
            total = total + getNumOfDaysInMonth(year, i);

        return total;
    }

    static int getNumOfDaysInMonth(int year, int month) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
                || month == 10 || month == 12)
            return 31;

        if (month == 4 || month == 6 || month == 9 || month == 11)
            return 30;

        if (month == 2)
            if (isLeapYear(year))
                return 29;
            else
                return 28;

        return 0;
    }

    static boolean isLeapYear(int year) {
        if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
            return true;

        return false;
    }

    static void printMonthBody(int startDay, int numOfDaysInMonth) {

        int i = 0;
        for (i = 0; i < startDay; i++)
            output += "    ";

        for (i = 1; i <= numOfDaysInMonth; i++) {
            if (i < 10)
                output += "   " + i;
            else
                output += "  " + i;

            if ((i + startDay) % 7 == 0)
                output += "\n";
        }

        output += "\n";
    }

    /** Print the month title, i.e. May, 1999 */
    static void printMonthTitle(int year, int month) {
        output += "         " + getMonthName(month) + ", " + year + "\n";
        output += "------------------------------------\n";
        output += " Sun Mon Tue Wed Thu Fri Sat\n";
    }

    static String getMonthName(int month) {
        String monthName = null;
        switch (month) {
        case 1:
            monthName = "January";
            break;
        case 2:
            monthName = "February";
            break;
        case 3:
            monthName = "March";
            break;
        case 4:
            monthName = "April";
            break;
        case 5:
            monthName = "May";
            break;
        case 6:
            monthName = "June";
            break;
        case 7:
            monthName = "July";
            break;
        case 8:
            monthName = "August";
            break;
        case 9:
            monthName = "September";
            break;
        case 10:
            monthName = "October";
            break;
        case 11:
            monthName = "November";
            break;
        case 12:
            monthName = "December";
        }

        return monthName;
    }
}
package 编程练习题chapter8;

public class Exercise8_7 {
    public static void main(String[] args) {
        Account account = new Account(1120, 20000);
        Account.setAnnualInterestRate(4.5);
        account.withdraw(2500);
        account.deposit(3000);
        System.out.println("Balance is " + account.getBalance());
        System.out.println("Monthly interest is "
                + account.getMonthlyTnterest());
        System.out.println("This account was created at "
                + account.getDateCreated());
    }
}

class Account {
    private int id;
    private double balance;
    private static double annualInterestRate;
    private java.util.Date dateCreated;

    public Account() {
        dateCreated = new java.util.Date();
    }

    public Account(int newId, double newBalance) {
        id = newId;
        balance = newBalance;
        dateCreated = new java.util.Date();
    }

    public int getId() {
        return this.id;
    }

    public double getBalance() {
        return balance;
    }

    public static double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setId(int newId) {
        id = newId;
    }

    public void setBalance(double newBalance) {
        balance = newBalance;
    }

    public static void setAnnualInterestRate(double newAnnualInterestRate) {
        annualInterestRate = newAnnualInterestRate;
    }

    public double getMonthlyTnterest() {
        return balance * (annualInterestRate / 1200);
    }

    public java.util.Date getDateCreated() {
        return dateCreated;
    }

    public void withdraw(double amount) {
        balance -= amount;
    }

    public void deposit(double amount) {
        balance += amount;
    }
}
package 编程练习题chapter8;

public class Exercise8_8 {
    public static void main(String[] args) {
        Fan1 fan1 = new Fan1();
        fan1.setSpeed(Fan1.SLOW);
        fan1.setRadius(10);
        fan1.setColor("yellow");
        fan1.setOn(true);
        System.out.println(fan1.toString());

        
        Fan1 fan2 = new Fan1();
        fan2.setSpeed(Fan1.MEDIUM);
        fan2.setRadius(5);
        fan2.setColor("blue");
        fan2.setOn(false);
        System.out.println(fan2.toString());
    }
}

class Fan1 {
    public static int SLOW = 1;
    public static int MEDIUM = 2;
    public static int FAST = 3;

    private int speed = SLOW;
    private boolean on = false;
    private double radius = 5;
    private String color = "white";

    public Fan1() {
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int newSpeed) {
        speed = newSpeed;
    }

    public boolean isOn() {
        return on;
    }

    public void setOn(boolean trueOrFalse) {
        this.on = trueOrFalse;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double newRadius) {
        radius = newRadius;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String newColor) {
        color = newColor;
    }

    public String toString() {
        return "speed " + speed + "\n" + "color " + color + "\n"
                + ((on) ? "fan is on" : "fan is off");
    }
}

 

package 编程练习题chapter8;

import java.util.Random;

public class Exercise8_4 {
    public static void main(String[] args) {
        Random random = new Random(1000);

        for (int i = 0; i < 50; i++)
            System.out.println(random.nextInt(100) + " ");
    }
}

 

package 编程练习题chapter8;

public class Exercise8_2 {
    public static void main(String[] args) {
        Stock stock = new Stock("JAVA", "Sun MicroSystems Inc.");
        stock.previousClosingPrice = 4.5;
        stock.currentPrice = 4.35;

        System.out.println("Previous Closing Price: "
                + stock.previousClosingPrice);
        System.out.println("Current Price: " + stock.currentPrice);
        System.out
                .println("Price Change: " + stock.changePercent() * 100 + "%");
    }
}

class Stock {
    String symbol;
    String name;
    double previousClosingPrice;
    double currentPrice;

    public Stock() {
    }

    public Stock(String newSymbol, String newName) {
        symbol = newSymbol;
        name = newName;
    }

    public double changePercent() {
        return (currentPrice - previousClosingPrice) / previousClosingPrice;
    }
}
package 编程练习题chapter8;

import java.util.Date;

public class Exercise8_3 {
    public static void main(String[] args) {
        Date date = new Date();

        int count = 1;
        long time = 10000;

        while (count < 10) {
            date.setTime(time);
            System.out.println(date.toString());
            count++;
            time *= 10;
        }
    }
}

 

package 编程练习题chapter8;

public class Exercise8_1 {
    public static void main(String[] args) {
        Rectangle myRectangle = new Rectangle(4,40);
        System.out.println("The area of rectangle with width " + 
          myRectangle.width + " and height " + myRectangle.height + " is " 
          + myRectangle.getArea());
        System.out.println("The perimeter of a rectangle is " + myRectangle.getPerimeter());
        
        Rectangle yourRectangle = new Rectangle(3.5, 35.9);
        System.out.println("The area of rectangle with width " + 
          yourRectangle.width + " and height " + yourRectangle.height + " is " 
          + yourRectangle.getArea());
        System.out.println("The perimeter of a rectangle is " + yourRectangle.getPerimeter());        
    }
}

class Rectangle {
    double width = 1, height = 1;
    
    public Rectangle(){
    }
    public Rectangle(double newWidth, double newHeight) {
        width = newWidth;
        height = newHeight;
    }
    public double getArea() {
        return width * height;
    }
    public double getPerimeter() {
        return 2 * (width + height);
    }
}