习题一: 设计Circle类
【问题描述】用面向对象方法设计Circle类,求面积和周长。并设计测试类,输入半径(double型),可输出面积和周长。
PI用Math类中的PI.结果保留3位小数
【输入形式】输入半径值,可以是整数或浮点数。
【输出形式】输出圆的面积和周长,第一行输出面积,第二行输出的周长。
【样例输入】
5.2
【样例输出】
84.949
32.673
运行结果:
源码:
package circle;
import java.util.Scanner;
public class Cricle {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
double input = in.nextDouble();
Cricle_Cri.show(input);
in.close();
}
}
class Cricle_Cri{
public static double area(double R) {
return R*R*Math.PI;
}
public static double perimeter(double R) {
return 2*R*Math.PI;
}
public static void show(double R) {
System.out.printf("%.3f",area(R));
System.out.println();
System.out.printf("%.3f",perimeter(R));
}
}
习题二: 股票类Stock及其测试类
【问题描述】
设计一个Stock类,这个类包括:
(1)成员变量:
股票代码:symbol,字符串数据域
股票名称:name,字符串数据域
昨日收盘价:previousClosingPrice,double型数据域
当前价:currentPrice,double型数据域
(2)方法:
getChangePercent():返回从previousClosingPrice变化到currentPrice的百分比
getDetails():返回由股票信息组成的字符串
根据创建对象的要求编写相应的构造方法。
编写测试类,创建两支股票,第一支股票的信息为:“600029”、“nanfanghangkong”,昨日收盘价、当前价由键盘输入;第二支股票信息全部由键盘输入。
输出两支股票信息,并输出今日涨幅(百分比),保留两位小数。
【输入形式】
第一支股票的昨日收盘价和当前价
第二支股票的代码、名称、昨日收盘价和当前价
【输出形式】
第一支股票的信息
第一支股票的涨幅
第二支股票的信息
第二支股票的涨幅
【样例输入】
10.61 10.21
600085 tongrentang 37.41 39.24
【样例输出】
the first:
Symbol:600029
Name:nanfanghangkong
PreviousClosingPrice:10.61
CurrentPrice:10.21
amount of increase:-3.77%
the second:
Symbol:600085
Name:tongrentang
PreviousClosingPrice:37.41
CurrentPrice:39.24
amount of increase:4.89%
运行结果:
源码:
package share_cer;
import java.text.NumberFormat;
import java.util.Scanner;
public class Share_Cer {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
double previousClosingPrice = in.nextDouble();
double currentPrice = in.nextDouble();
String symble = in.next();
String name = in.next();
double previousClosingPrice1 = in.nextDouble();
double currentPrice1 = in.nextDouble();
Stock.setPreviousClosingPrice(previousClosingPrice);
Stock.setCurrentPrice(currentPrice);
System.out.println("the first:");
Stock.getDetails();
System.out.println("\n");
Stock input1 = new Stock(symble,name,previousClosingPrice1,currentPrice1);
System.out.println("the second:");
Stock.getDetails();
in.close();
}
}
class Stock{
private static String symble = "600029";
private static String name = "nanfanghangkong";
private static double previousClosingPrice;
private static double currentPrice;
public static double getChangePercent() {
return (currentPrice-previousClosingPrice)/previousClosingPrice;
}
public static void getDetails() {
NumberFormat n = NumberFormat.getPercentInstance();
n.setMinimumFractionDigits(2);
System.out.print("Symbol:"+symble+"\n"+"Name:"+name+"\n"+
"PreviousClosingPrice:"+previousClosingPrice+"\n"+
"CurrentPrice:"+currentPrice+"\n"+"amount of increase:"+
n.format(getChangePercent()));
}
public static double getPreviousClosingPrice() {
return previousClosingPrice;
}
public static void setPreviousClosingPrice(double previousClosingPrice) {
Stock.previousClosingPrice = previousClosingPrice;
}
public static double getCurrentPrice() {
return currentPrice;
}
public static void setCurrentPrice(double currentPrice) {
Stock.currentPrice = currentPrice;
}
public Stock(String symble,String name,double previousClosingPrice,double currentPrice) {
Stock.symble = symble;
Stock.name = name;
Stock.previousClosingPrice = previousClosingPrice;
Stock.currentPrice = currentPrice;
}
}
习题三:账户类Account及测试类
【问题描述】
设计一个账户类Account,它包括:
这两类方法也需要定义,不在类图中反映:(1)无参构造方法(默认值:id:0, balalce:0, rate:0,
date:1970年1月1日0时0分0秒),带参构造方法;(2)标准方法(即 getters/setters方法)
设计测试类,(1)输入账号、余额、年利率,开户日期为2021年3月18日9时30分30秒,以这组数据创建Account类对象,输出该账户信息,取出3000元,输出取款信息,若取款时若账户余额不足,提示“The balance of account is insufficient!”,输出操作后的账户信息。
(2)调用无参构造方法创建第二个Account对象,输出账户信息。将年利率修改为2.5%,开户日期修改为2021年3月18日15时04分10秒。存入2500元,输出存款信息及账户信息。
提示:
Calendar类不能实例化,使用getInstance()获得实例,使用set(int,int,int,int,int,int)设置日期时间;使用获get(Calendar.XXX)获得日期时间,详细资料查阅java API或相关博客。Calendar中月份用0-11表示1-12月。在使用Calendar.HOUR_OF_DAY)对应的24小时制的时间。
【输入形式】账号 余额 年利率
【输出形式】见样例
【样例输入】
//以下为两组测试数据,每次运行只测试一组
1122 20000 0.045
1133 2000 0.04
【样例输出】
//以下为两组测试数据的运行结果,每次运行只测试一组,注释部分仅用于理解输出结果,不需要在程序中输出
//第一组测试结果
the first:
account:1122
balance:20000.0
rate:4.5%
date:2021/03/18 9:30:30
withdraw:3000.0
account:1122
balance:17000.0
rate:4.5%
date:2021/03/18 9:30:30
the second:
account:0
balance:0.0
rate:0.0%
date:2021/03/18 15:4:15
deposit:2500.0
account:0
balance:2500.0
rate:2.5%
date:2021/03/18 15:4:15
//第二组测试结果
the first:
account:1133
balance:2000.0
rate:4.0%
date:2021/03/18 9:30:30
withdraw:3000.0 The balance of account is insufficient!
account:1133
balance:2000.0
rate:4.0%
date:2021/03/18 9:30:30
the second:
account:0
balance:0.0
rate:0.0%
date:2021/03/18 15:4:15
deposit:2500.0
account:0
balance:2500.0
rate:2.5%
date:2021/03/18 15:4:15
运行结果:
源码:
/*
* 作者:Survivor
* 功能:银行账户管理系统
* 日期:5.15
*/
package person_account;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class Person_Account {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int account = in.nextInt();
double balance = in.nextDouble();
double rate = in.nextDouble();
System.out.println("the first:");
Acount a1 = new Acount(account, balance, rate);//引用带参构造方法
a1.show(a1.first);
a1.withdraw(3000.0);//取出3000元
if(a1.tap == 1)//用于判断是否输出超额提示
System.out.println("withdraw:3000.0 The balance of account is insufficient!");
else
System.out.println("withdraw:3000.0");
a1.show(a1.first);
System.out.println();
System.out.println("the second:");
Acount a2 = new Acount();//引用无参构造方法
a2.show(a2.second);
a2.deposit(2500.0,0.025);//存储2500元,并将年利率修改为2.5%
System.out.println("deposit:2500.0");//显示存储金额
a2.show(a2.second);
in.close();
}
}
class Acount {
private int id;
private double balance;
private double rate;
private Calendar data;
String second;
String first;
int tap = 0;//tap用于标识是否取款金额超额,初始值为0,若在问题域类withdraw方法中判断出已超额
//则将tap赋值为1,并在主类中输出超额提醒,表明取款失败
public String Cale(int h, int m, int s) {//Calendar方法,用于自定义所需时间
data = Calendar.getInstance();
data.set(Calendar.YEAR, 2021);
data.set(Calendar.MONTH, Calendar.MARCH);
data.set(Calendar.DAY_OF_MONTH, 18);
data.set(Calendar.HOUR_OF_DAY, h);
data.set(Calendar.MINUTE, m);
data.set(Calendar.SECOND, s);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//定义时间输出格式
String strDate = sdf.format(data.getTime());
return strDate;
}
public void withdraw(double money) {//取款方法
if (balance>money)//判断取款金额是否超额
this.balance = balance - money;
else
tap = 1;
}
public void deposit(double money,double rate) {//存款方法
this.balance = balance + money;
this.rate = rate;//修改年利润
}
public void show(String time) {//显示方法
System.out.println(getDetails() + time);
}
public String getDetails() {//生成字符串方法
NumberFormat n = NumberFormat.getPercentInstance();
n.setMinimumFractionDigits(1);
return "account:" + id + "\n" + "balance:" + balance + "\n" + "rate:" + n.format(rate) + "\n" + "date:";
}
public Acount() {//无参构造
this.id = 0;
this.balance = 0.0;
this.rate = 0;
second = Cale(15, 4, 15);
}
public Acount(int id, double balance, double rate) {//带参构造
this.id = id;
this.balance = balance;
this.rate = rate;
first = Cale(9, 30, 30);
}
}
END:you believe,you deserve!