java课后习题 第一次作业

P137-4

题目:
编写一个程序,读取一行句子作为输人,然后显示下面的响应:如果这个句子以问号结尾,并且输人中包含偶数个字符,那么显示单词Yes。如果这个句子以问号结尾,并且输人中包含奇数个字符,那么显示单词No。如果这个子句以感叹号结尾,则显示单词Wow。对于所有其他情况,显示单词 You always say,后面跟着用引号括起来的输人字符串。你的输出应该全部在一行中。千万要注意最后一种情况,你的输出必须包含将输人字符串括起来的引号,而对于所有其他情况,输出中没有任何引号。你的程序不必检查输入的是否是符合语法的句子。

代码:

import java.util.Scanner;
import java.util.Objects;
public class p137_4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please input a sentence:");
        while(sc.hasNext())//循坏获取句子
    	{	
        	
	        String jz= sc.nextLine(); //获取输入内容
	        
	        if (Objects.equals(jz, "end") )
	        {
	        	break;
	        }
	        
	        String ew = jz.substring(jz.length()-1); //读取句子末尾
	
	        if(Objects.equals(ew, "?") & jz.length()%2==0){
	            System.out.println("Yes");
	        }else if(Objects.equals(ew, "?") & jz.length()%2==1){
	            System.out.println("No");
	        }else if(Objects.equals(ew, "!")){
	            System.out.println("Wow");
	        }else{
	            System.out.println("You always say "+"\""+jz+"\"");
	        }
	        System.out.println("Please input next sentence:\nif you wanna quit,please input : end");
    	}
    }
}

测试:
测试中发现 Java会区分中英文标点,程序中匹配的都是英文标点

p138-6

题目:
输入整数x,x>100 | 50<=x<=70,输出YES,else,输出NO
题目有歧义
说是整数大于100或在50到70之间(包含在内),给的例子,100输出YES
判别式应为 x>=100 || (x>=50 && x<=70),
然而 例子中75 也是YES,这例子太假了
代码:

import java.util.Scanner;
public class p138_6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please input a integer:");
        int ig = sc.nextInt(); //ig:integer
        if(ig>100 || (ig>=50 && ig<=70)){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }
    }
}

P139-11

题目:
输入两个用HH:MM:SS AM或PM格式表示时间的字符串,输出两个时间的时间跨度,单位为分钟和秒。
Eg:11:58:10 PM
12:02:15 AM
输出4分5秒
这时间格式都不标准,也太不符合正常的逻辑思维了吧,那这个AM,PM有什么用,搞不懂它的意思。
我按标准的格式和逻辑思维写代码了,00<=HH<=11,00<=MM<=59,00<=SS<=59

代码:

import java.util.Objects;
import java.util.Scanner;
public class p139_11 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please input the standard time as HH:MM:SS AM/PM and 00<=HH<=11,00<=MM=<59,00<=SS<=59");
        System.out.println("Please input the first time :");
        String time1 = sc.nextLine();
        System.out.println("Please input the second time :");
        String time2 = sc.nextLine();
        int t1 = Integer.parseInt(time1.substring(0,2))*3600
                +Integer.parseInt(time1.substring(3,5))*60
                +Integer.parseInt(time1.substring(6,8));
        int t2 = Integer.parseInt(time2.substring(0,2))*3600
                +Integer.parseInt(time2.substring(3,5))*60
                +Integer.parseInt(time2.substring(6,8));
        String d1 = time1.substring(9,11); //是AM还是PM
        String d2 = time2.substring(9,11);
        
        int t = t2-t1;
        if(Objects.equals(d1, d2)){
        	 t = Math.abs(t); //都是AM或都是PM时直接相减取绝对值
        }else{
        	 t = Math.abs(t + 12*3600); //一个是AM一个是PM时加上12个小时时间差
        }
        		
        int mm = t / 60;//分钟
        int ss = t % 60;//秒
        System.out.println("时间跨度为"+mm+"分"+ss+"秒");
    }
}

P186-7

题目:
模拟弹力球,每秒计算一次高度,0时刻,高度0开始运动,初速度用户给,每过一秒,增加当前速度改变高度,速度减32,if高度小于0,h*(-0.5),v*(-0.5)模拟反弹,第五次反弹时停止运行。
Eg:
Enter the initial velocity of the ball: 100
Time:0 Height:0.0

Bounce!

代码:

import java.util.Scanner;
public class p186_7 {
    public static void main(String[] args)
    {
        int t = 0;
        double h = 0;
        System.out.println("Enter the initial velocity of the ball: ");
        Scanner sc = new Scanner(System.in);
        double v = sc.nextDouble();
        for(int i=0;i<5;)
        {
            System.out.println("Time:"+t+" Hight:"+h);
            t = t + 1;
            h = h + v;
            v = v - 32;
            
            if(h<0) 
            {
	            System.out.println("Bounce!");
	            h = h*(-0.5);
	            v = v*(-0.5);
	            i=i+1;
            }
        }
    }
}

P187-10

题目:
算符合下列条件的四位数字
1.4个数字都不相同
2.千位是十位的三倍
3.奇数
4.四个数字和为27
代码:

