第2章 java 面向对象:类的无参和有参方法 ------课后作业:

1.根据输入1~12月的数字,判断是春夏秋冬那个季节..........................

方法一:
public class Season {
    String season;//季节
   
    public  void show( int month) {
    	if(month>=1 && month<=3) {
    		season = "春季";
    	}else if(month>=4&& month<=6) {
    		season = "夏季";
    	}else if(month>=7 && month<=9) {
    		season = "秋季";
    	}else {
    		season = "冬季";
    	}
    	System.out.println(" ");
    	System.out.println("该季节是:"+season);
	}
    
    public static void main(String[] args) {
		Season sea = new Season();  //创建新的对象
		Scanner input = new Scanner(System.in);
		System.out.print("请输入月份:");
		int month = input.nextInt();
 		sea.show(month);  //调用方法
 	} 
}

方法二:
public class Season {
    String season;//季节
    int month;  //月份
    public  void show() {
    	Scanner input = new Scanner(System.in);
    	boolean falg = true;  //判断条件为真
		while(falg) {
			System.out.print("请输入月份:");
			int month = input.nextInt();
        switch(month) {
        case 1:
        case 2:
        case 3:	 
        	season ="春季";
        	System.out.println("该季节是:"+season);
        	falg = false;       //条件为假
        	break;
        case 4:
        case 5:
        case 6:	 
        	season ="夏季";
        	System.out.println("该季节是:"+season);
        	falg = false;
        	break;
        case 7:
        case 8:
        case 9:	 
        	season ="秋季";
        	System.out.println("该季节是:"+season);
        	falg = false;
        	break;
        case 10:
        case 11:
        case 12:	 
        	season ="冬季";
        	System.out.println("该季节是:"+season);
        	falg = false;
        	break;
        default :
        	System.out.println("\n请输入正确的月份!");
        	break;
           }
        }
	}
    
    public static void main(String[] args) {
		Season sea = new Season();  //创建新的对象
		sea.show();  //调用方法
	} 
}

2.

public class Calculator {
   double num1; //定义数字1
   double  num2; //定义数字2
   public double add() {
	 return  num1+num2;
       }
   public double minus() {
		 return  num1-num2;
	   }
   public double multiple() {
		 return  num1*num2;
	   }
   public double divide() {
		 return  num1/num2;
	   }
}
public class TestCalculator {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Calculator cal = new Calculator();
		System.out.print("请输入第一位数字:");
		    cal.num1 = input.nextDouble();
		    System.out.print("请输入第二位数字:");
		    cal.num2 = input.nextDouble();
         System.out.print("请输入你的运算方式:1加法 2减法 3乘法  4除法:");
             int algorithm = input.nextInt();
          switch (algorithm) {
		case 1:
			System.out.println(cal.add());
			break;
		case 2:
			System.out.println(cal.minus());
			break;
		case 3:
			System.out.println(cal.multiple());
			break;
		case 4:
			System.out.println(cal.divide());
			break;
		default :
			System.out.println("对不起,请输入正确选择!");
		}
	}
}

3.

public class QuessMachine {
     String commodity; //定义商品
     int price;   //价格
   /**
    * @author 商品信息方法
    */
     public void inital() {
    	 int num = (int)(Math.random()*10);
    	 if(num>=0 && num<=3) {
    		 commodity = "公主电动车";
    		 price =2400;
    	 }else if(num>4 && num<=6) {
    		 commodity = "opop手机";
    		 price =3000;
    	 }else if(num>7 && num<=9) {
    		 commodity = "电视机";
    		 price = 5000;
    	 }else {
    		 commodity = "iPhoneX";
    		 price = 8388;
    	 }
     }
     
     public static void main(String[] args) {
		 Scanner input = new Scanner(System.in);
		 QuessMachine st = new QuessMachine();
		 int num = 0;
 		 st.inital();
		 System.out.print("请猜测"+st.commodity+"的价格:");
		 int price = input.nextInt();
		 while (price!=st.price) {
		  num++;
			if(num>4) {                        //判断输入的次数
				System.out.println("\n4次内没有猜对,下次努力吧!");
				break;
			}
			if(price>st.price) {
				System.out.print("再小点!\n\n再猜一次吧:");
				price = input.nextInt();
			}else if (price < st.price) {
				System.out.print("再大点\n\n再猜一次吧:");
				price = input.nextInt();
			}
		}
  		    if(price ==st.price) {
			    System.out.println("恭喜您猜对了,并获得了"+st.commodity+"!");
		 }
	}
}

