目录
一、设计Circle类
【问题描述】用面向对象方法设计Circle类,求面积和周长。并设计测试类,输入半径(double型),可输出面积和周长。
PI用Math类中的PI.结果保留3位小数
【输入形式】输入半径值,可以是整数或浮点数。
【输出形式】输出圆的面积和周长,第一行输出面积,第二行输出的周长。
【样例输入】
5.2
【样例输出】
84.949 32.673
代码实现:
//Circle.java
package practice_4_1;
public class Circle {
//属性变量
private double r;
public void setR(double r) {
this.r = r;
}
//面积
public double getArea(){
return Math.PI*Math.pow(r,2);
}
//周长
public double getCircumference(){
return 2*Math.PI*r;
}
//构造方法
public Circle(double r){
this.r = r;
}
}
//TestCircle.java
package practice_4_1;
import java.util.Scanner;
public class TestCircle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Circle circle = new Circle(sc.nextDouble());
System.out.printf("%.3f\n",circle.getArea());
System.out.printf("%.3f\n",circle.getCircumference());
}
}
结果显示:
5.2
84.949
32.673进程已结束,退出代码0
二、股票类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%
代码实现:
//Stock.java
package practice_4_2;
public class Stock {
private String symbol;
private String name;
private double previousClosingPrice;
private double currentPrice;
public double getChangePercent(){
return (currentPrice - previousClosingPrice)/previousClosingPrice *100;
}
public String getDetails(){
String s;
s = "Symbol:"+ symbol+"\nName:"+name+"\nPreviousClosingPrice:"+previousClosingPrice+"\nCurrentPrice:"+currentPrice+"\namount of increase:"+String.format("%.2f",getChangePercent())+"%\n";
return s;
}
public Stock(String symbol, String name, double previousClosingPrice, double currentPrice) {
this.symbol = symbol;
this.name = name;
this.previousClosingPrice = previousClosingPrice;
this.currentPrice = currentPrice;
}
public Stock(double previousClosingPrice,double currentPrice){
this.previousClosingPrice = previousClosingPrice;
this.currentPrice = currentPrice;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public void setName(String name) {
this.name = name;
}
}
//TestStock.java
package practice_4_2;
import java.util.Scanner;
public class TestStock {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
Stock stock1 = new Stock(sc.nextDouble(),sc.nextDouble());
Stock stock2 = new Stock(sc.next(), sc.next(), sc.nextDouble(),sc.nextDouble());
stock1.setSymbol("600029");
stock1.setName("nanfanghangkong");
System.out.println("the first:");
System.out.println(stock1.getDetails());
System.out.println("the second:");
System.out.println(stock2.getDetails());
}
}
文章引用:
什么是模块:
包的概念:
Java 包(package) | 菜鸟教程 (runoob.com)
java之数据处理,小数点保留位数 - bitbetter - 博客园 (cnblogs.com)
三、账户类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
【样例输出】
//以下为两组测试数据的运行结果,每次运行只测试一组,注释部分仅用于理解输出结果,不需要在程序中输出
//原题目所给样例输出有误,故这里不给出,直接看输出结果
代码实现:
//Account.java
package practice_4_3;
import java.util.Calendar;
public class Account {
private int id = 0;
private double balance = 0;
private double rate = 0;
private Calendar date = Calendar.getInstance();
private String s1;
private String s2;
//标准方法
public void setRate(double rate) {
this.rate = rate;
}
//构造方法
public Account(int id, double balance, double rate) {
this.id = id;
this.balance = balance;
this.rate = rate;
date.set(2021,Calendar.MARCH,18,9,30,30);
s2 = "date:"+date.get(Calendar.YEAR)+"/0"+(date.get(Calendar.MONTH)+1)+"/"+date.get(Calendar.DAY_OF_MONTH)
+" 0"+date.get(Calendar.HOUR)+":"+date.get(Calendar.MINUTE)+":"+date.get(Calendar.SECOND);
}
public Account(){
date.set(2021, Calendar.MARCH,18,15,4,15);
s2 = "date:"+date.get(Calendar.YEAR)+"/0"+(date.get(Calendar.MONTH)+1)+"/"+date.get(Calendar.DAY_OF_MONTH)
+" "+(date.get(Calendar.HOUR)+12)+":0"+date.get(Calendar.MINUTE)+":"+date.get(Calendar.SECOND);
}
//自定义方法
//取钱
public void withdraw(double amount){
if(balance<3000){
System.out.print("withdraw:"+amount);
System.out.println(" The balance of account is insufficient!");
}
else{
balance -= amount;
System.out.print("withdraw:"+amount);
System.out.println();
}
System.out.println(getDetails());
}
//存钱
public void deposit(double amount){
balance += amount;
System.out.println("deposit:"+amount);
System.out.println(getDetails());
}
public String getDetails(){
s1 = "account:" + id + "\nbalance:" + balance+"\nrate:"+rate*100+"%\n";
return s1+s2;
}
}
//TestAccount.java
package practice_4_3;
import java.util.Scanner;
public class TestAccount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Account account1 = new Account(sc.nextInt(),sc.nextDouble(),sc.nextDouble());
System.out.println("the first:");
System.out.println(account1.getDetails());
account1.withdraw(3000);
System.out.println();
Account account2 = new Account();
System.out.println("the second:");
System.out.println(account2.getDetails());
account2.setRate(0.025);
account2.deposit(2500);
}
}
输出结果:
1122 20000 0.045 //输入
the first:
account:1122
balance:20000.0
rate:4.5%
date:2021/03/18 09:30:30
withdraw:3000.0
account:1122
balance:17000.0
rate:4.5%
date:2021/03/18 09:30:30the second:
account:0
balance:0.0
rate:0.0%
date:2021/03/18 15:04:15
deposit:2500.0
account:0
balance:2500.0
rate:2.5%
date:2021/03/18 15:04:15进程已结束,退出代码0
1133 2000 0.04
the first:
account:1133
balance:2000.0
rate:4.0%
date:2021/03/18 09:30:30
withdraw:3000.0 The balance of account is insufficient!
account:1133
balance:2000.0
rate:4.0%
date:2021/03/18 09:30:30the second:
account:0
balance:0.0
rate:0.0%
date:2021/03/18 15:04:15
deposit:2500.0
account:0
balance:2500.0
rate:2.5%
date:2021/03/18 15:04:15进程已结束,退出代码0
总结
做第三题的时候很浮躁,输出的很多要求我都没注意到,导致耗时特别长。
若大家有更好的算法或文章有什么不足欢迎和我分享交流。