第1章 java面向对象:类和对象------课后作业:

1.教员要求张浩使用面向对象的思想编写一个计算机类(Calculator),可以实现俩个整数的加,减,乘,除运算,如果你是张浩你怎么做?

public class Calculator {
     int  num ;  //定义一个数
    int  num2;
     int algorithm;//选择算法
     public void name() {
		switch (algorithm){
		case 1:
			System.out.println("计算结果:"+(num+num2));
			break;
		case 2:
			System.out.println("计算结果:"+(num-num2));
			break;
		case 3:
			System.out.println("计算结果:"+(num*num2));
			break;
		case 4:
			System.out.println("计算结果:"+(num/num2));
			break;
		}
	}
}


public class Calculator1 {

	public static void main(String[] args) {
	  Scanner input = new Scanner(System.in);
	  Calculator ceter = new Calculator();
        System.out.println("请输入你的第一个数字:");
        ceter.num = input.nextInt();
        System.out.println("请输入你的第二个数字:");
        ceter.num2 = input.nextInt();
        System.out.println("请输入你选择的算法:1加法,2减-法,3乘法,4除法");
        ceter.algorithm = input.nextInt();
        ceter.name();
	}
}

     2.假设当前时间是2015年5月12日10点11分00秒,编写一个Current TIme类,设置属性为该时间,定义show()方法调用该时间。

public class CurrentTime {
   int year;  //年
   int mont4; //月
   int day;   //日
   int spot; //点
   int fen;  //分
  String second="00";//秒
	public  void sj() {
		System.out.println("现在时间:"+year+"年"+month+"月"+day+"日"+spot+"点"+fen+"分"+second+"秒");
	}
}
 
 
public class CurrentTime1 {

	public static void main(String[] args) {
		CurrentTime CurTime = new CurrentTime();
		CurTime.year = 2015;  
		CurTime.month = 5;		
                CurTime.day = 12; 
		CurTime.spot =10; 
		CurTime.fen= 11;  
		CurTime.second = "00";
		CurTime.sj();
	}
}

 
 

3.将当前时间改为2015年5月12日10点11分30秒。

public class CurrentTime {
   int year=2018;  //年
   int month=4; //月
   int day=16;   //日
   int spot=16; //点
   int fen=47;  //分
  String second="00";//秒
	public  void sj() {
		System.out.println("现在时间:"+year+"年"+month+"月"+day+"日"+spot+"点"+fen+"分"+second+"秒");
	}
}
 
 
public class CurrentTime1 {

	public static void main(String[] args) {
		CurrentTime CurTime = new CurrentTime();
		CurTime.sj();
	}

}

 
 

4.使用类的方式描述计算机:

public class Computer {
    String  Cpu;//中央处理器
     String Mainboard; //主板
     String Monitor; //显示器
     String Harddisk; //硬盘
     String Memory;  //内存
	public  void showInfo() {
		System.out.println("计算机的主要部件有:Cpu:"+Cpu+",Mainboard:"+Mainboard+", Monitor:"+ Monitor+""
				+ ", Harddisk:"+ Harddisk+", Memory:"+ Memory);
	}

}
 
 
public class Computer1 {

	public static void main(String[] args) {
		Computer parts = new Computer();
		parts.Cpu = "Intel 酷睿i5 5200U";
	    parts.Mainboard = "i5 5200U";
	    parts.Harddisk = "500GB";
	    parts.Memory = "4GB";
	    parts.Monitor = "天逸100-15idb";
	    parts.showInfo();
	}
}

 
 

5.某公司开发新游戏,请用面向对象的思想设计英雄类,怪物类,和武器类。

public class School {
     String Hero;  //英雄姓名
     int HealthPoint;      //生命值
     String monster;   //怪物姓名
     String type; //类型
     String arms; //武器
     int attack;  //攻击力
	public  void show() {
		System.out.println("我是英雄,我的基本信息如下:");
		System.out.println("姓名:"+Hero+",生命值:"+HealthPoint);
		System.out.println("我是武器,我的基本信息如下:");
		System.out.println("武器姓名:"+arms+",攻击力:"+attack);
		System.out.println("我是怪物,我的基本信息如下:");
		System.out.println("姓名:"+monster+",生命值:"+HealthPoint+",类型:"+type);
	}
}
 
 
public class School1 {
	public static void main(String[] args) {
		School center = new School();
    	center.Hero = "李小侠";
    	center.HealthPoint = 300;
    	center.arms = "死神镰刀";
    	center. attack = 12;
    	center. monster = "小龟";
    	center.type = "潜水类";
    	center.show();
	}
}

 
 

猜你喜欢

转载自blog.csdn.net/gz98411/article/details/79986919