练习1:构造 & 方法调用

package com.practice_7;
/**
*ClassName Book*Description 代表教材*@author 阿木木* @date 2020/12/24 10:56*Version 1.0*/
public class Book {String title ;int pageNum = 200;/*** Description 构造函数,完成对象初始化工作**/public Book(String title,int pageNum) {if(pageNum < 200){System.out.println("页数不能小于200");}else{this.pageNum = pageNum;this.title = title;}}/*** Description 用于输出每本教材的名称和页数**/public  String detail(){return "名称:"+this.title +"\t页数:"+ this.pageNum;}}
/***Description 测试类**/
class BookTest{public static void main(String[] args) {Book book = new Book("语文",201);System.out.println(book.detail());}
}

练习 2:构造 & 方法调用

java download(String music){ this.sdcard.writeSdCard(music); }

增加Sdcard类,当手机下载音乐时,把音乐存放到sdcard中。
原手机类增加writeSDcard(….) 和 readSDcard(….) 两个方法。

String[capacity];SdCard(int  capacity){}?|void  read(...??) ?|void  write(…?) } ```

源代码:

package com.phone;/*** ClassName Phone* Description 手机* @author 阿木木* @date 2020/12/24 11:20* Version 1.0*/
public class Phone {String name;int price;Cell cell;SdCard sdCard;public Phone(String name, int price) {this.name = name;this.price = price;}public Phone(String name, int price, Cell cell) {this.name = name;this.price = price;this.cell = cell;this.sdCard = new SdCard(32);}public Phone(String name, int price, Cell cell, SdCard sdCard) {this.name = name;this.price = price;this.cell = cell;this.sdCard = sdCard;}/*** 获得电池* @return 电池*/public Cell getCell() {return this.cell;}void charging(int power) {if (this.cell.getPower()<100) {// 总电量 = 先获电池当前电量 + 充入的电量this.cell.setPower( this.cell.getPower() + power);System.out.println("当前电量" + this.getCell().getPower() );} else {System.out.println(this.cell.name + "已充满。" );}}/*** 下载歌曲*/public void download(String songName) {this.sdCard.storeSong(songName);}/*** 返回存储卡中的所有歌曲*/public String[] listSong() {return this.sdCard.songs;}/*** 播放指定位置的歌曲* @param index 歌曲位置*/public void playSong(int index){if (index >= 0 && index < this.sdCard.songs.length) {if (this.sdCard.songs[index] != null){System.out.println("播放歌曲:" + this.sdCard.songs[index]);} else {System.out.println("指定歌曲不存在!");}}else {System.out.println("指定歌曲不存在!");}}@Overridepublic String toString() {return "Phone{" +"name='" + name + '\'' +", price=" + price +", " + cell +   // cell.toString()'}';}
}
package com.phone;/*** ClassName SdCard* Description 手机电池* @author 阿木木* @date 2020/12/24 11:30* Version 1.0*/
public class SdCard {int capacity = 100;String[] songs;public SdCard(int capacity) {this.capacity = capacity;this.songs = new String[capacity];}/*** 获取容量* @return*/public int getCapacity() {return capacity;}public void storeSong(String song) {for (int i = 0; i < this.songs.length; i++) {if (this.songs[i] == null) {this.songs[i] = song;break; // 存入后要立即退出循环}}}}
package com.phone;/*** ClassName Cell* Description 手机测试* @author 阿木木* @date 2020/12/24 11:35* Version 1.0*/
public class Cell {String name;int power;public Cell(String name, int power) {this.name = name;this.power = power;}/*** 获得电池的电量* @return 电量*/public int getPower() {return this.power;}/*** 设置电池的电量** @param power 电量*/public void setPower(int power) {this.power = power;}@Overridepublic String toString() {return "Cell{" +"name='" + name + '\'' +", power=" + power +'}';}
}
package com.phone;/*** ClassName Test* Description 手机测试* @author 阿木木* @date 2020/12/24 12:03* Version 1.0*/
public class Test {public static void main(String[] args) throws InterruptedException {Phone phone = new Phone("IPhone XS", 8888, new Cell("飞毛腿电池", 80));System.out.println(phone);do {int randomPower = (int)(Math.random()*10);phone.charging(randomPower);Thread.sleep(1000);} while (phone.getCell().getPower()<100);System.out.println(phone.name + "当前电量" + phone.getCell().getPower() );phone.download("太阳");phone.download("夜半小夜曲");phone.download("花海");String[] songs = phone.listSong();for (String song : songs) {if (song != null) {System.out.println(song);}}phone.playSong(5);phone.playSong(1);}}

练习3:方法 (调皮的猪Pig)
在上一章作业的基础上,改进Pig 类,可以完成
(1) 可以增加自己的体重/减轻自己的体重
(2)可以打印金字塔,层数随意
*
***
*****
*******
*********
源代码:

package com.practice_7;/*** ClassName Pig* Description* @author 阿木木* @date 2020/12/24 18:53* Version 1.0*/
public class Pig {public  String name = "石都林";public  int age = 19;public  double weight = 120.0;public  String color = "black";/***Description 增加自己的体重/减轻自己的体重**/public  void changeWeight(int weight){this.weight +=weight;}/***Description 打印金字塔,层数随意**/public  void sanJiaoXing() {for (int i = 0; i < 10; i++) {{for (int j = 1; j <= 10 - i; j++) {System.out.print(" ");for (int x = 1; x <= 2 * i - 1; x++) {System.out.print("*");}System.out.println("");}}}}public static void main(String[] args) {Pig pig = new Pig();pig.changeWeight(-10);pig.sanJiaoXing();}
}

练习4:方法 (聪明的小猫)
请编写一个Cat 类,要求如下:
该猫可以做四则运算,
也可以进行面积计算。
如图:
将四则运算器和面积运算器合二为一,作一个运算器,主菜单让用户选择是做四则运算还是面积运算,分为两个子菜单,让用户选择加减乘除或者形状。如下界面:


源代码:

package com.practice_7;import java.util.Scanner;/*** ClassName SmartCat* Description** @author 阿木木* @date 2020/12/24 19:09* Version 1.0*/
public class SmartCat {/***Description 四则运算**/static Scanner input = new Scanner(System.in);public static void calculate(){System.out.println("1.加法\n2.减法\n3.乘法\n4.除法");int n = input.nextInt();int x ;int y;switch (n){case 1: {System.out.println("请输入两个运算数:");x = input.nextInt();y = input.nextInt();System.out.println(x + y);break;}case 2: {System.out.println("请输入两个运算数:");x = input.nextInt();y = input.nextInt();System.out.println(x - y);break;}case 3: {System.out.println("请输入两个运算数:");x = input.nextInt();y = input.nextInt();System.out.println(x * y);break;}case 4: {System.out.println("请输入两个运算数:");x = input.nextInt();y = input.nextInt();System.out.println(x / y);break;}default:break;}}/***Description  运算面积**/public static void calculateArea(){System.out.println("1.三角形");System.out.println("2.正方形");System.out.println("3.矩形");System.out.println("4.圆形");int n = input.nextInt();double x ,y;switch (n){case 1: {System.out.println("请输入三角形的底和高");x = input.nextInt();y = input.nextInt();System.out.println((x * y) / 2);break;}case 2: {System.out.println("请输入正方形的边长");x = input.nextInt();System.out.println((x * x));break;}case 3: {System.out.println("请输入矩形的长和宽");x = input.nextInt();y = input.nextInt();System.out.println((x * y));break;}case 4: {System.out.println("请输入圆的半径");x = input.nextDouble();System.out.println(3.14 * Math.pow(x, 2));break;}default:break;}}public static void main(String[] args) {System.out.println("请输入运算种类:\n1.四则运算:\n2.计算面积:");int n = input.nextInt();if(n == 1) {calculate();} else if(n == 2) {calculateArea();}}
}

练习5 API综合应用
编写一个方法参数接收身份证号,将身份证号中的出生日期转换成Date对象并返回该Date对象。

*/ public Date getBirthdayFromIdNo(String idNo){    // 代码逻辑略 //身份证号的正则表达式 //language=RegExp String regex =
"^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$";if (id.matches(regex)) {}return ?; } ```

