第一階段-模塊二-代碼詳解


1. 編程實現以下需求:

定義一個長度為[16][16]的整型二維數組並輸入或指定所有位置的元素值,分別實現二維數組中所有行和所有列中所有元素的累加和並打印。

再分別實現二維數組中左上角到右下角和右上角到左下角所有元素的累加和並打印。

package com.lagou.module02;


/**
 * 定義一個長度為[16][16]的整型二維數組並輸入或指定所有位置的元素值,分別實現二維數組中所有行和所有列中所有元素的累加和並打印。
 * 再分別實現二維數組中左上角到右下角和右上角到左下角所有元素的累加和並打印。
 *
 * 二維數組求列總和未解決
 */

//  第一步 封裝一個類
public class Code0201 {
  1、創建int類型的變量標記X、Y軸坐標
    創建arr二維數組
// 定義行/列/二位數組 private int sumRows; //private int sumCols; private int sumLeft; private int sumRight; private int x;    private int y;    private int[][] arr;   
  2、無參方式初始化二維數組
public Code0201() { this.sumRows = 0; this.sumLeft = 0; this.sumRight = 0; this.x = 0; this.y = 0; arr = new int[16][16];   }   
  有參方式初始化二維數組,把X軸Y軸坐標傳入數組中
public Code0201(int rows, int cols) { this.sumRows = 0; this.sumLeft = 0; this.sumRight = 0; this.x = 0; this.y = 0; arr = new int[rows][cols]; } public int[][] getArr() { return arr; } public void setArr(int[][] arr) { this.arr = arr; } 3、嵌套for循環把二層數組打印出來 public void addValue(){ for (int i=0;i<arr.length;i++){ for (int j=0;j<arr[i].length;j++){ arr[i][j] = j; System.out.print(arr[i][j] + " "); } System.out.println(); } }
  4、求和
public void addShow(){
        for循環把外層數組的長度取出來,放在I中
for (int i=0;i<arr.length;i++){
        當i達到總長度的時候,累加器還原到0的狀態
if(x == i){ sumRows = 0; }
          for循環把內層數組的長度取出來,放在J中
for (int j=0;j<arr[i].length;j++){           
          存儲每一個的總和,累加器等於當前i行j總長度的累加,j長度為16那么sumRows就等於0-15總和(就是當前i行的總和)
sumRows += arr[i][j]; if(j == arr[i].length-1) { System.out.println("第" + i + "行的值為:" + sumRows); x += 1; }

          存儲每列第一個元素的總和,每次循環當J為0時存儲在Y中,累加並打印當前循環Y的累加數值
if(0 == j){ y += arr[i][j]; System.out.println("Y = " + y); }           
          存儲左上角到右下角的總和,每行I與J的值相當把該元素存儲在累加器中並打印(第一行0,0/第二行1,1如此類推)
if(i == j) { sumLeft += arr[i][j]; }           
          存儲右上角到左下角的總和,當J的值等於每次循環每次循環最大長度1的時候的累加(第一次循環時16-1-0)
if(j == arr[i].length-1-i){ sumRight += arr[i][j]; } } } System.out.println("左上角到右下角的總和是:"+sumLeft); System.out.println("右上角到左下角的總和是:"+sumRight); } }

------------------------------------測試類------------------------------------------------
package com.lagou.module02;

public class Code0201Test {
public static void main(String[] args) {
Code0201 code0201 = new Code0201(16,16);
code0201.addValue();
code0201.addShow();
}
}
 

2. 編程實現控制台版並支持兩人對戰的五子棋游戲。 

(1)繪制棋盤 - 寫一個成員方法實現 

(2)提示黑方和白方分別下棋並重新繪制棋盤 - 寫一個成員方法實現。 

(3)每當一方下棋后判斷是否獲勝 - 寫一個成員方法實現。

 

 

 

package com.lagou.module02;

import java.util.Scanner;

