Java语言程序设计与数据结构(基础篇)课后练习题 第六章(一)

6.1

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	for(int i =1;i<=100;i++){
		System.out.print(getPentagonalNumber(i)+" ");
		if(i%10==0)
			System.out.print("\n");
	}
}

public static int getPentagonalNumber(int n){
	int num = (n*(3*n-1))/2;
	return num;
}

}

6.2

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.print("Enter an integer: ");
	long i = input.nextLong();
	System.out.println(sumDigits(i));
}

public static int sumDigits(long n){
	int sum=0;
	while(n != 0){
		sum += n%10;
		n/=10;
	}
	return sum;
}

}

6.3

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.print("Enter an integer: ");
	int num = input.nextInt();
	if(isPalindrome(num))
		System.out.println(num+" is a palindrome.");
	else 
		System.out.println(num+"is not a palindrome.");
}


public static int reverse(int number){
String num1 = Integer.toString(number);
String num2 = “”;
for(int i=num1.length()-1;i>=0;i–){
num2 += num1.charAt(i);
}
return Integer.parseInt(num2); //Integer.parseInt()是把()里的内容转换为整数。
}

public static boolean isPalindrome(int number){
	return number==reverse(number);
}

}

6.4

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.print("Enter an integer: ");
	int num = input.nextInt();
	reverse(num);
}
public static void reverse(int number){
	String num1 = Integer.toString(number); 
	String num2 = "";
	for(int i=num1.length()-1;i>=0;i--){
		num2 += num1.charAt(i);
	}
	System.out.println(num2);
}	

}

6.5

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.print("Enter three numbers: ");
	double num1 = input.nextDouble();
	double num2 = input.nextDouble();
	double num3 = input.nextDouble();
	displaySortedNumbers(num1, num2, num3);
}
public static void displaySortedNumbers(double num1,double num2,double num3){
	if(num1>num2){
		double tmp = num1;
		num1 = num2;
		num2 = tmp;
	}
	if(num2>num3){
		double tmp = num2;
		num2 = num3;
		num3 = tmp;
	}
	if(num1>num2){
		double tmp = num1;
		num1=num2;
		num2=tmp;
	}
	System.out.println(num1+","+num2+","+num3);
}	

}

6.6

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.print("Enter n: ");
	int n = input.nextInt();
	displayPattern(n);
}
public static void displayPattern(int n){
	for(int i=1;i<=n;i++){
		for(int j=0;j<n-i;j++){
			System.out.print("  ");
		}
        for(int j=i;j>=1;j--){
            System.out.printf("%2d",j);
		}
		System.out.println();
	}
}	

}

6.7

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input=new Scanner(System.in);
	System.out.print("The amount invested: ");
	double amount = input.nextDouble();
    System.out.print("Annual interest rate: ");
    double rate = input.nextDouble();
    System.out.println("Years    Future Value");
    for(int i=1;i<=30;i++)
            System.out.printf("%-2d      %9.2f\n",i,futureInvestmentValue(amount,rate/1200,i));
    }

 public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate,int years){
        return investmentAmount*Math.pow((1+monthlyInterestRate),years*12);
    }

}

6.8

该题涉及到两个类。

第一个类diliuzhang;

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	System.out.println(" 摄氏度	华氏度			华氏度		摄氏度   ");
	System.out.println("----------------------------------");

	for(int i =0;i<10;i++){
		double num = 40-i;
		double num2 = 120.0-10.0*i;
		System.out.printf("%3.1f\t%.1f\t\t%.1f\t%.2f\n",num,demo3.celsiusToFahrenheit(40.0-i),num2,demo3.fahrenheitToCelsius(num2));
	}
}

}

第二个类demo3;

package demo;

public class demo3 {

public static double celsiusToFahrenheit(double celsius){
	return (9.0/5)*celsius+32;
}
public static double fahrenheitToCelsius(double fahrenheit){
	return (5.0/9)*(fahrenheit-32);
}

}

6.9

第一个类diliuzhang;

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	System.out.println(" 英尺		米			米		英尺   ");
	System.out.println("-------------------------------------------------------------");

	for(int i =0;i<10;i++){
		double num1 = i*1.0;
		double num2 = i*5.0+15.0;
		System.out.printf("%3.1f\t\t%.1f\t\t\t%.1f\t\t%.2f\n", num1,demo3.footToMeter(num1) ,num2 ,demo3.meterToFoot(num2));
	}
}

}

第二个类demo3;

package demo;

public class demo3 {

public static double footToMeter(double foot){
	return 0.305*foot;
}
public static double meterToFoot(double meter){
	return 3.279*meter;
}

}

6.10

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
     int count=0;
        for(int i=1;i<10000;i++)
        {
            if(isPrime(i))
                count++;
        }
        System.out.println("There are "+count+" primes.");
    }

 public static boolean isPrime(int number)
    {
        for(int i=2;i<=number/2;i++){
            if(number%i==0){
                return false;
            }
        }
        return true;
}

}

6.11

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
        System.out.println("销售总额\t\t酬金");
        System.out.println("-------------------");
        for(int i=10000;i<=100000;i+=5000)
            System.out.printf("%-6d\t\t%7.1f\n",i,computeCommission(i*1.0));
    }
    public static double computeCommission(double salesAmount)
    {
        double num=0;
        if(salesAmount<=5000)
        	num = salesAmount*0.08;
        else if(salesAmount<=10000)
        	num = 5000*0.08+(salesAmount-5000)*0.10;
        else
        	num = 5000*0.08+5000*0.10+(salesAmount-10000)*0.12;
        return num;
    }

}

