7-12 图形继承 (20 分)

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

import java.util.Scanner;

class Shape //定义一个无自身属性,有一个返回值为0.0的求面积方法
{

public Shape()
{
System.out.println("Constructing Shape");
}
public double getArea()
{
return 0.0;
}
}

class Circle extends Shape//继承自Shape
{
public Circle()
{
System.out.println("Constructing Circle");
}
private double radius;//新定义一个半径

public void setRadius(double radius) {// 设置半径
this.radius = radius;
}

public double getRadius() {// 获取半径
return radius;
}

@Override
public double getArea() {
// TODO Auto-generated method stub
return Math.PI*radius*radius;//重写父类的方法
}

}
class Rectangle extends Shape
{
public Rectangle()
{
System.out.println("Constructing Rectangle");
}
private double width;
private double length;
public double getWidth() {
return width;
}

public void setWidth(double width) {
this.width = width;
}

public double getLength() {
return length;
}

public void setLength(double length) {
this.length = length;
}

@Override
public double getArea() {
// TODO Auto-generated method stub
return width*length;
}

}
class Ball extends Circle
{
public Ball()
{
System.out.println("Constructing Ball");
}

@Override
public double getArea() {
// TODO Auto-generated method stub
return 4.0*super.getArea();//方法的重载,super关键字
}
public double getVolume()
{
double r2=getRadius();
return 4.0/3.0*r2*r2*r2*Math.PI;
}

}
class Box extends Rectangle
{
public Box()
{
System.out.println("Constructing Box");
}
private double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getVolume()
{
return height*super.getArea();
}
@Override
public double getArea() {
// TODO Auto-generated method stub
double w2=getWidth();
double l2=getLength();
return 2*(w2*l2+w2*height+l2*height);
}


}
public class Main {

public static void main(String[] args) {
int inType;
Scanner scanner=new Scanner(System.in);
inType=scanner.nextInt();
switch(inType)
{
case 1:
double r=scanner.nextDouble();
if(r<0.0) {
System.out.println("Wrong Format");
}
else {
Circle circle=new Circle();
circle.setRadius(r);
System.out.println(String.format("Circle's area:%.2f",circle.getArea()));
}
break;

case 2:
double width=scanner.nextDouble();
double length=scanner.nextDouble();
if(width<0.0||length<0.0) {
System.out.println("Wrong Format");
}
else {
Rectangle rectangle=new Rectangle();
rectangle.setLength(length);
rectangle.setWidth(width);
System.out.println(String.format("Rectangle's area:%.2f",rectangle.getArea()));
}
break;
case 3:
double r2=scanner.nextDouble();
if(r2<0.0) {
System.out.println("Wrong Format");
}
else {
Ball ball=new Ball();
ball.setRadius(r2);
System.out.println(String.format("Ball's surface area:%.2f",ball.getArea()));
System.out.println(String.format("Ball's volume:%.2f",ball.getVolume()));
}
break;
case 4:
double width2=scanner.nextDouble();
double length2=scanner.nextDouble();
double height=scanner.nextDouble();
if(width2<0.0||length2<0.0||height<0.0) {
System.out.println("Wrong Format");
}
else {
Box box=new Box();
box.setHeight(height);
box.setWidth(width2);
box.setLength(length2);
System.out.println(String.format("Box's surface area:%.2f",box.getArea()));
System.out.println(String.format("Box's volume:%.2f",box.getVolume()));
}
break;
default:
System.out.println("Wrong Format");
}
}
}

7-13 jmu-Java-03面向对象基础-04-形状-继承 (15 分)

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
import java.util.Scanner;

abstract class Shape {
double PI = 3.14;

public abstract double getPerimeter();

public abstract double getArea();
}

class Rectangle extends Shape {
int wide, len;

Rectangle(int a, int b) {
wide = a;
len = b;
}

@Override
public double getPerimeter() {
// TODO Auto-generated method stub
return 2 * (wide + len);
}

@Override
public double getArea() {
// TODO Auto-generated method stub
return wide * len;
}

public String toString(){
return "[width=" + wide + ", length=" + len + "]";
}
}