1、創建二維數組
public class Code0202 { private String[][] board; private char white; private char black; private int rows; // private int cols; // Code0202() { }   
2、初始化二維數組 Code0202(
int rows, int cols, char white, char black) { setBoard(rows, cols); setWhite(white); setBlack(black); setRows(rows); setCols(cols); } public void setRows(int rows) { this.rows = rows; } public void setCols(int cols) { this.cols = cols; } public int getRows() { return rows; } public int getCols() { return cols; } public void setBoard(int rows, int cols) { board = new String[rows][cols]; } public void setWhite(char white) { this.white = white; } public void setBlack(char black) { this.black = black; } public String[][] getBoard(int rows, int cols) { return board; } public char getWhite() { return white; } public char getBlack() { return black; }   
  3、打印棋盤
public void getBoard() { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) {
          打印Y軸第一列,轉為為整形十六進制打印
if (i == 0) { board[i][j] = String.format("%x", j - 1);
          打印X軸第一列,轉換為整型十六進制打印 }
else if (j == 0) { board[i][j] = String.format("%x", i - 1); } else {
          剩下全部打印+號 board[i][j]
= "+"; } } } board[0][0] = " "; }   
  打印棋子
public void showBoard() { for (String[] str : board) { for (int j = 0; j < board[0].length; j++) { if ("白".equals(str[j])) { System.out.print(white + " "); } else if ("黑".equals(str[j])) { System.out.print(black + " "); } else { System.out.print(str[j] + " "); } } System.out.println(); } }   
  接受到游戲入口傳來的參數判斷獲勝條件
private Boolean isWin(int x, int y, String chess, int rows, int cols) { int countCol = -1; int countRow = -1; int countDiagonalLeft = -1; int countDiagonalRight = -1; int a; int b; a = x; b = y;
     無線循環,判斷獲勝條件
while (true) {
       從右到左判斷5個棋子是否連在一起,如果其中有一個棋子斷開就執行break,b=0的時候也執行break
if (chess.equals(board[a][b])) { countRow++; if (b == 0) { break; } b--; } else { break; } } a = x; b = y; while (true) {
       左到右判斷5個棋子是否連在一起,如果其中一個棋子斷開就執行break,b達到數組最末尾的時候也執行break
if (chess.equals(board[a][b])) { countRow++; if (b == (cols - 1)) { break; } b++; } else { break; } } a = x; b = y; while (true) {
      判斷縱列是否連城5只棋子,判斷條件與上述一樣
if (chess.equals(board[a][b])) { countCol++; if (a == 0) { break; } a--; } else { break; } } a = x; b = y; while (true) {
      判斷縱列是否5只棋子連城5只,判斷條件與上述一樣
if (chess.equals(board[a][b])) { countCol++; if (a == (rows - 1)) { break; } a++; } else { break; } } a = x; b = y; while (true) {
      判斷左到右斜角線5只棋子是否連成5只,判斷條件與上述一樣
if (chess.equals(board[a][b])) { countDiagonalLeft++; if (a == 0 || b == 0) { break; } a--; b--; } else { break; } } a = x; b = y; while (true) {
      判斷右到左5只棋子連成5只,判斷條件上述一樣
if (chess.equals(board[a][b])) { countDiagonalLeft++; if (a == (rows - 1) || b == (cols - 1)) { break; } a++; b++; } else { break; } } a = x; b = y; while (true) {
        判斷右上到左下5只棋子是否連在一起,判斷條件上述一樣
if (chess.equals(board[a][b])) { countDiagonalRight++; if (a == 0 || b == (cols - 1)) { break; } a--; b++; } else { break; } } a = x; b = y; while (true) {
      判斷左下到右上5只棋子是否連在一起,判斷條件與上述一樣
if (chess.equals(board[a][b])) { countDiagonalRight++; if (a == (rows - 1) || b == 0) { break; } a++; b--; } else { break; } } System.out.println("countRow:" + countRow); System.out.println("countCol:" + countCol); System.out.println("countDiagonalLeft:" + countDiagonalLeft); System.out.println("countDiagonalRight:" + countDiagonalRight); return (countRow >= 5 || countCol >= 5 || countDiagonalLeft >= 5 || countDiagonalRight >= 5); }   
   4、編寫游戲入口
    注意:該方法會一直重復執行,直到黑棋或者白棋一方在siWin()方法中獲勝為止。
