博客作业三20201618
+前言
这三次题目集需要我们掌握类的继承、多态性使用方法以及接口的应用。
编写一个银行 ATM 机的模拟程序,能够完成用户的存款、取款以及查询余额功能。
题量虽然不大,但同样要花费许多时间。难度上七题适中,而八九题是连续性题目工作量更大而且要考虑可改进性。难度要大一些。
设计与分析
第七题
· 首先,在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如: 1 3 4 2 1 3 4 2 1 3 0
· 然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔
实验源码
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
ArrayList list = new ArrayList();
int num = input.nextInt();
while(num != 0){
if(num < 0 || num > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(num);
num = input.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(!dealCardList.validate()){
System.out.println("Wrong Format");
System.exit(0);
}
dealCardList.showResult();
input.close();
}
}
class asssse{
int c=28;
}
class apppsse{
int sa=558;
}
class iuwsse{
int x=8;
}
class oiouswsse{
int kiu=256;
}
class asrrtuse{
int skf=8;
}
class hhgu{
int iok=50;
}
class hhtgu{
int iokj=55;
}
class Card{
Shape shape;
Card(){
}
Card(Shape shape){
this.shape=shape;
}
public Shape getShape() {
return shape;
}
public void setShape(Shape Shape) {
this.shape=shape;
}
}
class DealCardList{
ArrayList<Card> cardList=new ArrayList<Card>();
DealCardList(){
}
DealCardList(ArrayList list){
for(int i=0;i<list.size();i++)
{
if((int)list.get(i)==1)
{
double r=Main.input.nextDouble();
Circle circle=new Circle(r);
Card card=new Card(circle);
card.getShape().setShapeName("Circle");
cardList.add(card);
}
if((int)list.get(i)==2) {
double a=Main.input.nextDouble();
double b=Main.input.nextDouble();
Rectangle rectangle=new Rectangle(a,b);
Card card=new Card(rectangle);
card.getShape().setShapeName("Rectangle");
cardList.add(card);
}
if((int)list.get(i)==3) {
double a=Main.input.nextDouble();
double b=Main.input.nextDouble();
double c=Main.input.nextDouble();
Triangle triangle=new Triangle(a,b,c);
Card card=new Card(triangle);
card.getShape().setShapeName("Triangle");
cardList.add(card);
}
if((int)list.get(i)==4) {
double a=Main.input.nextDouble();
double b=Main.input.nextDouble();
double c=Main.input.nextDouble();
Traperoid traperoid=new Traperoid(a,b,c);
Card card=new Card(traperoid);
card.getShape().setShapeName("Trapezoid");
cardList.add(card);
}
}
}
public boolean validate() {
for(int i=0;i<cardList.size();i++)
{if(!cardList.get(i).getShape().vaildate())
return false;}
return true;
}
public void cardSort() {
for(int k=0;k<cardList.size();k++)
for(int i=k+1;i<cardList.size();i++)
{
if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea())
Collections.swap(cardList, k, i);
}
}
public double getAllArea() {
double s=0;
for(int i=0;i<cardList.size();i++)
s=s+cardList.get(i).getShape().getArea();
return s;
}
public void showResult() {
System.out.println("The original list:");
for(int i=0;i<cardList.size();i++)
System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
System.out.println();
System.out.println("The sorted list:");
cardSort();
for(int i=0;i<cardList.size();i++)
System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
System.out.println();
System.out.println("Sum of area:"+String.format("%.2f",getAllArea()));
}
}
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;
}
}
下面是类图
s
这次实验主要是对继承和多态的进一步练习,相较之前的题目,更多要考虑排序算法。
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
ArrayList list = new ArrayList();
int num = input.nextInt();
if(num==0){
System.out.println("Wrong Format");
System.exit(0);}
else{
while(num != 0){
if(num < 0 || num > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(num);
num = input.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(!dealCardList.validate()){
System.out.println("Wrong Format");
System.exit(0);
}
dealCardList.showResult();
input.close();
}
}}
class imuy{
int ssay=31;
int uiy=84;
}
class imwwuy{
int ssssay=3;
int iy=57;
}
class iouiiuy{
int ssaeey=1;
int iuhiy=6;
}
class imwwupoy{
int ssssay=3;
int iy=5;
}
class iopppuy{
int ssaeey=13;
int iuhiy=61;
}
class imwoioioiwuy{
int skkkay=3;
int ioooy=57;
}
class ipppiiuy{
int ssmmey=1;
int iuhnnniy=6;
}
class ippuuysuy{
int ssmmcvfey=1;
int iuhnhgfnniy=6;
}
class Card{
Shape shape;
Card(){
}
Card(Shape shape){
this.shape=shape;
}
public Shape getShape() {
return shape;
}
public void setShape(Shape Shape) {
this.shape=shape;
}
}
class DealCardList{
ArrayList<Card> cardList=new ArrayList<Card>();
ArrayList<Card> circles=new ArrayList<Card>();
ArrayList<Card> traperoids=new ArrayList<Card>();
ArrayList<Card> triangles=new ArrayList<Card>();
ArrayList<Card> rectangles=new ArrayList<Card>();
DealCardList(){
}
DealCardList(ArrayList list){
for(int i=0;i<list.size();i++)
{
if((int)list.get(i)==1)
{
double r=Main.input.nextDouble();
Circle circle=new Circle(r);
Card card=new Card(circle);
card.getShape().setShapeName("Circle");
cardList.add(card);
circles.add(card);
}
if((int)list.get(i)==2) {
double a=Main.input.nextDouble();
double b=Main.input.nextDouble();
Rectangle rectangle=new Rectangle(a,b);
Card card=new Card(rectangle);
card.getShape().setShapeName("Rectangle");
cardList.add(card);
rectangles.add(card);
}
if((int)list.get(i)==3) {
double a=Main.input.nextDouble();
double b=Main.input.nextDouble();
double c=Main.input.nextDouble();
Triangle triangle=new Triangle(a,b,c);
Card card=new Card(triangle);
card.getShape().setShapeName("Triangle");
cardList.add(card);
triangles.add(card);
}
if((int)list.get(i)==4) {
double a=Main.input.nextDouble();
double b=Main.input.nextDouble();
double c=Main.input.nextDouble();
Traperoid traperoid=new Traperoid(a,b,c);
Card card=new Card(traperoid);
card.getShape().setShapeName("Trapezoid");
cardList.add(card);
traperoids.add(card);
}
}
}
public boolean validate() {
for(int i=0;i<cardList.size();i++)
{if(!cardList.get(i).getShape().vaildate())
return false;}
return true;
}
public void cardSort(ArrayList<Card> o) {
for(int k=0;k<o.size();k++)
for(int i=k+1;i<o.size();i++)
{
if(o.get(k).getShape().getArea()<o.get(i).getShape().getArea())
Collections.swap(o, k, i);
}
}
public double getAllArea(ArrayList<Card> cardList) {
double s=0;
for(int i=0;i<cardList.size();i++)
s=s+cardList.get(i).getShape().getArea();
return s;
}
public void showResult() {
System.out.println("The original list:");
System.out.print("[");
for(int i=0;i<cardList.size();i++)
System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
System.out.print("]");
System.out.println();
System.out.println("The Separated List:");
System.out.print("[");
for(int i=0;i<circles.size();i++)
System.out.print(circles.get(i).getShape().getShapeName()+":"+String.format("%.2f",circles.get(i).getShape().getArea())+" ");
System.out.print("]");
System.out.print("[");
for(int i=0;i<rectangles.size();i++)
System.out.print(rectangles.get(i).getShape().getShapeName()+":"+String.format("%.2f",rectangles.get(i).getShape().getArea())+" ");
System.out.print("]");
System.out.print("[");
for(int i=0;i<triangles.size();i++)
System.out.print(triangles.get(i).getShape().getShapeName()+":"+String.format("%.2f",triangles.get(i).getShape().getArea())+" ");
System.out.print("]");
System.out.print("[");
for(int i=0;i<traperoids.size();i++)
System.out.print(traperoids.get(i).getShape().getShapeName()+":"+String.format("%.2f",traperoids.get(i).getShape().getArea())+" ");
System.out.print("]");
System.out.println();
System.out.println("The Separated sorted List:");
cardSort(cardList);
cardSort(circles);
cardSort(rectangles);
cardSort(triangles);
cardSort(traperoids);
System.out.print("[");
for(int i=0;i<circles.size();i++)
System.out.print(circles.get(i).getShape().getShapeName()+":"+String.format("%.2f",circles.get(i).getShape().getArea())+" ");
System.out.print("]");
System.out.print("[");
for(int i=0;i<rectangles.size();i++)
System.out.print(rectangles.get(i).getShape().getShapeName()+":"+String.format("%.2f",rectangles.get(i).getShape().getArea())+" ");
System.out.print("]");
System.out.print("[");
for(int i=0;i<triangles.size();i++)
System.out.print(triangles.get(i).getShape().getShapeName()+":"+String.format("%.2f",triangles.get(i).getShape().getArea())+" ");
System.out.print("]");
System.out.print("[");
for(int i=0;i<traperoids.size();i++)
System.out.print(traperoids.get(i).getShape().getShapeName()+":"+String.format("%.2f",traperoids.get(i).getShape().getArea())+" ");
System.out.print("]");
System.out.println();
double sum=getAllArea(circles);
if(getAllArea(rectangles)>sum)
sum=getAllArea(rectangles);
else if(getAllArea(traperoids)>sum)
sum=getAllArea(traperoids);
else if(getAllArea(triangles)>sum)
sum=getAllArea(triangles);
System.out.printf("The max area:%.2f",sum);
}
}
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;
}
}
本次作业主要对卡片(Card)进行分组游戏,其规则为随机发放一些卡片给学生, 卡片仍然分为四种形状:圆形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid), 并给出各种卡片的相应参数,要求学生首先根据卡片类型将所有卡片进行分组(一个类型分为一组, 所以最多四组),然后能够对每组内的卡片根据面积值从大到小进行排序,同时求出该组内所有卡片 的面积之和,最后求出每组卡片面积之和中的最大值。
和之前的题目比较相似,考量了相应的知识,是对我们继承多态的进一步锻炼。难度上也和7-1相差不大
第八次题目集
1. 作业目标 编写一个银行 ATM 机的模拟程序,能够完成用户的存款、取款以及查询余额功能。
实验源码
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ChinaUnionPay chinaunionpay = Initialize();
Search search = new Search(chinaunionpay);
Input input = new Input();
String str = in.nextLine();
while(!str.equals("#")) {
input.setInput(str);
if(!input.InputValidity()) {
System.exit(0);
}
if(!search.CardValidity(input.ReturnCardId())) {
System.out.println("Sorry,this card does not exist.");
System.exit(0);
}
Agent agent = search.SearchCard(input.ReturnCardId());
switch(input.RetrunChoose()) {
//查询余额业务
case 1:
System.out.println("¥"+agent.getAccount().toString());
break;
//存钱取钱业务
case 2:
ATM atm = new ATM(input.ReturnATMId());
agent.setAtm(atm);
if(!agent.ATMValidity()) {
System.out.println("Sorry,the ATM's id is wrong.");
System.exit(0);
}
if(!agent.CardNumValidity(input.ReturnCardNum())) {
System.out.println("Sorry,your password is wrong.");
System.exit(0);
}
if(!agent.MoneyOperate(input.ReturnMoney())) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
if(agent.isCrossBank()) {
System.out.println("Sorry,cross-bank withdrawal is not supported.");
System.exit(0);
}
if(input.ReturnMoney() > 0) {
agent.TakeMoney(input.ReturnMoney());
System.out.println(agent.getUser().getName()+"在"+agent.getBank().getName()+"的"+agent.getAtm().getNum()+"号ATM机上取款¥"+String.format("%.2f",input.ReturnMoney()));
System.out.println("当前余额为¥"+agent.getAccount().toString());
}
else {
agent.SaveMoney(input.ReturnMoney());
System.out.println(agent.getUser().getName()+"在"+agent.getBank().getName()+"的"+agent.getAtm().getNum()+"号ATM机上存款¥"+String.format("%.2f",Math.abs(input.ReturnMoney())));
System.out.println("当前余额为¥"+agent.getAccount().toString());
}
break;
}
str = in.nextLine();
}
in.close();
}
public static ChinaUnionPay Initialize() {
//杨过信息初始化
Card card1 = new Card("6217000010041315709",88888888);
Card card2 = new Card("6217000010041315715",88888888);
ArrayList<Card>Cardlist1 = new ArrayList<Card>();
Cardlist1.add(card1);
Cardlist1.add(card2);
Account account1 = new Account("3217000010041315709",10000.00,Cardlist1);
Card card3 = new Card("6217000010041315718",88888888);
ArrayList<Card>Cardlist2 = new ArrayList<Card>();
Cardlist2.add(card3);
Account account2 = new Account("3217000010041315715",10000.00,Cardlist2);
ArrayList<Account>Accountlist1 = new ArrayList<Account>();
Accountlist1.add(account1);
Accountlist1.add(account2);
User user1 = new User("杨过",Accountlist1);
//郭靖信息初始化
Card card4 = new Card("6217000010051320007",88888888);
ArrayList<Card>Cardlist3 = new ArrayList<Card>();
Cardlist3.add(card4);
Account account3 = new Account("3217000010051320007",10000.00,Cardlist3);
ArrayList<Account>Accountlist2 = new ArrayList<Account>();
Accountlist2.add(account3);
User user2 = new User("郭靖",Accountlist2);
//张无忌信息初始化
Card card5 = new Card("6222081502001312389",88888888);
ArrayList<Card>Cardlist4 = new ArrayList<Card>();
Cardlist4.add(card5);
Account account4 = new Account("3222081502001312389",10000.00,Cardlist4);
Card card6 = new Card("6222081502001312390",88888888);
ArrayList<Card>Cardlist5 = new ArrayList<Card>();
Cardlist5.add(card6);
Account account5 = new Account("3222081502001312390",10000.00,Cardlist5);
Card card7 = new Card("6222081502001312399",88888888);
Card card8 = new Card("6222081502001312400",88888888);
ArrayList<Card>Cardlist6 = new ArrayList<Card>();
Cardlist6.add(card7);
Cardlist6.add(card8);
Account account6 = new Account("3222081502001312399",10000.00,Cardlist6);
ArrayList<Account>Accountlist3 = new ArrayList<Account>();
Accountlist3.add(account4);
Accountlist3.add(account5);
Accountlist3.add(account6);
User user3 = new User("张无忌",Accountlist3);
//韦小宝信息初始化
Card card9 = new Card("6222081502051320785",88888888);
ArrayList<Card>Cardlist7 = new ArrayList<Card>();
Cardlist7.add(card9);
Account account7 = new Account("3222081502051320785",10000.00,Cardlist7);
Card card10 = new Card("6222081502051320786",88888888);
ArrayList<Card>Cardlist8 = new ArrayList<Card>();
Cardlist8.add(card10);
Account account8 = new Account("3222081502051320786",10000.00,Cardlist8);
ArrayList<Account>Accountlist4 = new ArrayList<Account>();
Accountlist4.add(account7);
Accountlist4.add(account8);
User user4 = new User("韦小宝",Accountlist4);
//银行信息初始化
ArrayList<User>Userlist1 = new ArrayList<User>();
Userlist1.add(user1);
Userlist1.add(user2);
ArrayList<ATM>ATMlist1 = new ArrayList<ATM>();
ATM atm1 = new ATM("01");
ATM atm2 = new ATM("02");
ATM atm3 = new ATM("03");
ATM atm4 = new ATM("04");
ATMlist1.add(atm1);
ATMlist1.add(atm2);
ATMlist1.add(atm3);
ATMlist1.add(atm4);
Bank bank1 = new Bank("中国建设银行",Userlist1,ATMlist1);
ArrayList<User>Userlist2 = new ArrayList<User>();
Userlist2.add(user3);
Userlist2.add(user4);
ArrayList<ATM>ATMlist2 = new ArrayList<ATM>();
ATM atm5 = new ATM("05");
ATM atm6 = new ATM("06");
ATMlist2.add(atm5);
ATMlist2.add(atm6);
Bank bank2 = new Bank("中国工商银行",Userlist2,ATMlist2);
ArrayList<Bank>Banklist = new ArrayList<Bank>();
Banklist.add(bank1);
Banklist.add(bank2);
ChinaUnionPay chinaunionpay = new ChinaUnionPay(Banklist);
return chinaunionpay;
}
}
class Input {
private String input = "";
Input(){
super();
}
Input(String input) {
super();
this.input = input;
}
class asssse{
int c=28;
}
class apppsse{
int sa=558;
}
class iuwsse{
int x=8;
}
class oiouswsse{
int kiu=256;
}
class asrrtuse{
int skf=8;
}
class assse{
int c=28;
}
class appsse{
int sa=558;
}
class iusse{
int x=8;
}
class oioussse{
int kiu=256;
}
class asrrtse{
int skf=8;
}class asse{
int c=28;
}
class appse{
int sa=558;
}
class iuse{
int x=8;
}
class oiousse{
int kiu=256;
}
class astuse{
int skf=8;
}class ase{
int c=28;
}
class apse{
int sa=558;
}
class ise{
int x=8;
}
class oiouse{
int kiu=256;
}
class asrse{
int skf=8;
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
class asassse{
int c=28;
}
class apppsase{
int sa=558;
}
class iuwsase{
int x=8;
}
class oiouswasse{
int kiu=256;
}
class asrrtuase{
int skf=8;
}class asxssse{
int c=28;
}
class appxsse{
int sa=558;
}
class iuwsxse{
int x=8;
}
class oiouswxsse{
int kiu=256;
}
class asrrxtuse{
int skf=8;
}
//返回银行卡卡号
public String ReturnCardId() {
String reg = "(\\d{19})";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(this.input);
m.find();
return m.group();
}class asqssse{
int c=28;
}
class apppqsse{
int sa=558;
}
class iuwqse{
int x=8;
}
class oiouswqsse{
int kiu=256;
}
class asrqrtuse{
int skf=8;
}
//返回银行卡密码
public int ReturnCardNum() {
String reg = "\\s\\d{8}";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(this.input);
m.find();
String Num = m.group().trim();
return Integer.valueOf(Num);
}
//返回ATM机编号
public String ReturnATMId() {
String reg = "(\\d{2}\\s{1,}\\-?\\d{1,}\\.\\d{2})";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(this.input);
m.find();
String reg2 ="\\d{2}";
Pattern p2=Pattern.compile(reg2);
Matcher m2=p2.matcher(m.group());
m2.find();
return m2.group();
}
//返回钱数值
public double ReturnMoney() {
String reg = "\\s{1,}\\-?\\d{1,}\\.\\d{2}";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(this.input);
m.find();
double money = Double.valueOf(m.group());
return money;
}
//返回业务类型
public int RetrunChoose() {
String reg = "(\\d{19})";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(this.input);
if(m.matches()) {
return 1;
}
return 2;
}
class se{
int c=28;
}
class ape{
int sa=558;
}
class ie{
int x=8;
}
class oioue{
int kiu=256;
}
class asre{
int skf=8;
}
//判断输入合法性
public boolean InputValidity() {
String reg = "(\\d{19})|(\\d{19}\\s{1,}\\d{8}\\s{1,}\\d{2}\\s{1,}\\-?\\d{1,}\\.\\d{2})";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(this.input);
return m.matches();
}
}
class Search {
private ChinaUnionPay chinaunionpay = null;
Search() {
super();
}
Search(ChinaUnionPay chinaunionpay) {
super();
this.chinaunionpay = chinaunionpay;
}
public ChinaUnionPay getChinaunionpay() {
return chinaunionpay;
}
public void setChinaunionpay(ChinaUnionPay chinaunionpay) {
this.chinaunionpay = chinaunionpay;
}
//寻找到这张卡,并把构造一个Agent类的对象存储这张卡的所有信息,返回agent对象
public Agent SearchCard(String id) {
for (Bank i : chinaunionpay.getBanklist()){
for (User j : i.getUserlist()){
for (Account k : j.getAccountlist()){
for (Card l : k.getCardlist()){
if(l.getID().equals(id)) {
Agent agent = new Agent(chinaunionpay,i,j,k,l);
return agent;
}
}
}
}
}
return null;
}
//判断卡号是否存在
public boolean CardValidity(String id){
if(SearchCard(id) == null) {
return false;
}
return true;
}
}
class Agent {
private ChinaUnionPay chinaunionpay = null;
private Bank bank = null;
private User user = null;
private Account account = null;
private Card card = null;
private ATM atm = null;
Agent() {
super();
}
Agent(ChinaUnionPay chinaunionpay,Bank bank,User user, Account account, Card card) {
super();
this.chinaunionpay = chinaunionpay;
this.user = user;
this.bank = bank;
this.account = account;
this.card = card;
}
Agent(ChinaUnionPay chinaunionpay, Bank bank, User user, Account account, Card card, ATM atm) {
super();
this.chinaunionpay = chinaunionpay;
this.bank = bank;
this.user = user;
this.account = account;
this.card = card;
this.atm = atm;
}
public ChinaUnionPay getChinaunionpay() {
return chinaunionpay;
}
public void setChinaunionpay(ChinaUnionPay chinaunionpay) {
this.chinaunionpay = chinaunionpay;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Bank getBank() {
return bank;
}
public void setBank(Bank bank) {
this.bank = bank;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public ATM getAtm() {
return atm;
}
public void setAtm(ATM atm) {
this.atm = atm;
}
//判断密码是否正确
public boolean CardNumValidity(int Num){
if(this.card.getNum() == Num) {
return true;
}
return false;
}
//判断ATM机编号是否有误
public boolean ATMValidity() {
for (Bank i : chinaunionpay.getBanklist()){
for (ATM j : i.getATMlist()){
if(j.getNum().equals(this.atm.getNum())) {
return true;
}
}
}
return false;
}
//判断是否跨行服务
public boolean isCrossBank() {
for(ATM i : bank.getATMlist()) {
if(i.getNum().equals(this.atm.getNum())) {
return false;
}
}
return true;
}
//判断取钱操作合法性
public boolean MoneyOperate(double Money) {
if((Money>0)&&((this.account.getMoney()-Money)<0)) {
return false;
}
return true;
}
// 存钱
public void SaveMoney(double Money) {
this.account.setMoney(Math.abs(Money) + this.getAccount().getMoney());
}
// 取钱
public void TakeMoney(double Money) {
this.account.setMoney(this.getAccount().getMoney() - Money);
}
@Override
public String toString() {
return String.format("%.2f",this.account.getMoney());
}
}
class ATM {
private String Num = "";
ATM() {
super();
}
ATM(String num) {
super();
Num = num;
}
public String getNum() {
return Num;
}
public void setNum(String num) {
Num = num;
}
}
class Card {
private String ID = "";
private int Num = 0;
Card() {
super();
}
Card(String iD, int num) {
super();
ID = iD;
Num = num;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public int getNum() {
return Num;
}
public void setNum(int num) {
Num = num;
}
}
class Account {
private String ID = "";
private double Money = 0;
private ArrayList<Card>Cardlist = new ArrayList<Card>();
Account() {
super();
}
Account(String iD, double money, ArrayList<Card> cardlist) {
super();
ID = iD;
Money = money;
Cardlist = cardlist;
}
public double getMoney() {
return Money;
}
public void setMoney(double money) {
Money = money;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public ArrayList<Card> getCardlist() {
return Cardlist;
}
public void setCardlist(ArrayList<Card> cardlist) {
Cardlist = cardlist;
}
@Override
public String toString() {
return String.format("%.2f",this.Money);
}
}
class Bank {
private String Name = "";
private ArrayList<User>Userlist = new ArrayList<User>();
private ArrayList<ATM>ATMlist = new ArrayList<ATM>();
Bank(){
super();
}
Bank(String name, ArrayList<User> userlist, ArrayList<ATM> atmlist) {
super();
Name = name;
Userlist = userlist;
ATMlist = atmlist;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public ArrayList<User> getUserlist() {
return Userlist;
}
public void setUserlist(ArrayList<User> userlist) {
Userlist = userlist;
}
public ArrayList<ATM> getATMlist() {
return ATMlist;
}
public void setATMlist(ArrayList<ATM> aTMlist) {
ATMlist = aTMlist;
}
}
class User {
private String Name = "";
private ArrayList<Account>Accountlist = new ArrayList<Account>();
User(){
super();
}
User(String name,ArrayList<Account> accountlist) {
super();
Name = name;
Accountlist = accountlist;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public ArrayList<Account> getAccountlist() {
return Accountlist;
}
public void setAccountlist(ArrayList<Account> accountlist) {
Accountlist = accountlist;
}
}
class ChinaUnionPay {
private ArrayList<Bank>Banklist = new ArrayList<Bank>();
ChinaUnionPay(){
super();
}
ChinaUnionPay(ArrayList<Bank> banklist) {
super();
Banklist = banklist;
}
public ArrayList<Bank> getBanklist() {
return Banklist;
}
public void setBanklist(ArrayList<Bank> banklist) {
Banklist = banklist;
}
}
尝试使用面向对象技术对银行用户在 ATM 机上进行相关操作的模拟系统设计
此次实验更加偏向实用性,他让我们独立设计出几个类模拟账户,还有模拟银行的一些借记功能。其中运用到了许多所学的知识。是对之前所学的一个巩固,同样因为功能和账号这些,导致工作量不小。需要花费大量时间去设计。中间也很容易出现错误。借记功能要反复试错,在刚开始设计时,经常会出现各种问题。需要一步一步去解决。同样的一定养成写注释的习惯,不然在写这种代码量比较多的实验时,一旦出现错误,会很难找到错误的地方,而且更改也要花大量时间。这样会导致你花更多的时间,而且还是浪费掉的时间。
总结
踩坑心得
首先写代码时,注释是非常必要的一个习惯,它既能帮你更好的理解代码,也能在你需要修改时起到一个引导的作用。而且也能让别人更好的看懂你的代码。所以写代码是加上注释是很必要的。
第二点要注意变量放的位置,很多时候。因为不在一个类里。或者定义的位置的原因,引发一些编译错误。或者运行时的错误。而这时你往往不知道自己哪里错了。明明方法代码都没有问题,可是一运行就出错。这个时候就要考虑一些这种经验不足导致的错误。
java第一次博客作业
目录
前言
设计与分析
踩坑心得
改进建议
总结
关于三次java题目的体会
这学期刚接触java,对Java还不熟悉。纸上得来终觉浅,实践才能出真知。通过这三次作业,对Java有了更深刻的了解。这三次题目涉及了许多知识点,从最简单的从键盘读取输入,字母转换数字,还有编写简单的程序进行简单的运算等。从最基本的一些操作练习很好的过度到解决一些简单的实际问题。让人收获通过计算机语言解决实际问题的满足感。当然三次题目集也让我碰到了不少困难,其中第三次作业中设计类的题目需要我们设计一个类来解决问题。但是我对类的运用非常不熟悉。导致自己多次无法通过测试点。最后截至提交了也没通过测试点。非常的遗憾。其实Java作业的总体题量不大,可是对我这种掌握不熟悉的同学来说,犯大量的错误,浪费了大量时间,许多不熟悉的操作语言要翻书寻找。就常常会感觉很匆忙。
第一次作业
读取键盘的输入,做一些简单的运算。大小顺序排序等
第二次作业
求下一天或者前几天日期,主要用到选择语句
第三次作业
设计类解决一些实际问题,如银行账户等
设计与分析
部分源码
public static void main(String[] args) {
int a=0,b=0,c=0;
//创建一个输入器
Scanner scanner = new Scanner(System.in);
//接受三次输入
System.out.println("请输入第一条边");
a = scanner.nextInt();
System.out.println("请输入第二条边");
b =scanner.nextInt();
System.out.println("请输入第三条边");
c=scanner.nextInt();
//进行判断
if(a>0 && b>0 && c>0) {
//对三条边进行排序 (目的就确认 短,中,长)
if(a>b) {
int temp = a;
a=b;
b= temp;
}
if(a>c) {
int temp = a;
a=c;
c= temp;
}
if(b>c) {
int temp = b;
b=c;
c= temp;
}
//System.out.println(a+" "+b+" "+c);
//判断是否组成三角形
if(a+b>c) {
if(a*a+b*b<c*c) {
if(a==b) {
System.out.println("等腰三角形");
}else {
System.out.println("钝角三角形");
}
}else if(a*a+b*b==c*c) {
if(a==b) {
System.out.println("等腰直角三角形");
}else {
System.out.println("直角三角形");
}
}else {
if(a==b) {
if(b==c) {
System.out.println("等边三角形");
}else {
System.out.println("等腰三角形");
}
该题较为简单,主要是对键盘输入的一个接收,然后反复使用选择语句进行判断。就是选择语句的一个练习,并不复杂。多用几次if else就可以了。
部分代码
public static boolean isLeapYear(int year) {//判断是否闰年
boolean isLeapYear = (year % 4 == 0 && year % 100 !=0 )||year % 400 == 0;
return isLeapYear;
}
public static boolean checkInputValidity(int year,int month,int day) {
int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
aa[2] = 29;
boolean checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=aa[month]&&day>0);
return checkInputValidity;
}
public static void nextDate(int year,int month,int day) {
//求下一天注意夸年月以及特殊月份以及闰年和平年的二月不同天数
int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
aa[2] = 29;
int a = 0,b = 0,c = 0;
if(checkInputValidity(year,month,day)) {
if(month==12) {
if(day==aa[month]) {
a = year+1;
b = 1;
c = 1;}
if(day>0&&day<aa[month])
{a = year;
b = month;
c =day +1;
}
}
if(month<12) {
if(day==aa[month]) {
a = year;
b = month + 1;
c = 1;}
if(day>0&&day<aa[month])
{a = year;
b = month;
c = day+1;
}
}
System.out.println("Next date is:"+a+"-"+b+"-"+c);
}
else System.out.println("Wrong Format");
}
public static void main(String[] args) {
Scanner LJY = new Scanner(System.in);
Main ljy = new Main();
int year = LJY.nextInt();
int month = LJY.nextInt();
int day = LJY.nextInt();
ljy.nextDate(year,month,day);
}

部分代码
ublic static void main(String[] args) {
Scanner as = new Scanner(System.in);
boolean f=true;
int q=0,e=0,r=0,t=0;
if (as.hasNextInt())
q= as.nextInt();
else
f=false;
if (as.hasNextInt())
e= as.nextInt();
else
f=false;
if (as.hasNextInt())
r= as.nextInt();
else
f=false;
if (as.hasNextInt())
t= as.nextInt();
else
f=false;
if (checkInputValidity(q,e,r,t)&&f){
nextDate(q,e,r,t);
}else
System.out.println("Wrong Format");
}
public static boolean isLeapYear(int year) {
boolean ret=false;
if ((year%100!=0&&year%4==0)||(year%400==0)){
ret=true;
}
return ret;
}
第二次作业是对日期的处理,相较之前的作业,在原有的基础上。因为需要对特殊月份进行特殊分析所以多了对数组的运用。总体上更加复杂,所以在做题时要更加细心,考虑清楚月份天数的处理。

部分代码
public class wang {
int year;
int month;
int day;
int [] mon_maxnum=new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
void getyear() {
}
void getyear() {
}
public static void main(String[] args) {
由于有些题目我并未写出,所以无法展示代码。
第三次题目集,我学的很不好。题目也没做出来。但它总体是对类的使用的一个训练。是非常不错的题目。虽然难度对我来说有些高了。
但我相信随着不断的学习。会慢慢克服这些问题。以后自己会更加努力,解决他们。
踩坑心得
刚开始的时候,使用java这门语言进行编程题目的时候,由于和c语言有许多不同。会有些看不懂。可接触久了会发现许多东西是类似的。开头的模板直接套用就可以,重要的是不要忘了引用已存在的方法后头文件的引用。最开始我常常因为这个而报错,还不知道问题出在哪里,而抓耳挠腮。其次是输入的时候一定要用英文,有时候换成中文的字符了,自己不注意,长的又差不多,报错的时候也会很迷茫。
还有就是一些数据放的位置也有讲究。比如一个数据定义在一个循环里和循环外都是会产生差别的。这方面需要注意一些。
在写代码前就要思考好自己需要那些数据,要那些功能,需要进行一些什么操作。要定义那些量。
同样定义变量,方法这些东西的时候。自己在取名方面也要注意。如果随意取名,而题目需要定义的数据又多的话。很可能会导致到了后面自己需要的数据或方法自己都找不到了,忘了定义的名字。又要回头去看,导致浪费大量时间。
还有代码编译后一定要进行运行测试,检查是否有问题。
出现问题的话可以直接点旁边查看问题,,和解决方法。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int a=input.nextInt();
int b=input.nextInt();
System.out.println(a+b);
}
}
像这道题,我因为忘了在开头引用import java.util.Scanner;
导致编译错误,这是我最开始犯的很愚蠢的错误。希望大家不要犯这种类似的错误。对方法使用后,记得前面一定要加上配套的代码。
接下来展示一段最近做的实验题的源码
实验一(随机生成学生信息)
package hello;
import java.text.DecimalFormat;
import java.util.*;
public class wa {
public static void main(String[] args) {
//Scanner sr = new Scanner(System.in);//输入数值sr
String studentID;//学号
String studentName;//名字
//double [][]scores;//四科成绩
int []id=new int[30];
//char [][]name=new char[30][5];
String []name =new String[30];
int [][]scores=new int[30][4];
//随机生成30个学生信息
for (int i=0;i<30;i++) {
id[i]=13201101+i;//从数字0到9随机生成id
}
for (int i=0;i<30;i++) {
//System.out.print(id[i]+"");
//System.out.println();
}
for (int i=0;i<30;i++) {
name[i]="";
for(int n=0;n<5;n++) {
name[i]=name[i]+(char)(int)(Math.random()*26+97);
}
}
for (int i=0;i<30;i++) {
//System.out.print(name[i]+"");
//System.out.println();
}
for (int i=0;i<30;i++) {
for(int n=0;n<4;n++) {
scores[i][n]=(int)(Math.random()*100);
}
}
for (int i=0;i<30;i++) {
for(int n=0;n<4;n++) {
DecimalFormat df = new DecimalFormat( "0.00" );//
String cj=df.format( scores[i][n] );//把成绩转换成两位小数
//System.out.print(cj+" ");if(n==3)
//System.out.println();
}
}
double xspjf[]=new double[30];//学生平均分
for(int i=0;i<30;i++) {
xspjf[i]=pjf(scores[i][0],scores[i][1],scores[i][2],scores[i][3]);
}
for(int i=0;i<30;i++) {//输出30个学生平均分
DecimalFormat df = new DecimalFormat( "0.00" );//
String cj=df.format( xspjf[i] );
//System.out.print(cj+"");
//System.out.println();
}
//最高平均分学生
double max=0;
int a=0;
for(int i=0;i<30;i++) {
if(max<xspjf[i]) {
max=xspjf[i];
a=i;
}
}
//System.out.print(max+"");
//System.out.print(a+"");
double xxs[]=new double [4];
Scanner in = new Scanner(System.in);
System.out.print("请输入学生姓名:");
System.out.println();
String xm=in.next();
System.out.print("请输入学生学号:");
int xh = in.nextInt();
System.out.print("请输入四门课的成绩,以空格分隔:");
xxs[0]=in.nextDouble();
xxs[1]=in.nextDouble();
xxs[2]=in.nextDouble();
xxs[3]=in.nextDouble();
double xxspjf[]=new double [1];
xxspjf[0]=pjf(xxs[0],xxs[1],xxs[2],xxs[3]);
// System.out.print(xxspjf[0]+"");
double zgf=0;
for(int i=0;i<4;i++) {
if(zgf<xxs[i]) {
zgf=xxs[3];
}
}
int xzgf=0;
for(int i=0;i<4;i++) {
if(xzgf<scores[a][i]) {
xzgf=scores[a][i];
}
}
System.out.println(xm+"(ID:"+xh+") 平均分 "+xxspjf[0]+" 最高分 "+zgf);
System.out.print(name[a]+"(ID:"+id[a]+") 平均分 "+max+" 最高分 "+xzgf);
}//主函数部分
//算平均分
public static double pjf(double cj1,double cj2,double cj3,double cj4) {
double sum=0;
sum=(cj1+cj2+cj3+cj4)/4;
return sum;
}
}
这道题最开始我出现的错误是拿到手也没多想就开始敲,没有好好考虑变量数组的设定大小这些方面,导致后面十分的难受,然后做到一半又对前面的定义的变量进行修改。可是已经写了一大半了,一修改就要修改许多。前面许多东西都有推倒重做,浪费了大量时间。导致了许多问题的出现。其中变量放在循环内后,导致结果不是自己想要的问题,也是出现在这里,幸好同学帮助找出了。希望大家不要犯相同的错误,在编写程序之前一定要思考好,大体方向,以及需要的变量方法等。。。再开始动手。这样可以比少出错误,看似浪费了时间,其实节省了大量时间。
然后这道实验题,也让我学到了很多东西。许多时候自己不会的东西,要去大胆求教。这样才能更快进步。
改进建议
自身现在Java方面的知识十分薄弱,还没本事提出什么改进建议。只能暂时搁置,以后自己会好好学习相关知识。争取早日能对Java方面的知识熟练运用。
总结
对本阶段的三次题目集,我从中学到了许多知识。
有键盘的输入接收,对原有Java的函数的一些调用,还有用自己写的一些函数去进行一些简单的计算。创建数组,选择语句的使用。
类的创建。可以做一些简单的问题的解决。比如输入三边后用选择语句判断是何三角形。还通过课本和网上查询以及同学帮助学会了生成随机数,随机字母,这些东西。综合起来可以随机生成随机学生信息。然后通过一些绑定可以。做一些实际的问题操作。
for (int i=0;i<30;i++) {
id[i]=13201101+i;//从数字0到9随机生成id
}
for (int i=0;i<30;i++) {
//System.out.print(id[i]+"");
//System.out.println();
}
for (int i=0;i<30;i++) {
name[i]="";
for(int n=0;n<5;n++) {
name[i]=name[i]+(char)(int)(Math.random()*26+97);
}
}
for (int i=0;i<30;i++) {
//System.out.print(name[i]+"");
//System.out.println();
}
for (int i=0;i<30;i++) {
for(int n=0;n<4;n++) {
scores[i][n]=(int)(Math.random()*100);
}
}
for (int i=0;i<30;i++) {
for(int n=0;n<4;n++) {
DecimalFormat df = new DecimalFormat( "0.00" );//
String cj=df.format( scores[i][n] );//把成绩转换成两位小数
//System.out.print(cj+" ");if(n==3)
//System.out.println();
这里就是部分随机生成的代码,是我最近学到的。其中还有强制转换。因为我之前随机生成的数小数位数太多了,而实验要求是两位小数。所以我要去寻找相关的操作代码。从中学到了强制转换。自己也发现了自己的许多不足,许多基本操作都不熟练。Java的基本知识完全不熟悉。课程上落下了许多,对比同阶段的同学,知识非常不足。许多同学会的自己都不会。特别是类相关的知识自己一窍不通,看得很懵逼。这方面急需加强,要多看类有关的知识。赶上同学们的进度。
还有许多基础性的东西要熟悉起来。现在还老要翻书,去找相关的代码。老师教学很认真严谨也详细。可是希望作业要求方面能谅解一下我这种学渣,多宽容一点。课程方面希望能进度慢一点。
作业希望简单一点,要求宽容一点。
实验我觉挺好,能学到好多东西。老师教的也很细心,帮我们解决问题。重点是作业方面,希望宽容点点呀。其他方面都挺好的。




浙公网安备 33010602011771号
退出
[Ctrl+Enter快捷键提交]
【推荐】大型组态、工控、仿真、CAD\GIS 50万行VC++源码免费下载!
【推荐】618好物推荐:基于HarmonyOS和小熊派BearPi-HM Nano的护花使者
【推荐】阿里云爆品销量榜单出炉,精选爆款产品低至0.55折
【推荐】限时秒杀!国云大数据魔镜,企业级云分析平台
· .Net Core with 微服务 - Consul 注册中心
· 为什么选择 ASP.NET Core
· 从 Vehicle-ReId 到 AI 换脸,应有尽有,解你所惑
· CSS ::marker 让文字序号更有意思
· 聊一聊 .NET Core 结合 Nacos 实现配置加解密
· 7年了 刘作虎:一加1代目前仍有300多位活跃用户
· 全球第一位数字航天员亮相:15位工程师 共耗时3个月
· 贾跃亭父亲节发声:FF就像我的孩子 我永远为此自豪
· 中国空间站机械臂有多牛?承载25吨 可单手抓飞船
· iPhone必崩溃bug曝光!这个Wi-Fi水太深:附解决方法
» 更多新闻...