public class p187_10 {
    public static void main(String[] args) {
        for(int x=1;x<9999;x++){
            int q = x/1000; //千位
            int b = (x-q*1000)/100; //百位
            int s = (x-q*1000-b*100)/10; //十位
            int g = (x-q*1000-b*100-s*10); //个位
            
            boolean b1 = (q!=b) & (q!=s) & (q!=g) & (b!=s) & (b!=g) & (s!=g); //条件1 四个数字各不相同
            
            boolean b2;//条件2 千位是十位3倍
            if(s!=0){
                b2 = q/s==3;
            }else{
                b2 = false;
            }            
            
            boolean b3 = x%2==1;//条件3 奇数
            
            boolean b4 = q+b+s+g==27;//条件4 和为27
            
            if(b1 & b2 & b3 & b4){
                System.out.println("the number is :"+ x);
                break;
            }
        }
    }
}

P187-11

题目:
密码:mmZ\dxZmx]Zpgy

基于ASCII码加密
加密方法:
if (originalchar+key>126) then
Encryptedchar=32+((originalchar+key)-127)
Else
Encryptedchar=(originalchar+key)

key属于1-100,解密
代码:

public class p187_11 {
    public static void main(String[] args) {
        for(int key=1;key<101;key++){
            String mm = ":mmZ\\dxZmx]Zpgy";//注意\需要转义字符,不然程序无法编译
            for(int i=0;i<mm.length();i++){
                int dechar1 = (mm.charAt(i)-key);		//+key<=126时的解码字符
                int dechar2 = (mm.charAt(i)-32+127-key);//+key>126时的解法
                if(dechar1+key>126||dechar1<32){ //a<32不是单个字符,是多个字符
                    mm = mm.substring(0,i)+(char)dechar2+mm.substring(i+1);//mm.substring(i+1),取i+1到最后的字符串
                }else{
                    mm = mm.substring(0,i)+(char)dechar1+mm.substring(i+1);
                }
            }
           System.out.println("key:"+key+"   decode: "+mm);
        }
    }
}

P264-10

题目:
建ScienceFairProjectRating类,使用RatingScore类
属性:
项目名称
项目唯一标识字符串
负责人名字
创新能力等第(max30)
科学思维等第(30)
严谨性(15)
技术能力(15)
思维清晰(10)
方法:
获取评价值
获取特定项目所有等第
返回特定项目等第总和
返回可能的等第总和的最大值(这句话也太迷惑了 什么意思也不清楚,我理解就是求最大值的等第总和,30+30+15+15+10)
返回用适合显示的格式表示的项目等第

前置条件(precondition)是一条条件语句,它在方法执行前必须为真。除非前置条件满足,否则不应该使用方法,也不能期待方法能正确执行。前置条件可以与方法参数的描述相关。例如,计算x平方根的方法可以用x≥0作为前置条件。
后置条件(postcondition)是一条语句,当前置条件满足且完全执行方法后,它为真。对于一个值方法,后置条件将描述方法返回的值。对于一个void方法,后置条件描述所做的动作及对调用对象的任何修改。一般地,后置条件描述方法调用产生的所有影响。
考虑后置条件有助于弄清楚方法的目的。注意,从前置条件到后置条件没有提到如何做,即我们将方法的规格说明与它的实现分离。

代码:
ScienceFairProjectRating类:

import RatingScore;

public class ScienceFairProjectRating {
    public String name;		//项目名称
    public String id;		//标识
    public String head;		//负责人
    public RatingScore cxnl = new RatingScore("cxnl",30);	//创新能力
    public RatingScore kxsw = new RatingScore("kxsw",30);	//科学思维
    public RatingScore yjx = new RatingScore("yjx",15);		//严谨性
    public RatingScore jsnl = new RatingScore("jsnl",15);	//技术能力
    public RatingScore swqx = new RatingScore("swqx",10);	//思维清晰

    //设置等第
    public void setGrade(int a,int b,int c,int d,int e){
        if(a>=0&a<=30 &b>=0&b<=30 &c>=0&c<=15 &d>=0&d<=15 &e>=0&e<=10){
            cxnl.setScore(a);
            kxsw.setScore(b);
            yjx.setScore(c);
            jsnl.setScore(d);
            swqx.setScore(e);
            System.out.println("分数设置成功");
        }else
            System.out.println("分数超过指定范围,不能设置");
    }
    
    //获取等第
    public int[] getGrade(){
        int[] x = new int[5];
        x[0] = cxnl.Score;
        x[1] = kxsw.Score;
        x[2] = yjx.Score;
        x[3] = jsnl.Score;
        x[4] = swqx.Score;
        return x;
    }

    //显示等第
    public void showGrade(){
        System.out.println("创新能力等第:"+cxnl.Score+"\n科学思维等第:"+kxsw.Score
                +"\n严谨性等第:"+yjx.Score+"\n技术能力等第:"+jsnl.Score+"\n思维清晰等第:"+swqx.Score);
    }

   
    public int getSum(){
        int[] x = getGrade();
        int sum = 0;
        for(int i = 0;i<=4;i++)
        {
             sum = sum+x[i];
        }
        return sum;
    }