class Circle extends Shape {
int radius;

Circle(int _radius) {
radius = _radius;
}

@Override
public double getPerimeter() {
// TODO Auto-generated method stub
return radius * 2 * PI;
}

@Override
public double getArea() {
// TODO Auto-generated method stub
return radius * radius * PI;
}

public String toString(){
return "[radius=" + radius + "]";
}

}

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
Shape A[] = new Shape[n];
int k = 0, j = 0;
double sumAllArea = 0, sumAllPerimeter = 0;
for (int i = 0; i < n; i++) {
String S = in.next();
if (S.equals("rect")) {
int wide = in.nextInt(), len = in.nextInt();
in.nextLine();
A[i] = new Rectangle(wide, len);
} else if (S.equals("cir")) {
int radius = in.nextInt();
in.nextLine();
A[i] = new Circle(radius);
}
sumAllArea += A[i].getArea();
sumAllPerimeter += A[i].getPerimeter();
}

System.out.println(sumAllPerimeter);
System.out.println(sumAllArea);
System.out.print("[");
for (int i = 0; i < n; i++) {
if(i != 0)
System.out.print(", ");
if (A[i] instanceof Rectangle) {
System.out.print("Rectangle ");
System.out.print(A[i].toString());
}
else {
System.out.print("Circle ");
System.out.print(A[i].toString());
}
}

System.out.println("]");

for(int i = 0;i < n;i++) {
if(A[i] instanceof Rectangle) {
System.out.println("class Rectangle,class Shape");
}else {
System.out.println("class Circle,class Shape");

}
}
in.close();
}
}

7-15 集体评分2 (10 分)

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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] grade = new int[5];
for(int i=0; i<grade.length; i++){
grade[i] = in.nextInt();
}

RR rr = new RT(grade);
double dd = rr.mark();
System.out.printf("%.2f",dd);
}
}
interface RR{
double mark();
}
class RT implements RR{
int[] grade;
public double mark() {
double max=0;
double min=0x3f3f3f3f;
double sum=0;
for(int i: grade) {
sum+=i;
if(i>max)max=i;
if(i<min)min=i;
}
sum-=(max+min);
sum/=(double)(grade.length-2);
return sum;
}
public RT(int[] grade){
this.grade = grade;
}
}

7-16 程序改错题2 (5 分)

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

public class Main {
public static void main(String[] args) {
Dog animal = new Dog();
animal.shout();
animal.run();
}
}

class Animal {
void shout() {
System.out.println("animal shout!");
}
}

class Dog extends Animal {
void shout() {
super.shout();
System.out.println("wangwang……");
}

void run() {
System.out.println("Dog is running");
}
}

7-17 程序填空题3 (5 分)

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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Son son = new Son();
son.method();
}
}

class Parent {
Parent() {
System.out.println("Parent's Constructor without parameter");
}

Parent(boolean b) {
System.out.println("Parent's Constructor with a boolean parameter");
}

public void method() {
System.out.println("Parent's method()");
}
}

class Son extends Parent {
//补全本类定义
Son(){
super(true);
System.out.println("Son's Constructor without parameter");
}
public void method() {
// super.method();
System.out.println("Son's method()");
super.method();
}
}

7-18 接口–四则计算器 (10 分)

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

import java.util.*;

interface ICompute{
int compute(int x,int y);
}

class Add implements ICompute{
@Override
public int compute(int x, int y) {
// TODO Auto-generated method stub
return x+y;
}
}

class Sub implements ICompute{
@Override
public int compute(int x, int y) {
// TODO Auto-generated method stub
return x-y;
}
}

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a=in.nextInt(),b=in.nextInt();
Add x=new Add();
Sub y=new Sub();
System.out.println(x.compute(a,b));
System.out.println(y.compute(a, b));
}
}

7-19 成绩录入时的及格与不及格人数统计 (10 分)

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
import java.util.Scanner;


public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int a=0,b=0;
for(int i=0;i<n;i++) {
int x=in.nextInt();
try {
if(x>100||x<0) throw new Exception(x+"invalid!");
else if(x>=60)a++;
else b++;
}
catch (Exception e) {
i--;
System.out.println(e.getMessage());
}
}
System.out.println(a);
System.out.println(b);
}
}

7-20 jmu-Java-02基本语法-02-StringBuilder (10 分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
import java.text.DecimalFormat;

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

while(in.hasNextInt()) {
int n,begin,end;
n=in.nextInt();
begin=in.nextInt();
end=in.nextInt();
StringBuilder s =new StringBuilder();
for(int i=0;i<n;i++) {
s.append(i);
}
System.out.println(s.substring(begin, end));
}
}
}