源代码:

package com.practice_7;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;/*** ClassName MyUsing* Description** @author 阿木木* @date 2020/12/24 20:16* Version 1.0*/
public class MyUsing {/***@param idNo 身份证号*/public Date getBirthdayFromIdNo(String idNo){String str = idNo.substring(6,10)+idNo.substring(10,12)+idNo.substring(12,14);String regex = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$";Date date = new Date();//获得SimpleDateFormat类,我们转换为yyyy-MM-dd的时间格式SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");if (idNo.matches(regex)) {try {//使用SimpleDateFormat的parse()方法生成Datedate = sf.parse(str);return date;} catch (ParseException e) {e.printStackTrace();}}return date;}static Scanner input = new Scanner(System.in);public static void main(String[] args) {MyUsing date = new MyUsing();String idNo = input.next();System.out.println(date.getBirthdayFromIdNo(idNo));}}

JavaSE练习—构造函数与方法调用相关推荐

  1. java super.start,java – 在字节码中确定哪里是super()方法调用所有构造函数必须在JVM上执行...

    实际上,字节码构造函数的规则比Java的规则要宽松得多. 唯一的规则是必须在任何正常返回的路径上调用一个构造函数,如果构造函数调用抛出异常,那么您也必须抛出异常. 除此之外,这意味着构造函数可能包含对 ...

