首页 > 代码库 > 小结(练习题)

小结(练习题)

四.函数题

定义一个名为Stock的股票类,这个类包括:一个名为symbol的字符串数据域表示股票代码。一个名为name的字符串数据域表示股票名称。一个名为previousClosingPrice的double数据域,它存储前一日的股票交易价格。一个名为currentPrice数据域,它存储当前的股票交易价格。创建一个有特定代码和名称的股票的构造方法。一个名为getChangePercent()方法返回从previousClosingPrice变化到currentPrice的百分比。

类名为:

Stock

裁判测试程序样例:

 1 import java.util.Scanner;
 2 /* 你提交的代码将被嵌入到这里 */
 3 
 4 public class Main {
 5   public static void main(String[] args) {
 6     Scanner input = new Scanner(System.in);
 7     String symbol1=input.next();
 8     String name1=input.next();    
 9     Stock stock = new Stock(symbol1, name1);
10 
11     stock.previousClosingPrice = input.nextDouble();
12 
13     // Input current price
14     stock.currentPrice = input.nextDouble();
15 
16     // Display stock info
17     System.out.println(stock.name+"price changed: " + stock.changePercent() * 100 + "%");
18     input.close();
19   }
20 }

 

输入样例:

002594
比亚迪
56.98
55.40

 

输出样例:

比亚迪股价涨跌: -2.77290277290277%
源代码:

源代码:

 1 import java.util.Scanner;
 2 class Stock{
 3     String name;
 4     String symbol;
 5     double previousClosingPrice;
 6     double currentPrice;
 7     public Stock(String symbol, String name) {
 8         // TODO Auto-generated constructor stub
 9         this.name = name;
10         this.symbol = symbol;
11     
12     }
13     
14     public double changePercent(){
15         double result;
16         result = (currentPrice - previousClosingPrice) / previousClosingPrice;
17         return result;
18     }
19 }
20 
21 public class Main {
22   public static void main(String[] args) {
23     Scanner input = new Scanner(System.in);
24     String symbol1=input.next();
25     String name1=input.next();    
26     Stock stock = new Stock(symbol1, name1);
27 
28     stock.previousClosingPrice = input.nextDouble();
29 
30     // Input current price
31     stock.currentPrice = input.nextDouble();
32 
33     // Display stock info
34     System.out.println(stock.name+"price changed: " + stock.changePercent() * 100 + "%");
35     input.close();
36   }
37 }

五.编程题

   1 定义ClassName接口,设计类Company,实现获取类名称的功能   (10分)

定义一个ClassName接口,接口中只有一个抽象方法getClassName();设计一个类Company,该类实现接口ClassName中的方法getClassName(),功能是获取该类的类名称;编写应用程序使用Company类

输出格式:

输出类名称。

输出样例:

Company

源代码:
 1 interface ClassName { 
 2     public abstract void getClassName();
 3 }
 4 
 5 class Company implements ClassName {
 6     @Override
 7     public void getClassName() {
 8         
 9         System.out.print(Company.class.getSimpleName());
10     }
11 }
12 
13 public class Main {
14     public static void main(String args[]) {
15         Company a = new Company();
16         a.getClassName();
17     }
18 }

 

2 给定两个点的坐标,求解两个点的距离。   (20分)

给定两个点的坐标,求解两个点的距离。

输入格式:

给定四个浮点数,作为线段的两个点。

输出格式:

输出该线段的距离。

输入样例:

0 0 1.0 1.0

 

输出样例:

The distance is 1.41

 


源代码:
 1 import java.util.Scanner;
 2 
 3 public class Main{
 4 
 5     public static void main(String[] args) {
 6          Scanner in = new Scanner(System.in);
 7          double a1 = in.nextDouble();
 8          double a2 = in.nextDouble();
 9          double a3 = in.nextDouble();
10          double a4 = in.nextDouble();
11          double s = Math.sqrt(Math.pow((a3-a1), 2)+Math.pow((a3-a1),2));
12          System.out.println(String.format("The distance is "+"%.2f", s));
13          
14     }
15 
16 }

3 找出最大的对象   (10分)

(找出最大的对象)编写一个方法,返回对象数组中最大的对象。方法签名如下:

public static Object max(Comparable[] a)

所有对象都是Comparable接口的实例。对象在数组中的顺序是由compareTo方法决定的。

编写测试程序,从键盘输入5个字符串和5个整数,创建一个由5个字符串构成的数组、一个由5个整数构成的数组。找出数组中最大的字符串、整数并输出。

输入格式:

输入

Xi‘an (输入5个字符串,每行一个)

Beijing

ShangHai

GuangZhou

ShenZhen

8 9 12 7 6 (输入5个整数,以空格分隔)

 

输出格式:

输出 Max string is Xi‘an (输出最大的字符串)

Max integer is 12 (输出最大的整数)

 

输入样例:

France
Japan
German
China
India
6 34 89 168 53

 

输出样例:

Max string is Japan
Max integer is 168

源代码:

 1 import java.util.Arrays;
 2 import java.util.Scanner;
 3 public class Main {
 4    
 5     public static void main(String[] args) {
 6         @SuppressWarnings("resource")
 7         Scanner sc = new Scanner(System.in);
 8             String [] list1 = new String[5];
 9             int [] list2 = new int[5];
10          for(int i=0;i<5;i++){
11              String str1 = sc.next();
12              list1[i]=str1;           
13          }
14          for(int i=0;i<5;i++){
15              int str2 = sc.nextInt();
16              list2[i]=str2;           
17          }
18          
19          
20            Arrays.sort(list1);
21            Arrays.sort(list2);
22            System.out.println("Max string is "+list1[4]);
23            System.out.println("Max integer is "+list2[4]);
24     }
25 }

4 字符串替换   (20分)

将文本文件中指定的字符串替换成新字符串。 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束。end后面有两个字符串,要求用第二个字符串替换文本中所有的第一个字符串。

输入格式:

Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology. The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

end (表示结束)

Institute (第一个字符串,要求用第二个字符串替换)

University (第二个字符串)

 

输出格式:

Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

 

输入样例:

Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end
Institute
University

 

输出样例:

Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

源代码:

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class Main{
 5     public static void main(String[] args) throws IOException {
 6         String x = null;
 7         Scanner in = new Scanner(System.in);
 8         String str = "";
 9         x = in.nextLine();
10         while (!x.equals("end")) {
11             str += x;
12             x = in.nextLine();
13         }
14         String a = in.nextLine();
15         String b = in.nextLine();
16         in.close();
17         String str1 = str.replace(a, b);
18         System.out.println(str1);
19 
20     }
21 }

5 求阶乘factorial   (20分)

编程从键盘输入一个整数,计算出阶乘并输出。

输入格式:

输入 39

 

输出格式:

输出:20397882081197443358640281739902897356800000000

 

输入样例:

58

输出样例:

2350561331282878571829474910515074683828862318181142924420699914240000000000000

源代码:

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     public static void main(String args[]) {
 6         BigInteger sum = new BigInteger("1");
 7         Scanner in = new Scanner(System.in);
 8         int n = in.nextInt();
 9         for(int i = 1; i<= n;i++){
10             String temp1 = Integer.toString(i); 
11             BigInteger temp2 = new  BigInteger(temp1); 
12             sum = sum.multiply(temp2);
13         }
14         System.out.println(sum);
15         
16     }
17 }

 

小结(练习题)