public void startGame() {    
     創建掃描器,獲取X軸Y軸 Scanner sc
= new Scanner(System.in); boolean flag = true; int x; int y; String chess;
     編寫無線循環切換白棋與黑棋下棋順序
while (true) { if (flag) { System.out.println("請白方落子。"); chess = "白"; } else { System.out.println("請黑方落子。"); chess = "黑"; } x = sc.nextInt() + 1; y = sc.nextInt() + 1;
       編寫規則,不允許棋子超出棋盤范圍或者坐標重復
if (x < 0 || y < 0 || x > 16 || y > 16) { System.out.println("落子超出棋盤范圍,請重新落子。"); continue; } if ("+".equals(board[x][y])) { board[x][y] = chess; } else { System.out.println("當前坐標不可落子,請重新落子。"); continue; } showBoard(); flag = !flag;
       把獲取到的橫縱坐標,數組傳入到iswin方法中
boolean isWin = isWin(x, y, chess, getRows(), getCols()); if (isWin) { System.out.printf("恭喜%s方獲勝!", chess); break; } }
     游戲獲勝后釋放內存空間 sc.close(); } }

-----------------------------------------------測試類----------------------------------------------------------
package com.lagou.module02;

public class Code0202Test {
public static void main(String[] args) {
// 自定義棋子
char white = 0x25cb;
char black = 0x25cf;
Code0202 code = new Code0202(17, 17, white, black);
// 繪制棋盤
code.getBoard();
// 顯示棋盤
code.showBoard();
// 開始游戲
code.startGame();
}

}

3. 按照要求設計並實現以下實體類和接口。 

    3.1 第一步:設計和實現以下類 

    (1)手機卡類 特征:卡類型、卡號、用戶名、密碼、賬戶余額、通話時長(分鍾)、上網流量 行為:顯示(卡號 + 用戶名 + 當前余額) 

    (2)通話套餐類 特征:通話時長、短信條數、每月資費 行為: 顯示所有套餐信息     (3)上網套餐類 特征:上網流量、每月資費 行為:顯示所有套餐信息 

    (4)用戶消費信息類 特征:統計通話時長、統計上網流量、每月消費金額 

    3.2 第二步:設計和實現以下枚舉類 手機卡的類型總共有 3 種:大卡、小卡、微型卡 

    3.3 第三步:實體類的優化 將通話套餐類和上網套餐類中相同的特征和行為提取出來組成抽象套餐類。 

    3.4 第四步:創建並實現以下接口 

    (1)通話服務接口 抽象方法: 參數1: 通話分鍾, 參數2: 手機卡類對象 讓通話套餐類實現通話服務接口。 

    (2)上網服務接口 抽象方法: 參數1: 上網流量, 參數2: 手機卡類對象 讓上網套餐類實現上網服務接口。

 

3.5 第五步:進行代碼測試

編寫測試類使用多態格式分別調用上述方法,方法體中打印一句話進行功能模擬即可。