4.改写第一章作业中的简答题第2题中的计算器类(Calculator)。要求将加减乘除的方法改成带参方法,在定义一个运算方法ope(),接收用户选择的运算和俩个数字,根据用户选择计算方式。

public class Calculator1 {
     int totality; //总数
	public int ope(int op, int num1, int num2) {
		switch (op) {
		case 1:
	          totality = num1+num2;
			break;
		case 2:
		      totality = num1-num2;
				break;
		case 3:
		      totality = num1*num2;
				break;
		case 4:
		      totality = num1/num2;
				break;
		default:
			System.out.println("请输入正确选择!");
			break;
		}
		return totality;
	}
	
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Calculator1  cus = new Calculator1();
		System.out.print("请选择运算方式  1加法 2减法 3乘法 4除法:");
		int op = input.nextInt();
		System.out.print("请输入第一位数字:");
		int num1 = input.nextInt();
		System.out.print("请输入第二位数字:");
		int num2 = input.nextInt();
		System.out.println("\n计算结果:"+cus.ope(op, num1, num2));
	}
 }
5.编写程序,向整型数组的指定位置插入元素。并输出插入前后数组的值。
public class Inset {
    public void  insetArray(int [] arr,int index,int value ) {
    	for (int i = arr.length-1;i > index; i--) {
			arr [i] = arr[i-1];  //index下标开始元素后移一个位置
		}
    	arr[index] = value;   //插入数据
    	
    }
     public void showArray(int [] arr) {
    	 for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+" ");
		}
     }
     
     public static void main(String[] args) {
		Scanner  input = new Scanner(System.in);
		Inset  inset = new Inset();
		int arrs [] = new int [5];
		for (int i = 0; i < arrs.length; i++) {
			System.out.print("请输入第"+(i+1)+"个整数:");
			arrs[i] = input.nextInt();
		}
		
		System.out.print("原数组:");
		inset.showArray(arrs);
		int [] arr = Arrays.copyOf(arrs,arrs.length+1);
		System.out.print("\n请输入你要插入的数字:");
		int value = input.nextInt();
		System.out.print("请输入你的下标:");
		int index = input.nextInt();
		inset.insetArray(arr,index,value);
		System.out.println("插入后的数组是:");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+" ");
		}
		Arrays.sort(arr);
		Arrays.sort(arrs);
		System.out.println("升序排列后:");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+" ");
		}
	}
}


6.本学期期末学员共参加了三门课程的考试,即 java,C#,SQL,编写方法计算每位学员三门课程的平均分。

public class Student {
	/**
	 * 3门课程成绩
	 * 
	 */
	int java; //java成绩
	int c;    //C#成绩
	int sql;  //SQL成绩

	public int totalScore(int java, int c, int sql) {
		int total = java + c + sql; // 总成绩
		return total;
	}

	public double getAvg(int java, int c, int sql) {
		double avg = (java + c + sql) / 3.0; // 平均成绩
		return avg;
	}

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Student stu = new Student();
		for (int i = 0; i < 3; i++) {
			System.out.print("请输入java第" + (i + 1) + "门成绩:");
			int java = input.nextInt();
			System.out.print("请输入C#第" + (i + 1) + "门成绩:");
			int c = input.nextInt();
			System.out.print("请输入第SQL" + (i + 1) + "门的成绩:");
			int sql = input.nextInt();
			System.out.println("\n总成绩是:" + stu.totalScore(java, c, sql));
			System.out.println("第" + (i + 1) + "门平均成绩是:" + stu.getAvg(java, c, sql));
			System.out.println(" ");
		}
	}
 }



你可能感兴趣的:(第2章 java 面向对象:类的无参和有参方法 ------课后作业:)