6.12

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
		printChars('1','Z',10);
    }
    public static void printChars(char ch1,char ch2,int numberPerLine){
    	for(int i=1;i<=ch2-ch1;i++){
    		System.out.print((char)(i+ch1-1)+" ");
    		if(i%numberPerLine==0)
    			System.out.println();
    	}
    }

}

6.13

package demo;

import java.util.Scanner;

public class diliuzhang {

   public static void main(String[] args)
    {
        System.out.println("i\t\tm(i)");
        System.out.println("-------------------");
        for(int i=1;i<=20;i++)
         	System.out.printf("%-2d\t\t%7.4f\n",i,lineSum(i));
    }
    public static  double lineSum(int n)
    {
        double sum=0.0;
        for(int i=1;i<=n;i++)
            sum+=1.0*i/(i+1);
        return sum;
    }

}

6.14

package demo;

import java.util.Scanner;

public class diliuzhang {

   public static void main(String[] args)
    {
        System.out.println("i\t\tm(i)");
        System.out.println("------------------");
        for(int i=1;i<=901;i+=100){
        	System.out.printf("%-2d\t\t%5.4f\n",i,lineSum(i));
        }
    }
    public static  double lineSum(int n)
    {
        double sum=0;
        for(int i=1;i<=n;i++){
            if(i%2==1)
            	sum += 1.0/(2*i-1);
            else 
            	sum -= 1.0/(2*i-1);
        }
        return 4.0*sum;
    }

}

6.15

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {

	printTableHead();

	for(int i = 50000;i <= 60000;i += 50){
		System.out.printf("%d\t\t%d\t\t%d\t\t\t%d\t\t%d\n", i, Math.round(computeTax(0,i)), Math.round(computeTax(1,i)), Math.round(computeTax(2,i)),Math.round(computeTax(3,i)));
	}
}

public static double computeTax(int status,double taxableIncome)

{

	double tax = 0;
	if (status == 0){ 
		if (taxableIncome <= 8350)
			tax = taxableIncome * 0.10;
		else if (taxableIncome <= 33950)
			tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15;
		else if (taxableIncome <= 82250)
			tax = 8350 * 0.10 + (33950 - 8350) * 0.15+(taxableIncome - 33950) * 0.25;
		else if (taxableIncome <= 171550)
			tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +(82250 - 33950) * 0.25 + (taxableIncome - 82250) * 0.28;
		else if (taxableIncome <= 372950)
			tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +(taxableIncome - 171550) * 0.33;
		else
			tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +(372950 - 171550) * 0.33 + (taxableIncome - 372950) * 0.35;
	}else if (status == 1){
		if (taxableIncome <= 16700)
			tax = taxableIncome * 0.10;
		else if (taxableIncome <= 67900)
			tax = 16700 * 0.10 + (taxableIncome - 16700) * 0.15;
		else if (taxableIncome <= 137050)
			tax = 16700 * 0.10 + (67900 - 16700) * 0.15+(taxableIncome - 67900) * 0.25;
		else if (taxableIncome <= 208850)
			tax = 16700 * 0.10 + (67900 - 16700) * 0.15 +(137050 - 67900) * 0.25 + (taxableIncome - 137050) * 0.28;
		else if (taxableIncome <= 372950)
			tax = 16700  * 0.10 + (67900 - 16700) * 0.15 +(137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 +(taxableIncome - 208850) * 0.33;
		else
			tax = 16700 * 0.10 + (67900 - 16700) * 0.15 +(137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 +(372950 - 208850) * 0.33 + (taxableIncome - 372950) * 0.35;
	}
	else if (status == 2){ 
		if (taxableIncome <= 8350)
			tax = taxableIncome * 0.10;
		else if (taxableIncome <= 33950)
			tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15;
		else if (taxableIncome <= 68525)
			tax = 8350 * 0.10 + (33950 - 8350) * 0.15+(taxableIncome - 33950) * 0.25;
		else if (taxableIncome <= 104425)
			tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +(68525 - 33950) * 0.25 + (taxableIncome - 68525) * 0.28;
		else if (taxableIncome <= 186475)
			tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +(68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 +(taxableIncome - 104425) * 0.33;
		else
			tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +(68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 +(186475 - 104425) * 0.33 + (taxableIncome - 186475) * 0.35;
	}else if (status == 3) { 
		if (taxableIncome <= 11950)
			tax = taxableIncome * 0.10;
		else if (taxableIncome <= 45500)
			tax = 11950 * 0.10 + (taxableIncome - 11950) * 0.15;
		else if (taxableIncome <= 117450)
			tax = 11950 * 0.10 + (45500 - 11950) * 0.15+(taxableIncome - 45500) * 0.25;
		else if (taxableIncome <= 190200)
			tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +(117450 - 45500) * 0.25 + (taxableIncome - 117450) * 0.28;
		else if (taxableIncome <= 372950)
			tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +(117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 +(taxableIncome - 190200) * 0.33;
		else
			tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +(117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 +(372950 - 190200) * 0.33 + (taxableIncome - 372950) * 0.35;
	}
	else 
		return -1; 
	return tax;
}

public static void printTableHead(){
	System.out.println("Taxable\t\tSingle\t\tMarried Joint\t\tMarried\t\tHead of");
	System.out.println("Income\t\t\t\tor Qualifying\t\tSeparate\tHouse hold");
	System.out.println("\t\t\t\tWidow(er)");
	System.out.print("-----------------------------------------");
	System.out.print("-----------------------------------------\n");

}

}

6.16

package demo;

import java.util.Scanner;

public class diliuzhang {

public static void main(String[] args) {
	for(int i=2000;i<=2020;i++){
		System.out.println(i+" has "+yearType(i)+" days.");
	}
}
public static int yearType(int year){
	if((year%4==0&&year%100!=0)||(year%400==0))
		return 366;
	else
		return 365;
}

}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值