一、画程序流程图
画图规范
二、代码题(类或者函数)
1.定义一个点类,其中必须含有计算两点之间距离的方法(或函数);定义直线类,其中必须含有两直线交点的方法(或函数),则其交点设为原点(0,0),交点的值必须为点类类型,再设计一个主类(主函数),验证所设计的两个类。
public class Point { //点类
private double x;
private double y;
public Point() {}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public static double getDist(Point a,Point b) {
double dx=a.getX()-b.getX();
double dy=a.getY()-b.getY();
return Math.sqrt( dx*dx+dy*dy );
}
}
public class Line { //直线类
private double k;
private double b;
public Line() {}
public Line(Point A,Point B) {
if(A.getY()==B.getY()) {
k=0;
b=A.getY();
}else {
k=(B.getY()-A.getY())/(B.getX()-A.getX());
b=A.getY()-k*A.getX();
}
}
public double getK() {
return k;
}
public void setK(double k) {
this.k = k;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public Point getJiaoDian(Line A,Line B) {
if(A.getK()==B.getK()) {
return new Point(0,0);
}
double x=0,y=0;
if(A.getK()==0) {
y=A.getB();
x=(y-B.getb())/B.getk();
}else if(B.getK()==0) {
y=B.getB();
x=(y-A.getb())/A.getk();
}else {
x=(B.b-A.b)/(A.k-B.k);
y=X*A.k+A.b;
}
return new Point(x,y);
}
}
public class Test { //测试类
public static void main(String[] args) {
Point A=new Point(2,1);
Point B=new Point(1,2);
System.out.println("两点间距离为:"+A.getDist(A, B));
Point C=new Point(2,5);
Point D=new Point(1,3);
Line l1=new Line(A,B);
Line l2=new Line(C,D);
System.out.println("两直线交点为:"+L1.getJiaoDian(L1, L2));
Point C1=new Point(3,1);
Point D1=new Point(1,3);
Line l3=new Line(A,B);
Line l4=new Line(C1,D1);
System.out.println("两直线交点为:"+:L1.getJiaoDian(L3, L4));
}
2.设计素数判定程序。进一步判定100——1000之间的所有素数,并验证哥德巴赫猜想
package zkj;
class fun
{
private int x;
private int y;
public fun() {};
public fun(int x,int y)
{
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void f(int x,int y)
{
for(int i=100;i<=1000;i++)
{
if(pd(i)==1)
{
System.out.println(i);
}
}
}
public int pd(int i)
{
for(int j=2;j<=Math.sqrt(i);j++)
{
if(i%j==0)
{
return 0;
}
}
return 1;
}
}
public class t1面向对象 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x=100;
int y=1000;
System.out.println("100-1000之间所有的素数:");
fun a=new fun(x,y);
a.f(x, y);
}
}
3.设计计算s=1+2+3+…+n和值的程序。要求,至少给出5种计算方法。
package zkj;
import java.util.Scanner;
class f{
private int n;
public f(int n)
{
this.n=n;
}
public void fun1()
{
int sum=0;
for(int i=1;i<=n;i++)
sum+=i;
System.out.print(sum);
}
public void fun2()
{
System.out.print(n*(1+n)/2);
}
public void fun3()
{
int i=1;
int sum=0;
while(i<=n)
{
sum+=i;
i++;
}
System.out.print(sum);
}
public int fun4(int n) {
if(n==0) return 0;
else
return fun4(n-1)+n;
}
public int fun5(int n,int total)
{
if(n==0)return total;
else
return fun5(n-1,total+n);
}
}
public class t2面向对象 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
int n;
n=cin.nextInt();
f a=new f(n);
a.fun1();System.out.println();
a.fun2();System.out.println();
a.fun3();System.out.println();
System.out.println(a.fun4(n));
System.out.println(a.fun5(n,0));
}
}
4.设计具有n个实数的数组A,所有元素的和值。要求,采用递归程序。
package zkj;
import java.util.Scanner;
class go
{
private int arr[]=new int [150];
private int n;
public go(int n,int arr[])
{
this.n=n;
for(int i=1;i<=n;i++)
{
this.arr[i]=arr[i];
}
}
public int fun(int n)
{
if(n==0)
return 0;
else
{
return fun(n-1)+arr[n];
}
}
}
public class t3面向对象 {
static int a[]=new int [100];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner (System.in);
int n;
n=cin.nextInt();
for(int i=1;i<=n;i++)
{
a[i]=cin.nextInt();
}
go g=new go(n,a);
System.out.print(g.fun(n));
}
}
5.设计具有n个实数的数组A,求其中最大值的程序。要求,采用递归程序。
package zkj;
import java.util.Scanner;
class acc
{
private int max=-9999;
private int arr[]=new int [150];
private int n;
public acc(int n,int arr[])
{
this.n=n;
for(int i=1;i<=n;i++)
{
this.arr[i]=arr[i];
}
}
public int f(int n) {
// TODO Auto-generated method stub
if(n==0) return 0;
else
{
max=Math.max(max, arr[n]);
return f(n-1);
}
}
}
public class t4面向对象 {
static int a[]=new int [100];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner (System.in);
int n;
n=cin.nextInt();
for(int i=1;i<=n;i++)
{
a[i]=cin.nextInt();
}
acc g=new acc(n,a);
System.out.print(g.f(n));
}
}
6.计算两个日期之间相差的天数
package zkj;
import java.util.Calendar;
class time
{
private Calendar c1;
private Calendar c2;
public time(Calendar c1,Calendar c2)
{
this.c1=c1;
this.c2=c2;
}
public void f()
{
long t1=c1.getTimeInMillis();
long t2=c2.getTimeInMillis();
long days=(t2-t1)/(24*60*60*1000);
System.out.println("两日期相差的天数为:"+days);
}
}
public class t5面向对象 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar c1=Calendar.getInstance();
c1.set(2020,12,4);
Calendar c2=Calendar.getInstance();
c2.set(2020,12,5);
time t=new time(c1,c2);
t.f();
}
}
7.使用for循环、while循环和递归写出3个函数来计算给定数列的总和:
package zkj;
public class t6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
f1(100);
f2(100);
System.out.print(f3(100));
}
private static int f3(int n) {
// TODO Auto-generated method stub
if(n==0) return 0;
else
return n+f3(n-1);
}
private static void f2(int n) {
// TODO Auto-generated method stub
int sum=0;
int i=1;
while(i<=n)
{
sum+=i;
i++;
}
System.out.println(sum);
}
private static void f1(int n) {
// TODO Auto-generated method stub
int sum=0;
for(int i=1;i<=n;i++)
sum+=i;
System.out.println(sum);
}
}
8.编写一个交错合并列表元素的函数。
package zkj;
import java.util.ArrayList;
import java.util.Scanner;
public class t7 {
static ArrayList<String> a= new ArrayList<>();
static ArrayList<Integer> b= new ArrayList<>();
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner (System.in);
int n,m;
n=cin.nextInt();
m=cin.nextInt();
for(int i=1;i<=n;i++)
{
String x=cin.next();
a.add(x);
}
for(int j=1;j<=m;j++)
{
int x=cin.nextInt();
b.add(x);
}
int s=Math.min(a.size(), b.size());
int la=Math.max(a.size(), b.size());
f(s,la);
for(int i=0;i<la;i++)
{
if(i<s)
{
System.out.print(a.get(i)+" "+b.get(i)+" ");
}
else if(la==a.size())
{
System.out.print(a.get(i)+" ");
}
else if(la==b.size())
{
System.out.print(b.get(i)+" ");
}
}
}
private static void f(int s, int la) {
// TODO Auto-generated method stub
}
}
9.编写一个计算前100位斐波那契数的函数。
package zkj;
public class t8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print(f(100));
}
private static int f(int n) {
// TODO Auto-generated method stub
if(n==1) return 0;
else if(n==3||n==2) return 1;
else
return f(n-1)+f(n-2);
}
}
10.编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,
使得计算结果总是100的程序,并输出所有的可能性。
例如:1+2+34–5+67–8+9=100。
package zkj;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class t9 {
private static Integer[] VALUES = { 50, 2, 100, 99, 5, 7, 51,50 ,11};
public static void main(String[] args) {
Arrays.sort(VALUES, new Comparator<Object>() {
public int compare(Object lhs, Object rhs) {
String v1 = lhs.toString();
String v2 = rhs.toString();
return (v1 + v2).compareTo(v2 + v1) * -1;
}
});
String result = "";
for (Integer integer : VALUES) {
result += integer.toString();
}
System.out.println(result);
}
}
11.编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,
使得计算结果总是100的程序,并输出所有的可能性。
例如:1+2+34–5+67–8+9=100。
package zkj;
import java.util.ArrayList;
public class t10 {
private static int TARGET_SUM = 100;
private static int[] VALUES = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static ArrayList add(int digit, String sign, ArrayList branches) {
for (int i = 0; i < branches.size(); i++) {
branches.set(i, digit + sign + branches.get(i));
}
return branches;
}
private static ArrayList f(int sum, int number, int index) {
int digit = Math.abs(number % 10);
if (index >= VALUES.length) {
if (sum == number) {
ArrayList result = new ArrayList();
result.add(Integer.toString(digit));
return result;
} else {
return new ArrayList();
}
}
ArrayList branch1 = f(sum - number, VALUES[index], index + 1);
ArrayList branch2 = f(sum - number, -VALUES[index], index + 1);
int concatenatedNumber = number >= 0 ? 10 * number + VALUES[index] : 10
* number - VALUES[index];
ArrayList branch3 = f(sum, concatenatedNumber, index + 1);
ArrayList results = new ArrayList();
results.addAll(add(digit, "+", branch1));
results.addAll(add(digit, "-", branch2));
results.addAll(add(digit, "", branch3));
return results;
}
public static void main(String[] args) {
ArrayList list = f(TARGET_SUM, VALUES[0], 1);
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
}
}
12.设计一个识别出一篇英语文章所有单词的程序,并将这些单词按字母顺序输出
四级英语考试中,阅读理解短文中生词率统计。
要求:根据4级单词大纲,对于一篇阅读理解中生词的多少,决定了文章的难易程度,一般生词率为5%.
```handlebars
package zkj;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
//设计一个识别出一篇英语文章所有单词的程序,并将这些单词按字母顺序输出
public class t11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
Set<String> text=new HashSet<String>();//开始先把单词输入到Set集合中
String a;
while(!(a=in.next()).equals("."))//以.结尾
{
text.add(a);
}
System.out.println(text.size());//输出不重复的单词数量,即Set集合中存储的所有元素的数量
String[] b = text.toArray(new String[text.size()-1]);//将Set中的元素返回到数组b中
ArrayList<String> text2=new ArrayList<String>();
for(int i=0;i<text.size();i++) text2.add(b[i]);//为了方便排序,再将数组b中的元素一一存到ArrayList集合中
text2.sort(null);
for(int i=0;i<text2.size();i++)//将ArrayList集合中排好序的元素输出前十个
{
String aa=text2.get(i);
if(aa.charAt(aa.length()-1)==','||aa.charAt(aa.length()-1)=='?'||aa.charAt(aa.length()-1)=='!')//可以改成范围
{
for(int j=0;j<aa.length()-1;j++)
{
System.out.print(aa.charAt(j));
}
System.out.println();
}
else
{
System.out.println(text2.get(i));
}
}
}
}
三、测试方法
黑盒测试详解(等价类划分)
白盒测试
四、结构化设计
dfd图