题目集7~9的总结性Blog
- 7-1 图形卡片排序游戏
掌握类的继承、多态性使用方法以及接口的应用。
输入格式:
- 首先,在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如: 1 3 4 2 1 3 4 2 1 3 0
- 然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
输入样例:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5>
输出样例
The original list:
Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02
The sorted list:
Circle:98.52 Triangle:4.02 Rectangle:3.22 Trapezoid:1.14
Sum of area:106.91 >
继承与多态 核心代码展示
class Shape {
private String shapeName;
Shape(){
}
Shape(String shapeName){
this.shapeName=shapeName;
}
public String getShapeName() {
return shapeName;
}
public void setShapeName(String shapeName) {
this.shapeName=shapeName;
}
public double getArea() {
return 0.0;
}
public boolean vaildate() {
return true;
}
}
class Circle extends Shape{
private double radius;
Circle(){
}
Circle(double radius){
this.radius=radius;
}
public double getArea() {
return Math.PI*radius*radius;
}
public boolean vaildate() {
if(radius>0)
return true;
else return false;
}
}
class Rectangle extends Shape{
private double width,length;
Rectangle (double width,double length){
this.width=width;
this.length=length;
}
public double getArea() {
return width*length;
}
public boolean vaildate() {
if(width>0&&length>0)
return true;
else return false;
}
}
class Triangle extends Shape{
double side1,side2,side3;
Triangle(double side1,double side2,double side3){
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
public double getArea() {
double c=(side1+side2+side3)/2;
double s=Math.sqrt(c(c-side1)(c-side2)*(c-side3));
return s;
}
public boolean vaildate() {
if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1)
return true;
else
return false;
}
}
class Traperoid extends Shape{
private double topSide,bottomSide,height;
Traperoid(){
}
Traperoid(double topSide,double bottomSide,double height){
this.bottomSide=bottomSide;
this.height=height;
this.topSide=topSide;
}
public double getArea() {
return (topSide+bottomSide)*height/2;
}
public boolean validate() {
if(topSide>0&&bottomSide>0&&height>0)
return true;
else return false;
}
}
- 7-1 ATM机类结构设计(一)
- 输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
存款、取款功能输入数据格式: 卡号 密码 ATM机编号 金额(由一个或多个空格分隔), 其中,当金额大于0时,代表取款,否则代表存款。
查询余额功能输入数据格式: 卡号
- 输出格式:
①输入错误处理
如果输入卡号不存在,则输出Sorry,this card does not exist.。
如果输入ATM机编号不存在,则输出Sorry,the ATM's id is wrong.。
如果输入银行卡密码错误,则输出Sorry,your password is wrong.。
如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.。
如果检测为跨行存取款,则输出Sorry,cross-bank withdrawal is not supported.。
代码展示
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str = "";
List<String>str_list=new ArrayList<String>();//存储数据
while(!str.equals("#")){
str=input.next();
str_list.add(str);
}
String num1="6217000010041315709";
String num2="6217000010041315715";
String num3="6217000010041315718";
String num4="6217000010051320007";
String num5="6222081502001312389";
String num6="6222081502001312390";
String num7="6222081502001312399";
String num8="6222081502001312400";
String num9="6222081502051320785";
String num10="6222081502051320786";
String sum1="01";
String sum2="02";
String sum3="03";
String sum4="04";
String sum5="05";
String sum6="06";
String passage1="88888888";
String Chushi="10000";
String card1 = str_list.get(0);
String passage = str_list.get(1);
String ATM=str_list.get(2);
String money=str_list.get(3);
float realmoney = Float.parseFloat(str_list.get(3));
if(!card1.equals(num1)&&!card1.equals(num2)&&!card1.equals(num3)&&!card1.equals(num4)&&!card1.equals(num5)
&&!card1.equals(num6)&&!card1.equals(num7)&&!card1.equals(num8)&&!card1.equals(num9)&&!card1.equals(num10))
{
System.out.print("Sorry,this card does not exist.");
}
else if(!ATM.equals(sum1)&&!ATM.equals(sum2)&&!ATM.equals(sum3)&&!ATM.equals(sum4)&&!ATM.equals(sum5)&&!ATM.equals(sum6)){
System.out.print("Sorry,the ATM's id is wrong.");
}
else if(!passage.equals(passage1)){
System.out.print("Sorry,your password is wrong.");
}
else if(realmoney>10000){
System.out.print("Sorry,your account balance is insufficient.");
}
else if(card1.equals(num1)||card1.equals(num2)||card1.equals(num3)){
String Name="杨过";
String Bank = null;
if(ATM.equals(sum1)||ATM.equals(sum2)||ATM.equals(sum3)||ATM.equals(sum4)){
Bank="中国建设银行";
}else if(ATM.equals(sum5)||ATM.equals(sum6)){
System.out.print("Sorry,cross-bank withdrawal is not supported.");
return;
}
if(realmoney<=0){
float money1;
money1=10000-realmoney;
realmoney=-realmoney;
System.out.print(Name+"在"+Bank+"的"+ATM+"号ATM机上存款¥"+String.format("%.2f",realmoney)+"\n"+"当前余额为¥"+String.format("%.2f", money1));
}
else if(realmoney>=0&&realmoney<=10000){
float money2;
money2=10000-realmoney;
System.out.print(Name+"在"+Bank+"的"+ATM+"号ATM机上取款¥"+String.format("%.2f", realmoney)+"\n"+"当前余额为¥"+String.format("%.2f", money2));
}
}
else if(card1.equals(num4)){
String Name="郭靖";
String Bank = null;
if(ATM.equals(sum1)||ATM.equals(sum2)||ATM.equals(sum3)||ATM.equals(sum4)){
Bank="中国建设银行";
}else if(ATM.equals(sum5)||ATM.equals(sum6)){
System.out.print("Sorry,cross-bank withdrawal is not supported.");
return;
}
if(realmoney<=0){
float money1;
money1=10000-realmoney;
realmoney=-realmoney;
System.out.print(Name+"在"+Bank+"的"+ATM+"号ATM机上存款¥"+String.format("%.2f",realmoney)+"\n"+"当前余额为¥"+String.format("%.2f", money1));
}
else if(realmoney>=0&&realmoney<=10000){
float money2;
money2=10000-realmoney;
System.out.print(Name+"在"+Bank+"的"+ATM+"号ATM机上取款¥"+String.format("%.2f", realmoney)+"\n"+"当前余额为¥"+String.format("%.2f", money2));
}
}
else if(card1.equals(num5)||card1.equals(num6)||card1.equals(num7)||card1.equals(num8)){
String Name="张无忌";
String Bank = null;
if(ATM.equals(sum1)||ATM.equals(sum2)||ATM.equals(sum3)||ATM.equals(sum4)){
System.out.print("Sorry,cross-bank withdrawal is not supported.");
return;
}else if(ATM.equals(sum5)||ATM.equals(sum6)){
Bank="中国工商银行";
}
if(realmoney<=0){
float money1;
money1=10000-realmoney;
realmoney=-realmoney;
System.out.print(Name+"在"+Bank+"的"+ATM+"号ATM机上存款¥"+String.format("%.2f",realmoney)+"\n"+"当前余额为¥"+String.format("%.2f", money1));
}
else if(realmoney>=0&&realmoney<=10000){
float money2;
money2=10000-realmoney;
System.out.print(Name+"在"+Bank+"的"+ATM+"号ATM机上取款¥"+String.format("%.2f", realmoney)+"\n"+"当前余额为¥"+String.format("%.2f", money2));
}
}
else if(card1.equals(num9)||card1.equals(num10)){
String Name="韦小宝";
String Bank = null;
if(ATM.equals(sum1)||ATM.equals(sum2)||ATM.equals(sum3)||ATM.equals(sum4)){
System.out.print("Sorry,cross-bank withdrawal is not supported.");
return;
}else if(ATM.equals(sum5)||ATM.equals(sum6)){
Bank="中国工商银行";
}
if(realmoney<=0){
float money1;
money1=10000-realmoney;
realmoney=-realmoney;
System.out.print(Name+"在"+Bank+"的"+ATM+"号ATM机上存款¥"+String.format("%.2f",realmoney)+"\n"+"当前余额为¥"+String.format("%.2f", money1));
}
else if(realmoney>=0&&realmoney<=10000){
float money2;
money2=10000-realmoney;
System.out.print(Name+"在"+Bank+"的"+ATM+"号ATM机上取款¥"+String.format("%.2f", realmoney)+"\n"+"当前余额为¥"+String.format("%.2f", money2));
}
}
}
}