面向对象第一章课后习题

package Homework;


import java.util.Scanner;


public class Calculator {     //(题目一:用面向对象的思想编写一个计算器)
int num1;
int num2;
double num3;


public void add() { // 加法
System.out.print("和为:" + (num1 + num2));


}


public void subt() { // 减法
System.out.print("相减为:" + (num1 - num2));
}


public void mlt() { // 乘法
System.out.print("相乘为:" + (num1 * num2));
}


public void dme() { // 除法
num3 = (num1*1.0)/(num2*1.0);
System.out.print("相除为:" + num3);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Calculator calculator = new Calculator();  //创造一个计算器类的对象来接收
System.out.print("请输入第一个整数:");
calculator.num1 = input.nextInt();
System.out.print("请输入第二个整数:");
calculator.num2 = input.nextInt();
System.out.print("请输入您需要进行的运算方式:");
String count = input.next();
switch (count) {

case "+":
calculator.add();

break;
case "-":
calculator.subt();
case "*":
calculator.mlt();
case "/":
calculator.dme();
default:
break;
}




}

}


*************************************************************************************



package Homework;

//(题目二:编写一个CurrentTime类 设置属性该时间,定时show( )方法显示时间)

public class CurrentTime {     //类
int year = 2015;
int month = 5;            //属性
int day = 12;
int clock = 10;
int minute = 11;
int second = 00;

public void show() {     //方法

System.out.print("当前时间为:"+year+"年"+month+"月"+day+"天"+clock+"点"+minute+"分"+second+"秒");

}
public static void main(String[] args) {      //调用方法
CurrentTime time= new CurrentTime();
time.show();  
}


}



*********************************************************************************************


package Homework;

//(题目三:改进第三题将时间的秒数变为30秒,编写一个Demo类)

public class CurrentTime {     //类
int year = 2015;
int month = 5;            //属性
int day = 12;
int clock = 10;
int minute = 11;
int second = 00;

public void show() {     //方法

    }

}


package Homework;


public class Demo {
       public static void main(String[] args) {
CurrentTime time = new CurrentTime();
   time.second = 30;
   time.show();
}

}



***********************************************************************************

package Homework;     //(题目4:使用类的方法秒数计算机)


public class Computer {
String CPU ="英特尔酷睿I5";             //处理器
String mainboard = "Vulcan Series";       //主板
String display = "三星";         //显示器
String HardDisk = "1T";         //硬盘
String internalStorage = "8GB";  //内存
    public void show() {
    System.out.println("处理器:"+CPU+"主板:"+mainboard+"显示器:"+display+"硬盘:"+HardDisk+"内存:"+internalStorage);
   
    }
    public static void main(String[] args) {
Computer computer = new Computer();
computer.show();
}
}


**************************************************************

package Homework;


public class Hero {                 //(题目:开发新游戏使用面向对象方法输出信息)
String name; // 英雄名字
int point; // 英雄生命值


public void show() { // 输出英雄信息方法
System.out.println("姓名:" + name + ",生命值:" + point);
}



public class Weapon {
String weaName; // 武器名
int ATK; // 攻击力


public void show() {
System.out.println("武器名:" + weaName + ",攻击力" + ATK);


}
}

public class Monster{
String moName;     //怪物名字
int moPoint;     //怪物生命值
String type;      //怪物类型

public void show() {
System.out.println("怪物名字:"+moName+",怪物生命值:"+moPoint+",类型"+type);

}
}

public static void main(String[] args) {
System.out.println("我是英雄,我的基本信息如下:");
Hero hero = new Hero();
hero.name = "李小侠";
hero.point = 300;
hero.show();

System.out.println("我是武器,我的基本信息如下:");
Weapon weapon =new Weapon();
weapon.weaName ="死神镰刀";
weapon.ATK = 12;
weapon.show();

System.out.println("我是怪物,我的基本信息如下:");
    Monster monster = new Monster();
    monster.moName = "小龟";
    monster.moPoint = 300;
    monster.type = "潜水类";
    monster.show();
    
    
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值