    public int getSumMax(){
        return cxnl.MaxScore + kxsw.MaxScore + yjx.MaxScore + jsnl.MaxScore + swqx.MaxScore;
    }

}

RatingScore类:

public class RatingScore {
   public String name;    //类别名
   public int MaxScore;	 //最高分
   public int Score;	//得分

   //构造方法
   public RatingScore(String n,int s){
        name = n;
        MaxScore = s;
    }

    public void setScore(int score) {
        Score = score;
    }

    public int getMaxScore(){
        return MaxScore;
    }

    public int getScore(){
        return Score;
    }

    public void showScore(){
        System.out.println(name+"得分:"+Score);
    }
}

测试代码:

import java.util.Scanner;
public class P264_10 {
	public static void main(String[] args)
	{
        ScienceFairProjectRating ysh = new ScienceFairProjectRating();
        ysh.name = "java作业";
        ysh.id = "201706110125";
        ysh.head = "杨苏杭";

        System.out.println("设置分数展示:");
        ysh.setGrade(100,100,100,100,900);
        ysh.setGrade(30,30,10,10,9);

        System.out.println("\n获取分数展示:");
        int[] grade = ysh.getGrade();
        for (int i = 0; i < grade.length; i++) {
            System.out.println("数组第"+(i+1)+"个值为"+grade[i]);
         }

        System.out.println("\n返回用适合显示的格式表示的项目等第展示:");
        ysh.showGrade();
        
        System.out.println("\n项目分数求和展示:");
        System.out.println(ysh.getSum());
        System.out.println("\n返回可能的等第总和的最大值展示:");
        System.out.println(ysh.getSumMax());

    }
}

P265-2

题目:
定义Counter类,记录非负整数数值
方法:
1.计数器设置为0,
2.递增1,
3.递减1,
不能让计数器值变负
4.访问器方法 返回计数值
5.显示计数值方法
不要定义输入方法
只需要一个实例变量
代码:
Counter 类:

public class Counter {
    private int count;

    //构造函数,定义类时 初始化为0
    public Counter() {
        count = 0;
    }

    public int add() {
        return count++;
    }

    public void subtract() {
        int now = count - 1;
        if (now >= 0) {
             --count;}
        else {
            System.out.println("The Counter can no longer be decremented!");

        }
    }

    public int getCount() {
        return count;
    }

    public void showCount(){
        System.out.println("the Counter now is:"+ count);
    }

    public void zeros(){
        count = 0;
    }
}

测试代码:

public class p265_2 {
    public static void main(String[] args) {

        System.out.println("create a counter ");
        Counter a = new Counter();
        a.showCount();

        System.out.println("\n递增展示:");
        a.add();
        a.showCount();
        
        System.out.println("\nget counter展示:");
        int b = a.getCount();
        System.out.println("b is:" + b);
        
        System.out.println("\n递减展示");
        a.subtract();
        a.showCount();
        
        a.subtract();
        a.showCount();       
        
        System.out.println("\n置零展示");
        a.add();
        a.add();
        a.showCount();
        a.zeros();
        a.showCount();
        
    }
}

P267-5

题目:

克林贡公牛种群数量100,增长率15%,生活在1500平方英里区域内,多长时间,种群密度会超过每平方英里1头?用Species类和getDensity方法
代码:
Species类:

public class Species {
    private String name;	 	//种群名称
    private int population;		//种群数量
    private double grothrate;	//增长率

    public void setSpecies(String newname,int newPopulation,double newGrowthRate){
        name = newname;
        population = newPopulation;
        grothrate = newGrowthRate;
    }

    public int predictPopulation(int years){
        int populationamount = 0;
        int year = years;
        double amount = population;
        //考虑增长率为负的情况
        while ((year>0)&&(amount>0)){
            amount = (1+grothrate*0.01)*amount;
            year --;
        }
        populationamount = (int)amount;
        return populationamount;
    }
}

测试代码:

public class P267_5 {
    public static void main(String[] args) {
        Species cow = new Species();
        cow.setSpecies("克林贡公牛", 100, 15);
        double d = 0;
        int year = 1;
        while(d<1){
        	int amount=cow.predictPopulation(year);
            d = getDensity(amount,1500);
            year++;
        }
        System.out.println("需要 "+year+" 年,种群密度会超过每平方英里1头");
    }
    
    //getDensity方法
    public static double getDensity(int amount ,int area){
        return amount/area;
    }
    
}

P186-7另一种写法

import java.util.Scanner;

public class P186_7 {
    public static void main(String[] args) {
        int t = 0;
        double h = 0;
        System.out.print("杈撳叆閫熷害锛�");
        Scanner sc = new Scanner(System.in);
        double v = sc.nextInt();
        for(int i=0;i<5;i++){
            while(h>=0){
                System.out.println("Time:"+t+" Hight:"+h);
                h = h + v;
                v = v - 32;
                t = t+1;
            }
            System.out.println("Bounce!");
            h = h*-0.5;
            v = v*-0.5;
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值