点击返回标题->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);
}
}
}