  2. JS-面向对象-This的指向---简单的函数调用 / 作为对象的方法调用时 / 作为构造函数调用时

    简单的函数调用 <!DOCTYPE html> <html lang="zh"><head><meta charset="UTF ...

  3. c++ 重载 重写_Java | 深入理解方法调用的本质(含重载与重写区别)

    前言 对于习惯使用面向对象开发的工程师们来说,重载 & 重写 这两个概念应该不会陌生了.在中 / 低级别面试中,也常常会考察面试者对它们的理解(隐约记得当年在校招面试时遇到过): 网上大多数资 ...

  4. C#中一些易混淆概念总结--------数据类型存储位置,方法调用,out和ref参数的使用...

    这几天一直在复习C#基础知识,过程中也发现了自己以前理解不清楚和混淆的概念.现在给大家分享出来我的笔记: 一,.NET平台的重要组成部分都是有哪些 1)FCL (所谓的.NET框架类库) 这些类是微软 ...

  5. 【Android 逆向】ART 脱壳 ( InMemoryDexClassLoader 脱壳 | DexFile 构造函数及相关调用函数 | Android 源码中查找 native 函数 )

    文章目录 一.DexFile 构造函数 二.DexFile.openInMemoryDexFile 函数 三.Android 源码中查找 native 函数 一.DexFile 构造函数 上一篇博客 ...

  6. JAVA方法调用中的解析与分派

    JAVA方法调用中的解析与分派 本文算是<深入理解JVM>的读书笔记,参考书中的相关代码示例,从字节码指令角度看看解析与分派的区别. 方法调用,其实就是要回答一个问题:JVM在执行一个方法 ...

  7. 系统间通信2:通信管理与远程方法调用RMI

    本文引用 : https://yinwj.blog.csdn.net/article/details/49120813 RMI : Remote Method Invocation,远程方法调用 RP ...

  8. JVM学习笔记之-运行时数据区概述及线程概述,程序计数器(PC寄存器),虚拟机栈(栈,局部变量表,操作数栈,动态连接,方法调用,方法返回地址等),本地方法接口,本地方法栈

    运行时数据区概述及线程概述 内存是非常重要的系统资源,是硬盘和CPU的中间仓库及桥梁,承载着操作系统和应用程序的实时运行.JVM内存布局规定了Java在运行过程中内存申请.分配.管理的策略,保证了JV ...

  9. @async方法不调用了_在Spring中使用Future对象调用Async方法调用

    @async方法不调用了 下一个示例将演示Spring容器内部的异步方法调用. 为什么我们需要异步方法调用? 在某些情况下,我们并不真正知道是否需要重播或何时应返回结果. 传统方式在Java EE世界 ...

最新文章

  1. Failed to resolve:com.android.support:appcompat-v7
  2. java中 queryparam_@PathParam 和 @QueryParam
  3. 使用NUnit做单元测试(总结版)
  4. 001 java_001Java开发环境
  5. linux wkhtmltopdf换字体,ubuntu – 更新后Wkhtmltopdf字体大小增加
  6. ASP.NET MVC view引入命名空间
  7. 在JavaScript中操作Cookie
  8. Mybatis 处理日期格式自动转换
  9. mysql 统计本月的_MySql查询当天、本周、本月、本季度、本年的数据
  10. Windows10内存泄漏,分页池高分析及解决方案
  11. C# winform开发的考试系统
  12. 所有程序员都应该知道的 6 个软件开发步骤
  13. vivado程序固化到flash
  14. word中安装Zotero插件
  15. 开源人物之九:赖霖枫
  16. 计算机病毒1000字,《大鱼海棠》观后感1000字
  17. 配置线在计算机端 使用什么端口,交换机怎么配置?这几种方式供你使用!
  18. 如何写好技术文档——来自Google十多年的文档经验
  19. 3D采集设备(三)激光雷达LMS511连接配置、控制指令和数据解码
  20. Unity3D游戏制作-----环境搭建

热门文章

  1. 如何卸载 没有卸载程序或者提示需要使用msiexec来卸载
  2. Github建立本地仓库上传代码
  3. Linux驱动进阶学习--HDMI设备之edid相关开发
  4. 2022内蒙古最新水利水电施工安全员模拟考试试题及答案
  5. php 支付宝回调验证失败,支付宝回调验证签名失败怎么解决?
  6. 报名 | MTK、Linaro、诚迈科技、AlphaSTAR极客社区携手带来MediaTek X20开发板技术公开课(上海站)
  7. centos 设置mtu_linux下修改mtu值
  8. 本次操作由于这台计算机的限制而被取消 win8,处理word超链接“本次操作由于这台计算的限制而被取消”的方法...
  9. 中断不可睡眠的一些理解
  10. 团体程序设计天梯赛 L2-013 红色警报