Java实验三

《Java程序设计》课程第三周实验题目和代码。

第一题

输出 101-200 之内的所有质数;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 //输出 101-200 之内的所有质数; 

public class PrimeNumber {

public static void main(String []arg) {
System.out.println("101-200的质数是:");

int i,j;

//输出1-200之间的所有质数
for(i=101;i<=200;i++) {

boolean flag=false;
//判断是否为质数,如果是,flag为true,否则为false
for(j=2;j<=Math.sqrt(i);j++) {
if(i%j==0) {
flag=false;
break;
}
else
flag=true;
}
if(flag)
System.out.println(i);
}

}

}

第二题

编写一个 Java 应用程序,具体内容如下:
首先,该程序中有 3 个类:Triangle、Circle 和 Cone,分别用来刻画“三
角形”、“圆形”和“圆锥体”。
(1) Triangle 类具有类型为 double 的三个边长度以及周长属性;定义构
造方法、返回周长方法和修改三个边的方法。另外,Triangle 类还具有一个
boolean 型的属性,该属性用来判断三条边能否构成一个三角形,请实现判断是
否为三角形的方法。
(2)Circle 类具有类型为 double 的属性半径和面积;定义设置半径,返
回面积的方法。
(3)Cone 类具有 Circle 类型的底,double 类型的高这两个属性,定义构
造方法,计算圆锥体体积的方法。
然后,在一个新的 public 类中创建并使用三个类的对象:
(1)Triangle 类:创建对象,并使用构造方法进行初始化。计算出周长值
并输出 ;修改三边值并判断是否构成三角形。
(2)Circle 类:创建对象并初始化,然后计算面积并输出。
(3)Cone 类:创建对象并初始化,修改 Cone 底的半径,计算体积并输出。
(4)要求:所有测试的三角形边、圆半径和圆椎体的高,从控制台输入。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// 编写一个 Java 应用程序,具体内容如下: 
//首先,该程序中有 3 个类:Triangle、Circle 和 Cone,分别用来刻画“三
//角形”、“圆形”和“圆锥体”。
//(1) Triangle 类具有类型为 double 的三个边长度以及周长属性;定义构
//造方法、返回周长方法和修改三个边的方法。另外,Triangle 类还具有一个
//boolean 型的属性,该属性用来判断三条边能否构成一个三角形,请实现判断是
//否为三角形的方法。
//(2)Circle 类具有类型为 double 的属性半径和面积;定义设置半径,返
//回面积的方法。
//(3)Cone 类具有 Circle 类型的底,double 类型的高这两个属性,定义构
//造方法,计算圆锥体体积的方法。
//然后,在一个新的 public 类中创建并使用三个类的对象:
//(1)Triangle 类:创建对象,并使用构造方法进行初始化。计算出周长值
//并输出 ;修改三边值并判断是否构成三角形。
//(2)Circle 类:创建对象并初始化,然后计算面积并输出。
//(3)Cone 类:创建对象并初始化,修改 Cone 底的半径,计算体积并输出。
//(4)要求:所有测试的三角形边、圆半径和圆椎体的高,从控制台输入。

import java.util.Scanner;

class Triangle{
double a,b,c,perimeter; //triangle's sides length a,b,c and perimeter
Triangle(double a,double b,double c){ //constructor
this.a=a;
this.b=b;
this.c=c;
perimeter=a+b+c;
System.out.println("the perimeter is: "+perimeter);
}

boolean isTriangle(double a,double b,double c) { //determine whether form triangle
if(a>=(b+c)||b>=(a+c)||c>=(a+b)) {
System.out.println("the length of sides "+a +"," + b +"," +c+" can not form the triangle.");
return false;
}
else {
System.out.println("the length of sides "+a +"," + b +"," +c+" can form the triangle.");
return true;
}
}

void setSideLength(double a,double b,double c) { //reset sides length of triangle
this.a=a;
this.b=b;
this.c=c;
boolean flag;
flag=isTriangle(a,b,c);

}

}


//create Circle class
class Circle{
private double radius; //Circle's radius

Circle(double radius){ //constructor
this.radius=radius;
System.out.println("the radius is:"+this.radius);
}

double calArea() { //calculate area of circle
System.out.println("the area is:"+3.14*radius*radius);
return 3.14*radius*radius;
}

void setRadius(double radius) { //reset radius
this.radius=radius;
System.out.println("the radius is set to "+this.radius);
}
}

//create Cone class extends Circle class
class Cone extends Circle{
double height,volume; //cone's height,volume

Cone(double radius,double height){ //constructor
super(radius);
this.height=height;
}

double calVolume() { //calculate volume of Cone
double volume,area;
area=super.calArea();
volume=area*height*(1.0/3.0);
System.out.println("the volume of Cone is:"+volume);
return volume;
}

void setBottomRadius(double r) { //reset radius
super.setRadius(r);
}
}


