练习1:
//工具类
package day07;
/* 题目要求:
*
* 1. 定义一个工具类
* 其中提供了对于整形数组和double类型数组的一些工具方法
* 方法分别有:
* 1.求数组值的和
* 2.求数组中的最大值
* 3.对数组进行升序排序
* 4.对数组进行倒序排序(也就是反转数组)
* 工具类要求:
* a.私有化构造方法
* b.不希望被继承
*/
/*
* 分析:对于整形数组和double类型数组的方法:涉及重载
* a.私有化构造方法:不能创建对象
b.不希望被继承:final关键字修饰
*/
public final class Array {
private Array() {
// 类私有化
}
// 1---数组求和方法(int 类型)
public static int arraySum(int[] a) {
int sum = 0;// 为什么private int sum=0;不合法
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
// 1---方法重载:数组求和方法(double类型)
public static double arraySum(double[] a) {
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
// 2---数组最大值的方法(int 类型)
public static int arrayMax(int[] a) {
int temp = a[0]; // 参量比较
for (int i = 1; i < a.length; i++) {
if (a[i] > temp) {
temp = a[i];
}
}
return temp;
}
// 2---重载:数组最大值的方法(double类型),原来对数组元素进行了错误的操作
public static double arrayMax(double[] a) {
double temp = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > temp) {
temp = a[i];
}
}
return temp;
}
// 3---数组升序排列(int 类型),采用冒泡排序方式
public static void arraySort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int temp = 0;
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
}
// 3---重载:数组升序排列(double 类型),采用冒泡排序方式
public static void arraySort(double[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
double temp = 0;
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
}
// 4---对数组进行倒序排序(也就是反转数组)int类型
public static void arrayIeversal(int[] a) {
for (int i = 0; i < a.length / 2; i++) {
int temp = 0;
temp = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = temp;
}
}
// 4---重载:对数组进行倒序排序(也就是反转数组)double类型
public static void arrayIeversal(double[] a) {
for (int i = 0; i < a.length / 2; i++) {
double temp = 0;
temp = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = temp;
}
}
}
测试程序:
package ceshi;//此源文件所在的包(ceshi),必须是源文件有效代码的第一行
/*
* 此测试仅以int型的数据类型进行测试
*/
import day07.Array;//导入自己自己创建工具类所在的包(因为测试与工具类不在一个包)
import java.util.Random;
import java.util.Arrays;
public class Test00 {
public static void main(String[] args) {
/*
* Array array=new Array();构造函数不可视,不能创建对象
*/
// 数组随机生成
int[] arr = new int[10];
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(100);
// arr[i] = random.nextFloat(100);//double类型
}
System.out.println("数组原始排列:" + Arrays.toString(arr));
System.out.println("数组的和:" + Array.arraySum(arr));
System.out.println("数组的最大值:" + Array.arrayMax(arr));
/*
Array.arraySort(arr);// 调用工具类中的升序排序方法
System.out.println("数组升序排列:" + Arrays.toString(arr));
//升序排列通过引用操作了数组元素
//这是对升序排列后的数组进行逆序排列,若想对原序列进行排序,对其进行注释即可
*/
Array.arrayIeversal(arr);
System.out.println("数组逆序排列:" +Arrays.toString(arr));
}
}
/*
* class ZiArray extends Array { //final类不能不继承 }
*/
Eclipse编译结果:
注:由于数组是随机生成的,所以每次的结果不尽相同
练习2:
package day07;
/* 2. a.定义一个英雄类 Hero
属性:(全部私有,提供公共方法让外部访问)年龄, 血量 ,攻击力,防御力
方法: 释放技能,加血.
必须至少包含一个构造方法,且该构造方法可以初始化所有四个成员变量
b.定义一个类BatMan继承Hero类
方法:飞行(方法中输出一行打印"飞行")
c.定义一个SuperBatMan类继承 BatMan类
方法:重写飞行方法(方法中输出一行打印"超级飞行")
最终分别创建BatMan对象和SuperBatMan对象,并调用飞行方法.
*/
public class Game {
public static void main(String[] args) {
BatMan batman = new BatMan();
batman.flight();
SuperBatMan superbatman = new SuperBatMan();
superbatman.flight();// 方法覆盖
}
}
class Hero {
private int age; // 年龄
private int blood;// 血量
private int atk; // 攻击力
private int def; // 防御力
Hero() {
// 默认构造方法(习惯性的添加),如果有其它构造方法,jvm不会默认添加
}
Hero(int age, int blood, int atk, int def) {
// 初始化方式1:构造方法初始化成员变量
this.age = age;
this.blood = blood;
this.atk = atk;
this.def = def;
}
//初始化方式2:通过公共方法
// 提供公共方法供外部访问:年龄(age)
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
// 提供公共方法供外部访问:血量(blood)
public void setBlood(int blood) {
this.blood = blood;
}
public int getBlood() {
return this.blood;
}
// 提供公共方法供外部访问:攻击力(atk)
public void setAtk(int atk) {
this.atk = atk;
}
public int getAtk() {
return this.atk;
}
// 提供公共方法供外部访问:防御力(def)
public void setDef(int def) {
this.def = def;
}
public int getDef() {
return this.def;
}
// 释放技能
public void cast() {
System.out.println("释放技能");
}
// 加血
public int addBlood(int blood, int add) {
return blood + add;
}
}
class BatMan extends Hero {
public void flight() {
System.out.println("飞行");
}
BatMan() {
// super();// 调用父类默认的构造方法(此行要不要均可)
super(20, 100, 20, 10);// 调用父类有参的构造方法
}
}
class SuperBatMan extends BatMan {
// 方法覆盖
public void flight() {
System.out.println("超级飞行");
}
}
编译结果:
练习3:
import java.util.Scanner;//工具类
import java.util.Random;//工具类
/*3. 实现一个猜数的小游戏,随机产生一个数(a)。
* Scanner 的方式来输入一个数字,并提供反馈,
* 告诉用户该输入的值比a大还是比a小,直到最终用户猜中,显示结果.
* //对100以内(包括100)自然数的猜测
*/
public class GuessNumber {
public static void main(String[] args) {
// 随机数的产生
Random random = new Random();
final int a = random.nextInt(101);// 系统随机产生的数字
for (int j = 0;; j++) {
Scanner sc = new Scanner(System.in);
System.out.print("用户输入的数字:");
int admiSystemIn = sc.nextInt();// nextInt()非静态方法
j++;
if (a == admiSystemIn) {
// System.out.println("您猜的次数为"+j+"击败了"+j+"%的用户,奖励小红花一朵!");
System.out.println("恭喜你猜对了,奖励小红花一朵!");
break;
} else if (a > admiSystemIn) {
System.out.println("输入的值比a小,继续努力哟!");
} else {
System.out.println("输入的值比a大,继续努力哟!");
}
System.out.println("用户准备下一次输入----------");
}
}
}
编译结果:
注:因为数字是随机生成的,所以每次测试结果不尽相同