黑马程序员训练营入学考试题

/**
 * 2、假如我们在开发一个系统时需要对员工进行建模,
 * 员工包含 3 个属性:姓名、工号以及工资。
 * 经理也是员工,除了含有员工的属性外,另为还有一个奖金属性。
 * 请使用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。
 */
package com.itheima;
class Employee{
 private String name;
 private int num;
 private double sal;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getNum() {
  return num;
 }
 public void setNum(int num) {
  this.num = num;
 }
 public double getSal() {
  return sal;
 }
 public void setSal(int sal) {
  this.sal = sal;
 }
}
class Manager extends Employee{
 private double prize;

 public double getPrize() {
  return prize;
 }

 public void setPrize(double prize) {
  this.prize = prize;
 }
 
}
public class Test2 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

 }

}

 

/**
 * 3、编写一个类,在main方法中定义一个Map对象(采用泛型),
 * 加入若干个对象,然后遍历并打印出各元素的key和value。
 */
package com.itheima;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Test3 {
 public static void main(String[] args) {
  Map<String,Integer> map=new HashMap<String,Integer>();
  map.put("壹", 1);
  map.put("贰", 2);
  map.put("叁", 3);
  map.put("肆", 4);
  map.put("伍", 5);
  //将Map集合转化为Set集合,目的是为了使用iterator()方法
  Set<Map.Entry<String,Integer>> set=map.entrySet();
  Iterator<Map.Entry<String,Integer>> iter=set.iterator();
  while(iter.hasNext()){
   //取的每个对象都是Map.Entry类型
   Map.Entry<String, Integer> me=iter.next();
   System.out.println(me.getKey()+"="+me.getValue());
  }
 }
}

 

/**
 * 4、假设迟到两次以内不扣款,从第三次开始依次扣10,20,30………编写一个方法传入迟到的次数返回所扣的金额
PS:比如说一个人迟到了5次那么所扣的金额是:0+0+10+20+30=60元
 */
package com.itheima;

import java.util.Scanner;

public class Test4 {
 public static final int N=2;
 public static void main(String[] args) throws Exception{
  Scanner input=new Scanner(System.in);
  do{
   System.out.println("请输入迟到次数:");
   int num=input.nextInt();
   System.out.println("您输入的迟到次数为"+num);
   if(num>2){
    System.out.println("应处罚金为:"+amerce(num)+"元。");
   }else{
    System.out.println("恭喜,没有处罚。");
   }
   System.out.println("还继续输入吗,y/n");
   String str=input.nextLine();//此行应该是没用,但是如果不加就没办法继续输入了,下行if中的input.nextLine()用str代替后也没办法输入,暂时不知,待解
   if(!input.nextLine().equalsIgnoreCase("y")){
    break;
   }
  }while(true);
  
 }
 public static int amerce(int a){
  return (int) ((a-N+1)*(a-N)/2*10);
 }
}

posted on 2016-10-02 22:12  明天的我  阅读(504)  评论(0编辑  收藏  举报

导航