Java语⾔程序设计与数据结构(基础篇)——梁勇第九章编程练习题答案(⾃
写)
9.1 Rectangle类
package ChapterNine;
// Rectangle类
public class Rectangle {
//数据域
double width =1;
double height =1;
// 构造⽅法
public Rectangle(){
}
public Rectangle(double width,double height){
this.width = width;
this.height = height;
}
// 获取⾯积
工作怠慢的意思public double getArea(){
return width * height;
}
// 获取周长
public double getPerimeter(){
return2* width * height;
}
}
测试类
package ChapterNine;
public class RectangleTest {
public static void main(String[] args){
Rectangle rectangle1 =new Rectangle(4,40);
Rectangle rectangle2 =new Rectangle(3.5,35.9);
System.out.println(rectangle1.width +" "+ rectangle1.height +" "+ Area()+" "
+ Perimeter());
System.out.println(rectangle2.width +" "+ rectangle2.height +" "+ Area()+" "
+ Perimeter());
}
}
9.2 Stock类
//股票类
public class Stock {
// 数据域
String symbol;
String name;
double previousClosingPrice;
double currentPrice;
// 构造⽅法
public Stock(String symbol, String name){
this.symbol = symbol;
this.name = name;
}
// 输⼊previousClosingPrice,currentPrice
public void setPPriceAndCPrice(double previousClosingPrice,double currentPrice){ this.previousClosingPrice = previousClosingPrice;
this.currentPrice = currentPrice;
}
// 获取变化百分⽐
public double getChangePercent(){
return1-(currentPrice / previousClosingPrice);
}
}
测试类
package ChapterNine;
public class StockTest {
public static void main(String[] args){
Stock orachleCorporation =new Stock("ORCL","Oracle Corporation");
orachleCorporation.setPPriceAndCPrice(34.5,34.35);
System.out.ChangePercent()*100+"%");
}
}
9.3 Date类
package ChapterNine;
public class Date {
//时间对象
public static void main(String[] args){
Date date1 =new Date(10000);
System.out.String());
}
}
9.4 Random类
//import java.util.Random;
public class Random {
public static void main(String[] args){
Random random1=new Random();
for(int i=1;i<=50;i++){java经典上机编程题
System.out.Int(100));
}
}
}
9.5 GregorianCalendar类
wps office文本框怎么设置透明
package ChapterNine;
import*;
public class GregorianCalendar {
public static void main(String[] args){
GregorianCalendar date1 =new GregorianCalendar();
int month = (Calendar.MONTH)+1;
System.out.(Calendar.YEAR)+"年"+ month +"⽉"+ (Calendar.DAY_OF_MONTH)+"⽇"); date1.setTimeInMillis(1234567898765L);
System.out.(Calendar.YEAR)+"年"+ (Calendar.MONTH)+"⽉"
+ (Calendar.DAY_OF_MONTH)+"⽇");
}
}
9.6 StopWatch类
package ChapterNine;
public class StopWatch {
// 私有数据域
private Date startTime;
private Date endTime;
// ⽆参构造⽅法
public StopWatch(){
}
// start时间
public void start(){
this.startTime =new Date();
持续集成可以完全实现自动化测试}
// stop时间
public void stop(){
}
// 流逝时间之差
public long getElapsedTime(){
Time()- Time();
}
}
测试类
public class StopWatchTest {
public static void main(String[] args){
StopWatch sw =new StopWatch();
sw.start();
for(int i=1;i<=100000;i++){
for(int j=1;j<=100000;j++){
}
}
sw.stop();
System.out.ElapsedTime());
}
}
9.7 Account类
package ChapterNine;
//Account类
public class Account {
// 私有数据域
private int id =0;
private double balance =0;
private double annualInterestRate =0;
private Date dateCreated;
最简单的javascript代码/
/ ⽆参构造⽅法
public Account(){
dateCreated =new Date();
}
// 带了id和balance的构造⽅法
public Account(int id,double balance){
dateCreated =new Date();
this.id = id;
this.balance = balance;
}
// id balance annualInterestRate的访问器和修改器
public int getId(){
return id;
}
public double getBalance(){
return balance;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
public void setId(int id){
this.id = id;
}
public void setBalance(double balance){
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate){ this.annualInterestRate = annualInterestRate;
js免费特效}
// dateCreated访问器
public String getDateCreated(){
String();
}
// ⽉利率
public double getMonthlyInterestRate(){
return annualInterestRate /12;
}
// ⽉利息
public double getMonthlyInterest(){
return balance * annualInterestRate /12/100;
}
// 取钱
public void withDraw(double money){
this.balance =this.balance - money;
}
/
/ 存钱
public void deposit(double money){
this.balance =this.balance + money;
}
}
测试类
package ChapterNine;
public class AccountTest {
public static void main(String[] args){
Account account1 =new Account(1122,20000);
account1.setAnnualInterestRate(4.5);
account1.withDraw(2500);
account1.deposit(3000);
System.out.println("余额:"+ Balance()+"\n⽉利息:"+ MonthlyInterest()+"\n开户⽇期:" + DateCreated());
}
}
9.8 Fan类
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论