7-21 单词替换 (20 分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;


public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s=new String(),a,b;
s=in.nextLine();
a=in.next();
b=in.next();
String[] arr=s.split("\\s+");
StringBuilder ans=new StringBuilder();
for(int i=0;i<arr.length;i++) {
if(arr[i].equals(a))ans.append(b);
else ans.append(arr[i]);
if(i!=arr.length-1)ans.append(" ");
}
System.out.println(ans);
}
}

7-22 jmu-java-随机数-使用蒙特卡罗法计算圆周率的值 (10 分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.*;
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
long seed = in.nextLong();
int n = in.nextInt();
Random r = new Random(seed);
int count = 0;
for (int i=0;i<n;i++) {
double x = r.nextDouble()*2-1;
double y = r.nextDouble()*2-1;
if ( x*x + y*y <=1) count++;
}
System.out.println(4*(double)count/n);
}
}


7-23 通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 (10 分)

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
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
sc.close();

char[] chars = str.toCharArray();
int letter=0,num=0,blank=0,other=0;
for(int i=0;i<chars.length;i++) {
if((chars[i]>='A' && chars[i]<='Z') || (chars[i]>='a' && chars[i]<='z')){
letter++;
}else if(chars[i]>=47 && chars[i]<=57) {
num++;
}else if(chars[i]==32) {
blank++;
}else {
other++;
}
}

System.out.println("字母个数:"+letter);
System.out.println("数字个数:"+num);
System.out.println("空格个数:"+blank);
System.out.println("其他字符个数:"+other);
}
}

7-24 jmu-Java-02基本语法-08-ArrayList入门 (10 分)

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

import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList <String> vector=new ArrayList<String> ();
String s;
while(true) {
s=in.next();
if(s.equals("!!end!!"))break;
vector.add(s);
}
vector.add("end");
vector.add(0,"begin");
System.out.println(vector);
s=in.next();
if(vector.contains(s))System.out.println("true"+"\n"+vector.indexOf(s));
else System.out.println("false"+"\n"+"-1");
System.out.println(vector.lastIndexOf(s));
System.out.println(vector.get(0));
vector.remove(0);
System.out.println(vector);
s=in.next();
vector.set(1, s);
System.out.println(vector);
s=in.next();
ArrayList<String> list=new ArrayList<String>();
for(String i: vector) {
if(i.contains(s))list.add(i);
}
System.out.println(list);
if(vector.contains(s))vector.remove(vector.indexOf(s));
System.out.println(vector);
vector.clear();
System.out.println(vector+","+vector.size()+","+vector.isEmpty());

}
}


7-25 找到出勤最多的人 (10 分)

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

import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<String, Integer> ma =new HashMap<String, Integer>();
String s;
int max=0;
ArrayList<String> ans=new ArrayList<String>();
while(in.hasNext()) {
s=in.next();
Integer vis=ma.get(s);
vis=vis==null?1:vis+1;
ma.put(s, vis);
if(vis>max) {
max=vis;
ans.clear();
ans.add(s);
}
else if(vis==max) {
ans.add(s);
}
}
int cou=0;
for(String i: ans) {
cou++;
System.out.println(i);
if(cou!=ans.size())System.out.println(" ");
}
}
}


7-26 创建一个倒数计数线程 (10 分)

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
import java.util.*;

class Task implements Runnable{
int n;
Task(int n){
this.n=n;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=n;i>=0;i--) {
int time=500;
while(time-->0);
System.out.println(i);
}
}

}

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Thread t = new Thread(new Task(n));
t.start();
}
}


7-27 程序改错题4 (5 分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.*;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Thread t = new Thread(new RunHandler());
t.run();
}
}

class RunHandler implements Runnable{
public void run() {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
while(x-->0)
System.out.println("run");
}
}

7-28 USB接口的定义 (10 分)

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
import java.util.*;

interface USB{
public abstract void work();
public abstract void stop();
}

class Mouse implements USB{

@Override
public void stop() {
// TODO Auto-generated method stub
System.out.println("我不能点了");
}
@Override
public void work() {
// TODO Auto-generated method stub
System.out.println("我点点点");
}
}

class UPan implements USB{
@Override
public void work() {
// TODO Auto-generated method stub
System.out.println("我存存存");
}
@Override
public void stop() {
// TODO Auto-generated method stub
System.out.println("我走了");
}
}

public class Main {
public static void main(String[] args) {
USB usb1 =new Mouse();
usb1.work();
usb1.stop();
USB usb[]=new USB[6];
usb[0]=new UPan();
usb[1]=new Mouse();
for(USB i: usb) {
i.work();
i.stop();
}
}
}

