Java语⾔程序设计与数据结构(基础篇)课后练习题第九章9.1
package demo;
public class dijiuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle r1 = new Rectangle(4, 40);
Rectangle r2 = new Rectangle(3.5, 35.9);
System.out.println("r1 " + r1.getWidth() + " " + r1.getHeight() + " " + r1.getArea() + " " + r1.getPerimeter());
System.out.println("r2 " + r2.getWidth() + " " + r2.getHeight() + " " + r2.getArea() + " " + r2.getPerimeter());
}
}
class Rectangle {
private double width;
private double height;
public Rectangle() {
this(1, 1);
}
public Rectangle(double w, double h) {
this.width = w;
this.height = h;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width * height);
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
}
9.2
package demo;
public class dijiuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
Stock stock = new Stock("ORCL","Oracle Corporation");
stock.setPreviousClosingPrice(34.5);
stock.setCurrentPrice(34.35);
System.out.println("The change percent is "+ChangePercent()+"%");
}
}
class Stock {
private String symbol;
private String name;
java程序设计基础视频private double previousClosingPrice;
private double currentPrice;
Stock(String symbol,String name){
this.symbol = symbol;
this.name = name;
}
public void setPreviousClosingPrice(double previousClosingPrice){
this.previousClosingPrice = previousClosingPrice;
}
public void setCurrentPrice(double price){
this.currentPrice = price;
}
public double getChangePercent(){
return (currentPrice-previousClosingPrice)/previousClosingPrice*100; }
}
9.3
package demo;
public class dijiuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
long seconds = 10000;
for(int i=0;i<8;i++){
java.util.Date date = new java.util.Date(seconds);
System.out.String());
seconds *= 10;
}
}
}
9.4
package demo;
public class dijiuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Random random = new java.util.Random(1000);
for(int i=0;i<50;i++)
System.out.Int(100)+" ");
}
}
9.5
package demo;
import java.util.GregorianCalendar;
public class dijiuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
GregorianCalendar g = new GregorianCalendar();
System.out.(GregorianCalendar.YEAR)+"/"+g.get(GregorianCalendar.MONTH)+"/"+g.get(GregorianCalendar.DAY_OF_MONTH));
g.setTimeInMillis(1234567898765L);
System.out.(GregorianCalendar.YEAR)+"/"+g.get(GregorianCalendar.MONTH)+"/"+g.get(GregorianCalendar.DAY_OF_MONTH)); }
}
9.6
package demo;
public class dijiuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
StopWatch time = new StopWatch();
int[] array = new int[100000];
for (int i = 0; i < 100000; i++) {
array[i] = (int) (Math.random() * 1000000);
}
time.start();
for (int i = 0; i < array.length - 1; i++) {
int currentMin = array[i];
int currentMinIndex = i;
for (int j = i + 1; j < array.length; j++) {
if (currentMin > array[j]) {
currentMin = array[j];
currentMinIndex = j;
}
}
if (currentMinIndex != i) {
array[currentMinIndex] = array[i];
array[i] = currentMin;
}
}
time.stop();
System.out.println("timeMill: "+ElapsedTime()); //这⾥⽣成的是毫秒。
}
}
class StopWatch{
private long startTime;
private long endTime;
StopWatch(){
this.startTime = System.currentTimeMillis();
}
public void start(){
this.startTime = System.currentTimeMillis();
}
public void stop(){
}
public long getElapsedTime(){
dTime-this.startTime;
}
}
9.7
package demo;
import java.util.Date;
public class dijiuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
Account s = new Account(1122,2000);
s.setAnnualInterestRate(4.5/100);
s.withDraw(2500);
s.deposit(3000);
System.out.println("Balance: "+s.getBalance());
System.out.println("Monthly Interest: "+s.getMonthlyInterest());
System.out.println("Register Date: "+s.getDateCreated().toString()); }
}
class Account{
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
public Account(){
id=0;
balance=0;
annualInterestRate=0;
dateCreated=new Date();
}
public Account(int di,double b){
id=di;
balance=b;
annualInterestRate=0;
dateCreated=new Date();
}
public int getId(){
return id;
}
public void setId(int j){
id=j;
}
public double getBalance(){
return balance;
}
public void setBalance(double j){
balance=j;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
public void setAnnualInterestRate(double j){        annualInterestRate=j;
}
public Date getDateCreated(){
return dateCreated;
}
public double getMonthlyInterestRate(){
return annualInterestRate/12;
}
public double getMonthlyInterest(){
return annualInterestRate/12*balance;    }
public void withDraw(double m){
balance -= m;
}
public void deposit(double m){
balance += m;
}
}
9.8
package demo;
public class dijiuzhang {

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