點擊返回標題->23年Java期末復習-CSDN博客
第1題.
定義一個類Person,定義name和age私有屬性,定義有參的構造方法對name和age進行初始化。在測試類中創建該類的2個對象,姓名、年齡分別為lili、19和lucy、20,在屏幕打印出2個對象的姓名和年齡。
?
public class Main {public static void main(String[] args) {Person p1 = new Person("lily",19);Person p2 = new Person("lucy",20);System.out.println(p1.toString());System.out.println(p2.toString());}
}
class Person{private String name;private int age;//有參構造給對象賦值Person(String name,int age){this.name = name;this.age = age;}//toString返回由name和age組成的預定字符串public String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}
}
第2題.
首先定義一個計算長方形面積的類rectangleClass,要求類中有一個定義長方形左上角和右下角座標的構造函數,以及一個通過長方形右下角座標與左上角座標計算長方形面積,并實例化兩個長方形進行測試.
?
public class Main {public static void main(String[] args) {rectangleClass rec1 = new rectangleClass(-5, 3, -3, 1);rectangleClass rec2 = new rectangleClass(2, -3, 4, -6);System.out.println("rec1" + rec1.getPoint() + "其面積為:" + rec1.getArea());System.out.println("rec2" + rec2.getPoint() + "其面積為:" + rec2.getArea());}
}
class rectangleClass {int x1, y1, x2, y2;//有參構造,定義長方形左上角和右下角座標rectangleClass(int x1, int y1, int x2, int y2) {this.x1 = x1;this.x2 = x2;this.y1 = y1;this.y2 = y2;}//返回計算的面積double getArea() {return Math.abs(x1 - x2) * Math.abs(y1 - y2);}//獲取當前長方形左上角點和右下角點的坐標String getPoint() {return "左上角坐標為:(" + this.x1 + "," + this.y1 + ")," + "右下角角坐標為:(" + this.x2 + "," + this.y2 + "),";}
}
第3題.
設計一個表示圖書的Book類,它包含圖書的書名、作者、月銷售量等私有屬性,另有兩個構造方法(一個不帶參數,另一個帶參數),成員方法setBook( ) 和printBook()分別用于設置和輸出書名、作者、月銷售量等數據。并設計相應的測試Book類的應用程序主類,測試并顯示輸出提供所有功能的結果。
?
public class Main {public static void main(String[] args) {//b1采取有參構造實例化對象并賦值的形式Book b1 = new Book("三體", "劉慈欣", 114514);b1.printBook();//b2采取無參構造實例化對象,調用setBook()成員方法賦值的形式Book b2 = new Book();b2.setBook("龍族", "江南", 10086);b2.printBook();}
}
class Book {private String name, author;private int sale_volume;//無參構造Book() {System.out.println("無參構造被調用");}//有參構造Book(String name, String author, int sale_volume) {System.out.println("有參構造被調用");this.name = name;this.author = author;this.sale_volume = sale_volume;}//設置書本信息void setBook(String name, String author, int sale_volume) {this.name = name;this.author = author;this.sale_volume = sale_volume;}//打印書本信息void printBook() {System.out.printf("書名:%s 作者:%s 月銷售量:%d\n", this.name, this.author, this.sale_volume);}
}
第4題.
請創建一個銀行帳戶類,要求如下:(1)類包括帳戶名、帳戶號、存款額等私有屬性;(3)有三個參數的構造方法(2)可實現余額查詢,存款和取款的操作。(3)創建該類的對象,驗證以上兩項。
?
import java.util.Scanner;
public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);Bank b1 = new Bank("張三", "123456", 100);int input;do {//循環彈出菜單并請求選擇menu();input = sc.nextInt();switch (input) {case 1:b1.getBalance();break;case 2:b1.save_money();break;case 3:if(b1.getBalance()==0){System.out.println("你的賬戶余額為0,無分文可取!");break;}b1.withdraw_money();break;case 0:System.out.println("已退出,歡迎再次使用!");break;default:System.out.println("該選項尚未開發……");break;}} while (input != 0);}public static void menu() {//打印菜單System.out.println("************************");System.out.println("****** 1.查詢余額 ********");System.out.println("****** 2.存款 ********");System.out.println("****** 3.取款 ********");System.out.println("****** 0.退出賬戶 ********");}static class Bank {private String account_name, account_number;private int balance;public Bank(String account_name, String account_number, int balance) {this.account_name = account_name;this.account_number = account_number;this.balance = balance;}int getBalance() {//余額查詢System.out.printf("用戶名:%s,當前余額:%d\n", this.account_name, this.balance);return this.balance;}void save_money() {//存錢int save;Scanner sc = new Scanner(System.in);while (true) {System.out.println("你要存多少錢?請輸入->");save = sc.nextInt();if (save <= 0) {System.out.println("請輸入合理的存款數!");continue;}break;}this.balance += save;System.out.printf("完成存款,本次存款共計%d元,當前余額%d\n", save, this.balance);}void withdraw_money() {//取錢int withdraw;Scanner sc = new Scanner(System.in);while (true) {System.out.println("你要取多少錢?請輸入->");withdraw = sc.nextInt();if (withdraw <= 0) {System.out.println("請輸入合理的取款數!");continue;} else if (withdraw > this.balance) {System.out.println("你沒用這么多錢可供取款!");continue;}break;}this.balance -= withdraw;System.out.printf("完成取款,本次取款共計%d元,當前余額%d\n", withdraw, this.balance);}}
}