//test class
public class Inherit {
public static void main(String []arg) {
int k=-1; //receive data from console to choose operation
boolean xunhuan=true; //control when loop ends
for(;xunhuan;) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入1-3进行选择");
System.out.println("1.创建Triangle类");
System.out.println("2.创建Circle类");
System.out.println("3.创建Cone类");
k=scan.nextInt();
switch(k) {
//create triangle class
case 1:
System.out.println("请输入三角形的边长:");
double a,b,c; //receive data of sides length of triangle from console
Scanner st = new Scanner(System.in);
a=st.nextDouble();
b=st.nextDouble();
c=st.nextDouble();

Triangle t =new Triangle(a,b,c); //create triangle class t
t.isTriangle(a, b, c); //determine whether form the triangle

System.out.println("是否修改三角形的边长,是输入1,否输入2"); //whether reset sides length
int shuru=0;
Scanner st1 = new Scanner(System.in);
shuru=st1.nextInt();
if(shuru==1) {
System.out.println("请输入要修改的三角形的边长:");
double a1,b1,c1;
Scanner st2 = new Scanner(System.in);
a1=st2.nextDouble();
b1=st2.nextDouble();
c1=st2.nextDouble();
t.setSideLength(a1,b1,c1);
}
//end or continue
System.out.println("是否退出?退出请按1,继续请按2");
Scanner s1 = new Scanner(System.in);
int shu1=0;
shu1=s1.nextInt();
if(shu1==1)
xunhuan=false;

break;

case 2:
//create circle class
System.out.println("请输入圆的半径:");
double r; //receive data of circle's radius from console
Scanner sc = new Scanner(System.in);
r=sc.nextDouble();

Circle cir= new Circle(r); //create circle class cir
double area;
area=cir.calArea(); //calculate area of circle
System.out.println("圆的面积是:"+area);

//end or continue
System.out.println("是否退出?退出请按1,继续请按2");
int shu2=0;
shu2=sc.nextInt();
if(shu2==1)
xunhuan=false;
break;

case 3:
//create cone class
System.out.println("请输入圆锥的半径和高:");
double ra,h; //receive data of cone's radius,height from console
Scanner sco = new Scanner(System.in);
ra=sco.nextDouble();
h=sco.nextDouble();

Cone cone=new Cone(ra,h); //create Cone class cone
cone.calVolume(); //calculate volume

//reset radius
System.out.println("是否修改圆锥底的半径,是输入1,否输入2");
int shur=0;
shur=sco.nextInt();
if(shur==1) {
System.out.println("请输入要修改圆锥底的半径:");
double ra1;
ra1=sco.nextDouble();
cone.setBottomRadius(ra1);
cone.calVolume();
}

//end or continue
System.out.println("是否退出?退出请按1,继续请按2");
Scanner st3 = new Scanner(System.in);
int shu3=0;
shu3=st3.nextInt();
if(shu3==1)
xunhuan=false;
break;
}
}

}

}

第三题

给定一组输入数据(从键盘输入),通过一种排序算法(如插入排序、选择
排序、归并排序、冒泡排序、快速排序、堆排序等一种)排序后输出;再从
键盘输入一个数据,通过二分查找算法,检索该数据是否在排序的数据中,
若在请输出其索引号;若不存在,请给出提示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//给定一组输入数据(从键盘输入),通过一种排序算法(如插入排序、选择
//排序、归并排序、冒泡排序、快速排序、堆排序等一种)排序后输出;再从
//键盘输入一个数据,通过二分查找算法,检索该数据是否在排序的数据中,
//若在请输出其索引号;若不存在,请给出提示。

import java.util.Scanner;

public class Sort {
public static void main(String []arg) {
int i,j;

System.out.println("请输入想要排序的数据,数字之间用空格分离,回车结束输入:");
Scanner sc=new Scanner(System.in);
String temp=sc.nextLine(); //input end with enter key
String arr[]=temp.split(" "); //separate character by regular expression into array arr
int len=arr.length; //arr's length
int []sort=new int[len];

//convert character into integer
for(i=0;i<len;i++) {
sort[i]=Integer.parseInt(arr[i]);
}

//sort array sort
for(i=0;i<len-1;i++) {
for(j=0; j<len-1-i;j++) {
int t=0;
if(sort[j]>sort[j+1]) {
t=sort[j];
sort[j]=sort[j+1];
sort[j+1]=t;
}

}

}


//find data from console by dichotomy
System.out.println("排序后的数据:");
for(i=0;i<len;i++)
System.out.printf(sort[i]+",");
System.out.printf("\n");
System.out.println("输入想要查找的数:");
int m,a=0,b=len;
boolean flag=false;
m=sc.nextInt();
for(i=len/2;i<b&&i>a;) {
if(sort[i]==m) {
System.out.println(m+"的索引为:"+i);
flag=true;
break;
}
else if(m>sort[i]) {
a=i;
i=(a+b)/2;
}
else {
b=i;
i=(a+b)/2;
}
}
if(flag==false)
System.out.println("你输入的数不存在");

}
}


打赏
  • © 2025 Aoxue
  • Hexo Theme Ayer by shenyu
    • PV:
    • UV:

请我喝杯咖啡吧~

支付宝
微信