----------------------------------------抽象類--------------------------------------------------------
1、編寫抽象類,抽象內中有兩個成員對象以及一個抽象方法
package
com.lagou.module02; /** * 抽象類套餐類 */ public abstract class Code0203Abstract { private int expenses; private int quantity; /** * expenses 資費 * quantity 數量 */ public Code0203Abstract() { } public Code0203Abstract(int expenses,int quantity) { setExpenses(expenses); setQuantity(quantity); } public int getExpenses() { return expenses; } public void setExpenses(int expenses) { this.expenses = expenses; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public abstract void show(); }
--------------------------------------通話接口-------------------------------------------------------
2、通話接口,接口中也有一個抽象方法傳入時間與對象
package com.lagou.module02;

/**
* 通話服務接口(通話分鍾,手機卡類型)
*/
public interface Code0203CallInterface {
public abstract void callPackage(int callTime,Code0203PhoneCard code0203PhoneCard);
}
 
         
--------------------------------------上網接口-------------------------------------------------------
3、上網接口,接口中也有抽象方法傳入流量與對象
package com.lagou.module02;

/**
* 上網流量接口(上網流量,手機卡類型)
*/
public interface Code0203InternetInterface {
public abstract void internetPackage(int internetTraffic,Code0203PhoneCard code0203PhoneCard);
}
 
         
--------------------------------------普通通話類-------------------------------------------------------
4、編寫普通通話類實現通話類,該類繼承了抽象類並且實現通話接口
package com.lagou.module02;

import java.util.Scanner;

/**
* 通話套餐類
*/
public class Code0203CallPackage extends Code0203Abstract implements Code0203CallInterface{
private int message;

Code0203Consumption code = new Code0203Consumption();
/**
* expenses 資費
* quantity 贈送通話時間
* message 贈送短信
*/

public Code0203CallPackage() {
}

public Code0203CallPackage(int expenses, int quantity, int message) {
super(expenses, quantity);
this.message = message;
}

public int getMessage() {
return message;
}

public void setMessage(int message) {
this.message = message;
}
  
  重寫通話接口,打印繼承下來的抽象成員Expenses/quantity/以及自身的成員message
// 通話類:打印套餐情況
@Override
public void show() {
System.out.printf("通訊套餐:國內語音%d元/分鍾,贈送%d分鍾國內語音,贈送國內短彩信%d條,包含國內接聽來電顯示。",getExpenses(),getQuantity(),getMessage());
}
  
  重寫接口抽象方法,計算傳入進來的時間並把時間和對象傳回code類的countCalltime方法中
// 重寫通話服務接口方法
@Override
public void callPackage(int callTime,Code0203PhoneCard code0203PhoneCard) {
if (getQuantity() == 0){
setQuantity(1);
}
code.countCallTime(callTime,getQuantity(),code0203PhoneCard);
}
}
 
         
--------------------------------------普通上網類-------------------------------------------------------
5、編寫普通上網類,繼承抽象方法並且實現接口
package com.lagou.module02;

/**
* 上網套餐類
*/
public class Code0203InternetPackage extends Code0203Abstract implements Code0203InternetInterface{
/**
* expenses 資費
* quantity 數量
*/

Code0203Consumption code = new Code0203Consumption();
Code0203InternetPackage() {
}

public Code0203InternetPackage(int expenses, int quantity) {
super(expenses, quantity);
}
  
  重寫抽象方法,並且打印繼承下來的抽象方法中的成員以及自身的成員
// 重寫抽象套餐類,打印套餐信息(充值)
@Override
public void show() {
System.out.printf("上網套餐:國內流量日租%d元/GB,贈送國內流量%dGB",getExpenses(),getQuantity());
}
  
  重寫接口中的抽象方法,把流量、資費、對象傳回code。countInternetTraffic方法中
// 重寫上網流量接口(使用)
@Override
public void internetPackage(int internetTraffic, Code0203PhoneCard code0203PhoneCard) {
if(getExpenses() == 0){
setExpenses(1);
}
code.countInternetTraffic(internetTraffic,getExpenses(),code0203PhoneCard);
}
}
 
         
--------------------------------------枚舉類-------------------------------------------------------
5、使用enum關鍵字創建枚舉類
package com.lagou.module02;

/**
* 枚舉類
*/
public enum Code0203Enum {
  枚舉類要求最上面第一行創建對象,對象的數量固定好外部不能new新的對象
A("大卡"),B("小卡"),C("微型卡");

private final String size;

private Code0203Enum(String size){
this.size = size;
}

public String getSize() {
return size;
}
}

--------------------------------------手機卡類-------------------------------------------------------
6、創建手機卡類,並且打印手機卡信息
package com.lagou.module02;

import com.lagou.task10.StaticOuter;

import java.awt.print.Pageable;

/**
* 手機卡類
*/
public class Code0203PhoneCard {
private String cardType;
private String phoneNumber;
private String name;
private String password;
private int accountBalance;
private int callTime;
private int internetTraffic;

Code0203CallPackage callPackage = new Code0203CallPackage();
Code0203InternetPackage internetPackage = new Code0203InternetPackage();
/**
* cardType 卡類型
* phoneNumber 卡號
* name 用戶名
* password 密碼
* accountBalance 賬戶余額
* callTime 通話時間
* internetTraffic 總上網流量
*/

Code0203PhoneCard(){};
  
  初始化手機卡
public Code0203PhoneCard(String cardType, String phoneNumber, String name, String password, int accountBalance, int callTime, int internetTraffic) {
setCardType(cardType);
setPhoneNumber(phoneNumber);
setName(name);
setPassword(password);
setAccountBalance(accountBalance);
setCallTime(callTime);
setInternetTraffic(internetTraffic);
}

public String getCardType() {
return cardType;
}

public void setCardType(String cardType) {
this.cardType = cardType;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getAccountBalance() {
return accountBalance;
}

public void setAccountBalance(int accountBalance) {
this.accountBalance = accountBalance;
}

public int getCallTime() {
return callTime;
}

public void setCallTime(int callTime) {
this.callTime = callTime;
}

public int getInternetTraffic() {
return internetTraffic;
}

public void setInternetTraffic(int internetTraffic) {
this.internetTraffic = internetTraffic;
}

// 訂購上網套餐

  1、show方法使用傳多個參數方式接收對象
// 打印賬戶信息
public void show(int... args){
System.out.println("============================手機卡信息==================================");
for(int i = 0;i<args.length;i++){
if(i == 0){
setCallTime(args[i]);
}else{
setInternetTraffic(args[i]);
}
}
showAccountBalance();
}
  
  2、showAccountBalance方法打印賬戶情況
public void showAccountBalance(){
// 顯示賬戶余額
System.out.println("==============================賬戶余額================================");
System.out.println("手機卡類型:"+getCardType()+"\n卡號:"+getPhoneNumber()+"\n用戶名:"+getName()+"\n密碼:"+getPassword()+
"\n賬戶余額:"+getAccountBalance()+"\n歷史通話時長:"+getCallTime()+"分鍾\n歷史流量使用情況:"+getInternetTraffic()+"GB");
}
  
  3、創建通話消費方法addCall,傳入到普通通話類中
// 通話消費
public void addCall(int a,Code0203PhoneCard code0203PhoneCard){
callPackage.callPackage(a,code0203PhoneCard);
}
  
  4、創建流量消費方法addinternet,傳入到普通上網類中
// 流量消費
public void addInternet(int a,Code0203PhoneCard code0203PhoneCard){
internetPackage.internetPackage(a,code0203PhoneCard);
}
}
 
         
--------------------------------------歷史賬單累計類-------------------------------------------------------
7、統計類
package com.lagou.module02;

import java.util.Scanner;

/**
* 用戶消費類
* 1、統計通話時長
* 2、統計上網流量
* 3、每月消費金額
*/
public class Code0203Consumption {
private int callTime;
private int internetTraffic;
private int expenses;
/**
* callTime 統計通話時長
* internetTraffic 統計上網流量
* expenses 統計消費金額
*/

Code0203Consumption() {
}

public Code0203Consumption(int callTime, int internetTraffic, int expenses) {
setCallTime(callTime);
setInternetTraffic(internetTraffic);
setExpenses(expenses);
}

public int getCallTime() {
return callTime;
}

public void setCallTime(int callTime) {
this.callTime = callTime;
}

public int getInternetTraffic() {
return internetTraffic;
}

public void setInternetTraffic(int internetTraffic) {
this.internetTraffic = internetTraffic;
}

public int getExpenses() {
return expenses;
}

public void setExpenses(int expenses) {
this.expenses = expenses;
}
  通話時長方法與統計上網流量方法只能依順序進行,確保賬戶扣款的准確性
  1、接收到普通通話類傳過來的參數,根據手機卡情況統計消費時長並且打印賬戶余額,如果賬戶中通話時長為0就是初始通話調用if中的語句,如果賬戶中通話時長大於0就是歷史通話調用else語句
public void countCallTime(int quantity,int expenses,Code0203PhoneCard code0203PhoneCard){
// 統計通話時長
if(code0203PhoneCard.getCallTime() == 0){
code0203PhoneCard.setCallTime(quantity); // 初次通話時間
}else {
setCallTime(code0203PhoneCard.getCallTime()+quantity);
code0203PhoneCard.setCallTime(getCallTime()); // 登記歷史通話時間
}
code0203PhoneCard.setAccountBalance(code0203PhoneCard.getAccountBalance()-(quantity*expenses));
System.out.println("============================歷史通話時間==================================");
System.out.printf("歷史通話時間:%d分鍾,賬戶余額%d元\n",code0203PhoneCard.getCallTime(),code0203PhoneCard.getAccountBalance());
}
  2、接收普通上網類傳過來的參數,根據手機卡情況統計上網情況,原理與上述方法一致
public void countInternetTraffic(int quantity,int expenses,Code0203PhoneCard code0203PhoneCard){
// 統計上網流量
if(code0203PhoneCard.getInternetTraffic() == 0){
code0203PhoneCard.setInternetTraffic(quantity); //初次登記歷史通話時間
}else {
setInternetTraffic(code0203PhoneCard.getInternetTraffic()+quantity);
code0203PhoneCard.setInternetTraffic(getInternetTraffic());
}
code0203PhoneCard.setAccountBalance(code0203PhoneCard.getAccountBalance()-(quantity*expenses));
System.out.println("==========================歷史流量使用情況================================");
System.out.printf("歷史流量使用情況:%dGB,賬戶余額%d元\n",code0203PhoneCard.getInternetTraffic(),code0203PhoneCard.getAccountBalance());
}

}
 
         
--------------------------------------測試類-------------------------------------------------------
 
         
package com.lagou.module02;

/**
* 接口
* 1、通話服務接口(通話時間,手機卡類型) Code0203CallInterface
* 2、上網流量接口(上網流量,手機卡類型) Code0203InternetInterface
* 抽象類
* 1、抽象類 Code0203Abstract
* 普通類
* 1、通話套餐類 Code0203CallPackage 實現抽象類/通話服務接口 抽象類方法未重寫
* 2、上網套餐類 Code0203InternetPackage 實現抽象類/上網服務接口 抽象類方法未重寫
* 3、用戶消費類 Code0203Consumption 未實現統計方法
* 4、手機卡類 Code0203PhoneCard
* 枚舉類
* 1、枚舉類 Code0203Enum
*/
public class Code0203Test {
public static void main(String[] args) {
System.out.println("============================注冊手機==================================");
// 訂購通話套餐
Code0203Abstract code0203CallPackage = new Code0203CallPackage(1,0,0);
// 訂購上網套餐
Code0203Abstract code0203InternetPackage = new Code0203InternetPackage(1,0);
// 創建消費統計
Code0203Consumption code0203Consumption = new Code0203Consumption(code0203CallPackage.getQuantity(),code0203InternetPackage.getQuantity(),20);
// 注冊一張手機卡,枚舉手機卡類型:大卡
Code0203PhoneCard phoneCard = new Code0203PhoneCard(Code0203Enum.B.getSize(),"10000","用戶名","密碼",1000,
code0203CallPackage.getQuantity(),code0203InternetPackage.getQuantity());
// 打印手機卡信息
phoneCard.show(code0203CallPackage.getQuantity(),code0203InternetPackage.getQuantity());

// 消費
phoneCard.addCall(120,phoneCard);
phoneCard.addInternet(20,phoneCard);

//顯示賬戶余額
phoneCard.showAccountBalance();

//再次消費測試
phoneCard.addCall(120,phoneCard);
phoneCard.addInternet(20,phoneCard);
phoneCard.addCall(120,phoneCard);
phoneCard.addInternet(20,phoneCard);
phoneCard.showAccountBalance();
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM