第十一章作业

这篇博客展示了如何使用面向对象编程思想设计计算器类、当前时间类以及游戏中的英雄、怪物和武器类。内容包括类的定义、属性和方法的实现,以及对象的创建和交互。通过具体的实例,解释了类与对象的关系以及如何在Java中实现这些概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.简述什么是类和对象,以及二者之间的关系。
答案:类是一个抽象的概念
而对象是类抽象概念的实物表达

2.2.老师要求张浩使用面向对象的思想编写一个计算器类(Calculator),可以实现两个整数的加,减,乘,除的运算.。
答案:package lenovo_11;

import java.util.Scanner;

public class Calculator {
int zheng1;
int zheng2;
int zheng3;
public void show(){
Scanner input=new Scanner(System.in);
System.out.println(“请输入您的选择:1是加,2是减,3是乘,4是除”);
while(true){int xuan=input.nextInt();
switch(xuan){
case 1:
zheng3=zheng1+zheng2;
System.out.println(zheng1+"+"+zheng2+"="+zheng3);
break;
case 2:
zheng3=zheng1-zheng2;
System.out.println(zheng1+"+"+zheng2+"="+zheng3);
break;
case 3:
zheng3=zheng1zheng2;
System.out.println(zheng1+"
"+zheng2+"="+zheng3);
break;
case 4:
zheng3=zheng1/zheng2;
System.out.println(zheng1+"/"+zheng2+"="+zheng3);
break;
default:
System.out.print(“请输入有效的数字”);

   }

}}
}

package lenovo_11;

import java.util.Scanner;

public class Calculator1 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input=new Scanner(System.in);
	 Calculator calculator=new  Calculator();
	 System.out.print("请输入你想要计算的第一个整数:");
	 calculator.zheng1=input.nextInt();
	 System.out.print("请输入你想要计算的第二个整数:");
	 calculator.zheng2=input.nextInt();
	 calculator.show(); 

}

}
3.3.假设当前时间是2015年5月12日11分00秒,编写一个CurrentTime类,设置属性为该时间,定义show()方法显示该时间。
答案:package lenovo_11;

public class CurTime {
int year;
int month;
int day;
int hours;
int min;
int second;
public void show(){
System.out.println(year+“年”+month+“月”+day+“日”+hours+“时”+min+“分”+second+“秒”);
}

}

package lenovo_11;

public class CurTime1 {
String time;
public void show(){
System.out.println(time);
}

}
4.4.改进第3题,将当前时间改为2015年5月12日11分30秒。编写一个Dome类,改变CurrentTine类中设定的时间,并打印输出。
答案:package lenovo_11;

public class CurTime3 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	CurTime1 Curtime=new CurTime1();
	Curtime.time="2015年5月12日10点11分00秒";
	Curtime.show();

}

}

package lenovo_11;

import java.util.Scanner;

public class CurTime4 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	CurTime1 Curtime=new CurTime1();
	System.out.println("当前时间为:");
	Curtime.show();
	CurTime curtime1=new CurTime();
	System.out.println("你想改的年月日时分秒");
	Scanner input=new Scanner(System.in);
	System.out.print("年");
	curtime1.year=input.nextInt();
	System.out.print("月");
	curtime1.month=input.nextInt();
	System.out.print("日");
	curtime1.day=input.nextInt();
	System.out.print("时");
	curtime1.hours=input.nextInt();
	System.out.print("分");
	curtime1.min=input.nextInt();
	System.out.print("秒");
	curtime1.second=input.nextInt();
	curtime1.show();
	

}

}

5.5.是用类的方式描述计算机
答案:package lenovo_11;

import java.util.Scanner;

public class Computer {
String[] info=new String[]{“主板”,“CPU”,“显示器”,“硬盘”,“内存”};
public void showinfo(){
Scanner input=new Scanner (System.in);
for (int i = 0; 1 < info.length-1; i++) {
System.out.println(“请输入您的”+info[i]);
info[i]=input.next();

}

}

}

package lenovo_11;

public class Computer1 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Computer Computer1=new Computer();
	Computer1.showinfo();

}

}
6.6.某公司要开发新游戏,请用面向对象的思想设计英雄类,怪物类和武器类
答案:package lenovo_11;

public class zhandou {
String name=“天基炮”;
int ll=99999999;
String info=“卫星轨道炮,超高伤害,毁灭一切”;
public void show() {
System.out.println(“武器信息:”);
System.out.println(name+" 攻击力:"+ll+" 效果:"+info+"\n");
}

}
package lenovo_11;

public class zhandou2 {
String name=“汤米”;
int sm=99999999;
String info=“无所不能,易溶于水”;
public void show() {
System.out.println(“英雄信息:”);
System.out.println(name+" 生命值:"+sm+" 基本介绍:"+info+"\n");
}

}package lenovo_11;

public class zhandou3 {
String name=“牛头人”;
int sm=541;
String info=“小怪类”;
public void show() {
System.out.println(“怪物信息:”);
System.out.println(name+" 生命值:"+sm+" 怪物类型:"+info+"\n");
}

}
package lenovo_11;

public class zhandou4 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	zhandou zhandou4=new zhandou();
	System.out.print("我是武器,");
	zhandou4.show();
	zhandou2 zhandou5=new zhandou2();
	System.out.print("我是英雄,");
	zhandou5.show();
	zhandou3 zhandou6=new zhandou3();
	System.out.print("我是怪物,");
	zhandou6.show();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值