当前位置: 首页 > news >正文

Java期末复习题之封装

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

http://www.lryc.cn/news/256423.html

相关文章:

  • 湖科大计网:计算机网络概述
  • 每日一道c语言
  • (C)一些题11
  • 多级路由component页面不加载
  • 【原创】Mac mini M1安装home-brew
  • 【python交互界面】实现动态观察图像在给定HSV范围的区域显示
  • Vue3中定义变量是选择ref还是reactive?
  • 数据结构 | 查漏补缺之哈希表、最短路径、二叉树与森林的转换
  • SpringCloud
  • fastadmin嵌套关联查询,thinkPHP5嵌套关联查询
  • Power BI - 5分钟学习拆分列
  • ELK(四)—els基本操作
  • 【100天精通Python】Day75:Python机器学习-第一个机器学习小项目_鸾尾花分类项目(上)
  • gitlab高级功能之容器镜像仓库
  • 线程的使用(二)
  • k8s之镜像拉取时使用secret
  • mysql面试题——MVCC
  • 【华为数据之道学习笔记】1-2华为数字化转型与数据治理
  • 微服务01
  • 作业12.8
  • 已解决error: (-215:Assertion failed) inv_scale_x > 0 in function ‘cv::resize‘
  • Android View.inflate 和 LayoutInflater.from(this).inflate 的区别
  • etcd 与 Consul 的一致性读对比
  • Docker 安装Apache Superset 并实现汉化和快速入门
  • 差异计算基础知识 - 了解期末业务操作、WIP 和差异
  • spring boot定时器实现定时同步数据
  • 第一百九十六回 通过蓝牙发送数据的细节
  • 26.Python 网络爬虫
  • Spring Boot 在启动之前还做了哪些准备工作?
  • SQL语句常用语法(开发场景中)