Java作业6-Java类的基本概念三

编程1

java 复制代码
import java.util.*;

abstract class Rodent//抽象类
{
	public abstract String findFood();//抽象方法
	public abstract String chewFood();
}
class Mouse extends Rodent
{
	public String findFood()
	{	return "大米";	}
	public String chewFood()
	{	return "Mouse"+"吃"+findFood();	}
}
class Gerbil extends Rodent
{
	public String findFood()
	{	return "种子";	}
	public String chewFood()
	{	return "Gerbil"+"吃"+findFood();	}
}
class Hamster extends Rodent
{
	public String findFood()
	{	return "坚果";	}
	public String chewFood()
	{	return "Hamster"+"吃"+findFood();	}
}
public class Main{
	public static void main(String[] args)
	{
		Rodent[] rodents = new Rodent[3];
		rodents[0] = new Mouse();
		rodents[1] = new Gerbil();
		rodents[2] = new Hamster();
		System.out.println(rodents[0].chewFood());
		System.out.println(rodents[1].chewFood());
		System.out.println(rodents[2].chewFood());
	}
}
bash 复制代码
Mouse吃大米
Gerbil吃种子
Hamster吃坚果

编程2

用接口改造上一题,实现要求的所有功能

接口详细教程Java 接口 | 菜鸟教程 (runoob.com)

java 复制代码
import java.util.*;

interface Rodent
{
	public String findFood();
	public String chewFood();
}
class Mouse implements Rodent
{
	public String findFood()
	{	return "大米";	}
	public String chewFood()
	{	return "Mouse"+"吃"+findFood();	}
}
class Gerbil implements Rodent
{
	public String findFood()
	{	return "种子";	}
	public String chewFood()
	{	return "Gerbil"+"吃"+findFood();	}
}
class Hamster implements Rodent
{
	public String findFood()
	{	return "坚果";	}
	public String chewFood()
	{	return "Hamster"+"吃"+findFood();	}
}
public class Main{
	public static void main(String[] args)
	{
		Rodent[] rodents = new Rodent[3];
		rodents[0] = new Mouse();
		rodents[1] = new Gerbil();
		rodents[2] = new Hamster();
		System.out.println(rodents[0].chewFood());
		System.out.println(rodents[1].chewFood());
		System.out.println(rodents[2].chewFood());
	}
}
bash 复制代码
Mouse吃大米
Gerbil吃种子
Hamster吃坚果

编程3

java 复制代码
import java.util.*;

interface Rodent
{
	public String findFood();
	public String chewFood();
}
interface Fly
{
	public String fly();
}
class Mouse implements Rodent
{
	public String findFood()
	{	return "大米";	}
	public String chewFood()
	{	return "Mouse"+"吃"+findFood();	}
}
class Gerbil implements Rodent
{
	public String findFood()
	{	return "种子";	}
	public String chewFood()
	{	return "Gerbil"+"吃"+findFood();	}
}
class Hamster implements Rodent
{
	public String findFood()
	{	return "坚果";	}
	public String chewFood()
	{	return "Hamster"+"吃"+findFood();	}
}
class Pteromyini implements Rodent,Fly
{
	public String findFood()
	{	return "野果";	}
	public String chewFood()
	{	return "Pteromyini"+"吃"+findFood();	}
	public String fly()
	{	return "会飞";	}
}
public class Main{
	public static void main(String[] args)
	{
		Rodent[] rodents = new Rodent[4];
		rodents[0] = new Mouse();
		rodents[1] = new Gerbil();
		rodents[2] = new Hamster();
		rodents[3] = new Pteromyini();
		System.out.println(rodents[0].chewFood());
		System.out.println(rodents[1].chewFood());
		System.out.println(rodents[2].chewFood());
		System.out.println(rodents[3].chewFood()+" "+((Pteromyini)rodents[3]).fly());
	}
}
bash 复制代码
Mouse吃大米
Gerbil吃种子
Hamster吃坚果
Pteromyini吃野果 会飞

编程4

这道题给我看懵了,直接上AI

java 复制代码
import java.util.Date;

public class MaxFinder {

    // 实现找到Comparable对象数组中的最大元素的方法
    public static Comparable max(Comparable[] a) {
        if (a == null || a.length == 0) {
            return null; // 返回null表示数组为空或未初始化
        }
        Comparable max = a[0]; // 假设第一个元素是最大的
        for (int i = 1; i < a.length; i++) {
            if (max.compareTo(a[i]) < 0) {
                max = a[i]; // 发现更大的元素,更新max
            }
        }
        return max;
    }

    public static void main(String[] args) {
        // 测试字符串数组
        String[] strings = {"apple", "orange", "banana", "zoo", "cherry", "date", "fig", "grape", "honeydew", "kiwi"};
        System.out.println("最大的字符串: " + max(strings));

        // 测试整数数组
        Integer[] integers = {20, 35, 1, 67, 55, 23, 99, 18, 42, 88};
        System.out.println("最大的整数: " + max(integers));

        // 测试日期数组
        Date[] dates = new Date[10];
        for (int i = 0; i < dates.length; i++) {
            // 创建从现在开始的不同天的日期
            dates[i] = new Date(System.currentTimeMillis() + i * 24 * 60 * 60 * 1000);
        }
        System.out.println("最新的日期: " + max(dates));
    }
}
bash 复制代码
最大的字符串: zoo
最大的整数: 99
最新的日期: Mon Apr 22 21:35:46 CST 2024
相关推荐
五行星辰6 分钟前
用 Java 发送 HTML 内容并带附件的电子邮件
java·html
DaphneOdera1711 分钟前
Git Bash 配置 zsh
开发语言·git·bash
Code侠客行18 分钟前
Scala语言的编程范式
开发语言·后端·golang
BestandW1shEs27 分钟前
快速入门Flink
java·大数据·flink
奈葵34 分钟前
Spring Boot/MVC
java·数据库·spring boot
lozhyf37 分钟前
Go语言-学习一
开发语言·学习·golang
小小小小关同学42 分钟前
【JVM】垃圾收集器详解
java·jvm·算法
dujunqiu1 小时前
bash: ./xxx: No such file or directory
开发语言·bash
爱偷懒的程序源1 小时前
解决go.mod文件中replace不生效的问题
开发语言·golang
日月星宿~1 小时前
【JVM】调优
java·开发语言·jvm