7-29 Circle类 (10 分)

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
import java.util.*;

class Circle{
private int r;
public Circle(){
this.r=2;
System.out.println("this is a constructor");
}
public Circle(int r){
if(r<=0)this.r=2;
else this.r=r;
System.out.println("this is a constructor with para");
}
public int get() {
return r;
}
public void set(int r) {
if(r<=0) this.r=2;
else this.r = r;
}
public int getArea() {
return (int)(Math.PI*r*r);
}
@Override
public String toString() {
return "Circle [radius=" + r + "]";
}
}

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Circle c1 =new Circle();
System.out.println(c1);
System.out.println("c1:area="+c1.getArea());
Circle c2 = new Circle();
System.out.println(c2);
int r=in.nextInt();
c2.set(r);
System.out.println(c2);
System.out.println("c2:area="+c2.getArea());
Circle c3 =new Circle(in.nextInt());
System.out.println(c3);
System.out.println("c3:area="+c3.getArea());
}
}


7-30 数组与对象 (10 分)

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
import java.util.*;

import org.omg.PortableInterceptor.NON_EXISTENT;

class Person{
String name;
public Person(){
this.name="none";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(String name) {
this.name=name;
}
public String toString(){ return "name:"+name; }
}

public class Main {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
Person a[]=new Person[2];
a[0]=new Person();
a[1]=new Person(in.next());
for(Person i: a) {
System.out.println(i);
}
}
}


7-31 圆柱体类设计 (10 分)

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
import java.util.*;

import org.omg.PortableInterceptor.NON_EXISTENT;

class Yz{
int r,h;
public Yz() {
this(2,1);
System.out.println("Constructor no para");
}
public Yz(int r,int h) {
this.r=r;
this.h=h;
System.out.println("Constructor with para");
}
public int getV(){
return (int)(Math.PI*r*r*h);
}
public int getR() {
return r;
}

public void setR(int r) {
this.r = r;
}

public int getH() {
return h;
}

public void setH(int h) {
this.h = h;
}

}

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int r=in.nextInt();
int h=in.nextInt();
Yz c1=new Yz(r,h);
System.out.println(c1.getV());
Yz c2=new Yz();
System.out.println(c2.getV());
}
}


7-32 List的使用 (15 分)

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
import java.util.*;

import org.omg.PortableInterceptor.NON_EXISTENT;

class Person{
String name;
int age;
public Person() {

}
public Person(String name,int age) {
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}

}

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List <Person> list =new ArrayList<Person>();
int n=in.nextInt();
while(n-->0) {
list.add(new Person(in.next(),in.nextInt()));
}
for(Person i: list) {
System.out.println(i);
}
String name=in.next();
boolean flag=true;
for(Person i: list) {
if(i.name.equals(name)) {
System.out.println(i);
flag=false;
break;
}
}
if(flag) System.out.println("此人不存在");
}
}


7-33 jmu-Java-03面向对象基础-03-形状 (10 分)

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
import java.util.Arrays;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Rectangle[] rectangle = new Rectangle[2];
Circle[] circle = new Circle[2];
Scanner sc = new Scanner(System.in);
for(int i=0;i<rectangle.length;i++) {
rectangle[i] = new Rectangle(sc.nextInt(),sc.nextInt());
}
for(int i=0;i<circle.length;i++) {
circle[i] = new Circle(sc.nextInt());
}
System.out.println(rectangle[0].getPerimeter()+rectangle[1].getPerimeter()+
circle[0].getPerimeter()+circle[1].getPerimeter());
System.out.println(rectangle[0].getArea()+rectangle[1].getArea()+
circle[0].getArea()+circle[1].getArea());
System.out.println(Arrays.deepToString(rectangle));
System.out.println(Arrays.deepToString(circle));
sc.close();
}
}


class Rectangle{
private int width;
private int length;

public Rectangle(int width,int length) {
this.width = width;
this.length = length;
}

//求周长方法
public int getPerimeter() {
return 2*(width+length);
}
//求面积方法
public int getArea() {
return width*length;
}

@Override
public String toString() {
return "Rectangle [width=" + width + ", length=" + length + "]";
}

}


class Circle{
private int radius;

public Circle(int radius) {
this.radius = radius;
}

//求周长方法
public int getPerimeter() {
return (int)(2*Math.PI*radius);
}
//求面积方法
public int getArea() {
return (int)(Math.PI*radius*radius);
}

@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}

}