当前位置:网站首页>Day11作业
Day11作业
2022-04-21 14:20:00 【Hello World️】
A:简答题
请解释Object类中的下列方法
public boolean equals(Object obj)
比较两个对象是否相等
public final Class getClass()
一个对象编译之后的字节码
表示此对象的运行时类的类对象
public int hashCode()
返回对象的int类型的哈希码值(是根据对象的地址值计算的)
类似对象的地址值
不同的对象哈希码值不同
public String toString()
打印对象的信息
返回该对象的信息由字符串表示
B:看程序写结果(写出自己的分析理由),程序填空,改错,看程序写结果。
1、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。
class Demo {
public static void main (String[] args) {
int a = 10,b = 20;
change(a, b);
System.out.println("a=" + a + ",b=" + b);
String s1 = "aaa";
String s2 = "bbb";
change(s1, s2);
System.out.println("s1=" + s1 + ",s2=" + s2);
int[] arr = {
1, 2, 3, 4, 5};
change( arr );
System.out.println(arr[3]);
}
public static void change(int x, int y){
x = 100;
y = 200;
}
public static void change (String s1, String s2) {
s1 = "java";
s2 = "android";
}
public static void change(int[] arr) {
for (int i=0; i<arr.length; i++) {
arr[i] *= 2;
}
}
}
/* a=10,b=20 s1=aaa,s2=bbb 8 */
C:编程题
1、编写java程序,分析如下需求,写出你分析的类有哪些,以及功能。最后写一个测试类,针对每个类的功能进行测试。
A:自己新建一个项目:eclipse_test
B:在项目的src目录下建立一个包:cn.itcast
C:在包cn.itcast下建立一个学生类:Student
包含成员变量:name,age,sex
请给出构造方法:无参构造,带三个参数的构造
请给出get/set方法:
并给出一个show方法。用于显示学生类的所有成员变量值。
D:写一个测试类:StudentTest
在里面对学生类的成员赋值并使用。
package cn.itcast;
public class StudentTest {
public static void main(String[] args) {
Student student = new Student("张三", 15, '男');
System.out.println(student.show());
}
}
//name=张三 age=15 sex=男
package cn.itcast;
public class Student {
//三个属性
private String name;
private int age;
private char sex;
//无参,有参构造
public Student() {
}
public Student(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
//get/set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
//show方法 显示学生类的所有成员变量值
public String show() {
return "name=" + name +
" age=" + age +
" sex=" + sex ;
}
}
2、编写java程序,分析如下需求,写出你分析的类有哪些,以及功能。最后写一个测试类,针对每个类的功能进行测试。
动物园里有很多种动物:
比如说,狗,猫等。
狗有姓名和年龄,猫也有姓名和年龄。
狗有跑步的方法,猫也有跑步的方法。而且都仅仅是跑步。
狗有吃饭的方法,猫也有吃饭的方法。只不过,狗吃骨头,猫吃鱼。
请用所学知识,对这个问题进行解决。
抽象类:
姓名,年龄
跑步,
抽象吃饭。
狗类:具体吃饭
猫类:具体吃饭
public class Test {
public static void main(String[] args) {
Animal dog = new Dog("大黄", 2);
dog.run();
dog.eat();
Animal cat = new Cat("小花", 1);
cat.run();
cat.eat();
}
}
/* 大黄跑步 狗吃骨头 小花跑步 猫吃鱼 */
//Animal类
public abstract class Animal {
//共有属性
private String name;
private int age;
//共有方法
public void run(){
System.out.println(name + "跑步");
}
//抽象方法
public abstract void eat();
//有参构造
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
//get/set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//Dog类
public class Dog extends Animal{
public Dog(String name, int age) {
super(name, age);
}
//重写抽象类方法
@Override
public void eat() {
System.out.println("狗吃骨头");
}
}
//Cat类
public class Cat extends Animal {
public Cat(String name, int age) {
super(name, age);
}
//重写抽象类方法
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
3、编程练习
A:数组遍历
int[] arr = {98,23,16,35,72};
写一个对象数组操作的功能,实现把数组中的数据按照如下格式返回:
[98, 23, 16, 35, 72]
B:查找数组中指定元素第一次出现的索引值。
int[] arr = {98,23,16,35,72};
查找23在数组中的索引值。
public class Test {
public static void main(String[] args) {
int[] arr = {
98,23,16,35,72};
traverse(arr);
find(arr, 23);
}
/* 98 23 16 35 72 找到了该数组的元素下标:1 */
//A:数组遍历
public static void traverse(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
//B:查找数组中指定元素第一次出现的索引值。
public static void find(int[] arr, int a){
//a代表数组元素
int index = -1;//记录元素的下标值
for (int i = 0; i < arr.length; i++) {
if (arr[i] == a){
index = i;
}
}
//进行判断
if (index == -1){
System.out.println("该数组中没有查找的数组元素:" + a);
}else {
System.out.println("找到了该数组的元素下标:" + index);
}
}
}
版权声明
本文为[Hello World️]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_51318597/article/details/124286154
边栏推荐
- Implementation of broadcast and monitoring at source level
- Insect double linked list
- Dynamic creation of array
- 腾讯二面:MySQL的半同步是什么?
- shell sed 和 gawk 编辑器使用
- 无人驾驶虚拟仿真(十四)--图像处理之交通标志牌识别2
- 如何在 OpenShift 中运行 Collabora Office
- Tencent side 2: what is the semi synchronization of MySQL?
- 无人驾驶虚拟仿真(十五)--障碍物检测与识别1
- 干掉项目中杂乱 的 if-else,试试状态模式,这才是优雅的实现方式
猜你喜欢

如何申请免费SSL证书?宝塔面板SSL证书安装部署完整教程

一个悄然崛起的国产软件

In depth analysis of TCP three handshakes, the interviewer applauded

如何关闭VS Code eslint校验,快来看看吧

Unmanned virtual simulation (XVI) -- obstacle detection and recognition 2

两天两夜,1M图片优化到100kb

从源码里的一个注释,我追溯到了12年前,有点意思

ROS2学习笔记(九)-- ROS2命令行操作常用指令总结(二)

软件测试入职2个月想辞职了
![[groovy] mop meta object protocol and meta programming (use groovy meta programming to intercept functions and call other methods of the class through metaclass invokemethod method)](/img/d1/4944c77d1daf3d6ee1457a7934954f.png)
[groovy] mop meta object protocol and meta programming (use groovy meta programming to intercept functions and call other methods of the class through metaclass invokemethod method)
随机推荐
录制你的第一个web 自动化测试用例
[groovy] mop meta object protocol and meta programming (use groovy meta programming to intercept functions and call other methods of the class through metaclass invokemethod method)
A quietly rising domestic software
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
word输入公式快捷键
ROS2学习笔记(七)-- 自定义动作实现路口转弯
顺序表--链表实现
一个悄然崛起的国产软件
Worm ring list
技术分享 | SeleniumIDE用例录制
Use go language to complete the student information management system through restful API
2023年北京外国语大学汉语国际教育考研上岸前辈备考经验指导
Experience and guidance of seniors preparing for the postgraduate entrance examination of Chinese International Education in Beijing Foreign Studies University in 2023
Vs enterprise code diagram
深度剖析TCP三次握手,面试官拍案叫绝
暴力破解美团最新JVM面试题:无限执行
答应我, 不要再用 if (obj = null) 判空了
使用枚举做的红绿灯,有界面
两天两夜,1M图片优化到100kb
代码重构之引入解释性变量