课后作业
1.简述什么的类和对象,以及二者之间的关系
答案:
类:具有相同特性(数据元素)和行为(功能)的对象的抽象就是类
对象:对象是用来描述客观事物的一个实体,由一组属性和方法组成
类和对象的关系:类是对象的类型,对象是类的实例
2.教员要求张浩使用面向对象的思想编写一个计算器类(Calculator),可以实现两个整数的加,减,乘,除运算,如果你是张浩,准备如何实现编程
代码如下:
//Calculator类
public class Calculator {
String jia; // 加
String jian; // 减
String cheng; // 乘
String chu; // 除
double num; // 得数
double sum1; // 第一个数
double sum2; // 第二个数
int sum3; // 加减乘除序列号
// Calculator类的方法
public void show() {
switch (sum3) {
case 1:
num = sum1 + sum2; // 加
break;
case 2:
num = sum1 - sum2; // 减
break;
case 3:
num = sum1 * sum2; // 乘
break;
case 4:
num = sum1 / sum2; // 除
break;
default:
break;
}
System.out.println("得数为:" + num);
}
}
import java.util.*;
public class Caloulator1 {
//控制台输出信息
public static void main(String[] args) {
Calculator ps = new Calculator();
ps.jia="+";
ps.jian ="-";
ps.cheng ="×";
ps.chu = "÷";
Scanner input = new Scanner (System.in);
System.out.print("请输入第一个数:");
ps.sum1 = input.nextDouble();
System.out.print("请输入第二个数:");
ps.sum2 = input.nextDouble();
System.out.print("请输入序列号(1.加2.减3.乘4.除):");
ps.sum3 = input.nextInt();
ps.show();
input.close();
}
}
3.假设当前时间是2015年5月12日10点11分00秒,编写一个Current Time 类,设置属性为该时间,定义show( )方法显示该时间
代码如下:
public class CurrentTime {
//时间类
int a, b, c, d, e, f; // 年,月,日,时,分,秒
public void show() { // 时间的方法
System.out.println("现在是北京时间:" + a + "年" + b + "月" + c + "日" + d + "点"
+ e + "分" + f + "秒");
}
}
public class CurrentTime1 {
//控制台输出
public static void main(String[] args) {
// TODO Auto-generated method stub
CurrentTime ae = new CurrentTime();
ae.a=2015; //年
ae.b = 5; //月
ae.c = 12; //日
ae.d = 10; //时
ae.e = 11; //分
ae.f = 00; //秒
ae.show();
}
}
4.改进第三题,将当前时间改进为2015年5月12日10点11分30秒编写一个Demo类,改变CurrentTime类中的设定的时间,并打印输出
代码如下:
public class CurrentTime {
int a, b, c, d, e, f;
public void show() {
System.out.println("当前时间是:" + a + "年" + b + "月" + c + "日" + d + "点" + e + "分" + f + "秒");
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
CurrentTime ae = new CurrentTime();
ae.a=2015; //年
ae.b = 5; //月
ae.c = 12; //日
ae.d = 10; //时
ae.e = 11; //分
ae.f = 00; //秒
ae.show();
}
}
5.使用类的方式描述计算机
代码如下:
public class computer {
String computermodel; // 电脑型号
String operatingsystem; // 操作系统
String CPU; // 处理器
String Amainboard; // 主板
String Memory; // 内存
String Harddisk; // 主硬盘
String GPU; // 显卡
String Monitor; // 显示器
//计算机方法
public void show(){
System.out.println("计算机配置相关信息:");
System.out.println("电脑型号:"+computermodel);
System.out.println("操作系统:"+operatingsystem);
System.out.println("处理器:"+CPU);
System.out.println("主板:"+Amainboard);
System.out.println("内存:"+Memory);
System.out.println("主硬盘:"+Harddisk);
System.out.println("显卡:"+GPU);
System.out.println("显示器"+Monitor);
}
}
public class GTX1060 {
public static void main(String[] args) {
// TODO Auto-generated method stub
computer gtx = new computer ();
gtx.computermodel ="华硕 TUF GAMING FX504GM_FX80GM";
gtx.operatingsystem ="Windows 10 64位";
gtx.CPU ="英特尔 Core i7-8750H @2.20GHz 六核";
gtx.Amainboard ="华硕 FX504GM";
gtx.Memory ="三星 DDR4 2666MHz(16G)";
gtx.Harddisk ="三星 MZVLW256HEHP-00000(256G/固态硬盘)";
gtx.GPU ="Nvidia GeForce GTX 1060(6G)";
gtx.Monitor ="奇美 CMN15F4(15.5寸)";
gtx.show();
}
}
6.某公司要开发新游戏,请用面向对象的思想设计英雄类,怪物类,武器类,编写武器类,创建游戏对象,怪物对象,和武器对象,并输出各自的信息,其中设定如下:
英雄类:
1.属性:英雄名字,生命值
2.方法:输出基本信息
怪物类:
1.属性:怪物名字,生命值,类型
2.方法:输出基本信息
武器类:
1.属性:武器名字,攻击力
2.方法:输出基本信息
代码如下:
public class YouXi {
//英雄类
String Hero ; //英雄名称
int Herolife ; //英雄生命值
//怪物类
String monster; //怪物名称
int monsterlife; //怪物生命值
String Type; //怪物类型
//武器类
String arms; //武器名称
int Aggressivity ; //武器攻击力
public void show (){ //游戏类的方法
System.out.println("我是英雄,我的基本信息如下:"); //英雄方法
System.out.println("姓名:"+Hero+",生命值:"+Herolife);
System.out.println("我是武器,我的基本信息如下:");
System.out.println("武器名:"+arms+",攻击力:"+Aggressivity);
System.out.println("我是怪物,我的基本信息如下:");
System.out.println("姓名:"+monster+",生命值:"+monsterlife+",类型:"+Type);
}
}
public class Game {
public static void main(String[] args) {
// TODO Auto-generated method stub
YouXi game = new YouXi ();
game.Hero="李小侠"; //英雄名
game.Herolife=300; //英雄生命值
game.arms ="死神镰刀"; //武器名
game.Aggressivity = 12; //武器攻击力
game.monster= "小龟"; //怪物名
game.monsterlife=300; //怪物生命值
game.Type = "潜水类"; //怪物类型
game.show();
}
}