N个JAVA经典例题附详细答案

更新时间:2024-03-28 08:08:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

N个JAVA经典例题附详细答案

【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

//这是一个菲波拉契数列问题 public class lianxi01 {

public static void main(String[] args) {

System.out.println(\第1个月的兔子对数: 1\ System.out.println(\第2个月的兔子对数: 1\ int f1 = 1, f2 = 1, f, M=24; for(int i=3; i<=M; i++) { f = f2;

f2 = f1 + f2; f1 = f;

System.out.println(\第\个月的兔子对数: \ } } }

【程序2】 题目:判断101-200之间有多少个素数,并输出所有素数。 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。 public class lianxi02 {

public static void main(String[] args) { int count = 0;

for(int i=101; i<200; i+=2) { boolean b = false;

for(int j=2; j<=Math.sqrt(i); j++) {

if(i % j == 0) { b = false; break; } else { b = true; } }

if(b == true) {count ++;System.out.println(i );} } System.out.println( \素数个数是: \ } }

【程序3】 题目:打印出所有的 \水仙花数 \,所谓 \水仙花数 \是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 \水仙花数 \,因为153=1的三次方+5的三次方+3的三次方。 public class lianxi03 {

public static void main(String[] args) { int b1, b2, b3;

for(int m=101; m<1000; m++) { b3 = m / 100; b2 = m % 100 / 10; b1 = m % 10;

if((b3*b3*b3 + b2*b2*b2 + b1*b1*b1) == m) { System.out.println(m+\是一个水仙花数\ } } }

【程序4】 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。 (2)如果n <> k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。

(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。 import java.util.*;

public class lianxi04{ public static void main(String[] args) { Scanner s = new Scanner(System.in);

System.out.print( \请键入一个正整数: \ int n = s.nextInt(); int k=2;

System.out.print(n + \ while(k <= n) {

if(k == n) {System.out.println(n);break;}

else if( n % k == 0) {System.out.print(k + \ else k++; } }

} 【程序5】 题目:利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。 import java.util.*; public class lianxi05 {

public static void main(String[] args) { int x; char grade;

Scanner s = new Scanner(System.in); System.out.print( \请输入一个成绩: \ x = s.nextInt(); grade = x >= 90 ? 'A' : x >= 60 ? 'B' :'C';

System.out.println(\等级为:\ }

} 【程序6】 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。 /**在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。* / import java.util.*;

public class lianxi06 { public static void main(String[] args) { int a ,b,m;

Scanner s = new Scanner(System.in); System.out.print( \键入一个整数: \ a = s.nextInt();

System.out.print( \再键入一个整数: \ b = s.nextInt();

deff cd = new deff(); m = cd.deff(a,b); int n = a * b / m;

System.out.println(\最大公约数: \ System.out.println(\最小公倍数: \ }

} class deff{

public int deff(int x, int y) { int t; if(x < y) { t = x; x = y; y = t; } while(y != 0) { if(x == y) return x; else { int k = x % y; x = y; y = k; } } return x; }

} 【程序7】 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 import java.util.*; public class lianxi07 {

public static void main(String[] args) { int digital = 0; int character = 0; int other = 0; int blank = 0; char[] ch = null;

Scanner sc = new Scanner(System.in); String s = sc.nextLine(); ch = s.toCharArray(); for(int i=0; i= '0' && ch <= '9') { digital ++;

} else if((ch >= 'a' && ch <= 'z') || ch > 'A' && ch <= 'Z') { character ++; } else if(ch == ' ') { blank ++; } else { other ++; } }

System.out.println(\数字个数: \ System.out.println(\英文字母个数: \ System.out.println(\空格个数: \ System.out.println(\其他字符个数:\ } }

【程序8】 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。 import java.util.*; public class lianxi08 {

public static void main(String[] args) { long a , b = 0, sum = 0;

Scanner s = new Scanner(System.in); System.out.print(\输入数字a的值: \ a = s.nextInt();

System.out.print(\输入相加的项数:\ int n = s.nextInt(); int i = 0; while(i < n) { b = b + a; sum = sum + b; a = a * 10; ++ i; }

System.out.println(sum);

}

} 【程序9】 题目:一个数如果恰好等于它的因子之和,这个数就称为 \完数 \。例如6=1+2+3.编程 找出1000以内的所有完数。 public class lianxi09 {

public static void main(String[] args) {

System.out.println(\到1000的完数有: \ for(int i=1; i<1000; i++) { int t = 0;

for(int j=1; j<= i/2; j++) { if(i % j == 0) { t = t + j; } } if(t == i) {

System.out.print(i + \ \ } } }

【程序10】 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高? public class lianxi10 {

public static void main(String[] args) { double h = 100,s = 100; for(int i=1; i<10; i++) { s = s + h; h = h / 2; }

System.out.println(\经过路程:\ System.out.println(\反弹高度:\ } }

【程序11】 题目:有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? public class lianxi11 {

public static void main(String[] args) { int count = 0;

for(int x=1; x<5; x++) { for(int y=1; y<5; y++) { for(int z=1; z<5; z++) { if(x != y && y != z && x != z) { count ++;

System.out.println(x*100 + y*10 + z ); } } } }

System.out.println(\共有\个三位数\ }

} 【程序12】 题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润,求应发放奖金总数? import java.util.*; public class lianxi12 {

public static void main(String[] args) { double x = 0,y = 0;

System.out.print(\输入当月利润(万):\ Scanner s = new Scanner(System.in); x = s.nextInt(); if(x > 0 && x <= 10) { y = x * 0.1;

} else if(x > 10 && x <= 20) { y = 10 * 0.1 + (x - 10) * 0.075; } else if(x > 20 && x <= 40) {

y = 10 * 0.1 + 10 * 0.075 + (x - 20) * 0.05; } else if(x > 40 && x <= 60) {

y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40) * 0.03; } else if(x > 60 && x <= 100) {

y = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (x - 60) * 0.015; } else if(x > 100) {

y = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (x - 100) * 0.01; }

System.out.println(\应该提取的奖金是 \万\ } }

【程序13】 题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? public class lianxi13 {

public static void main(String[] args) { for(int x =1; x<100000; x++) { if(Math.sqrt(x+100) % 1 == 0) { if(Math.sqrt(x+268) % 1 == 0) {

System.out.println(x + \加100是一个完全平方数,再加168又是一个完全平方数\ } } } } }

/*按题意循环应该从-100开始(整数包括正整数、负整数、零),这样会多一个满足条件的数-99。

但是我看到大部分人解这道题目时都把题中的“整数”理解成正整数,我也就随大流了。*/【程序14】 题目:输入某年某月某日,判断这一天是这一年的第几天? import java.util.*; public class lianxi14 {

public static void main(String[] args) { int year, month, day; int days = 0; int d = 0; int e;

input fymd = new input(); do { e = 0;

System.out.print(\输入年:\ year =fymd.input();

System.out.print(\输入月:\ month = fymd.input(); System.out.print(\输入天:\ day = fymd.input();

if (year < 0 || month < 0 || month > 12 || day < 0 || day > 31) { System.out.println(\输入错误,请重新输入!\ e=1 ; }

}while( e==1);

for (int i=1; i

if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { days = 29; } else { days = 28; } break; } d += days;

}

System.out.println(year + \是这年的第\天。\ } } class input{ public int input() { int value = 0;

Scanner s = new Scanner(System.in); value = s.nextInt(); return value; } }

【程序15】 题目:输入三个整数x,y,z,请把这三个数由小到大输出。 import java.util.*; public class lianxi15 {

public static void main(String[] args) { input fnc = new input(); int x=0, y=0, z=0;

System.out.print(\输入第一个数字:\ x = fnc.input();

System.out.print(\输入第二个数字:\ y = fnc.input();

System.out.print(\输入第三个数字:\ z = fnc.input(); if(x > y) { int t = x; x = y; y = t; } if(x > z) { int t = x; x = z; z = t; } if(y > z) {

int t = y; y = z; z = t; }

System.out.println( \三个数字由小到大排列为: \ } } class input{ public int input() { int value = 0;

Scanner s = new Scanner(System.in); value = s.nextInt(); return value; }

} 【程序16】

题目:输出9*9口诀。 public class lianxi16 {

public static void main(String[] args) { for(int i=1; i<10; i++) { for(int j=1; j<=i; j++) {

System.out.print(j + \ \ if(j*i<10){System.out.print(\ }

System.out.println(); } }

} 【程序17】 题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。 public class lianxi18 { static char[] m = { 'a', 'b', 'c' }; static char[] n = { 'x', 'y', 'z' }; public static void main(String[] args) {

for (int i = 0; i < m.length; i++) { for (int j = 0; j < n.length; j++) { if (m[i] == 'a' && n[j] == 'x') { continue;

} else if (m[i] == 'a' && n[j] == 'y') { continue;

} else if ((m[i] == 'c' && n[j] == 'x') || (m[i] == 'c' && n[j] == 'z')) { continue;

} else if ((m[i] == 'b' && n[j] == 'z') || (m[i] == 'b' && n[j] == 'y')) { continue; } else

System.out.println(m[i] + \ } } } }

【程序18】 题目:打印出如下图案(菱形) * *** ***** ******* ***** *** *

public class lianxi19 {

public static void main(String[] args) {

int H = 7, W = 7;//高和宽必须是相等的奇数 for(int i=0; i<(H+1) / 2; i++) { for(int j=0; j

for(int k=1; k<(i+1)*2; k++) { System.out.print('*'); }

System.out.println(); }

for(int i=1; i<=H/2; i++) { for(int j=1; j<=i; j++) { System.out.print(\ }

for(int k=1; k<=W-2*i; k++) { System.out.print('*'); }

System.out.println(); } } }

【程序19】 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

public class lianxi20 {

public static void main(String[] args) { int x = 2, y = 1, t; double sum = 0; for(int i=1; i<=20; i++) { sum = sum + (double)x / y; t = y; y = x; x = y + t; }

System.out.println(\前20项相加之和是: \ } }

(风扇类Fan)设计一个名为Fan的类来表示一个风扇。这个类包括:

(1)三个名为SLOW、MEDIUM和FAST而值是1、2和3的常量表示风扇的速度 (2)一个名为speed的int类型私有数据域表示风扇的速度(默认值为SLOW) (3)一个名为on的boolean类型私有数据域表示风扇是否打开(默认值为false) (4)一个名为radius的double类型私有数据域表示风扇的半径(默认值为5) (5)一个名为color的String类型数据域表示风扇的颜色(默认值为blue) (6)这四个数据域的访问器(get方法)和修改器(set方法) (7)一个创建默认风扇的无参构造方法

(8)一个名为toString()的方法返回描述风扇的字符串。如果风扇是打开的,那么该方法在(9)一个组合的字符串中返回风扇的速度、颜色和半径。如果风扇没有打开,该方法就会返回一个“fan is off”和风扇颜色以及半径组合的字符串

画出该类的UML类图。实现这个类。编写一个测试程序,创建两个Fan对象。将第一个对象设置为最大速度、半径为10、颜色为yellow、状态为打开。将第二个对象设置为中等速度、半径为5、颜色为blue、状态为关闭。通过调用它们的toString方法显示这些对象

\\1.风扇类:

[java] view plaincopy

? ? ? ? ? ? ? ? ?

public class Fan{

public static void main(String[] args) { Fan1 fan1 = new Fan1(); fan1.setSpeed(Fan1.FAST); fan1.setRadius(10); fan1.setColor(\); fan1.setOn(true);

System.out.println(fan1.toString());

?? Fan1 fan2 = new Fan1(); ?? fan2.setSpeed(Fan1.MEDIUM); ?? fan2.setRadius(5); ?? fan2.setColor(\); ?? fan2.setOn(false);

?? System.out.println(fan2.toString()); ?? } ?? } ??

?? class Fan1 {

?? public static int SLOW = 1; ?? public static int MEDIUM = 2; ?? public static int FAST = 3; ??

?? private int speed = SLOW; ?? private boolean on = false; ?? private double radius = 5; ?? private String color = \; ??

?? public Fan1() { ?? } ??

?? public int getSpeed() { ?? return speed; ?? } ??

?? public void setSpeed(int newSpeed) { ?? speed = newSpeed; ?? } ??

?? public boolean isOn() {

?? return on; ?? } ??

?? public void setOn(boolean trueOrFalse) { ?? this.on = trueOrFalse; ?? } ??

?? public double getRadius() { ?? return radius; ?? } ??

?? public void setRadius(double newRadius) { ?? radius = newRadius; ?? } ??

?? public String getColor() { ?? return color; ?? } ??

?? public void setColor(String newColor) { ?? color = newColor; ?? } ??

?? public String toString() { ?? return \ + speed + \ ?? + \ + color + \ ?? + \ + radius + \

?? + ((on) ? \ : \); ?? } ?? }

3.(求天数GetDays)设计一个名为GetDays的类来求出两个日期的间隔天数。这个类包括:

(1)三个名为BIRTHYEAR、BIRTHMONTH、BIRTHDAY而值是你的出生年、月、日的私有常量

(2)三个名为year、month、day的int类型私有数据域表示你所输入的日期(默认值分别

为1900、1、1)

(3)三个私有数据域的访问器(get方法)和修改器(set方法) (4)一个创建默认求天数的无参构造方法

(5)一个创建可以指定特定年、月、日的有参构造方法

(6)一个名为isLeapYear的方法,返回类型为boolean类型,其功能为判断某一年是否为闰年

(7)一个名为getDaysNumber的方法,求出特定日期与你的出生年月日之间的间隔天数 (8)一个名为toString()的方法返回按下列格式描述的字符串:

yyyy年mm月dd日距离我的出生日期yyyy年mm月dd日的间隔天数为***天 画出该类的UML类图。实现这个类。编写一个测试程序,从键盘输入特定的日期(例如系统当前日期),创建一个GetDays对象。通过调用toString()方法输出日期之间的间隔。

GetDays类

[java] view plaincopy

?? class GetDays {

?? private static final int []mon_maxnum = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};

?? private final int YEAR = 1995,MONTH = 5,DAY = 25; ?? private int year = 1900, month = 1, day = 1; ??

?? public GetDays() ?? {} ??

?? public GetDays(int year,int month,int day){ ?? this.year = year; ?? this.month = month; ?? this.day = day; ?? } ??

?? public int getYear(){

?? return year; ?? } ??

?? public int getMonth(){ ?? return month; ?? } ??

?? public int getDay(){ ?? return day; ?? } ??

?? public void setYear(int year){ ?? this.year = year; ?? } ???

??? public void setMonth(int month){ ??? this.month = month; ??? } ???

??? public void setDay(int day){ ??? this.day = day; ??? } ???

??? public static boolean IsLeapYear(int year){

??? return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; ??? } ???

??? public int Getdays(int year,int month,int day){ ??? int sum = 0;

??? for(int i = 0; i < month - 1; i++){ ??? sum += mon_maxnum[i]; ??? }

??? if(month > 2 && IsLeapYear(year)){ ??? sum ++; ??? }

??? sum += day; ??? return sum; ??? } ???

??? public int getDaysNumber(){ ??? int sum = 0;

??? for(int i = YEAR; i < year; i++){ ??? if(IsLeapYear(i)){ ??? sum += 366;

??? }else{

??? sum += 365; ??? } ??? }

??? sum -= Getdays(YEAR,MONTH,DAY); ??? sum += Getdays(year,month,day); ??? sum ++; ??? return sum; ??? } ??? ??? }

TestGetDays类

[java] view plaincopy

??? import java.util.Scanner; ???

??? public class TestGetDays{

??? public static void main(String[] args){ ??? int year,month,day; ??? int sum = 0; ???

??? Scanner input = new Scanner(System.in); ???

??? System.out.println(\); ??? year = input.nextInt(); ??? month = input.nextInt(); ??? day = input.nextInt(); ???

??? GetDays obj = new GetDays(year,month,day);

??? System.out.println(\ + obj.getDaysNumber() ); ??? } ??? }

1.(二次方程式)为二次方程式ax2+bx+c=0设计一个名为QuadraticEquation的类。这类包括: (1)代表三个系数的私有数据域a、b、c (2)一个参数为a、b和c的构造方法

(3)a、b、c的三个get方法

(4)一个名为getDiscriminant()的方法返回判别式,b2-4ac (5)一个名为getRoot1()和getRoot2()的方法返回等式的两个根

这些方法只有在判别式为非负数时才有用。如果判别式为负,这些方法返回0.

画出该类的UML图。实现这个类。编写一个测试程序,提示用户输入a、b和c的值,然后显示判别式的结果。如果判别式为正数,显示两个根;如果判别式为0,显示一个根;否则,显示“The equation has no roots.”。示例如下所示: Enter a, b, c: 1.0 3 1 The roots are 1.0 and -4.0

Enter a, b, c: 1 2.0 1 The root is -1.0

Enter a, b, c: 1 2 3 The equation has no roots

[java] view plaincopy

??? import java.util.Scanner; ???

??? public class Test1 {

??? public static void main(String[] args) { ??? Scanner input = new Scanner(System.in); ??? System.out.print(\);

??? double a = input.nextDouble(); ??? double b = input.nextDouble(); ??? double c = input.nextDouble(); ???

??? QuadraticEquation equation = new QuadraticEquation(a, b, c); ??? double discriminant = equation.getDiscriminant(); ???

??? if (discriminant < 0) {

??? System.out.println(\); ??? }

??? else if (discriminant == 0) ??? {

??? System.out.println(\ + equation.getRoot1()); ??? }

??? else // (discriminant >= 0) ??? {

??? System.out.println(\ + equation.getRoot1() ??? + \ + equation.getRoot2()); ??? } ??? } ??? } ???

??? class QuadraticEquation { ??? private double a; ??? private double b; ??? private double c; ???

??? public QuadraticEquation(double newA, double newB, double newC) { ??? a = newA; ??? b = newB; ??? c = newC; ??? } ???

??? double getA() { ??? return a; ??? } ???

??? double getB() { ??? return b; ??? } ???

??? double getC() { ??? return c; ??? }

???

??? double getDiscriminant() { ??? return b * b - 4 * a * c; ??? } ???

??? double getRoot1() {

??? if (getDiscriminant() < 0) ??? return 0; ??? else {

??? return (-b + getDiscriminant()) / (2 * a); ??? } ??? } ???

??? double getRoot2() {

??? if (getDiscriminant() < 0) ??? return 0; ??? else {

??? return (-b - getDiscriminant()) / (2 * a); ??? } ??? } ??? }

2.(Person、Student、Employee、Faculty和Staff类)设计一个名为Person的类和它的两个名为Student和Employee的子类。Employee类又有子类:教员类Faculty和职员类Staff。每个人都有姓名,地址,电话号码和电子邮件地址。学生有班级状态(大一、大二、大三和大四)。将这些状态定义为常量。一个雇员有办公室,工资和受聘日期。定义一个名为MyDate的类,包含数据域:year、month、day。教员有办公时间和级别。职员有职务称号。覆盖每个类中的toString()方法,显示相应的类名和人名。

画出这些类的UML图。实现这些类。编写一个测试程序,创建Person、Student、Employee、Faculty和Staff对象,并调用它们的toString()方法。

[java] view plaincopy

??? public class Test2 {

??? public static void main(String[] args) { ??? ??? }

??? } ???

??? class Person {

??? protected String name; ??? protected String address; ??? protected String phoneNumber; ??? protected String email; ???

??? public String toString() { ??? return \; ??? } ??? } ???

??? class Student1 extends Person { ??? public static int FRESHMAN = 1; ??? public static int SOPHOMORE = 2; ??? public static int JUNIOR = 3; ??? public static int SENIOR = 4; ???

??? protected int status; ???

??? public String toString() { ??? return \; ??? } ??? } ???

??? class Employee extends Person { ??? protected String office; ??? protected int salary; ??? protected MyDate dateHired; ???

??? public String toString() { ??? return \; ??? } ??? } ???

??? class MyDate { ??? int year; ??? int month; ??? int day; ??? } ???

??? class Faculty extends Employee { ??? public static int LECTURER = 1;

??? public static int ASSISTANT_PROFESSOR = 2; ??? public static int ASSOCIATE_PROFESSOR = 3; ??? public static int PROFESSOR = 4; ???

??? protected String officeHours; ??? protected int rank; ???

??? public String toString() { ??? return \; ??? } ??? } ???

??? class Staff extends Employee { ??? protected String title; ???

??? public String toString() {

??? return \ + title; ??? } ??? }

3.(BMI类)根据以下类图,编写BMI类,实现类图中所要求的功能。

其中,BMI的状态的计算方法为:

BMI 说明 16以下 严重偏轻 16~18 偏轻 18~24 正常体重 24~29 超重 20~35 严重超重 35以上 非常严重超重 再新建一个UseBMIClass类,在其中编写一个主方法,分别建立四个对象(同宿舍中同学),然后输出每名室友的BMI的值及状态。

[java] view plaincopy

??? public class BMI { ??? private String name; ??? private int age;

??? private double weight; // in pounds ??? private double height; // in inches

??? public static final double KILOGRAMS_PER_POUND = 0.45359237; ??? public static final double METERS_PER_INCH = 0.0254; ???

??? public BMI(String name, int age, double weight, double height) { ??? this.name = name; ??? this.age = age; ??? this.weight = weight; ??? this.height = height; ??? } ???

??? public BMI(String name, double weight, double height) { ??? this(name, 20, weight, height); ??? } ???

??? public double getBMI() {

??? double bmi = weight * KILOGRAMS_PER_POUND /

??? ((height * METERS_PER_INCH) * (height * METERS_PER_INCH)); ??? return Math.round(bmi * 100) / 100.0; ??? } ???

??? public String getStatus() { ??? double bmi = getBMI();

??? if (bmi < 16)

??? return \; ??? else if (bmi < 18) ??? return \; ??? else if (bmi < 24) ??? return \; ??? else if (bmi < 29) ??? return \; ??? else if (bmi < 35)

??? return \; ??? else

??? return \; ??? } ???

??? public String getName() { ??? return name; ??? } ???

??? public int getAge() { ??? return age; ??? } ???

??? public double getWeight() { ??? return weight; ??? } ???

??? public double getHeight() { ??? return height; ??? } ??? }

4.(StackOfIntegers类)编写一个存储整型数据的栈类,其类图如下所示。编写测试程序,对该栈类进行入栈、出栈、取栈顶、判栈空等操作进行测试。

[java] view plaincopy

??? public class StackOfIntegers { ??? private int[] elements; ??? private int size;

??? public static final int DEFAULT_CAPACITY = 16; ???

??? /** Construct a stack with the default capacity 16 */ ??? public StackOfIntegers() { ??? this(DEFAULT_CAPACITY); ??? } ???

??? /** Construct a stack with the specified maximum capacity */ ??? public StackOfIntegers(int capacity) { ??? elements = new int[capacity]; ??? } ???

??? /** Push a new integer into the top of the stack */ ??? public void push(int value) { ??? if (size >= elements.length) {

??? int[] temp = new int[elements.length * 2];

??? System.arraycopy(elements, 0, temp, 0, elements.length); ??? elements = temp; ??? } ???

??? elements[size++] = value; ??? }

???

??? /** Return and remove the top element from the stack */ ??? public int pop() {

??? return elements[--size]; ??? } ???

??? /** Return the top element from the stack */ ??? public int peek() {

??? return elements[size - 1]; ??? } ???

??? /** Test whether the stack is empty */ ??? public boolean empty() { ??? return size == 0; ??? } ???

??? /** Return the number of elements in the stack */ ??? public int getSize() { ??? return size; ??? } ??? }

5.(MyStack类),利用ArrayList实现一个栈,其类图如下所示。编写测试程序,对该栈类进行入栈、出栈、取栈顶、判栈空等操作进行测试。

其中,ArrayList类的原型如下:

[java] view plaincopy

??? public class MyStack {

??? private java.util.ArrayList list = new java.util.ArrayList(); ???

??? public boolean isEmpty() { ??? return list.isEmpty(); ??? } ???

??? public int getSize() { ??? return list.size(); ??? } ???

??? public Object peek() {

??? return list.get(getSize() - 1); ??? } ???

??? public Object pop() {

??? Object o = list.get(getSize() - 1); ??? list.remove(getSize() - 1); ??? return o; ??? } ???

??? public void push(Object o) {

??? list.add(o); ??? } ???

??? public int search(Object o) { ??? return list.lastIndexOf(o); ??? } ???

??? /** Override the toString in the Object class */ ??? public String toString() {

??? return \ + list.toString(); ??? } ??? }

6.(统计字符串中字母的个数)使用下面的方法头编写一个方法,统计字母在字符串中出现的个数。

public static int countLetters(String s)

编写一个测试程序,提示用户输入一个字符串,然后显示这个字符串中字母的个数。 题:

[java] view plaincopy

??? public class Test6 {

??? public static void main(String[] args) { ??? // Prompt the user to enter a string

??? java.util.Scanner input = new java.util.Scanner(System.in); ??? System.out.print(\); ??? String s = input.nextLine(); ???

??? System.out.println(\ + countLetters(s)); ??? } ???

??? public static int countLetters(String s) { ??? int count = 0;

??? for (int i = 0; i < s.length(); i++) { ??? if (Character.isLetter(s.charAt(i))) { ??? count++;

??? } ??? } ???

??? return count; ??? } ??? }

7.(检验密码)一些网站设定了一些密码设定的规则,要求编写如下方法检验用户输入的密码字符串是否为合法的密码:

public int validatePassword(String password) 检测的规则如下:

(1)密码至少有8个字符,不满足则返回-1; (2)密码只能包含字母和数字,不满足则返回-2; (3)密码必须至少有2个数字,不满足则返回-3; (4)以上规则都满足,则返回0.

[java] view plaincopy

??? import java.util.Scanner; ??? import java.lang.Character; ???

??? public class TestPassword {

??? public static int validatePassword(String password){ ??? if(password.length() < 8){ ??? return -1; ??? }

??? for(int i = 0; i < password.length(); i++){

??? if(Character.isLetterOrDigit(password.charAt(i)) == false){ ??? return -2; ??? } ??? }

??? int digitNum = 0;

??? for(int j = 0; j < password.length(); j++){

??? if(Character.isDigit(password.charAt(j)) == true){ ??? digitNum++; ??? } ??? }

??? if(digitNum < 2){ ??? return -3; ??? }

??? return 0; ??? } ???

??? public static void main(String[] args){ ??? String password = new String();

??? Scanner scanner = new Scanner(System.in); ??? password = scanner.next();

??? switch(validatePassword(password)){

??? case 0:System.out.println(\);break;

??? case -1:System.out.println(\is wrong,not match rule 1\);break; ??? case -2:System.out.println(\is wrong,not match rule 2\);break; ??? case -3:System.out.println(\is wrong,not match rule 3\);break; ??? } ??? } ??? }

本文来源:https://www.bwwdw.com/article/sqsr.html

Top